This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 7bb1a667fed9e521735091e64c4eb24a1e76d7d0
Author: Ricardo Zanini <[email protected]>
AuthorDate: Thu Dec 14 23:28:29 2017 -0200

    CAMEL-12080: allow creating csvcontent without last eol
---
 .../camel/dataformat/bindy/BindyCsvFactory.java    |   9 ++
 .../dataformat/bindy/annotation/CsvRecord.java     |   5 +
 .../dataformat/bindy/csv/BindyCsvDataFormat.java   |  14 ++-
 .../csv/BindyMarshalEndWithLineBreakTest.java      |  87 ++++++++++++++
 .../dataformat/bindy/model/csv/MyCsvRecord2.java   | 130 +++++++++++++++++++++
 5 files changed, 239 insertions(+), 6 deletions(-)

diff --git 
a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
 
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
index 994bed0..9906402 100755
--- 
a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
+++ 
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
@@ -74,6 +74,7 @@ public class BindyCsvFactory extends BindyAbstractFactory 
implements BindyFactor
     private boolean autospanLine;
     private boolean allowEmptyStream;
     private boolean quotingEscaped;
+    private boolean endWithLineBreak;
 
     public BindyCsvFactory(Class<?> type) throws Exception {
         super(type);
@@ -624,6 +625,10 @@ public class BindyCsvFactory extends BindyAbstractFactory 
implements BindyFactor
                     // Get quotingEscaped parameter
                     quotingEscaped = record.quotingEscaped();
                     LOG.debug("Escape quote character flag of the CSV: {}" + 
quotingEscaped);
+                    
+                    // Get endWithLineBreak parameter
+                    endWithLineBreak = record.endWithLineBreak();
+                    LOG.debug("End with line break: {}" + endWithLineBreak);
                 }
 
                 if (section != null) {
@@ -710,4 +715,8 @@ public class BindyCsvFactory extends BindyAbstractFactory 
implements BindyFactor
     public boolean isAllowEmptyStream() {
         return allowEmptyStream;
     }
+    
+    public boolean isEndWithLineBreak() {
+        return endWithLineBreak;
+    }
 }
diff --git 
a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/CsvRecord.java
 
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/CsvRecord.java
index 63f1d11..dd7bd18 100755
--- 
a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/CsvRecord.java
+++ 
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/CsvRecord.java
@@ -95,5 +95,10 @@ public @interface CsvRecord {
      * the unavaiable stream for CSV file.
      */
     boolean allowEmptyStream() default false;
+    
+    /**
+     * The endWithLineBreak parameter flags if the CSV file should end with a 
line break or not (optional)
+     */
+    boolean endWithLineBreak() default true;
 
 }
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 ad24a99..00ea809 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
@@ -94,16 +94,18 @@ public class BindyCsvDataFormat extends 
BindyAbstractDataFormat {
                 models.add(row);
             }
         }
-
-        for (Map<String, Object> model : models) {
-
-            String result = factory.unbind(getCamelContext(), model);
+        
+        Iterator<Map<String, Object>> modelsMap = models.iterator();
+        while (modelsMap.hasNext()) {
+            String result = factory.unbind(getCamelContext(), 
modelsMap.next());
 
             byte[] bytes = 
exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, 
result);
             outputStream.write(bytes);
 
-            // Add a carriage return
-            outputStream.write(bytesCRLF);
+            if (factory.isEndWithLineBreak() || modelsMap.hasNext()) {
+                // Add a carriage return
+                outputStream.write(bytesCRLF);
+            }
         }
     }
 
diff --git 
a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalEndWithLineBreakTest.java
 
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalEndWithLineBreakTest.java
new file mode 100644
index 0000000..a404845
--- /dev/null
+++ 
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalEndWithLineBreakTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.annotation.CsvRecord;
+import org.apache.camel.dataformat.bindy.model.csv.MyCsvRecord;
+import org.apache.camel.dataformat.bindy.model.csv.MyCsvRecord2;
+import org.apache.camel.dataformat.bindy.util.ConverterUtils;
+import org.apache.camel.model.dataformat.BindyType;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class BindyMarshalEndWithLineBreakTest extends CamelTestSupport {
+
+    @Test
+    public void testCsvWithEndingLineBreak() throws Exception {
+        final CsvRecord record = 
MyCsvRecord.class.getAnnotation(CsvRecord.class);
+        final MyCsvRecord csvRecord = new MyCsvRecord();
+        csvRecord.setAddressLine1("221b Baker Street");
+        csvRecord.setCity("London");
+        csvRecord.setCountry("England");
+        csvRecord.setAttention("1");
+        
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        
mock.message(0).body().convertToString().endsWith(ConverterUtils.getStringCarriageReturn(record.crlf()));
+        
+        template.sendBody("direct:withlb", csvRecord);
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    @Test
+    public void testCsvWithoutEndingLineBreak() throws Exception {
+        final CsvRecord record = 
MyCsvRecord2.class.getAnnotation(CsvRecord.class);
+        final MyCsvRecord2 csvRecord2 = new MyCsvRecord2();
+        csvRecord2.setAddressLine1("221b Baker Street");
+        csvRecord2.setCity("London");
+        csvRecord2.setCountry("England");
+        csvRecord2.setAttention("1");
+        
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().convertToString().endsWith(record.separator());
+        
+        template.sendBody("direct:withoutlb", csvRecord2);
+
+        assertMockEndpointsSatisfied();        
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:withoutlb") //
+                    .marshal().bindy(BindyType.Csv, MyCsvRecord2.class) //
+                    .to("log:after.unmarshal") //
+                    .to("mock:result"); //
+
+                from("direct:withlb") //
+                    .marshal().bindy(BindyType.Csv, MyCsvRecord.class) //
+                    .to("log:after.marshal") //
+                    .to("mock:result");
+            }
+        };
+    }
+}
diff --git 
a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/csv/MyCsvRecord2.java
 
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/csv/MyCsvRecord2.java
new file mode 100644
index 0000000..8b09765
--- /dev/null
+++ 
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/csv/MyCsvRecord2.java
@@ -0,0 +1,130 @@
+/**
+ * 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.csv;
+
+import java.io.Serializable;
+
+import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
+import org.apache.camel.dataformat.bindy.annotation.DataField;
+
+@CsvRecord(separator = ",", skipFirstLine = false, endWithLineBreak = false)
+public class MyCsvRecord2 implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @DataField(pos = 1)
+    private String attention;
+    @DataField(pos = 2)
+    private String addressLine1;
+    @DataField(pos = 3)
+    private String addressLine2;
+    @DataField(pos = 4)
+    private String city;
+    @DataField(pos = 5)
+    private String state;
+    @DataField(pos = 6)
+    private String zip;
+    @DataField(pos = 7)
+    private String country;
+    @DataField(pos = 8)
+    private String dummy1;
+    @DataField(pos = 9)
+    private String dummy2;
+
+    public MyCsvRecord2() {
+    }
+
+    public String getAttention() {
+        return attention;
+    }
+
+    public void setAttention(String attention) {
+        this.attention = attention;
+    }
+
+    public String getAddressLine1() {
+        return addressLine1;
+    }
+
+    public void setAddressLine1(String addressLine1) {
+        this.addressLine1 = addressLine1;
+    }
+
+    public String getAddressLine2() {
+        return addressLine2;
+    }
+
+    public void setAddressLine2(String addressLine2) {
+        this.addressLine2 = addressLine2;
+    }
+
+    public String getCity() {
+        return city;
+    }
+
+    public void setCity(String city) {
+        this.city = city;
+    }
+
+    public String getState() {
+        return state;
+    }
+
+    public void setState(String state) {
+        this.state = state;
+    }
+
+    public String getZip() {
+        return zip;
+    }
+
+    public void setZip(String zip) {
+        this.zip = zip;
+    }
+
+    public String getCountry() {
+        return country;
+    }
+
+    public void setCountry(String country) {
+        this.country = country;
+    }
+
+    public String getDummy1() {
+        return dummy1;
+    }
+
+    public void setDummy1(String dummy1) {
+        this.dummy1 = dummy1;
+    }
+
+    public String getDummy2() {
+        return dummy2;
+    }
+
+    public void setDummy2(String dummy2) {
+        this.dummy2 = dummy2;
+    }
+
+    @Override
+    public String toString() {
+        return "Record [attention=" + attention + ", addressLine1=" + 
addressLine1 + ", addressLine2="
+               + addressLine2 + ", city=" + city + ", state=" + state + ", 
zip=" + zip + ", country="
+               + country + ", dummy1=" + dummy1 + ", dummy2=" + dummy2 + "]";
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to