camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/6] camel git commit: [CAMEL-10840] Make 'CsvRecordConverter' public and visible
Date Fri, 17 Feb 2017 08:59:41 GMT
[CAMEL-10840] Make 'CsvRecordConverter' public and visible

Tests included.

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

Branch: refs/heads/master
Commit: d6b06755de8c18c96ab4b047115bb7ee0895ed23
Parents: 78d7a81
Author: Christian Ribeaud <christian@ribeaud.ch>
Authored: Fri Feb 17 07:40:18 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Feb 17 09:53:21 2017 +0100

----------------------------------------------------------------------
 .../dataformat/csv/CsvRecordConverter.java      | 76 ++++++++++----------
 .../CsvDataFormatCustomRecordConverterTest.java | 64 +++++++++++++++++
 .../csv/converter/MyCvsRecordConverter.java     | 30 ++++++++
 .../csv/CsvDataFormatCustomRecordConverter.xml  | 43 +++++++++++
 4 files changed, 175 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d6b06755/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvRecordConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvRecordConverter.java
b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvRecordConverter.java
index 01abf9b..ca3a2e1 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvRecordConverter.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvRecordConverter.java
@@ -1,38 +1,38 @@
-/**
- * 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.CSVRecord;
-
-/**
- * This interface is used to define a converter that transform a {@link org.apache.commons.csv.CSVRecord}
into another
- * type.
- * <p/>
- * The {@link org.apache.camel.dataformat.csv.CsvRecordConverters} class defines common converters.
- *
- * @param <T> Conversion type
- * @see org.apache.camel.dataformat.csv.CsvRecordConverters
- */
-interface CsvRecordConverter<T> {
-    /**
-     * Converts the CSV record into another type.
-     *
-     * @param record CSV record to convert
-     * @return converted CSV record
-     */
-    T convertRecord(CSVRecord record);
-}
+/**
+ * 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.CSVRecord;
+
+/**
+ * This interface is used to define a converter that transform a {@link org.apache.commons.csv.CSVRecord}
into another
+ * type.
+ * <p/>
+ * The {@link org.apache.camel.dataformat.csv.CsvRecordConverters} class defines common converters.
+ *
+ * @param <T> Conversion type
+ * @see org.apache.camel.dataformat.csv.CsvRecordConverters
+ */
+public interface CsvRecordConverter<T> {
+    /**
+     * Converts the CSV record into another type.
+     *
+     * @param record CSV record to convert
+     * @return converted CSV record
+     */
+    T convertRecord(CSVRecord record);
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/d6b06755/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverterTest.java
b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverterTest.java
new file mode 100644
index 0000000..67c341f
--- /dev/null
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverterTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.camel.Message;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.apache.camel.util.CastUtils;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Test cases for {@link CsvRecordConverter}.
+ */
+public class CsvDataFormatCustomRecordConverterTest extends CamelSpringTestSupport {
+
+    @Test
+    public void unmarshalTest() throws InterruptedException {
+        MockEndpoint mock = getMockEndpoint("mock:unmarshaled");
+        mock.expectedMessageCount(1);
+        template.sendBody("direct:unmarshal", getData());
+        mock.assertIsSatisfied();
+        Message message = mock.getReceivedExchanges().get(0).getIn();
+        List<List<String>> body = CastUtils.cast((List)message.getBody());
+        assertNotNull(body);
+        assertEquals(body.size(), 1);
+        List<String> row = body.get(0);
+        assertEquals(row.size(), 3);
+        assertEquals(row.toString(), "[Hello, Again, Democracy]");
+    }
+
+    private String getData() {
+        return Stream.of("A1", "B1", "C1").collect(Collectors.joining(";"));
+    }
+
+    @Override
+    protected ClassPathXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext(
+                                                  "org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverter.xml");
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/d6b06755/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/converter/MyCvsRecordConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/converter/MyCvsRecordConverter.java
b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/converter/MyCvsRecordConverter.java
new file mode 100644
index 0000000..edd25fd
--- /dev/null
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/converter/MyCvsRecordConverter.java
@@ -0,0 +1,30 @@
+package org.apache.camel.dataformat.csv.converter;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.camel.dataformat.csv.CsvRecordConverter;
+import org.apache.commons.csv.CSVRecord;
+
+/**
+ * Test {@link CsvRecordConverter} implementation.
+ * <p>
+ * This implementation is explicitely created in a subpackage to check the
+ * visibility of {@link CsvRecordConverter}.
+ * </p>
+ */
+public class MyCvsRecordConverter implements CsvRecordConverter<List<String>>
{
+
+    private final String[] record;
+
+    public MyCvsRecordConverter(String... record) {
+        assert record != null : "Unspecified record";
+        this.record = record;
+    }
+
+    @Override
+    public List<String> convertRecord(CSVRecord record) {
+        assert record != null : "Unspecified record";
+        return Arrays.asList(this.record);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/d6b06755/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverter.xml
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverter.xml
b/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverter.xml
new file mode 100644
index 0000000..7fbb7c2
--- /dev/null
+++ b/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatCustomRecordConverter.xml
@@ -0,0 +1,43 @@
+<?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">
+
+	<bean id="record-converter" class="org.apache.camel.dataformat.csv.converter.MyCvsRecordConverter">
+		<constructor-arg>
+			<array>
+				<value>Hello</value>
+				<value>Again</value>
+				<value>Democracy</value>
+			</array>
+		</constructor-arg>
+	</bean>
+
+	<camelContext id="csvCamelContext" xmlns="http://camel.apache.org/schema/spring">
+		<route>
+			<from uri="direct:unmarshal" />
+			<unmarshal>
+				<csv recordConverterRef="record-converter" delimiter=";" headerDisabled="true" />
+			</unmarshal>
+			<to uri="mock:unmarshaled" />
+		</route>
+	</camelContext>
+
+</beans>
\ No newline at end of file


Mime
View raw message