camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bvah...@apache.org
Subject svn commit: r1302006 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/dataformat/ components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/ components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/
Date Sat, 17 Mar 2012 20:55:24 GMT
Author: bvahdat
Date: Sat Mar 17 20:55:23 2012
New Revision: 1302006

URL: http://svn.apache.org/viewvc?rev=1302006&view=rev
Log:
CAMEL-5105: Add the options configRef and strategyRef to csv data format in XML DSL.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java
    camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java
    camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml

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=1302006&r1=1302005&r2=1302006&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
Sat Mar 17 20:55:23 2012
@@ -23,6 +23,9 @@ import javax.xml.bind.annotation.XmlRoot
 
 import org.apache.camel.model.DataFormatDefinition;
 import org.apache.camel.spi.DataFormat;
+import org.apache.camel.spi.RouteContext;
+import org.apache.camel.util.CamelContextHelper;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * Represents a CSV (Comma Separated Values) {@link org.apache.camel.spi.DataFormat}
@@ -36,6 +39,10 @@ public class CsvDataFormat extends DataF
     private Boolean autogenColumns;
     @XmlAttribute
     private String delimiter;
+    @XmlAttribute
+    private String configRef;
+    @XmlAttribute
+    private String strategyRef;
 
     public CsvDataFormat() {
         super("csv");
@@ -62,6 +69,38 @@ public class CsvDataFormat extends DataF
         this.delimiter = delimiter;
     }
 
+    public String getConfigRef() {
+        return configRef;
+    }
+
+    public void setConfigRef(String configRef) {
+        this.configRef = configRef;
+    }
+
+    public String getStrategyRef() {
+        return strategyRef;
+    }
+
+    public void setStrategyRef(String strategyRef) {
+        this.strategyRef = strategyRef;
+    }
+
+    @Override
+    protected DataFormat createDataFormat(RouteContext routeContext) {
+        DataFormat csvFormat = super.createDataFormat(routeContext);
+
+        if (ObjectHelper.isNotEmpty(configRef)) {
+            Object config = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(),
configRef);
+            setProperty(csvFormat, "config", config);
+        }
+        if (ObjectHelper.isNotEmpty(strategyRef)) {
+            Object strategy = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(),
strategyRef);
+            setProperty(csvFormat, "strategy", strategy);
+        }
+
+        return csvFormat;
+    }
+
     @Override
     protected void configureDataFormat(DataFormat dataFormat) {
         if (autogenColumns != null) {

Modified: camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java?rev=1302006&r1=1302005&r2=1302006&view=diff
==============================================================================
--- camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java
(original)
+++ camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java
Sat Mar 17 20:55:23 2012
@@ -30,8 +30,8 @@ import org.junit.Test;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 /**
- * Spring based integration test for the <code>CsvDataFormat</code>
- * @version
+ * Spring based integration test for the <code>CsvDataFormat</code> demonstrating
the usage of
+ * the <tt>autogenColumns</tt>, <tt>configRef</tt> and <tt>strategyRef</tt>
options.
  */
 public class CsvMarshalAutogenColumnsSpringTest extends CamelSpringTestSupport {
 
@@ -42,23 +42,33 @@ public class CsvMarshalAutogenColumnsSpr
     private MockEndpoint result2;
 
     @Test
-    public void testWithAutogenColumnsAttributeSet() throws Exception {
+    public void retrieveColumnsWithAutogenColumnsFalseAndItemColumnsSet() throws Exception
{
         result.expectedMessageCount(1);
-        result2.expectedMessageCount(1);
 
         template.sendBody("direct:start", createBody());
-        template.sendBody("direct:start2", createBody());
 
-        assertMockEndpointsSatisfied();
+        result.assertIsSatisfied();
 
         String body = result.getReceivedExchanges().get(0).getIn().getBody(String.class);
-        assertEquals("The flag autogenColumns set to false didn't take effect", "\n\n", body);
+        String[] lines = body.split("\n");
+        assertEquals(2, lines.length);
+        assertEquals("Camel in Action", lines[0]);
+        assertEquals("ActiveMQ in Action", lines[1]);
+    }
+
+    @Test
+    public void retrieveColumnsWithAutogenColumnsFalseAndOrderIdAmountColumnsSet() throws
Exception {
+        result2.expectedMessageCount(1);
+
+        template.sendBody("direct:start2", createBody());
+
+        result2.assertIsSatisfied();
 
-        String body2 = result2.getReceivedExchanges().get(0).getIn().getBody(String.class);
-        String[] lines2 = body2.split("\n");
-        assertEquals(2, lines2.length);
-        assertEquals("123|Camel in Action|1", lines2[0]);
-        assertEquals("124|ActiveMQ in Action|2", lines2[1]);
+        String body = result2.getReceivedExchanges().get(0).getIn().getBody(String.class);
+        String[] lines = body.split("\n");
+        assertEquals(2, lines.length);
+        assertEquals("123|1", lines[0]);
+        assertEquals("124|2", lines[1]);
     }
 
     private static List<Map<String, Object>> createBody() {

Modified: camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml?rev=1302006&r1=1302005&r2=1302006&view=diff
==============================================================================
--- camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml
(original)
+++ camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml
Sat Mar 17 20:55:23 2012
@@ -25,8 +25,7 @@
     <route>
       <from uri="direct:start" />
       <marshal>
-        <!-- per default the autogenColumns flag is 'true' -->
-        <csv autogenColumns="false" delimiter="|" />
+        <csv autogenColumns="false" delimiter="|" configRef="csvConfig1" />
       </marshal>
       <convertBodyTo type="java.lang.String" />
       <to uri="mock:result" />
@@ -35,11 +34,42 @@
     <route>
       <from uri="direct:start2" />
       <marshal>
-        <csv autogenColumns="true" delimiter="|" />
+        <!-- make use of a strategy other than the default one which is 'org.apache.commons.csv.CSVStrategy.DEFAULT_STRATEGY'
-->
+        <csv autogenColumns="false" delimiter="|" configRef="csvConfig2" strategyRef="excelStrategy"
/>
       </marshal>
       <convertBodyTo type="java.lang.String" />
       <to uri="mock:result2" />
     </route>
 
   </camelContext>
+
+  <bean id="csvConfig1" class="org.apache.commons.csv.writer.CSVConfig">
+    <property name="fields">
+      <list>
+        <!-- here we're only interested in 'item' but not in 'orderId' or 'amount' -->
+        <bean class="org.apache.commons.csv.writer.CSVField">
+          <property name="name" value="item" />
+        </bean>
+      </list>
+    </property>
+  </bean>
+
+  <bean id="csvConfig2" class="org.apache.commons.csv.writer.CSVConfig">
+    <property name="fields">
+      <list>
+        <!-- here we're interested in both 'orderId' and 'amount' but not in 'item' -->
+        <bean class="org.apache.commons.csv.writer.CSVField">
+          <property name="name" value="orderId" />
+        </bean>
+        <bean class="org.apache.commons.csv.writer.CSVField">
+          <property name="name" value="amount" />
+        </bean>
+      </list>
+    </property>
+  </bean>
+
+  <bean id="excelStrategy" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
+    <property name="staticField" value="org.apache.commons.csv.CSVStrategy.EXCEL_STRATEGY"
/>
+  </bean>
+
 </beans>
\ No newline at end of file



Mime
View raw message