You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/02/25 08:11:52 UTC

camel git commit: CAMEL-9432 - Bindy CSV separator not treated as regex but fixed character in all cases

Repository: camel
Updated Branches:
  refs/heads/master f3671b48a -> e26e8be21


CAMEL-9432 - Bindy CSV separator not treated as regex but fixed character in all cases


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

Branch: refs/heads/master
Commit: e26e8be211b0d5f6f07c3e99b328a17892ee0c35
Parents: f3671b4
Author: lburgazzoli <lb...@gmail.com>
Authored: Wed Feb 24 14:31:46 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Feb 25 08:00:50 2016 +0100

----------------------------------------------------------------------
 .../bindy/csv/BindyCsvDataFormat.java           | 38 +----------
 .../BindySimpleCsvRegexAutospanLineTest.java    | 72 ++++++++++++++++++++
 .../spanLastRecord/RegexSpanLastRecord.java     | 61 +++++++++++++++++
 3 files changed, 134 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e26e8be2/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
index cff8e7b..e88e6ea 100755
--- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
@@ -165,7 +165,7 @@ public class BindyCsvDataFormat extends BindyAbstractDataFormat {
 
                 // Split the CSV record according to the separator defined in
                 // annotated class @CSVRecord
-                String[] tokens = line.split(separator, -1);
+                String[] tokens = line.split(separator, factory.getAutospanLine() ? factory.getMaxpos() : -1);
                 List<String> result = Arrays.asList(tokens);
                 // must unquote tokens before use
                 result = unquoteTokens(result, separator, quote);
@@ -177,10 +177,6 @@ public class BindyCsvDataFormat extends BindyAbstractDataFormat {
                         LOG.debug("Size of the record splitted : {}", result.size());
                     }
 
-                    if (factory.getAutospanLine()) {
-                        result = autospanLine(result, factory.getMaxpos(), separator);
-                    }
-
                     // Bind data from CSV record with model classes
                     factory.bind(result, model, count);
 
@@ -210,38 +206,6 @@ public class BindyCsvDataFormat extends BindyAbstractDataFormat {
     }
 
     /**
-     * Concatenate "the rest of the line" as the last record. Works similar as if quoted
-     *
-     * @param result    input result set
-     * @param maxpos    position of maximum record
-     * @param separator csv separator char
-     * @return List<String> with concatenated last record
-     */
-    private static List<String> autospanLine(final List<String> result, final int maxpos, final String separator) {
-        if (result.size() <= maxpos) {
-            return result;
-        }
-
-        final List<String> answer = new ArrayList<String>();
-        final StringBuilder lastRecord = new StringBuilder();
-
-        final Iterator<String> it = result.iterator();
-        for (int counter = 0; counter < maxpos - 1; counter++) {
-            answer.add(it.next());
-        }
-
-        while (it.hasNext()) {
-            lastRecord.append(it.next());
-            if (it.hasNext()) {
-                lastRecord.append(separator);
-            }
-        }
-        answer.add(lastRecord.toString());
-
-        return answer;
-    }
-
-    /**
      * Unquote the tokens, by removing leading and trailing quote chars,
      * as will handling fixing broken tokens which may have been split
      * by a separator inside a quote.

http://git-wip-us.apache.org/repos/asf/camel/blob/e26e8be2/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvRegexAutospanLineTest.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvRegexAutospanLineTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvRegexAutospanLineTest.java
new file mode 100644
index 0000000..066a2b5
--- /dev/null
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvRegexAutospanLineTest.java
@@ -0,0 +1,72 @@
+/**
+ * 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.bindy.csv;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.dataformat.bindy.model.simple.spanLastRecord.RegexSpanLastRecord;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class BindySimpleCsvRegexAutospanLineTest extends CamelTestSupport {
+
+    @Test
+    public void testUnmarshalNoNeedToSpanLine() throws Exception {
+        final MockEndpoint mock = getMockEndpoint("mock:unmarshal");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:unmarshal", "1 hei kommentar");
+
+        assertMockEndpointsSatisfied();
+
+        final RegexSpanLastRecord order = mock.getReceivedExchanges().get(0).getIn().getBody(RegexSpanLastRecord.class);
+        
+        assertEquals(1, order.getRecordId());
+        assertEquals("hei", order.getName());
+        assertEquals("kommentar", order.getComment());
+    }
+
+    @Test
+    public void testUnmarshalSpanningLine() throws Exception {
+        final MockEndpoint mock = getMockEndpoint("mock:unmarshal");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:unmarshal", "1 hei kommentar test noe hei");
+
+        assertMockEndpointsSatisfied();
+
+        final RegexSpanLastRecord order = mock.getReceivedExchanges().get(0).getIn().getBody(RegexSpanLastRecord.class);
+        
+        assertEquals(1, order.getRecordId());
+        assertEquals("hei", order.getName());
+        assertEquals("kommentar test noe hei", order.getComment());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                final BindyCsvDataFormat bindy = new BindyCsvDataFormat(RegexSpanLastRecord.class);
+
+                from("direct:unmarshal")
+                        .unmarshal(bindy)
+                        .to("mock:unmarshal");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e26e8be2/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/spanLastRecord/RegexSpanLastRecord.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/spanLastRecord/RegexSpanLastRecord.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/spanLastRecord/RegexSpanLastRecord.java
new file mode 100644
index 0000000..878ade8
--- /dev/null
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/spanLastRecord/RegexSpanLastRecord.java
@@ -0,0 +1,61 @@
+/**
+ * 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.bindy.model.simple.spanLastRecord;
+
+import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
+import org.apache.camel.dataformat.bindy.annotation.DataField;
+
+@CsvRecord(separator = "\\s+", autospanLine = true)
+public class RegexSpanLastRecord {
+
+    @DataField(pos = 1)
+    private int recordId;
+    @DataField(pos = 2)
+    private String name;
+    @DataField(pos = 3)
+    private String comment;
+
+    public int getRecordId() {
+        return recordId;
+    }
+
+    public void setRecordId(final int recordId) {
+        this.recordId = recordId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(final String name) {
+        this.name = name;
+    }
+
+    public String getComment() {
+        return comment;
+    }
+
+    public void setComment(final String comment) {
+        this.comment = comment;
+    }
+
+    @Override
+    public String toString() {
+        return "SpanLastRecord [recordId=" + recordId + ", name=" + name + ", comment=" + comment + "]";
+    }
+
+}