jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject svn commit: r1783620 - in /jmeter/trunk: docs/images/screenshots/dns-cache-manager.png src/core/org/apache/jmeter/report/core/CsvSampleReader.java test/src/org/apache/jmeter/report/core/CsvSampleReaderTest.java
Date Sun, 19 Feb 2017 10:38:43 GMT
Author: fschumacher
Date: Sun Feb 19 10:38:43 2017
New Revision: 1783620

URL: http://svn.apache.org/viewvc?rev=1783620&view=rev
Log:
Close all streams in case of an exception and add tests.

Added:
    jmeter/trunk/test/src/org/apache/jmeter/report/core/CsvSampleReaderTest.java   (with props)
Modified:
    jmeter/trunk/docs/images/screenshots/dns-cache-manager.png
    jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleReader.java

Modified: jmeter/trunk/docs/images/screenshots/dns-cache-manager.png
URL: http://svn.apache.org/viewvc/jmeter/trunk/docs/images/screenshots/dns-cache-manager.png?rev=1783620&r1=1783619&r2=1783620&view=diff
==============================================================================
Binary files - no diff available.

Modified: jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleReader.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleReader.java?rev=1783620&r1=1783619&r2=1783620&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleReader.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/report/core/CsvSampleReader.java Sun Feb 19 10:38:43
2017
@@ -23,7 +23,9 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
 
@@ -103,10 +105,16 @@ public class CsvSampleReader implements
                     + " does not exist or is not readable");
         }
         this.file = inputFile;
+        InputStream fis = null;
+        Reader isr = null;
         try {
-            this.reader = new BufferedReader(new InputStreamReader(
-                    new FileInputStream(file), CHARSET), BUF_SIZE);
+            fis = new FileInputStream(file);
+            isr = new InputStreamReader(fis, CHARSET);
+            this.reader = new BufferedReader(isr, BUF_SIZE);
         } catch (FileNotFoundException | UnsupportedEncodingException ex) {
+            JOrphanUtils.closeQuietly(isr);
+            JOrphanUtils.closeQuietly(fis);
+            JOrphanUtils.closeQuietly(this.reader);
             throw new SampleException("Could not create file reader !", ex);
         }
         if (metadata == null) {

Added: jmeter/trunk/test/src/org/apache/jmeter/report/core/CsvSampleReaderTest.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/report/core/CsvSampleReaderTest.java?rev=1783620&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/report/core/CsvSampleReaderTest.java (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/report/core/CsvSampleReaderTest.java Sun Feb 19
10:38:43 2017
@@ -0,0 +1,130 @@
+/*
+ * 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 static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.jmeter.util.JMeterUtils;
+import org.hamcrest.CoreMatchers;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CsvSampleReaderTest {
+
+    private static final int NR_ROWS = 100;
+    private File tempCsv;
+    private SampleMetadata metadata = new SampleMetadata(',', "a","b");
+
+    @Before
+    public void setUp() throws IOException {
+        // We have to initialize JMeterUtils
+        if (JMeterUtils.getJMeterHome() == null) {
+            JMeterUtils.setJMeterHome(System.getenv("JMETER_HOME"));
+        }
+        JMeterUtils.loadJMeterProperties(
+                JMeterUtils.getJMeterBinDir() + "/jmeter.properties");
+        tempCsv = File.createTempFile("samplecsv", ".csv");
+        try (CsvSampleWriter writer = new CsvSampleWriter(tempCsv, metadata)) {
+            writer.setSeparator(',');
+            for (long i = 0; i < NR_ROWS; i++) {
+                writer.write(new SampleBuilder(metadata).add(i).add("a" + i)
+                        .build());
+            }
+        }
+    }
+
+    @After
+    public void tearDown() {
+        if (tempCsv.exists()) {
+            tempCsv.delete();
+        }
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testConstructorWithInvalidFile() throws Exception {
+        try (CsvSampleReader csv = new CsvSampleReader(
+                new File("/not/available.csv"), metadata)) {
+            Assert.fail("File should not be readable and therefor illegal");
+        }
+    }
+
+    @Test
+    public void testGetMetadata() {
+        try (CsvSampleReader reader = new CsvSampleReader(tempCsv, metadata)) {
+            Assert.assertThat(reader.getMetadata().toString(),
+                    CoreMatchers.is(metadata.toString()));
+        }
+    }
+
+    @Test
+    public void testReadSample() {
+        try (CsvSampleReader reader = new CsvSampleReader(tempCsv, metadata)) {
+            for (long i = 0; i < NR_ROWS; i++) {
+                Sample expected = new SampleBuilder(metadata).add(i)
+                        .add("a" + i).build();
+                Assert.assertThat(reader.readSample().toString(),
+                        CoreMatchers.is(expected.toString()));
+            }
+        }
+    }
+
+    @Test
+    public void testPeek() {
+        try (CsvSampleReader reader = new CsvSampleReader(tempCsv, metadata)) {
+            for (long i = 0; i < NR_ROWS; i++) {
+                Sample expected = new SampleBuilder(metadata).add(i)
+                        .add("a" + i).build();
+                Assert.assertThat(reader.peek().toString(),
+                        CoreMatchers.is(expected.toString()));
+                reader.readSample();
+            }
+        }
+    }
+
+    @Test
+    public void testHasNext() {
+        try (CsvSampleReader reader = new CsvSampleReader(tempCsv, metadata)) {
+            for (long i = 0; i < NR_ROWS; i++) {
+                Sample expected = new SampleBuilder(metadata).add(i)
+                        .add("a" + i).build();
+                Assert.assertTrue(reader.hasNext());
+                reader.readSample();
+            }
+            Assert.assertFalse(reader.hasNext());
+        }
+    }
+
+    @Test
+    public void testClose() {
+        CsvSampleReader reader = new CsvSampleReader(tempCsv, metadata);
+        reader.close();
+        try {
+            reader.readSample();
+            fail("Stream should be closed.");
+        } catch (SampleException e) {
+            // All is well
+        }
+    }
+
+}

Propchange: jmeter/trunk/test/src/org/apache/jmeter/report/core/CsvSampleReaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message