jmeter-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: svn commit: r1720242 - in /jmeter/trunk: src/core/org/apache/jmeter/report/core/CsvSampleWriter.java test/src/org/apache/jmeter/report/core/TestCsvSampleWriter.java
Date Wed, 16 Dec 2015 00:25:48 GMT
On 15 December 2015 at 20:39,  <fschumacher@apache.org> wrote:
> Author: fschumacher
> Date: Tue Dec 15 20:39:12 2015
> New Revision: 1720242
>
> URL: http://svn.apache.org/viewvc?rev=1720242&view=rev
> Log:
> Use Validate methods from commons lang3 and throw NPE instead of ArgumentNullException.
Add test cases.
>
> Added:
>     jmeter/trunk/test/src/org/apache/jmeter/report/core/TestCsvSampleWriter.java
> Modified:
>     jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleWriter.java
>
> Modified: jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleWriter.java
> URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleWriter.java?rev=1720242&r1=1720241&r2=1720242&view=diff
> ==============================================================================
> --- jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleWriter.java (original)
> +++ jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleWriter.java Tue Dec
15 20:39:12 2015
> @@ -22,6 +22,7 @@ import java.io.OutputStream;
>  import java.io.Writer;
>
>  import org.apache.commons.lang3.CharUtils;
> +import org.apache.commons.lang3.Validate;
>  import org.apache.jmeter.report.core.AbstractSampleWriter;
>  import org.apache.jmeter.report.core.Sample;
>  import org.apache.jmeter.report.core.SampleMetadata;
> @@ -39,6 +40,7 @@ import org.apache.jmeter.save.CSVSaveSer
>   */
>  public class CsvSampleWriter extends AbstractSampleWriter {
>
> +    private static final String MUST_NOT_BE_NULL = "%1s must not be null";
>      private int columnCount;
>
>      private char separator;
> @@ -48,9 +50,7 @@ public class CsvSampleWriter extends Abs
>      private long sampleCount;
>
>      public CsvSampleWriter(SampleMetadata metadata) {
> -        if (metadata == null) {
> -            throw new ArgumentNullException("metadata");
> -        }
> +        Validate.notNull(metadata, MUST_NOT_BE_NULL, "metadata");
>          this.metadata = metadata;
>          this.columnCount = metadata.getColumnCount();

If the null check is omitted, the above line will throw NPE.
I think it would be sufficient here to state that the argument must
not be null in the Javadoc.

>          this.separator = metadata.getSeparator();
> @@ -59,25 +59,19 @@ public class CsvSampleWriter extends Abs
>
>      public CsvSampleWriter(Writer output, SampleMetadata metadata) {
>          this(metadata);
> -        if (output == null) {
> -            throw new ArgumentNullException("output");
> -        }
> +        Validate.notNull(output, MUST_NOT_BE_NULL, "output");

This is different, as the NPE would not occur immediately.

>          setWriter(output);
>      }
>
>      public CsvSampleWriter(OutputStream output, SampleMetadata metadata) {
>          this(metadata);
> -        if (output == null) {
> -            throw new ArgumentNullException("output");
> -        }
> +        Validate.notNull(output, MUST_NOT_BE_NULL, "output");
>          setOutputStream(output);
>      }
>
>      public CsvSampleWriter(File output, SampleMetadata metadata) {
>          this(metadata);
> -        if (output == null) {
> -            throw new ArgumentNullException("output");
> -        }
> +        Validate.notNull(output, MUST_NOT_BE_NULL, "output");
>          setOutputFile(output);
>      }
>
> @@ -112,13 +106,8 @@ public class CsvSampleWriter extends Abs
>
>      @Override
>      public long write(Sample sample) {
> -        if (sample == null) {
> -            throw new ArgumentNullException("sample");
> -        }
> -        if (writer == null) {
> -            throw new IllegalStateException(
> -                    "No writer set! Call setWriter() first!");
> -        }
> +        Validate.notNull(sample, MUST_NOT_BE_NULL, "sample");
> +        Validate.validState(writer != null, "No writer set! Call setWriter() first!");
>
>          StringBuilder row = new StringBuilder();
>          char[] specials = new char[] { separator,
>
> Added: jmeter/trunk/test/src/org/apache/jmeter/report/core/TestCsvSampleWriter.java
> URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/report/core/TestCsvSampleWriter.java?rev=1720242&view=auto
> ==============================================================================
> --- jmeter/trunk/test/src/org/apache/jmeter/report/core/TestCsvSampleWriter.java (added)
> +++ jmeter/trunk/test/src/org/apache/jmeter/report/core/TestCsvSampleWriter.java Tue
Dec 15 20:39:12 2015
> @@ -0,0 +1,104 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements.  See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You under the Apache License, Version 2.0
> + * (the "License"); you may not use this file except in compliance with
> + * the License.  You may obtain a copy of the License at
> + *
> + *   http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + *
> + */
> +package org.apache.jmeter.report.core;
> +
> +import java.io.IOException;
> +import java.io.StringWriter;
> +import java.io.Writer;
> +
> +import org.apache.jmeter.util.JMeterUtils;
> +
> +import junit.framework.TestCase;
> +
> +public class TestCsvSampleWriter extends TestCase {
> +
> +    protected void setUp() throws Exception {
> +        // We have to initialize JMeterUtils
> +        JMeterUtils.loadJMeterProperties("jmeter.properties");
> +    };
> +
> +    SampleMetadata metadata = new SampleMetadata(',', "a", "b");
> +
> +    public void testCsvSampleWriterConstructorWithNull() throws Exception {
> +        try {
> +            CsvSampleWriter dummy = new CsvSampleWriter(null);
> +            dummy.close(); // We should never get here, but it would be a
> +                           // writer, so close it
> +            fail("NPE expected");
> +        } catch (NullPointerException e) {
> +            // OK, we should land here
> +        }
> +    }
> +
> +    public void testCsvSampleWriterConstructorWithWriter() throws Exception {
> +        try (Writer writer = new StringWriter();
> +                CsvSampleWriter csvWriter = new CsvSampleWriter(writer,
> +                        metadata)) {
> +            csvWriter.writeHeader();
> +            // need to replace the writer to flush the original one
> +            replaceWriter(csvWriter);
> +            assertEquals("a,b\n", writer.toString());
> +        }
> +    }
> +
> +    private void replaceWriter(CsvSampleWriter csvWriter) throws IOException {
> +        try (Writer replacement = new StringWriter()) {
> +            csvWriter.setWriter(replacement);
> +        }
> +    }
> +
> +    public void testWriteWithoutWriter() throws Exception {
> +        try (CsvSampleWriter csvWriter = new CsvSampleWriter(metadata)) {
> +            Sample sample = new SampleBuilder(metadata).add("a1").add("b1")
> +                    .build();
> +            try {
> +                csvWriter.write(sample);
> +                fail("ISE expected");
> +            } catch (IllegalStateException e) {
> +                // OK, we should land here
> +            }
> +        }
> +    }
> +
> +    public void testWriteWithoutSample() throws Exception {
> +        try (Writer writer = new StringWriter();
> +                CsvSampleWriter csvWriter = new CsvSampleWriter(writer,
> +                        metadata)) {
> +            try {
> +                csvWriter.write(null);
> +                fail("NPE expected");
> +            } catch (NullPointerException e) {
> +                assertEquals("sample must not be null", e.getMessage());
> +            }
> +        }
> +    }
> +
> +    public void testWrite() throws Exception {
> +        try (Writer writer = new StringWriter();
> +                CsvSampleWriter csvWriter = new CsvSampleWriter(writer,
> +                        metadata)) {
> +            Sample sample = new SampleBuilder(metadata).add("a1").add("b1")
> +                    .build();
> +            csvWriter.write(sample);
> +            // need to replace the writer to flush the original one
> +            replaceWriter(csvWriter);
> +            assertEquals("a1,b1\n", writer.toString());
> +        }
> +    }
> +
> +}
>
>

Mime
View raw message