You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bv...@apache.org on 2012/04/27 13:24:15 UTC

svn commit: r1331359 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/dataformat/ components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/ components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/ components/camel...

Author: bvahdat
Date: Fri Apr 27 11:24:14 2012
New Revision: 1331359

URL: http://svn.apache.org/viewvc?rev=1331359&view=rev
Log:
CAMEL-5166: Add the skipFirstLine option to the CSV DataFormat.

Added:
    camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java   (with props)
    camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineSpringTest-context.xml   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java
    camel/trunk/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java?rev=1331359&r1=1331358&r2=1331359&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java Fri Apr 27 11:24:14 2012
@@ -43,6 +43,8 @@ public class CsvDataFormat extends DataF
     private String configRef;
     @XmlAttribute
     private String strategyRef;
+    @XmlAttribute
+    private Boolean skipFirstLine;
 
     public CsvDataFormat() {
         super("csv");
@@ -85,6 +87,14 @@ public class CsvDataFormat extends DataF
         this.strategyRef = strategyRef;
     }
 
+    public Boolean isSkipFirstLine() {
+        return autogenColumns;
+    }
+
+    public void setSkipFirstLine(Boolean skipFirstLine) {
+        this.skipFirstLine = skipFirstLine;
+    }
+
     @Override
     protected DataFormat createDataFormat(RouteContext routeContext) {
         DataFormat csvFormat = super.createDataFormat(routeContext);
@@ -116,5 +126,9 @@ public class CsvDataFormat extends DataF
             // the default delimiter is ','
             setProperty(dataFormat, "delimiter", ",");
         }
+
+        if (skipFirstLine != null) {
+            setProperty(dataFormat, "skipFirstLine", skipFirstLine);
+        }
     }
 }
\ No newline at end of file

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=1331359&r1=1331358&r2=1331359&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 Fri Apr 27 11:24:14 2012
@@ -53,6 +53,7 @@ public class CsvDataFormat implements Da
     private CSVConfig config = new CSVConfig();
     private boolean autogenColumns = true;
     private String delimiter;
+    private boolean skipFirstLine;
 
     public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws Exception {
         if (delimiter != null) {
@@ -97,8 +98,16 @@ public class CsvDataFormat implements Da
         try {
             CSVParser parser = new CSVParser(in, strategy);
             List<List<String>> list = new ArrayList<List<String>>();
+            boolean isFirstLine = true;
             while (true) {
                 String[] strings = parser.getLine();
+                if (isFirstLine) {
+                    isFirstLine = false;
+                    if (skipFirstLine) {
+                        // skip considering the first line if we're asked to do so
+                        continue;
+                    }
+                }
                 if (strings == null) {
                     break;
                 }
@@ -151,6 +160,14 @@ public class CsvDataFormat implements Da
         this.autogenColumns = autogenColumns;
     }
 
+    public boolean isSkipFirstLine() {
+        return skipFirstLine;
+    }
+
+    public void setSkipFirstLine(boolean skipFirstLine) {
+        this.skipFirstLine = skipFirstLine;
+    }
+
     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/CsvUnmarshalSkipFirstLineTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java?rev=1331359&view=auto
==============================================================================
--- camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java (added)
+++ camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineTest.java Fri Apr 27 11:24:14 2012
@@ -0,0 +1,80 @@
+/**
+ * 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> demonstrating the usage of
+ * the <tt>skipFirstLine</tt> option.
+ */
+public class CsvUnmarshalSkipFirstLineTest 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<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));        
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testCsvUnMarshalSingleLine() throws Exception {
+        result.expectedMessageCount(1);
+
+        // the first line, the same as the second line as well, contains also a data row but as we set
+        // skipFirstLine to true the first row gets simply ignored and not unmarshalled
+        template.sendBody("direct:start", "124|ActiveMQ in Action|2\n123|Camel in Action|1");
+
+        assertMockEndpointsSatisfied();
+
+        List<List<String>> body = result.getReceivedExchanges().get(0).getIn().getBody(List.class);
+        assertEquals(1, 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));
+    }
+    
+    @Override
+    protected ClassPathXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvUnmarshalSkipFirstLineSpringTest-context.xml");
+    }
+}
\ No newline at end of file

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

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