camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bvah...@apache.org
Subject git commit: CAMEL-7098: Cleaned up the CSV DataFormat unmarshalling logic. Added one more test to CsvUnmarshalSkipFirstLineTest. Also polished CsvUnmarshalStreamSpringTest and added more asserts to it.
Date Fri, 27 Dec 2013 07:29:03 GMT
Updated Branches:
  refs/heads/master 2db92cdbc -> 56acebd43


CAMEL-7098: Cleaned up the CSV DataFormat unmarshalling logic. Added one more test to CsvUnmarshalSkipFirstLineTest.
Also polished CsvUnmarshalStreamSpringTest and added more asserts to it.

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/56acebd4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/56acebd4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/56acebd4

Branch: refs/heads/master
Commit: 56acebd431dad1211ca68e5449c81029411d6634
Parents: 2db92cd
Author: Babak Vahdat <bvahdat@apache.org>
Authored: Fri Dec 27 08:28:53 2013 +0100
Committer: Babak Vahdat <bvahdat@apache.org>
Committed: Fri Dec 27 08:28:53 2013 +0100

----------------------------------------------------------------------
 .../camel/dataformat/csv/CsvDataFormat.java     | 58 ++++++++------------
 .../camel/dataformat/csv/CsvIterator.java       | 11 ++--
 .../csv/CsvUnmarshalSkipFirstLineTest.java      | 21 +++++--
 .../csv/CsvUnmarshalStreamSpringTest.java       | 20 ++++---
 4 files changed, 59 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/56acebd4/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
index aff2d2a..ae172db 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
@@ -16,24 +16,20 @@
  */
 package org.apache.camel.dataformat.csv;
 
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.NoSuchElementException;
 import java.util.Set;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.csv.CSVParser;
 import org.apache.commons.csv.CSVStrategy;
 import org.apache.commons.csv.writer.CSVConfig;
@@ -102,46 +98,40 @@ public class CsvDataFormat implements DataFormat {
         }
         strategy.setDelimiter(config.getDelimiter());
 
-        InputStreamReader in = new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange));
-        boolean closeStream = false;
-        CsvIterator csvIterator = null;
+        InputStreamReader in = null;
+        boolean error = false;
         try {
-            CSVParser parser = createParser(in);
-            if (parser == null) {
-                closeStream = true;
-                // return an empty Iterator
-                return ObjectHelper.createIterator(null);
+            in = new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange));
+            CSVParser parser = new CSVParser(in, strategy);
+
+            if (skipFirstLine) {
+                // read one line ahead
+                parser.getLine();
             }
-            csvIterator = new CsvIterator(parser, in);
-        } catch (IOException ioe) {
-            closeStream = true;
+
+            CsvIterator csvIterator = new CsvIterator(parser, in);
+            return lazyLoad ? csvIterator : loadAllAsList(csvIterator);
+        } catch (Exception e) {
+            error = true;
+            throw e;
         } finally {
-            if (closeStream) {
+            if (error) {
                 IOHelper.close(in);
             }
         }
-        if (lazyLoad) {
-            return csvIterator;
-        }
-        return loadAllAsList(csvIterator);
     }
 
-    private CSVParser createParser(InputStreamReader in) throws IOException {
-        CSVParser parser = new CSVParser(in, strategy);
-        if (skipFirstLine) {
-            if (parser.getLine() == null) {
-                return null;
+    private List<List<String>> loadAllAsList(CsvIterator iter) {
+        try {
+            List<List<String>> list = new ArrayList<List<String>>();
+            while (iter.hasNext()) {
+                list.add(iter.next());
             }
+            return list;
+        } finally {
+            // close the stream as we've loaded all the data upfront
+            IOHelper.close(iter);
         }
-        return parser;
-    }
-
-    private List<List<String>> loadAllAsList(CsvIterator iter) throws IOException
{
-        List<List<String>> list = new ArrayList<List<String>>();
-        while (iter.hasNext()) {
-            list.add(iter.next());
-        }
-        return list;
     }
 
     public String getDelimiter() {

http://git-wip-us.apache.org/repos/asf/camel/blob/56acebd4/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvIterator.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvIterator.java
b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvIterator.java
index e9d4c41..6b732da 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvIterator.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvIterator.java
@@ -36,8 +36,7 @@ public class CsvIterator implements Iterator<List<String>>,
Closeable {
     private final InputStreamReader in;
     private String[] line;
 
-    public CsvIterator(CSVParser parser, InputStreamReader in) throws IOException
-    {
+    public CsvIterator(CSVParser parser, InputStreamReader in) throws IOException {
         this.parser = parser;
         this.in = in;
         line = parser.getLine();
@@ -58,11 +57,11 @@ public class CsvIterator implements Iterator<List<String>>,
Closeable {
             line = parser.getLine();
         } catch (IOException e) {
             line = null;
-            IOHelper.close(in);
+            close();
             throw new IllegalStateException(e);
         }
         if (line == null) {
-            IOHelper.close(in);
+            close();
         }
         return result;
     }
@@ -73,7 +72,7 @@ public class CsvIterator implements Iterator<List<String>>,
Closeable {
     }
 
     @Override
-    public void close() throws IOException {
-        in.close();
+    public void close() {
+        IOHelper.close(in);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/56acebd4/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java
b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java
index 34dcdc4..800d949 100644
--- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java
@@ -27,7 +27,7 @@ import org.junit.Test;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 /**
- * Spring based integration test for the <code>CsvDataFormat</code> demonstrating
the usage of
+ * Spring based test for the <code>CsvDataFormat</code> demonstrating the usage
of
  * the <tt>skipFirstLine</tt> option.
  */
 public class CsvUnmarshalSkipFirstLineTest extends CamelSpringTestSupport {
@@ -60,8 +60,8 @@ public class CsvUnmarshalSkipFirstLineTest extends CamelSpringTestSupport
{
     public void testCsvUnMarshalSingleLine() throws Exception {
         result.expectedMessageCount(1);
 
-        // the first line, the same as the second line as well, contains also a data row
but as we set
-        // skipFirstLine to true the first row gets simply ignored and not unmarshalled
+        // the first line contains a data row but as we set skipFirstLine
+        // to true the first line gets simply skipped and not unmarshalled
         template.sendBody("direct:start", "124|ActiveMQ in Action|2\n123|Camel in Action|1");
 
         assertMockEndpointsSatisfied();
@@ -72,7 +72,20 @@ public class CsvUnmarshalSkipFirstLineTest extends CamelSpringTestSupport
{
         assertEquals("Camel in Action", body.get(0).get(1));
         assertEquals("1", body.get(0).get(2));
     }
-    
+
+    @Test
+    public void testCsvUnMarshalNoLine() throws Exception {
+        result.expectedMessageCount(1);
+
+        // the first and last line we intend to skip
+        template.sendBody("direct:start", "123|Camel in Action|1\n");
+
+        assertMockEndpointsSatisfied();
+
+        List<?> body = result.getReceivedExchanges().get(0).getIn().getBody(List.class);
+        assertEquals(0, body.size());
+    }
+
     @Override
     protected ClassPathXmlApplicationContext createApplicationContext() {
         return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineSpringTest-context.xml");

http://git-wip-us.apache.org/repos/asf/camel/blob/56acebd4/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamSpringTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamSpringTest.java
b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamSpringTest.java
index 17465b6..42014d1 100644
--- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamSpringTest.java
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamSpringTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.dataformat.csv;
 
 import java.util.Arrays;
 import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 import org.apache.camel.EndpointInject;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -33,7 +34,6 @@ public class CsvUnmarshalStreamSpringTest extends CamelSpringTestSupport
{
     @EndpointInject(uri = "mock:result")
     private MockEndpoint result;
 
-    @SuppressWarnings("unchecked")
     @Test
     public void testCsvUnMarshal() throws Exception {
         result.expectedMessageCount(1);
@@ -42,15 +42,21 @@ public class CsvUnmarshalStreamSpringTest extends CamelSpringTestSupport
{
 
         assertMockEndpointsSatisfied();
 
-        Iterator<String> body = result.getReceivedExchanges().get(0)
-                .getIn().getBody(Iterator.class);
-        assertEquals(CsvIterator.class, body.getClass());
-        assertEquals(Arrays.asList(MESSAGE), body.next());
+        Iterator<?> body = result.getReceivedExchanges().get(0).getIn().getBody(Iterator.class);
+        CsvIterator iterator = assertIsInstanceOf(CsvIterator.class, body);
+        assertTrue(iterator.hasNext());
+        assertEquals(Arrays.asList(MESSAGE), iterator.next());
+        assertFalse(iterator.hasNext());
+        try {
+            iterator.next();
+            fail("Should have thrown exception");
+        } catch (NoSuchElementException nsee) {
+            // expected
+        }
     }
 
     @Override
     protected AbstractApplicationContext createApplicationContext() {
-        return new ClassPathXmlApplicationContext(
-                "org/apache/camel/dataformat/csv/CsvUnmarshalStreamSpringTest-context.xml");
+        return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvUnmarshalStreamSpringTest-context.xml");
     }
 }


Mime
View raw message