camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bvah...@apache.org
Subject git commit: CAMEL-7142: Don't modify the properties of the CSVStrategy constants as this would cause side effects when multiple CsvDataFormat objects are in use. Conflicts: components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat
Date Sat, 18 Jan 2014 12:13:51 GMT
Updated Branches:
  refs/heads/camel-2.11.x 525fd0cc3 -> 1b7162f6d


CAMEL-7142: Don't modify the properties of the CSVStrategy constants as this would cause side
effects when multiple CsvDataFormat objects are in use.
Conflicts:
	components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java


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

Branch: refs/heads/camel-2.11.x
Commit: 1b7162f6d03e5bbb5d07b8230644f92331dbbe84
Parents: 525fd0c
Author: Babak Vahdat <bvahdat@apache.org>
Authored: Sat Jan 18 12:37:55 2014 +0100
Committer: Babak Vahdat <bvahdat@apache.org>
Committed: Sat Jan 18 12:57:15 2014 +0100

----------------------------------------------------------------------
 .../camel/dataformat/csv/CsvDataFormat.java     | 30 ++++++-
 .../csv/CsvUnmarshalTwoCsvDataFormatTest.java   | 88 ++++++++++++++++++++
 2 files changed, 116 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1b7162f6/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
index 9ad7b53..26a2b0b 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
@@ -21,6 +21,7 @@ import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -31,11 +32,14 @@ import org.apache.camel.Exchange;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.IOHelper;
+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;
 import org.apache.commons.csv.writer.CSVField;
 import org.apache.commons.csv.writer.CSVWriter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * CSV Data format.
@@ -49,12 +53,34 @@ import org.apache.commons.csv.writer.CSVWriter;
  * @version 
  */
 public class CsvDataFormat implements DataFormat {
-    private CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
+    private static final Logger LOGGER = LoggerFactory.getLogger(CsvDataFormat.class);
+
+    private CSVStrategy strategy = cloneCSVStrategyIfNecessary(CSVStrategy.DEFAULT_STRATEGY);
     private CSVConfig config = new CSVConfig();
     private boolean autogenColumns = true;
     private String delimiter;
     private boolean skipFirstLine;
 
+    private static CSVStrategy cloneCSVStrategyIfNecessary(CSVStrategy csvStrategy) {
+        for (Field field : CSVStrategy.class.getFields()) {
+            try {
+                if (field.get(null) == csvStrategy) {
+                    // return a safe copy of the static constant so that we don't cause any
side effect
+                    // by the other CsvDataFormat objects in case we would change any property
on this
+                    // strategy itself (e.g. it's delimiter through the #unmarshal() method
below)
+                    LOGGER.debug("Returning a clone of {} as it is the declared constant
{} by the CSVStrategy class", csvStrategy, field.getName());
+
+                    return (CSVStrategy) csvStrategy.clone();
+                }
+            } catch (Exception e) {
+                ObjectHelper.wrapRuntimeCamelException(e);
+            }
+        }
+
+        // not a declared constant of CSVStrategy, so return it as is
+        return csvStrategy;
+    }
+
     public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws
Exception {
         if (delimiter != null) {
             config.setDelimiter(delimiter.charAt(0));
@@ -146,7 +172,7 @@ public class CsvDataFormat implements DataFormat {
     }
 
     public void setStrategy(CSVStrategy strategy) {
-        this.strategy = strategy;
+        this.strategy = cloneCSVStrategyIfNecessary(strategy);
     }
 
     public boolean isAutogenColumns() {

http://git-wip-us.apache.org/repos/asf/camel/blob/1b7162f6/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalTwoCsvDataFormatTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalTwoCsvDataFormatTest.java
b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalTwoCsvDataFormatTest.java
new file mode 100644
index 0000000..ef318be
--- /dev/null
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalTwoCsvDataFormatTest.java
@@ -0,0 +1,88 @@
+/**
+ * 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.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class CsvUnmarshalTwoCsvDataFormatTest extends CamelTestSupport {
+
+    @EndpointInject(uri = "mock:result")
+    private MockEndpoint result;
+
+    @EndpointInject(uri = "mock:result2")
+    private MockEndpoint result2;
+
+    @Test
+    public void testCsvUnMarshal() throws Exception {
+        result.expectedMessageCount(1);
+        result2.expectedMessageCount(1);
+        sendAndVerify("|", result);
+
+        resetMocks();
+
+        result.expectedMessageCount(1);
+        result2.expectedMessageCount(1);
+        sendAndVerify(";", result2);
+    }
+
+    private void sendAndVerify(String delimiter, MockEndpoint mock) throws InterruptedException
{
+        template.sendBody("direct:start", "123" + delimiter + "Camel in Action" + delimiter
+ "1\n124" + delimiter + "ActiveMQ in Action" + delimiter + "2");
+        assertMockEndpointsSatisfied();
+
+        @SuppressWarnings("unchecked")
+        List<List<String>> body = mock.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();
+                csv.setDelimiter("|");
+                CsvDataFormat csv2 = new CsvDataFormat();
+                csv2.setDelimiter(";");
+
+                from("direct:start").multicast().parallelProcessing().to("direct:csv", "direct:csv2");
+
+                from("direct:csv")
+                        .unmarshal(csv)
+                        .to("mock:result");
+
+                from("direct:csv2")
+                        .unmarshal(csv2)
+                        .to("mock:result2");
+            }
+        };
+    }
+}
\ No newline at end of file


Mime
View raw message