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.
Date Sat, 18 Jan 2014 11:38:19 GMT
Updated Branches:
  refs/heads/master 6695496ac -> f96f669c2


CAMEL-7142: Don't modify the properties of the CSVStrategy constants as this would cause side
effects when multiple CsvDataFormat objects are in use.

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

Branch: refs/heads/master
Commit: f96f669c26c5c5a262d20db7351e2cdde65aa8b2
Parents: 6695496
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:37:55 2014 +0100

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


http://git-wip-us.apache.org/repos/asf/camel/blob/f96f669c/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 848ac49..64d9036 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
@@ -22,6 +22,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.Writer;
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -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,7 +53,9 @@ 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;
@@ -60,6 +66,26 @@ public class CsvDataFormat implements DataFormat {
     private boolean lazyLoad;
     private boolean useMaps;
 
+    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));
@@ -117,7 +143,7 @@ public class CsvDataFormat implements DataFormat {
                 }
             }
 
-            @SuppressWarnings("unchecked") CsvIterator<?> csvIterator = new CsvIterator(parser,
reader, lineConverter);
+            @SuppressWarnings({"unchecked", "rawtypes"}) CsvIterator<?> csvIterator
= new CsvIterator(parser, reader, lineConverter);
             return lazyLoad ? csvIterator : loadAllAsList(csvIterator);
         } catch (Exception e) {
             error = true;
@@ -166,7 +192,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/f96f669c/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