You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/01/09 09:58:52 UTC

[1/2] git commit: CAMEL-6969 Add an option on the CSV unmarshalling in order to generate Maps with headers as keys

Updated Branches:
  refs/heads/master f85d3e56a -> f4e8992f3


CAMEL-6969 Add an option on the CSV unmarshalling in order to generate Maps with headers as keys


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

Branch: refs/heads/master
Commit: 1ea2dbb2642e7b896272de7f3dc47ec473fc3949
Parents: f85d3e5
Author: Antoine DESSAIGNE <an...@gmail.com>
Authored: Wed Jan 8 16:58:09 2014 +0100
Committer: Antoine DESSAIGNE <an...@gmail.com>
Committed: Wed Jan 8 16:58:09 2014 +0100

----------------------------------------------------------------------
 .../camel/model/dataformat/CsvDataFormat.java   |  16 ++-
 .../camel/dataformat/csv/CsvDataFormat.java     |  40 ++++++--
 .../camel/dataformat/csv/CsvIterator.java       |  12 +--
 .../camel/dataformat/csv/CsvLineConverter.java  |  32 ++++++
 .../camel/dataformat/csv/CsvLineConverters.java | 101 +++++++++++++++++++
 .../camel/dataformat/csv/CsvIteratorTest.java   |  28 +++--
 .../dataformat/csv/CsvLineConvertersTest.java   |  68 +++++++++++++
 .../dataformat/csv/CsvUnmarshalMapLineTest.java |  73 ++++++++++++++
 .../CsvUnmarshalMapLineSpringTest-context.xml   |  32 ++++++
 9 files changed, 369 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java
index cfd4f6b..b6fdae0 100644
--- a/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java
@@ -31,7 +31,7 @@ import org.apache.camel.util.ObjectHelper;
 /**
  * Represents a CSV (Comma Separated Values) {@link org.apache.camel.spi.DataFormat}
  *
- * @version 
+ * @version
  */
 @XmlRootElement(name = "csv")
 @XmlAccessorType(XmlAccessType.FIELD)
@@ -48,6 +48,8 @@ public class CsvDataFormat extends DataFormatDefinition {
     private Boolean skipFirstLine;
     @XmlAttribute
     private Boolean lazyLoad;
+    @XmlAttribute
+    private Boolean useMaps;
 
     public CsvDataFormat() {
         super("csv");
@@ -111,6 +113,14 @@ public class CsvDataFormat extends DataFormatDefinition {
         this.lazyLoad = lazyLoad;
     }
 
+    public Boolean getUseMaps() {
+        return useMaps;
+    }
+
+    public void setUseMaps(Boolean useMaps) {
+        this.useMaps = useMaps;
+    }
+
     @Override
     protected DataFormat createDataFormat(RouteContext routeContext) {
         DataFormat csvFormat = super.createDataFormat(routeContext);
@@ -150,5 +160,9 @@ public class CsvDataFormat extends DataFormatDefinition {
         if (lazyLoad != null) {
             setProperty(camelContext, dataFormat, "lazyLoad", lazyLoad);
         }
+
+        if (useMaps != null) {
+            setProperty(camelContext, dataFormat, "useMaps", useMaps);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/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 649bc81..848ac49 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
@@ -46,7 +46,7 @@ import org.apache.commons.csv.writer.CSVWriter;
  * Autogeneration can be disabled. In this case, only the fields defined in
  * csvConfig are written on the output.
  *
- * @version 
+ * @version
  */
 public class CsvDataFormat implements DataFormat {
     private CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
@@ -58,6 +58,7 @@ public class CsvDataFormat implements DataFormat {
      * Lazy row loading with iterator for big files.
      */
     private boolean lazyLoad;
+    private boolean useMaps;
 
     public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws Exception {
         if (delimiter != null) {
@@ -105,12 +106,18 @@ public class CsvDataFormat implements DataFormat {
             reader = IOHelper.buffered(new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange)));
             CSVParser parser = new CSVParser(reader, strategy);
 
-            if (skipFirstLine) {
-                // read one line ahead and skip it
-                parser.getLine();
+            CsvLineConverter<?> lineConverter;
+            if (useMaps) {
+                lineConverter = CsvLineConverters.getMapLineConverter(parser.getLine());
+            } else {
+                lineConverter = CsvLineConverters.getListConverter();
+                if (skipFirstLine) {
+                    // read one line ahead and skip it
+                    parser.getLine();
+                }
             }
 
-            CsvIterator csvIterator = new CsvIterator(parser, reader);
+            @SuppressWarnings("unchecked") CsvIterator<?> csvIterator = new CsvIterator(parser, reader, lineConverter);
             return lazyLoad ? csvIterator : loadAllAsList(csvIterator);
         } catch (Exception e) {
             error = true;
@@ -122,9 +129,9 @@ public class CsvDataFormat implements DataFormat {
         }
     }
 
-    private List<List<String>> loadAllAsList(CsvIterator iter) {
+    private <T> List<T> loadAllAsList(CsvIterator<T> iter) {
         try {
-            List<List<String>> list = new ArrayList<List<String>>();
+            List<T> list = new ArrayList<T>();
             while (iter.hasNext()) {
                 list.add(iter.next());
             }
@@ -145,7 +152,7 @@ public class CsvDataFormat implements DataFormat {
         }
         this.delimiter = delimiter;
     }
-    
+
     public CSVConfig getConfig() {
         return config;
     }
@@ -191,6 +198,20 @@ public class CsvDataFormat implements DataFormat {
         this.lazyLoad = lazyLoad;
     }
 
+    public boolean isUseMaps() {
+        return useMaps;
+    }
+
+    /**
+     * Sets whether or not the result of the unmarshalling should be a {@code java.util.Map} instead of a {@code java.util.List}. It uses the first line as a
+     * header line and uses it as keys of the maps.
+     *
+     * @param useMaps {@code true} in order to use {@code java.util.Map} instead of {@code java.util.List}, {@code false} otherwise.
+     */
+    public void setUseMaps(boolean useMaps) {
+        this.useMaps = useMaps;
+    }
+
     private synchronized void updateFieldsInConfig(Set<?> set, Exchange exchange) {
         for (Object value : set) {
             if (value != null) {
@@ -203,5 +224,4 @@ public class CsvDataFormat implements DataFormat {
             }
         }
     }
-    
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/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 fece082..9d26edb 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
@@ -20,9 +20,7 @@ package org.apache.camel.dataformat.csv;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.Reader;
-import java.util.Arrays;
 import java.util.Iterator;
-import java.util.List;
 import java.util.NoSuchElementException;
 
 import org.apache.camel.util.IOHelper;
@@ -30,15 +28,17 @@ import org.apache.commons.csv.CSVParser;
 
 /**
  */
-public class CsvIterator implements Iterator<List<String>>, Closeable {
+public class CsvIterator<T> implements Iterator<T>, Closeable {
 
     private final CSVParser parser;
     private final Reader reader;
+    private final CsvLineConverter<T> lineConverter;
     private String[] line;
 
-    public CsvIterator(CSVParser parser, Reader reader) throws IOException {
+    public CsvIterator(CSVParser parser, Reader reader, CsvLineConverter<T> lineConverter) throws IOException {
         this.parser = parser;
         this.reader = reader;
+        this.lineConverter = lineConverter;
         line = parser.getLine();
     }
 
@@ -48,11 +48,11 @@ public class CsvIterator implements Iterator<List<String>>, Closeable {
     }
 
     @Override
-    public List<String> next() {
+    public T next() {
         if (!hasNext()) {
             throw new NoSuchElementException();
         }
-        List<String> result = Arrays.asList(line);
+        T result = lineConverter.convertLine(line);
         try {
             line = parser.getLine();
         } catch (IOException e) {

http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java
new file mode 100644
index 0000000..7f91636
--- /dev/null
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java
@@ -0,0 +1,32 @@
+/**
+ * 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.camel.dataformat.csv;
+
+/**
+ * This interface helps converting a single CSV line into another representation.
+ *
+ * @param <T> Class for representing a single line
+ */
+public interface CsvLineConverter<T> {
+    /**
+     * Converts a single CSV line.
+     *
+     * @param line CSV line
+     * @return Another representation of the CSV line
+     */
+    public T convertLine(String[] line);
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java
new file mode 100644
index 0000000..1f3ad30
--- /dev/null
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java
@@ -0,0 +1,101 @@
+/**
+ * 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.camel.dataformat.csv;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * This {@code CsvLineConverters} class provides common implementations of the {@code CsvLineConverter} interface.
+ */
+public final class CsvLineConverters {
+    /**
+     * Provides an implementation of {@code CsvLineConverter} that converts a line into a {@code List}.
+     *
+     * @return List-based {@code CsvLineConverter} implementation
+     */
+    public static CsvLineConverter<List<String>> getListConverter() {
+        return ListLineConverter.SINGLETON;
+    }
+
+    /**
+     * Provides an implementation of {@code CsvLineConverter} that converts a line into a {@code Map}.
+     * <p/>
+     * It requires to have unique {@code headers} values as well as the same number of item in each line.
+     *
+     * @param headers Headers of the CSV file
+     * @return Map-based {@code CsvLineConverter} implementation
+     */
+    public static CsvLineConverter<Map<String, String>> getMapLineConverter(String[] headers) {
+        return new MapLineConverter(headers);
+    }
+
+    private static class ListLineConverter implements CsvLineConverter<List<String>> {
+        public static final ListLineConverter SINGLETON = new ListLineConverter();
+
+        @Override
+        public List<String> convertLine(String[] line) {
+            return Arrays.asList(line);
+        }
+    }
+
+    private static class MapLineConverter implements CsvLineConverter<Map<String, String>> {
+        private final String[] headers;
+
+        private MapLineConverter(String[] headers) {
+            this.headers = checkHeaders(headers);
+        }
+
+        @Override
+        public Map<String, String> convertLine(String[] line) {
+            if (line.length != headers.length) {
+                throw new IllegalStateException("This line does not have the same number of items than the header");
+            }
+
+            Map<String, String> result = new HashMap<String, String>(line.length);
+            for (int i = 0; i < line.length; i++) {
+                result.put(headers[i], line[i]);
+            }
+            return result;
+        }
+
+        private static String[] checkHeaders(String[] headers) {
+            // Check that we have headers
+            if (headers == null || headers.length == 0) {
+                throw new IllegalArgumentException("Missing headers for the CSV parsing");
+            }
+
+            // Check that there is no duplicates
+            Set<String> headerSet = new HashSet<String>(headers.length);
+            Collections.addAll(headerSet, headers);
+            if (headerSet.size() != headers.length) {
+                throw new IllegalArgumentException("There are duplicate headers");
+            }
+
+            return headers;
+        }
+    }
+
+    private CsvLineConverters() {
+        // Prevent instantiation
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java
index 091faf6..cd5425a 100644
--- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java
@@ -19,29 +19,27 @@ package org.apache.camel.dataformat.csv;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Arrays;
+import java.util.List;
 import java.util.NoSuchElementException;
-
-import mockit.Expectations;
-import mockit.Injectable;
 import org.apache.commons.csv.CSVParser;
 import org.junit.Assert;
 import org.junit.Test;
+import mockit.Expectations;
+import mockit.Injectable;
 
 public class CsvIteratorTest {
 
     public static final String HDD_CRASH = "HDD crash";
 
     @Test
-    public void closeIfError(
-            @Injectable final  InputStreamReader reader,
-            @Injectable final CSVParser parser) throws IOException {
+    public void closeIfError(@Injectable final InputStreamReader reader, @Injectable final CSVParser parser) throws IOException {
         new Expectations() {
             {
                 parser.getLine();
-                result = new String[] {"1"};
+                result = new String[]{"1"};
 
                 parser.getLine();
-                result = new String[] {"2"};
+                result = new String[]{"2"};
 
                 parser.getLine();
                 result = new IOException(HDD_CRASH);
@@ -52,7 +50,7 @@ public class CsvIteratorTest {
         };
 
         @SuppressWarnings("resource")
-        CsvIterator iterator = new CsvIterator(parser, reader);
+        CsvIterator<List<String>> iterator = new CsvIterator<List<String>>(parser, reader, CsvLineConverters.getListConverter());
         Assert.assertTrue(iterator.hasNext());
         Assert.assertEquals(Arrays.asList("1"), iterator.next());
         Assert.assertTrue(iterator.hasNext());
@@ -75,15 +73,14 @@ public class CsvIteratorTest {
     }
 
     @Test
-    public void normalCycle(@Injectable final InputStreamReader reader,
-                            @Injectable final CSVParser parser) throws IOException {
+    public void normalCycle(@Injectable final InputStreamReader reader, @Injectable final CSVParser parser) throws IOException {
         new Expectations() {
             {
                 parser.getLine();
-                result = new String[] {"1"};
+                result = new String[]{"1"};
 
                 parser.getLine();
-                result = new String[] {"2"};
+                result = new String[]{"2"};
 
                 parser.getLine();
                 result = null;
@@ -92,9 +89,9 @@ public class CsvIteratorTest {
                 reader.close();
             }
         };
-       
+
         @SuppressWarnings("resource")
-        CsvIterator iterator = new CsvIterator(parser, reader);
+        CsvIterator<List<String>> iterator = new CsvIterator<List<String>>(parser, reader, CsvLineConverters.getListConverter());
         Assert.assertTrue(iterator.hasNext());
         Assert.assertEquals(Arrays.asList("1"), iterator.next());
 
@@ -109,6 +106,5 @@ public class CsvIteratorTest {
         } catch (NoSuchElementException e) {
             // okay
         }
-        
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvLineConvertersTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvLineConvertersTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvLineConvertersTest.java
new file mode 100644
index 0000000..fcb2c4f
--- /dev/null
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvLineConvertersTest.java
@@ -0,0 +1,68 @@
+/**
+ * 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.camel.dataformat.csv;
+
+import java.util.List;
+import java.util.Map;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class CsvLineConvertersTest {
+    @Test
+    public void shouldConvertAsList() {
+        CsvLineConverter<?> converter = CsvLineConverters.getListConverter();
+
+        Object result = converter.convertLine(new String[]{"foo", "bar"});
+
+        assertTrue(result instanceof List);
+        List list = (List) result;
+        assertEquals(2, list.size());
+        assertEquals("foo", list.get(0));
+        assertEquals("bar", list.get(1));
+    }
+
+    @Test
+    public void shouldConvertAsMap() {
+        CsvLineConverter<?> converter = CsvLineConverters.getMapLineConverter(new String[]{"HEADER_1", "HEADER_2"});
+
+        Object result = converter.convertLine(new String[]{"foo", "bar"});
+
+        assertTrue(result instanceof Map);
+        Map map = (Map) result;
+        assertEquals(2, map.size());
+        assertEquals("foo", map.get("HEADER_1"));
+        assertEquals("bar", map.get("HEADER_2"));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void shouldNotConvertAsMapWithNullHeaders() {
+        CsvLineConverters.getMapLineConverter(null);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void shouldNotConvertAsMapWithNoHeaders() {
+        CsvLineConverters.getMapLineConverter(new String[]{});
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void shouldNotConvertAsMapWithInvalidLine() {
+        CsvLineConverter<?> converter = CsvLineConverters.getMapLineConverter(new String[]{"HEADER_1", "HEADER_2"});
+
+        converter.convertLine(new String[]{"foo"});
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineTest.java
new file mode 100644
index 0000000..5f02dc3
--- /dev/null
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineTest.java
@@ -0,0 +1,73 @@
+/**
+ * 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.camel.dataformat.csv;
+
+import java.util.List;
+import java.util.Map;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Spring based test for the <code>CsvDataFormat</code> demonstrating the usage of
+ * the <tt>useMaps</tt> option.
+ */
+public class CsvUnmarshalMapLineTest extends CamelSpringTestSupport {
+
+    @EndpointInject(uri = "mock:result")
+    private MockEndpoint result;
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testCsvUnMarshal() throws Exception {
+        result.expectedMessageCount(1);
+
+        // the first line contains the column names which we intend to skip
+        template.sendBody("direct:start", "OrderId|Item|Amount\n123|Camel in Action|1\n124|ActiveMQ in Action|2");
+
+        assertMockEndpointsSatisfied();
+
+        List<Map<String, String>> body = result.getReceivedExchanges().get(0).getIn().getBody(List.class);
+        assertEquals(2, body.size());
+        assertEquals("123", body.get(0).get("OrderId"));
+        assertEquals("Camel in Action", body.get(0).get("Item"));
+        assertEquals("1", body.get(0).get("Amount"));
+        assertEquals("124", body.get(1).get("OrderId"));
+        assertEquals("ActiveMQ in Action", body.get(1).get("Item"));
+        assertEquals("2", body.get(1).get("Amount"));
+    }
+
+    @Test
+    public void testCsvUnMarshalNoLine() throws Exception {
+        result.expectedMessageCount(1);
+
+        // the first and last line we intend to skip
+        template.sendBody("direct:start", "OrderId|Item|Amount\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/CsvUnmarshalMapLineSpringTest-context.xml");
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1ea2dbb2/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest-context.xml
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest-context.xml b/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest-context.xml
new file mode 100644
index 0000000..17e7325
--- /dev/null
+++ b/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest-context.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+  <!--
+    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.
+  -->
+<beans xmlns="http://www.springframework.org/schema/beans"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="
+    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
+
+  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+    <route>
+      <from uri="direct:start" />
+      <unmarshal>
+        <csv delimiter="|" useMaps="true" />
+      </unmarshal>
+      <to uri="mock:result" />
+    </route>
+  </camelContext>
+</beans>


[2/2] git commit: CAMEL-6969: Fixed CS

Posted by da...@apache.org.
CAMEL-6969: Fixed CS


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

Branch: refs/heads/master
Commit: f4e8992f37d4a304582d9d499ae64b099a945ad6
Parents: 1ea2dbb
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Jan 9 10:02:47 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Jan 9 10:02:47 2014 +0100

----------------------------------------------------------------------
 .../apache/camel/dataformat/csv/CsvLineConverter.java    |  3 ++-
 .../apache/camel/dataformat/csv/CsvLineConverters.java   | 11 ++++++-----
 .../org/apache/camel/dataformat/csv/CsvIteratorTest.java |  5 +++--
 3 files changed, 11 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f4e8992f/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java
index 7f91636..8bc3c67 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverter.java
@@ -22,11 +22,12 @@ package org.apache.camel.dataformat.csv;
  * @param <T> Class for representing a single line
  */
 public interface CsvLineConverter<T> {
+
     /**
      * Converts a single CSV line.
      *
      * @param line CSV line
      * @return Another representation of the CSV line
      */
-    public T convertLine(String[] line);
+    T convertLine(String[] line);
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/f4e8992f/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java
index 1f3ad30..bba71d4 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvLineConverters.java
@@ -28,6 +28,10 @@ import java.util.Set;
  * This {@code CsvLineConverters} class provides common implementations of the {@code CsvLineConverter} interface.
  */
 public final class CsvLineConverters {
+
+    private CsvLineConverters() {
+        // Prevent instantiation
+    }
     /**
      * Provides an implementation of {@code CsvLineConverter} that converts a line into a {@code List}.
      *
@@ -49,7 +53,7 @@ public final class CsvLineConverters {
         return new MapLineConverter(headers);
     }
 
-    private static class ListLineConverter implements CsvLineConverter<List<String>> {
+    private static final class ListLineConverter implements CsvLineConverter<List<String>> {
         public static final ListLineConverter SINGLETON = new ListLineConverter();
 
         @Override
@@ -58,7 +62,7 @@ public final class CsvLineConverters {
         }
     }
 
-    private static class MapLineConverter implements CsvLineConverter<Map<String, String>> {
+    private static final class MapLineConverter implements CsvLineConverter<Map<String, String>> {
         private final String[] headers;
 
         private MapLineConverter(String[] headers) {
@@ -95,7 +99,4 @@ public final class CsvLineConverters {
         }
     }
 
-    private CsvLineConverters() {
-        // Prevent instantiation
-    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/f4e8992f/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java
index cd5425a..fedc5a3 100644
--- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvIteratorTest.java
@@ -21,11 +21,12 @@ import java.io.InputStreamReader;
 import java.util.Arrays;
 import java.util.List;
 import java.util.NoSuchElementException;
+
+import mockit.Expectations;
+import mockit.Injectable;
 import org.apache.commons.csv.CSVParser;
 import org.junit.Assert;
 import org.junit.Test;
-import mockit.Expectations;
-import mockit.Injectable;
 
 public class CsvIteratorTest {