CAMEL-7450 Fixed the unit test errors and added an unit for the map skipFirstLine
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/093192f8 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/093192f8 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/093192f8 Branch: refs/heads/camel-2.13.x Commit: 093192f8f537b12e859cde23d444b41060941ee3 Parents: e3a2c59 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Mon May 26 19:58:46 2014 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Mon May 26 20:26:30 2014 +0800 ---------------------------------------------------------------------- .../camel/dataformat/csv/CsvDataFormat.java | 22 ++++++++++---------- .../dataformat/csv/CsvUnmarshalMapLineTest.java | 20 ++++++++++++++++++ .../CsvUnmarshalMapLineSpringTest-context.xml | 7 +++++++ 3 files changed, 38 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/093192f8/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 c2e1541..7f40a0a 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 @@ -137,17 +137,17 @@ public class CsvDataFormat implements DataFormat { } CsvLineConverter<?> lineConverter; if (useMaps) { - final CSVField[] fields=this.config.getFields(); - final String[] fieldS; - if (fields!=null){ - fieldS=new String[fields.length]; - for (int i=0; i<fields.length; i++){ - fieldS[i]=fields[i].getName(); - } - } else { - fieldS=parser.getLine(); - } - lineConverter = CsvLineConverters.getMapLineConverter(fieldS); + final CSVField[] fields = this.config.getFields(); + final String[] fieldS; + if (fields != null && fields.length > 0) { + fieldS = new String[fields.length]; + for (int i = 0; i < fields.length; i++) { + fieldS[i] = fields[i].getName(); + } + } else { + fieldS = parser.getLine(); + } + lineConverter = CsvLineConverters.getMapLineConverter(fieldS); } else { lineConverter = CsvLineConverters.getListConverter(); } http://git-wip-us.apache.org/repos/asf/camel/blob/093192f8/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 index 5f02dc3..567f48e 100644 --- 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 @@ -65,6 +65,26 @@ public class CsvUnmarshalMapLineTest extends CamelSpringTestSupport { List<?> body = result.getReceivedExchanges().get(0).getIn().getBody(List.class); assertEquals(0, body.size()); } + + @SuppressWarnings("unchecked") + @Test + public void testCsvSkipFirstLineUnMarshal() throws Exception { + result.expectedMessageCount(1); + + // the first line contains the column names which we intend to skip + template.sendBody("direct:skipFirstline", "Camel CSV test\nOrderId|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")); + } @Override protected ClassPathXmlApplicationContext createApplicationContext() { http://git-wip-us.apache.org/repos/asf/camel/blob/093192f8/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 index 17e7325..fe0cf7f 100644 --- 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 @@ -28,5 +28,12 @@ </unmarshal> <to uri="mock:result" /> </route> + <route> + <from uri="direct:skipFirstline" /> + <unmarshal> + <csv delimiter="|" useMaps="true" skipFirstLine="true"/> + </unmarshal> + <to uri="mock:result" /> + </route> </camelContext> </beans>