camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-9022: camel-csv should not try to convert to list/map by just iterate the body given in marshal
Date Mon, 27 Jul 2015 12:55:27 GMT
Repository: camel
Updated Branches:
  refs/heads/master 08a5722f5 -> 25f0078c1


CAMEL-9022: camel-csv should not try to convert to list/map by just iterate the body given
in marshal


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

Branch: refs/heads/master
Commit: 25f0078c12209feeb1672e32bbaf47307d79b518
Parents: 08a5722
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Mon Jul 27 15:02:36 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Mon Jul 27 15:02:36 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/dataformat/csv/CsvMarshaller.java    | 14 +++++++-------
 .../apache/camel/dataformat/csv/CsvMarshalTest.java   | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/25f0078c/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java
b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java
index d04fb0c..f23f1ec 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java
@@ -21,6 +21,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
@@ -29,6 +30,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVPrinter;
 
@@ -74,13 +76,10 @@ abstract class CsvMarshaller {
     public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws
NoTypeConversionAvailableException, IOException {
         CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(outputStream), format);
         try {
-            List<?> list = exchange.getContext().getTypeConverter().tryConvertTo(List.class,
exchange, object);
-            if (list != null) {
-                for (Object child : list) {
-                    printer.printRecord(getRecordValues(exchange, child));
-                }
-            } else {
-                printer.printRecord(getRecordValues(exchange, object));
+            Iterator it = ObjectHelper.createIterator(object);
+            while (it.hasNext()) {
+                Object child = it.next();
+                printer.printRecord(getRecordValues(exchange, child));
             }
         } finally {
             IOHelper.close(printer);
@@ -88,6 +87,7 @@ abstract class CsvMarshaller {
     }
 
     private Iterable<?> getRecordValues(Exchange exchange, Object data) throws NoTypeConversionAvailableException
{
+        // each row must be a map or list based
         Map<?, ?> map = exchange.getContext().getTypeConverter().tryConvertTo(Map.class,
exchange, data);
         if (map != null) {
             return getMapRecordValues(map);

http://git-wip-us.apache.org/repos/asf/camel/blob/25f0078c/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalTest.java
b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalTest.java
index 580e7e0..f232ad5 100644
--- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalTest.java
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalTest.java
@@ -49,6 +49,20 @@ public class CsvMarshalTest extends CamelTestSupport {
     }
 
     @Test
+    public void shouldMarshalListsOneRow() throws Exception {
+        output.expectedMessageCount(1);
+
+        template.sendBody("direct:default", Arrays.<List>asList(
+                Arrays.asList("1"),
+                Arrays.asList("one")
+        ));
+        output.assertIsSatisfied();
+
+        String[] actuals = readOutputLines();
+        assertArrayEquals(new String[]{"1", "one"}, actuals);
+    }
+
+    @Test
     public void shouldMarshalMaps() throws Exception {
         output.expectedMessageCount(1);
 


Mime
View raw message