camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cmuel...@apache.org
Subject svn commit: r958420 - in /camel/trunk/components/camel-csv/src: main/java/org/apache/camel/dataformat/csv/ test/java/org/apache/camel/dataformat/csv/ test/resources/org/ test/resources/org/apache/ test/resources/org/apache/camel/ test/resources/org/apa...
Date Sun, 27 Jun 2010 20:41:27 GMT
Author: cmueller
Date: Sun Jun 27 20:41:27 2010
New Revision: 958420

URL: http://svn.apache.org/viewvc?rev=958420&view=rev
Log:
CAMEL-2837: CsvDataFromat delimiter should also be configurable in the XML/Spring DSL

Added:
    camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java
  (with props)
    camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest.java
  (with props)
    camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterTest.java
  (with props)
    camel/trunk/components/camel-csv/src/test/resources/org/
    camel/trunk/components/camel-csv/src/test/resources/org/apache/
    camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/
    camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/
    camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/
    camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml
  (with props)
    camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest-context.xml
  (with props)
Modified:
    camel/trunk/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
    camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java

Modified: camel/trunk/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java?rev=958420&r1=958419&r2=958420&view=diff
==============================================================================
--- camel/trunk/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
(original)
+++ camel/trunk/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
Sun Jun 27 20:41:27 2010
@@ -30,7 +30,6 @@ import java.util.Set;
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.util.ExchangeHelper;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.csv.CSVParser;
 import org.apache.commons.csv.CSVStrategy;
 import org.apache.commons.csv.writer.CSVConfig;
@@ -52,9 +51,12 @@ public class CsvDataFormat implements Da
     private CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
     private CSVConfig config = new CSVConfig();
     private boolean autogenColumns = true;
+    private String delimiter;
 
     public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws
Exception {
-        ObjectHelper.notNull(config, "config");
+        if (delimiter != null) {
+            config.setDelimiter(delimiter.charAt(0));
+        }
 
         OutputStreamWriter out = new OutputStreamWriter(outputStream);
         CSVWriter csv = new CSVWriter(config);
@@ -87,8 +89,12 @@ public class CsvDataFormat implements Da
 
     public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception
{
         InputStreamReader in = new InputStreamReader(inputStream);
+        if (delimiter != null) {
+            strategy.setDelimiter(delimiter.charAt(0));
+        }
+        
         try {
-            CSVParser parser = new CSVParser(in, getStrategy());
+            CSVParser parser = new CSVParser(in, strategy);
             List<List<String>> list = new ArrayList<List<String>>();
             while (true) {
                 String[] strings = parser.getLine();
@@ -107,6 +113,21 @@ public class CsvDataFormat implements Da
             in.close();
         }
     }
+    
+    public String getDelimiter() {
+        return delimiter;
+    }
+
+    public void setDelimiter(String delimiter) {
+        if (delimiter != null && delimiter.length() > 1) {
+            throw new IllegalArgumentException("Delimiter must have a length of one!");
+        }
+        this.delimiter = delimiter;
+    }
+    
+    public CSVConfig getConfig() {
+        return config;
+    }
 
     public void setConfig(CSVConfig config) {
         this.config = config;
@@ -133,10 +154,6 @@ public class CsvDataFormat implements Da
         this.autogenColumns = autogenColumns;
     }
 
-    protected CSVConfig createConfig() {
-        return new CSVConfig();
-    }
-
     private synchronized void updateFieldsInConfig(Set set, Exchange exchange) {
         for (Object value : set) {
             if (value != null) {

Added: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java?rev=958420&view=auto
==============================================================================
--- camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java
(added)
+++ camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java
Sun Jun 27 20:41:27 2010
@@ -0,0 +1,77 @@
+/**
+ * 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.junit4.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Spring based integration test for the <code>CsvDataFormat</code>
+ * @version $Revision: $
+ * @author cmueller
+ */
+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");
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java?rev=958420&r1=958419&r2=958420&view=diff
==============================================================================
--- camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java
(original)
+++ camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterTest.java
Sun Jun 27 20:41:27 2010
@@ -78,6 +78,10 @@ public class CsvMarshalPipeDelimiterTest
                 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");

Added: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest.java?rev=958420&view=auto
==============================================================================
--- camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest.java
(added)
+++ camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest.java
Sun Jun 27 20:41:27 2010
@@ -0,0 +1,60 @@
+/**
+ * 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.EndpointInject;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Spring based integration test for the <code>CsvDataFormat</code>
+ * @version $Revision: $
+ * @author cmueller
+ */
+public class CsvUnmarshalPipeDelimiterSpringTest extends CamelSpringTestSupport {
+
+    @EndpointInject(uri = "mock:result")
+    private MockEndpoint result;
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testCsvMarshal() throws Exception {
+        result.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "123|Camel in Action|1\n124|ActiveMQ in Action|2");
+
+        assertMockEndpointsSatisfied();
+
+        List<List<String>> body = result.getReceivedExchanges().get(0).getIn().getBody(List.class);
+        assertEquals(2, body.size());
+        assertEquals("123", body.get(0).get(0));
+        assertEquals("Camel in Action", body.get(0).get(1));
+        assertEquals("1", body.get(0).get(2));
+        assertEquals("124", body.get(1).get(0));
+        assertEquals("ActiveMQ in Action", body.get(1).get(1));
+        assertEquals("2", body.get(1).get(2));        
+    }
+
+    @Override
+    protected ClassPathXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest-context.xml");
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterTest.java?rev=958420&view=auto
==============================================================================
--- camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterTest.java
(added)
+++ camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterTest.java
Sun Jun 27 20:41:27 2010
@@ -0,0 +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.List;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.CamelTestSupport;
+import org.apache.commons.csv.CSVStrategy;
+import org.junit.Test;
+
+/**
+ * Spring based integration test for the <code>CsvDataFormat</code>
+ * @version $Revision: $
+ * @author cmueller
+ */
+public class CsvUnmarshalPipeDelimiterTest extends CamelTestSupport {
+
+    @EndpointInject(uri = "mock:result")
+    private MockEndpoint result;
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testCsvMarshal() throws Exception {
+        result.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "123|Camel in Action|1\n124|ActiveMQ in Action|2");
+
+        assertMockEndpointsSatisfied();
+
+        List<List<String>> body = result.getReceivedExchanges().get(0).getIn().getBody(List.class);
+        assertEquals(2, body.size());
+        assertEquals("123", body.get(0).get(0));
+        assertEquals("Camel in Action", body.get(0).get(1));
+        assertEquals("1", body.get(0).get(2));
+        assertEquals("124", body.get(1).get(0));
+        assertEquals("ActiveMQ in Action", body.get(1).get(1));
+        assertEquals("2", body.get(1).get(2));        
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                CsvDataFormat csv = new CsvDataFormat();
+                CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
+                strategy.setDelimiter('|');
+                csv.setStrategy(strategy);
+                
+                // also possible
+                // CsvDataFormat csv = new CsvDataFormat();
+                // csv.setDelimiter("|");
+
+                from("direct:start").unmarshal(csv)
+                    .to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml?rev=958420&view=auto
==============================================================================
--- camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml
(added)
+++ camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml
Sun Jun 27 20:41:27 2010
@@ -0,0 +1,33 @@
+<?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-2.5.xsd
+    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
+
+  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+    <route>
+      <from uri="direct:start" />
+      <marshal>
+        <csv delimiter="|" />
+      </marshal>
+      <convertBodyTo type="java.lang.String" />
+      <to uri="mock:result" />
+    </route>
+  </camelContext>
+</beans>
\ No newline at end of file

Propchange: camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalPipeDelimiterSpringTest-context.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest-context.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest-context.xml?rev=958420&view=auto
==============================================================================
--- camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest-context.xml
(added)
+++ camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest-context.xml
Sun Jun 27 20:41:27 2010
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+  <!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements. See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version
+    2.0 (the "License"); you may not use this file except in compliance
+    with the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+    applicable law or agreed to in writing, software distributed under
+    the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
+    OR CONDITIONS OF ANY KIND, either express or implied. See the
+    License for the specific language governing permissions and
+    limitations under the License.
+  -->
+<beans xmlns="http://www.springframework.org/schema/beans"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="
+    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
+
+  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+    <route>
+      <from uri="direct:start" />
+      <unmarshal>
+        <csv delimiter="|" />
+      </unmarshal>
+      <to uri="mock:result" />
+    </route>
+  </camelContext>
+</beans>
\ No newline at end of file

Propchange: camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest-context.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalPipeDelimiterSpringTest-context.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message