http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java new file mode 100644 index 0000000..1d6e7b3 --- /dev/null +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java @@ -0,0 +1,438 @@ +/** + * 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 org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVRecord; +import org.apache.commons.csv.QuoteMode; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +/** + * This class tests the creation of the proper {@link org.apache.commons.csv.CSVFormat} based on the properties of + * {@link org.apache.camel.dataformat.csv.CsvDataFormat}. + * It doesn't test the marshalling and unmarshalling based on the CSV format. + */ +public class CsvDataFormatTest { + @Test + public void shouldUseDefaultFormat() { + CsvDataFormat dataFormat = new CsvDataFormat(); + + // Properly initialized + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + + // Properly used + assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat()); + } + + @Test + public void shouldUseFormatFromConstructor() { + CsvDataFormat dataFormat = new CsvDataFormat(CSVFormat.EXCEL); + + // Properly initialized + assertSame(CSVFormat.EXCEL, dataFormat.getFormat()); + + // Properly used + assertEquals(CSVFormat.EXCEL, dataFormat.getActiveFormat()); + } + + @Test + public void shouldUseSpecifiedFormat() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setFormat(CSVFormat.MYSQL); + + // Properly saved + assertSame(CSVFormat.MYSQL, dataFormat.getFormat()); + + // Properly used + assertEquals(CSVFormat.MYSQL, dataFormat.getActiveFormat()); + } + + @Test + public void shouldFallbackToDefaultFormat() { + CsvDataFormat dataFormat = new CsvDataFormat(CSVFormat.EXCEL) + .setFormat(null); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + + // Properly used + assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat()); + } + + @Test + public void shouldDefineFormatByName() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setFormatName("EXCEL"); + + // Properly saved + assertSame(CSVFormat.EXCEL, dataFormat.getFormat()); + + // Properly used + assertEquals(CSVFormat.EXCEL, dataFormat.getActiveFormat()); + } + + @Test + public void shouldDisableCommentMarker() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setCommentMarkerDisabled(true) + .setCommentMarker('c'); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertTrue(dataFormat.isCommentMarkerDisabled()); + assertEquals(Character.valueOf('c'), dataFormat.getCommentMarker()); + + // Properly used + assertNull(dataFormat.getActiveFormat().getCommentMarker()); + } + + @Test + public void shouldOverrideCommentMarker() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setCommentMarker('c'); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Character.valueOf('c'), dataFormat.getCommentMarker()); + + // Properly used + assertEquals(Character.valueOf('c'), dataFormat.getActiveFormat().getCommentMarker()); + } + + @Test + public void shouldOverrideDelimiter() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setDelimiter('d'); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Character.valueOf('d'), dataFormat.getDelimiter()); + + // Properly used + assertEquals('d', dataFormat.getActiveFormat().getDelimiter()); + } + + @Test + public void shouldDisableEscape() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setEscapeDisabled(true) + .setEscape('e'); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertTrue(dataFormat.isEscapeDisabled()); + assertEquals(Character.valueOf('e'), dataFormat.getEscape()); + + // Properly used + assertNull(dataFormat.getActiveFormat().getEscapeCharacter()); + } + + @Test + public void shouldOverrideEscape() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setEscape('e'); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Character.valueOf('e'), dataFormat.getEscape()); + + // Properly used + assertEquals(Character.valueOf('e'), dataFormat.getActiveFormat().getEscapeCharacter()); + } + + @Test + public void shouldDisableHeader() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setHeaderDisabled(true) + .setHeader(new String[]{"a", "b", "c"}); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertTrue(dataFormat.isHeaderDisabled()); + assertArrayEquals(new String[]{"a", "b", "c"}, dataFormat.getHeader()); + + // Properly used + assertNull(dataFormat.getActiveFormat().getHeader()); + } + + @Test + public void shouldOverrideHeader() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setHeader(new String[]{"a", "b", "c"}); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertArrayEquals(new String[]{"a", "b", "c"}, dataFormat.getHeader()); + + // Properly used + assertArrayEquals(new String[]{"a", "b", "c"}, dataFormat.getActiveFormat().getHeader()); + } + + @Test + public void shouldAllowMissingColumnNames() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setAllowMissingColumnNames(true); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.TRUE, dataFormat.getAllowMissingColumnNames()); + + // Properly used + assertTrue(dataFormat.getActiveFormat().getAllowMissingColumnNames()); + } + + @Test + public void shouldNotAllowMissingColumnNames() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setAllowMissingColumnNames(false); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.FALSE, dataFormat.getAllowMissingColumnNames()); + + // Properly used + assertFalse(dataFormat.getActiveFormat().getAllowMissingColumnNames()); + } + + @Test + public void shouldIgnoreEmptyLines() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setIgnoreEmptyLines(true); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.TRUE, dataFormat.getIgnoreEmptyLines()); + + // Properly used + assertTrue(dataFormat.getActiveFormat().getIgnoreEmptyLines()); + } + + @Test + public void shouldNotIgnoreEmptyLines() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setIgnoreEmptyLines(false); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.FALSE, dataFormat.getIgnoreEmptyLines()); + + // Properly used + assertFalse(dataFormat.getActiveFormat().getIgnoreEmptyLines()); + } + + @Test + public void shouldIgnoreSurroundingSpaces() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setIgnoreSurroundingSpaces(true); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.TRUE, dataFormat.getIgnoreSurroundingSpaces()); + + // Properly used + assertTrue(dataFormat.getActiveFormat().getIgnoreSurroundingSpaces()); + } + + @Test + public void shouldNotIgnoreSurroundingSpaces() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setIgnoreSurroundingSpaces(false); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.FALSE, dataFormat.getIgnoreSurroundingSpaces()); + + // Properly used + assertFalse(dataFormat.getActiveFormat().getIgnoreSurroundingSpaces()); + } + + @Test + public void shouldDisableNullString() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setNullStringDisabled(true) + .setNullString("****"); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertTrue(dataFormat.isNullStringDisabled()); + assertEquals("****", dataFormat.getNullString()); + + // Properly used + assertNull(dataFormat.getActiveFormat().getNullString()); + } + + @Test + public void shouldOverrideNullString() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setNullString("****"); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals("****", dataFormat.getNullString()); + + // Properly used + assertEquals("****", dataFormat.getActiveFormat().getNullString()); + } + + @Test + public void shouldDisableQuote() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setQuoteDisabled(true) + .setQuote('q'); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertTrue(dataFormat.isQuoteDisabled()); + assertEquals(Character.valueOf('q'), dataFormat.getQuote()); + + // Properly used + assertNull(dataFormat.getActiveFormat().getQuoteCharacter()); + } + + @Test + public void shouldOverrideQuote() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setQuote('q'); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Character.valueOf('q'), dataFormat.getQuote()); + + // Properly used + assertEquals(Character.valueOf('q'), dataFormat.getActiveFormat().getQuoteCharacter()); + } + + @Test + public void shouldOverrideQuoteMode() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setQuoteMode(QuoteMode.ALL); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(QuoteMode.ALL, dataFormat.getQuoteMode()); + + // Properly used + assertEquals(QuoteMode.ALL, dataFormat.getActiveFormat().getQuoteMode()); + } + + @Test + public void shouldDisableRecordSeparator() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setRecordSeparatorDisabled(true) + .setRecordSeparator("separator"); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertTrue(dataFormat.isRecordSeparatorDisabled()); + assertEquals("separator", dataFormat.getRecordSeparator()); + + // Properly used + assertNull(dataFormat.getActiveFormat().getRecordSeparator()); + } + + @Test + public void shouldOverrideRecordSeparator() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setRecordSeparator("separator"); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals("separator", dataFormat.getRecordSeparator()); + + // Properly used + assertEquals("separator", dataFormat.getActiveFormat().getRecordSeparator()); + } + + @Test + public void shouldSkipHeaderRecord() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setSkipHeaderRecord(true); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.TRUE, dataFormat.getSkipHeaderRecord()); + + // Properly used + assertTrue(dataFormat.getActiveFormat().getSkipHeaderRecord()); + } + + @Test + public void shouldNotSkipHeaderRecord() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setSkipHeaderRecord(false); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertEquals(Boolean.FALSE, dataFormat.getSkipHeaderRecord()); + + // Properly used + assertFalse(dataFormat.getActiveFormat().getSkipHeaderRecord()); + } + + @Test + public void shouldHandleLazyLoad() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setLazyLoad(true); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertTrue(dataFormat.isLazyLoad()); + + // Properly used (it doesn't modify the format) + assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat()); + } + + @Test + public void shouldHandleUseMaps() { + CsvDataFormat dataFormat = new CsvDataFormat() + .setUseMaps(true); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertTrue(dataFormat.isUseMaps()); + + // Properly used (it doesn't modify the format) + assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat()); + } + + @Test + public void shouldHandleRecordConverter() { + CsvRecordConverter<String> converter = new CsvRecordConverter<String>() { + @Override + public String convertRecord(CSVRecord record) { + return record.toString(); + } + }; + + CsvDataFormat dataFormat = new CsvDataFormat() + .setRecordConverter(converter); + + // Properly saved + assertSame(CSVFormat.DEFAULT, dataFormat.getFormat()); + assertSame(converter, dataFormat.getRecordConverter()); + + // Properly used (it doesn't modify the format) + assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat()); + } +}
http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/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 deleted file mode 100644 index fcb2c4f..0000000 --- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvLineConvertersTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * 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/3af1165a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java index 0737ad7..2ec2ada 100644 --- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java @@ -1,95 +1,94 @@ -/** - * 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.ArrayList; -import java.util.LinkedHashMap; -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 integration test for the <code>CsvDataFormat</code> demonstrating the usage of - * the <tt>autogenColumns</tt>, <tt>configRef</tt> and <tt>strategyRef</tt> options. - */ -public class CsvMarshalAutogenColumnsSpringTest extends CamelSpringTestSupport { - - @EndpointInject(uri = "mock:result") - private MockEndpoint result; - - @EndpointInject(uri = "mock:result2") - private MockEndpoint result2; - - @Test - public void retrieveColumnsWithAutogenColumnsFalseAndItemColumnsSet() throws Exception { - result.expectedMessageCount(1); - - template.sendBody("direct:start", createBody()); - - result.assertIsSatisfied(); - - String body = result.getReceivedExchanges().get(0).getIn().getBody(String.class); - String[] lines = body.split("\n"); - assertEquals(2, lines.length); - assertEquals("Camel in Action", lines[0]); - assertEquals("ActiveMQ in Action", lines[1]); - } - - @Test - public void retrieveColumnsWithAutogenColumnsFalseAndOrderIdAmountColumnsSet() throws Exception { - result2.expectedMessageCount(1); - - template.sendBody("direct:start2", createBody()); - - result2.assertIsSatisfied(); - - String body = result2.getReceivedExchanges().get(0).getIn().getBody(String.class); - String[] lines = body.split("\n"); - assertEquals(2, lines.length); - assertEquals("123|1", lines[0]); - assertEquals("124|2", lines[1]); - } - - private static List<Map<String, Object>> createBody() { - List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); - - Map<String, Object> row1 = new LinkedHashMap<String, Object>(); - row1.put("orderId", 123); - row1.put("item", "Camel in Action"); - row1.put("amount", 1); - data.add(row1); - - Map<String, Object> row2 = new LinkedHashMap<String, Object>(); - row2.put("orderId", 124); - row2.put("item", "ActiveMQ in Action"); - row2.put("amount", 2); - data.add(row2); - return data; - } - - @Override - protected ClassPathXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml"); - } +/** + * 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.ArrayList; +import java.util.LinkedHashMap; +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 integration test for the <code>CsvDataFormat</code> demonstrating the usage of + * the <tt>autogenColumns</tt>, <tt>configRef</tt> and <tt>strategyRef</tt> options. + */ +public class CsvMarshalAutogenColumnsSpringTest extends CamelSpringTestSupport { + + @EndpointInject(uri = "mock:result") + private MockEndpoint result; + + @EndpointInject(uri = "mock:result2") + private MockEndpoint result2; + + @Test + public void retrieveColumnsWithAutogenColumnsFalseAndItemColumnsSet() throws Exception { + result.expectedMessageCount(1); + + template.sendBody("direct:start", createBody()); + + result.assertIsSatisfied(); + + String body = result.getReceivedExchanges().get(0).getIn().getBody(String.class); + String[] lines = body.split(System.lineSeparator()); + assertEquals(2, lines.length); + assertEquals("Camel in Action", lines[0]); + assertEquals("ActiveMQ in Action", lines[1]); + } + + @Test + public void retrieveColumnsWithAutogenColumnsFalseAndOrderIdAmountColumnsSet() throws Exception { + result2.expectedMessageCount(1); + + template.sendBody("direct:start2", createBody()); + + result2.assertIsSatisfied(); + + String body = result2.getReceivedExchanges().get(0).getIn().getBody(String.class); + String[] lines = body.split(System.lineSeparator()); + assertEquals(2, lines.length); + assertEquals("123|1", lines[0]); + assertEquals("124|2", lines[1]); + } + + private static List<Map<String, Object>> createBody() { + List<Map<String, Object>> data = new ArrayList<>(); + + Map<String, Object> row1 = new LinkedHashMap<>(); + row1.put("orderId", 123); + row1.put("item", "Camel in Action"); + row1.put("amount", 1); + data.add(row1); + + Map<String, Object> row2 = new LinkedHashMap<>(); + row2.put("orderId", 124); + row2.put("item", "ActiveMQ in Action"); + row2.put("amount", 2); + data.add(row2); + return data; + } + + @Override + protected ClassPathXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml"); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java index 38a0aa6..0db70ae 100644 --- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java @@ -1,76 +1,76 @@ -/** - * 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.ArrayList; -import java.util.LinkedHashMap; -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 integration test for the <code>CsvDataFormat</code> - * @version - */ -public class CsvMarshalPipeDelimiterSpringTest extends CamelSpringTestSupport { - - @EndpointInject(uri = "mock:result") - private MockEndpoint result; - - @Test - public void testCsvMarshal() throws Exception { - result.expectedMessageCount(1); - - template.sendBody("direct:start", createBody()); - - assertMockEndpointsSatisfied(); - - String body = result.getReceivedExchanges().get(0).getIn().getBody( - String.class); - String[] lines = body.split("\n"); - assertEquals(2, lines.length); - assertEquals("123|Camel in Action|1", lines[0]); - assertEquals("124|ActiveMQ in Action|2", lines[1]); - } - - private List<Map<String, Object>> createBody() { - List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); - - Map<String, Object> row1 = new LinkedHashMap<String, Object>(); - row1.put("orderId", 123); - row1.put("item", "Camel in Action"); - row1.put("amount", 1); - data.add(row1); - - Map<String, Object> row2 = new LinkedHashMap<String, Object>(); - row2.put("orderId", 124); - row2.put("item", "ActiveMQ in Action"); - row2.put("amount", 2); - data.add(row2); - return data; - } - - @Override - protected ClassPathXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml"); - } +/** + * 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.ArrayList; +import java.util.LinkedHashMap; +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 integration test for the <code>CsvDataFormat</code> + */ +public class CsvMarshalPipeDelimiterSpringTest extends CamelSpringTestSupport { + + @EndpointInject(uri = "mock:result") + private MockEndpoint result; + + @Test + public void testCsvMarshal() throws Exception { + result.expectedMessageCount(1); + + template.sendBody("direct:start", createBody()); + + assertMockEndpointsSatisfied(); + + String body = result.getReceivedExchanges().get(0).getIn().getBody( + String.class); + String[] lines = body.split(System.lineSeparator()); + assertEquals(2, lines.length); + assertEquals("123|Camel in Action|1", lines[0]); + assertEquals("124|ActiveMQ in Action|2", lines[1]); + } + + private List<Map<String, Object>> createBody() { + List<Map<String, Object>> data = new ArrayList<>(); + + Map<String, Object> row1 = new LinkedHashMap<>(); + row1.put("orderId", 123); + row1.put("item", "Camel in Action"); + row1.put("amount", 1); + data.add(row1); + + Map<String, Object> row2 = new LinkedHashMap<>(); + row2.put("orderId", 124); + row2.put("item", "ActiveMQ in Action"); + row2.put("amount", 2); + data.add(row2); + return data; + } + + @Override + protected ClassPathXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml"); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java index 14e0252..ad88927 100644 --- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java @@ -1,91 +1,81 @@ -/** - * 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.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.apache.camel.EndpointInject; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.apache.commons.csv.writer.CSVConfig; -import org.junit.Test; - -/** - * @version - */ -public class CsvMarshalPipeDelimiterTest extends CamelTestSupport { - - @EndpointInject(uri = "mock:result") - private MockEndpoint result; - - @Test - public void testCsvMarshal() throws Exception { - result.expectedMessageCount(1); - - template.sendBody("direct:start", createBody()); - - assertMockEndpointsSatisfied(); - - String body = result.getReceivedExchanges().get(0).getIn().getBody( - String.class); - String[] lines = body.split("\n"); - assertEquals(2, lines.length); - assertEquals("123|Camel in Action|1", lines[0]); - assertEquals("124|ActiveMQ in Action|2", lines[1]); - } - - private List<Map<String, Object>> createBody() { - List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); - - Map<String, Object> row1 = new LinkedHashMap<String, Object>(); - row1.put("orderId", 123); - row1.put("item", "Camel in Action"); - row1.put("amount", 1); - data.add(row1); - - Map<String, Object> row2 = new LinkedHashMap<String, Object>(); - row2.put("orderId", 124); - row2.put("item", "ActiveMQ in Action"); - row2.put("amount", 2); - data.add(row2); - return data; - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - CsvDataFormat csv = new CsvDataFormat(); - CSVConfig config = new CSVConfig(); - config.setDelimiter('|'); - csv.setConfig(config); - - // also possible - // CsvDataFormat csv = new CsvDataFormat(); - // csv.setDelimiter("|"); - - from("direct:start").marshal(csv).convertBodyTo(String.class) - .to("mock:result"); - } - }; - } +/** + * 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.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.camel.EndpointInject; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; + +import org.junit.Test; + +public class CsvMarshalPipeDelimiterTest extends CamelTestSupport { + + @EndpointInject(uri = "mock:result") + private MockEndpoint result; + + @Test + public void testCsvMarshal() throws Exception { + result.expectedMessageCount(1); + + template.sendBody("direct:start", createBody()); + + assertMockEndpointsSatisfied(); + + String body = result.getReceivedExchanges().get(0).getIn().getBody( + String.class); + String[] lines = body.split(System.lineSeparator()); + assertEquals(2, lines.length); + assertEquals("123|Camel in Action|1", lines[0]); + assertEquals("124|ActiveMQ in Action|2", lines[1]); + } + + private List<Map<String, Object>> createBody() { + List<Map<String, Object>> data = new ArrayList<>(); + + Map<String, Object> row1 = new LinkedHashMap<>(); + row1.put("orderId", 123); + row1.put("item", "Camel in Action"); + row1.put("amount", 1); + data.add(row1); + + Map<String, Object> row2 = new LinkedHashMap<>(); + row2.put("orderId", 124); + row2.put("item", "ActiveMQ in Action"); + row2.put("amount", 2); + data.add(row2); + return data; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + CsvDataFormat csv = new CsvDataFormat().setDelimiter('|').setHeaderDisabled(true); + + from("direct:start").marshal(csv).convertBodyTo(String.class) + .to("mock:result"); + } + }; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/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 000b9c7..d8d8a2e 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 @@ -1,76 +1,110 @@ -/** - * 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.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Test; - -/** - * @version - */ -public class CsvMarshalTest extends CamelTestSupport { - - @Test - public void testCsvMarshal() throws Exception { - MockEndpoint mock = getMockEndpoint("mock:result"); - mock.expectedMessageCount(1); - - List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); - - Map<String, Object> row1 = new LinkedHashMap<String, Object>(); - row1.put("orderId", 123); - row1.put("item", "Camel in Action"); - row1.put("amount", 1); - data.add(row1); - - Map<String, Object> row2 = new LinkedHashMap<String, Object>(); - row2.put("orderId", 124); - row2.put("item", "ActiveMQ in Action"); - row2.put("amount", 2); - data.add(row2); - - template.sendBody("direct:toCsv", data); - - assertMockEndpointsSatisfied(); - - String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class); - String[] lines = body.split("\n"); - assertEquals("There should be 2 rows", 2, lines.length); - assertEquals("123,Camel in Action,1", lines[0]); - assertEquals("124,ActiveMQ in Action,2", lines[1]); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:toCsv") - .marshal().csv() - .convertBodyTo(String.class) - .to("mock:result"); - } - }; - } -} +/** + * 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.List; +import java.util.Map; + +import org.apache.camel.EndpointInject; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; + +import org.junit.Test; + +/** + * This class tests standard marshalling + */ +public class CsvMarshalTest extends CamelTestSupport { + @EndpointInject(uri = "mock:output") + MockEndpoint output; + + @Test + public void shouldMarshalLists() throws Exception { + output.expectedMessageCount(1); + + template.sendBody("direct:default", Arrays.<List>asList( + Arrays.asList("1", "2", "3"), + Arrays.asList("one", "two", "three") + )); + output.assertIsSatisfied(); + + String[] actuals = readOutputLines(); + assertArrayEquals(new String[]{"1,2,3", "one,two,three"}, actuals); + } + + @Test + public void shouldMarshalMaps() throws Exception { + output.expectedMessageCount(1); + + template.sendBody("direct:default", Arrays.<Map>asList( + TestUtils.asMap("A", "1", "B", "2", "C", "3"), + TestUtils.asMap("A", "one", "B", "two", "C", "three") + )); + output.assertIsSatisfied(); + + assertArrayEquals(new String[]{"1,2,3", "one,two,three"}, readOutputLines()); + } + + @Test + public void shouldMarshalSingleMap() throws Exception { + output.expectedMessageCount(1); + + template.sendBody("direct:default", TestUtils.asMap("A", "1", "B", "2", "C", "3")); + output.assertIsSatisfied(); + + assertArrayEquals(new String[]{"1,2,3"}, readOutputLines()); + } + + @Test + public void shouldHandleColumns() throws Exception { + output.expectedMessageCount(1); + + template.sendBody("direct:headers", Arrays.<Map>asList( + TestUtils.asMap("A", "1", "B", "2", "C", "3"), + TestUtils.asMap("A", "one", "B", "two", "C", "three") + )); + output.assertIsSatisfied(); + + assertArrayEquals(new String[]{"A,C", "1,3", "one,three"}, readOutputLines()); + } + + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // Default format + from("direct:default") + .marshal(new CsvDataFormat()) + .to("mock:output"); + + // Format with special headers + from("direct:headers") + .marshal(new CsvDataFormat().setHeader(new String[]{"A", "C"})) + .to("mock:output"); + } + }; + } + + private String[] readOutputLines() { + return output.getExchanges().get(0).getIn().getBody(String.class).split("\r\n|\r|\n"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRecordConvertersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRecordConvertersTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRecordConvertersTest.java new file mode 100644 index 0000000..e295d53 --- /dev/null +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRecordConvertersTest.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.io.StringReader; +import java.util.List; +import java.util.Map; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * This class tests the common {@link CsvRecordConverter} implementations of + * {@link org.apache.camel.dataformat.csv.CsvRecordConverters}. + */ +public class CsvRecordConvertersTest { + private CSVRecord record; + + @Before + public void setUp() throws Exception { + CSVFormat format = CSVFormat.DEFAULT.withHeader("A", "B", "C"); + CSVParser parser = new CSVParser(new StringReader("1,2,3"), format); + List<CSVRecord> records = parser.getRecords(); + record = records.get(0); + } + + @Test + public void shouldConvertAsList() { + List<String> list = CsvRecordConverters.listConverter().convertRecord(record); + + assertNotNull(list); + assertEquals(3, list.size()); + assertEquals("1", list.get(0)); + assertEquals("2", list.get(1)); + assertEquals("3", list.get(2)); + } + + @Test + public void shouldConvertAsMap() { + Map<String, String> map = CsvRecordConverters.mapConverter().convertRecord(record); + + assertNotNull(map); + assertEquals(3, map.size()); + assertEquals("1", map.get("A")); + assertEquals("2", map.get("B")); + assertEquals("3", map.get("C")); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteCharsetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteCharsetTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteCharsetTest.java index 6579af4..7e0aa35 100644 --- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteCharsetTest.java +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteCharsetTest.java @@ -1,71 +1,69 @@ -/** - * 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. - */ -/** - * 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 org.apache.camel.Exchange; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Test; - -/** - * @version - */ -public class CsvRouteCharsetTest extends CamelTestSupport { - - @SuppressWarnings("unchecked") - @Test - public void testUnMarshal() throws Exception { - MockEndpoint endpoint = getMockEndpoint("mock:daltons"); - endpoint.expectedMessageCount(1); - endpoint.assertIsSatisfied(); - - Exchange exchange = endpoint.getExchanges().get(0); - List<List<String>> data = (List<List<String>>) exchange.getIn().getBody(); - assertEquals("Jäck Dalton", data.get(0).get(0)); - assertEquals("Jöe Dalton", data.get(1).get(0)); - assertEquals("Lücky Luke", data.get(2).get(0)); - } - - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - public void configure() { - from("file:src/test/resources/?fileName=daltons-utf-8.csv&noop=true"). - unmarshal().csv(). - to("mock:daltons"); - } - }; - } +/** + * 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. + */ +/** + * 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 org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; + +import org.junit.Test; + +public class CsvRouteCharsetTest extends CamelTestSupport { + + @SuppressWarnings("unchecked") + @Test + public void testUnMarshal() throws Exception { + MockEndpoint endpoint = getMockEndpoint("mock:daltons"); + endpoint.expectedMessageCount(1); + endpoint.assertIsSatisfied(); + + Exchange exchange = endpoint.getExchanges().get(0); + List<List<String>> data = (List<List<String>>) exchange.getIn().getBody(); + assertEquals("Jäck Dalton", data.get(0).get(0)); + assertEquals("Jöe Dalton", data.get(1).get(0)); + assertEquals("Lücky Luke", data.get(2).get(0)); + } + + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("file:src/test/resources/?fileName=daltons-utf-8.csv&noop=true"). + unmarshal().csv(). + to("mock:daltons"); + } + }; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java index 422d3a0..c4e2447 100644 --- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java @@ -1,211 +1,203 @@ -/** - * 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. - */ -/** - * 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.HashMap; -import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; - -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.apache.commons.csv.writer.CSVConfig; -import org.apache.commons.csv.writer.CSVField; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @version - */ -public class CsvRouteTest extends CamelTestSupport { - private static final Logger LOG = LoggerFactory.getLogger(CsvRouteTest.class); - - @Test - public void testSendMessage() throws Exception { - MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class); - resultEndpoint.expectedMessageCount(1); - - // START SNIPPET: marshalInput - Map<String, Object> body = new HashMap<String, Object>(); - body.put("foo", "abc"); - body.put("bar", 123); - // END SNIPPET: marshalInput - template.sendBody("direct:start", body); - - resultEndpoint.assertIsSatisfied(); - List<Exchange> list = resultEndpoint.getReceivedExchanges(); - for (Exchange exchange : list) { - Message in = exchange.getIn(); - String text = in.getBody(String.class); - - log.debug("Received " + text); - assertNotNull("Should be able to convert received body to a string", text); - - // order is not guaranteed with a Map (which was passed in before) - // so we need to check for both combinations - assertTrue("Text body has wrong value.", "abc,123".equals(text.trim()) - || "123,abc".equals(text.trim())); - } - } - - @Test - public void testMultipleMessages() throws Exception { - MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:resultMulti", - MockEndpoint.class); - resultEndpoint.expectedMessageCount(2); - Map<String, Object> body1 = new HashMap<String, Object>(); - body1.put("foo", "abc"); - body1.put("bar", 123); - - Map<String, Object> body2 = new HashMap<String, Object>(); - body2.put("foo", "def"); - body2.put("bar", 456); - body2.put("baz", 789); - - template.sendBody("direct:startMulti", body1); - template.sendBody("direct:startMulti", body2); - - resultEndpoint.assertIsSatisfied(); - List<Exchange> list = resultEndpoint.getReceivedExchanges(); - Message in1 = list.get(0).getIn(); - String text1 = in1.getBody(String.class); - - log.debug("Received " + text1); - assertTrue("First CSV body has wrong value", - Pattern.matches("(abc,123)|(123,abc)", text1.trim())); - - Message in2 = list.get(1).getIn(); - String text2 = in2.getBody(String.class); - - log.debug("Received " + text2); - - // fields should keep the same order from one call to the other - if (text1.trim().equals("abc,123")) { - assertEquals("Second CSV body has wrong value", - "def,456,789", text2.trim()); - } else { - assertEquals("Second CSV body has wrong value", - "456,def,789", text2.trim()); - } - } - - @Test - public void testPresetConfig() throws Exception { - MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:resultMultiCustom", - MockEndpoint.class); - resultEndpoint.expectedMessageCount(2); - Map<String, Object> body1 = new HashMap<String, Object>(); - body1.put("foo", "abc"); - body1.put("bar", 123); - - Map<String, Object> body2 = new HashMap<String, Object>(); - body2.put("foo", "def"); - body2.put("bar", 456); - body2.put("baz", 789); - body2.put("buz", "000"); - - template.sendBody("direct:startMultiCustom", body1); - template.sendBody("direct:startMultiCustom", body2); - - List<Exchange> list = resultEndpoint.getReceivedExchanges(); - Message in1 = list.get(0).getIn(); - String text1 = in1.getBody(String.class); - - log.debug("Received " + text1); - assertEquals("First CSV body has wrong value", - "abc;;123", text1.trim()); - - Message in2 = list.get(1).getIn(); - String text2 = in2.getBody(String.class); - - log.debug("Received " + text2); - assertEquals("Second CSV body has wrong value", - "def;789;456", text2.trim()); - - } - - @SuppressWarnings("unchecked") - @Test - public void testUnMarshal() throws Exception { - MockEndpoint endpoint = getMockEndpoint("mock:daltons"); - endpoint.expectedMessageCount(1); - endpoint.assertIsSatisfied(); - Exchange exchange = endpoint.getExchanges().get(0); - // START SNIPPET : unmarshalResult - List<List<String>> data = (List<List<String>>) exchange.getIn().getBody(); - for (List<String> line : data) { - LOG.debug(String.format("%s has an IQ of %s and is currently %s", - line.get(0), line.get(1), line.get(2))); - } - // END SNIPPET : unmarshalResult - } - - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - public void configure() { - // START SNIPPET: marshalRoute - from("direct:start"). - marshal().csv(). - to("mock:result"); - // END SNIPPET: marshalRoute - - from("direct:startMulti"). - marshal().csv(). - to("mock:resultMulti"); - - CsvDataFormat customCsv = new CsvDataFormat(); - CSVConfig custom = new CSVConfig(); - custom.setDelimiter(';'); - custom.addField(new CSVField("foo")); - custom.addField(new CSVField("baz")); - custom.addField(new CSVField("bar")); - customCsv.setConfig(custom); - customCsv.setAutogenColumns(false); - - from("direct:startMultiCustom"). - marshal(customCsv). - to("mock:resultMultiCustom"); - - // START SNIPPET: unmarshalRoute - from("file:src/test/resources/?fileName=daltons.csv&noop=true"). - unmarshal().csv(). - to("mock:daltons"); - // END SNIPPET: unmarshalRoute - } - }; - } -} +/** + * 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. + */ +/** + * 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.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CsvRouteTest extends CamelTestSupport { + private static final Logger LOG = LoggerFactory.getLogger(CsvRouteTest.class); + + @Test + public void testSendMessage() throws Exception { + MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class); + resultEndpoint.expectedMessageCount(1); + + // START SNIPPET: marshalInput + Map<String, Object> body = new HashMap<>(); + body.put("foo", "abc"); + body.put("bar", 123); + // END SNIPPET: marshalInput + template.sendBody("direct:start", body); + + resultEndpoint.assertIsSatisfied(); + List<Exchange> list = resultEndpoint.getReceivedExchanges(); + for (Exchange exchange : list) { + Message in = exchange.getIn(); + String text = in.getBody(String.class); + + log.debug("Received " + text); + assertNotNull("Should be able to convert received body to a string", text); + + // order is not guaranteed with a Map (which was passed in before) + // so we need to check for both combinations + assertTrue("Text body has wrong value.", "abc,123".equals(text.trim()) + || "123,abc".equals(text.trim())); + } + } + + @Test + public void testMultipleMessages() throws Exception { + MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:resultMulti", + MockEndpoint.class); + resultEndpoint.expectedMessageCount(2); + Map<String, Object> body1 = new HashMap<>(); + body1.put("foo", "abc"); + body1.put("bar", 123); + + Map<String, Object> body2 = new HashMap<>(); + body2.put("foo", "def"); + body2.put("bar", 456); + body2.put("baz", 789); + + template.sendBody("direct:startMulti", body1); + template.sendBody("direct:startMulti", body2); + + resultEndpoint.assertIsSatisfied(); + List<Exchange> list = resultEndpoint.getReceivedExchanges(); + Message in1 = list.get(0).getIn(); + String text1 = in1.getBody(String.class); + + log.debug("Received " + text1); + assertTrue("First CSV body has wrong value", + Pattern.matches("(abc,123)|(123,abc)", text1.trim())); + + Message in2 = list.get(1).getIn(); + String text2 = in2.getBody(String.class); + + log.debug("Received " + text2); + + // fields should keep the same order from one call to the other + if (text1.trim().equals("abc,123")) { + assertEquals("Second CSV body has wrong value", + "def,456,789", text2.trim()); + } else { + assertEquals("Second CSV body has wrong value", + "456,def,789", text2.trim()); + } + } + + @Test + public void testPresetConfig() throws Exception { + MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:resultMultiCustom", + MockEndpoint.class); + resultEndpoint.expectedMessageCount(2); + Map<String, Object> body1 = new HashMap<>(); + body1.put("foo", "abc"); + body1.put("bar", 123); + + Map<String, Object> body2 = new HashMap<>(); + body2.put("foo", "def"); + body2.put("bar", 456); + body2.put("baz", 789); + body2.put("buz", "000"); + + template.sendBody("direct:startMultiCustom", body1); + template.sendBody("direct:startMultiCustom", body2); + + List<Exchange> list = resultEndpoint.getReceivedExchanges(); + Message in1 = list.get(0).getIn(); + String text1 = in1.getBody(String.class); + + log.debug("Received " + text1); + assertEquals("First CSV body has wrong value", + "abc;;123", text1.trim()); + + Message in2 = list.get(1).getIn(); + String text2 = in2.getBody(String.class); + + log.debug("Received " + text2); + assertEquals("Second CSV body has wrong value", + "def;789;456", text2.trim()); + + } + + @SuppressWarnings("unchecked") + @Test + public void testUnMarshal() throws Exception { + MockEndpoint endpoint = getMockEndpoint("mock:daltons"); + endpoint.expectedMessageCount(1); + endpoint.assertIsSatisfied(); + Exchange exchange = endpoint.getExchanges().get(0); + // START SNIPPET : unmarshalResult + List<List<String>> data = (List<List<String>>) exchange.getIn().getBody(); + for (List<String> line : data) { + LOG.debug(String.format("%s has an IQ of %s and is currently %s", + line.get(0), line.get(1), line.get(2))); + } + // END SNIPPET : unmarshalResult + } + + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + // START SNIPPET: marshalRoute + from("direct:start"). + marshal().csv(). + to("mock:result"); + // END SNIPPET: marshalRoute + + from("direct:startMulti"). + marshal().csv(). + to("mock:resultMulti"); + + CsvDataFormat customCsv = new CsvDataFormat() + .setDelimiter(';') + .setHeader(new String[]{"foo", "baz", "bar"}) + .setSkipHeaderRecord(true); + + from("direct:startMultiCustom"). + marshal(customCsv). + to("mock:resultMultiCustom"); + + // START SNIPPET: unmarshalRoute + from("file:src/test/resources/?fileName=daltons.csv&noop=true"). + unmarshal().csv(). + to("mock:daltons"); + // END SNIPPET: unmarshalRoute + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/3af1165a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest.java new file mode 100644 index 0000000..3a93546 --- /dev/null +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest.java @@ -0,0 +1,112 @@ +/** + * 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 CsvUnmarshalMapLineSpringTest 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()); + } + + @SuppressWarnings("unchecked") + @Test + public void shouldUseExplicitHeadersInMap() throws Exception { + result.expectedMessageCount(1); + + template.sendBody("direct:explicitHeader", "123|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("MyOrderId")); + assertEquals("Camel in Action", body.get(0).get("MyItem")); + assertEquals("1", body.get(0).get("MyAmount")); + assertEquals("124", body.get(1).get("MyOrderId")); + assertEquals("ActiveMQ in Action", body.get(1).get("MyItem")); + assertEquals("2", body.get(1).get("MyAmount")); + } + + @SuppressWarnings("unchecked") + @Test + public void shouldReplaceHeaderInMap() throws Exception { + result.expectedMessageCount(1); + + template.sendBody("direct:replaceHeader", "a|b|c\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("MyOrderId")); + assertEquals("Camel in Action", body.get(0).get("MyItem")); + assertEquals("1", body.get(0).get("MyAmount")); + assertEquals("124", body.get(1).get("MyOrderId")); + assertEquals("ActiveMQ in Action", body.get(1).get("MyItem")); + assertEquals("2", body.get(1).get("MyAmount")); + } + + @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/3af1165a/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 deleted file mode 100644 index 955a39f..0000000 --- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalMapLineTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/** - * 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()); - } - - @SuppressWarnings("unchecked") - @Test - public void testCsvSkipFirstLineWithHeaderUnMarshal() throws Exception { - result.expectedMessageCount(1); - - // the first line contains the column names which we intend to skip - template.sendBody("direct:start2", "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")); - } - - @SuppressWarnings("unchecked") - @Test - public void testCsvSkipFirstLineWithoutHeaderUnMarshalWithoutHeader() throws Exception { - result.expectedMessageCount(1); - - // the first line contains the column names which we intend to skip - template.sendBody("direct:start3", "123|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("MyOrderId")); - assertEquals("Camel in Action", body.get(0).get("MyItem")); - assertEquals("1", body.get(0).get("MyAmount")); - assertEquals("124", body.get(1).get("MyOrderId")); - assertEquals("ActiveMQ in Action", body.get(1).get("MyItem")); - assertEquals("2", body.get(1).get("MyAmount")); - } - - @Override - protected ClassPathXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvUnmarshalMapLineSpringTest-context.xml"); - } -}