Author: bvahdat
Date: Fri Mar 16 17:18:44 2012
New Revision: 1301656
URL: http://svn.apache.org/viewvc?rev=1301656&view=rev
Log:
CAMEL-5100: Add option autogenColumns to csv data format in XML DSL.
Added:
camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java
(with props)
camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml
(with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/CsvDataFormat.java
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=1301656&r1=1301655&r2=1301656&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
Fri Mar 16 17:18:44 2012
@@ -33,17 +33,27 @@ import org.apache.camel.spi.DataFormat;
@XmlAccessorType(XmlAccessType.FIELD)
public class CsvDataFormat extends DataFormatDefinition {
@XmlAttribute
+ private Boolean autogenColumns;
+ @XmlAttribute
private String delimiter;
public CsvDataFormat() {
super("csv");
}
-
+
public CsvDataFormat(String delimiter) {
this();
setDelimiter(delimiter);
}
+ public boolean isAutogenColumns() {
+ return autogenColumns;
+ }
+
+ public void setAutogenColumns(boolean autogenColumns) {
+ this.autogenColumns = autogenColumns;
+ }
+
public String getDelimiter() {
return delimiter;
}
@@ -51,15 +61,20 @@ public class CsvDataFormat extends DataF
public void setDelimiter(String delimiter) {
this.delimiter = delimiter;
}
-
+
@Override
protected void configureDataFormat(DataFormat dataFormat) {
+ if (autogenColumns != null) {
+ setProperty(dataFormat, "autogenColumns", autogenColumns);
+ }
+
if (delimiter != null) {
if (delimiter.length() > 1) {
throw new IllegalArgumentException("Delimiter must have a
length of one!");
}
setProperty(dataFormat, "delimiter", delimiter);
- } else { // the default delimiter is ','
+ } else {
+ // the default delimiter is ','
setProperty(dataFormat, "delimiter", ",");
}
}
Added:
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=1301656&view=auto
==============================================================================
---
camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java
(added)
+++
camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java
Fri Mar 16 17:18:44 2012
@@ -0,0 +1,85 @@
+/**
+ * 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.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelSpringTestSupport;
+
+import org.junit.Test;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Spring based integration test for the <code>CsvDataFormat</code>
+ * @version
+ */
+public class CsvMarshalAutogenColumnsSpringTest extends CamelSpringTestSupport
{
+
+ @EndpointInject(uri = "mock:result")
+ private MockEndpoint result;
+
+ @EndpointInject(uri = "mock:result2")
+ private MockEndpoint result2;
+
+ @Test
+ public void testWithAutogenColumnsAttributeSet() throws Exception {
+ result.expectedMessageCount(1);
+ result2.expectedMessageCount(1);
+
+ template.sendBody("direct:start", createBody());
+ template.sendBody("direct:start2", createBody());
+
+ assertMockEndpointsSatisfied();
+
+ 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 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]);
+ }
+
+ private static List<Map<String, Object>> createBody() {
+ List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
+
+ Map<String, Object> row1 = new LinkedHashMap<String, Object>();
+ row1.put("orderId", 123);
+ row1.put("item", "Camel in Action");
+ row1.put("amount", 1);
+ data.add(row1);
+
+ Map<String, Object> row2 = new LinkedHashMap<String, Object>();
+ row2.put("orderId", 124);
+ row2.put("item", "ActiveMQ in Action");
+ row2.put("amount", 2);
+ data.add(row2);
+ return data;
+ }
+
+ @Override
+ protected ClassPathXmlApplicationContext createApplicationContext() {
+ return new
ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml");
+ }
+}
\ No newline at end of file
Propchange:
camel/trunk/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
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=1301656&view=auto
==============================================================================
---
camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml
(added)
+++
camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml
Fri Mar 16 17:18:44 2012
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+ <!--
+ 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.
+ -->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
+
+ <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+
+ <route>
+ <from uri="direct:start" />
+ <marshal>
+ <!-- per default the autogenColumns flag is 'true' -->
+ <csv autogenColumns="false" delimiter="|" />
+ </marshal>
+ <convertBodyTo type="java.lang.String" />
+ <to uri="mock:result" />
+ </route>
+
+ <route>
+ <from uri="direct:start2" />
+ <marshal>
+ <csv autogenColumns="true" delimiter="|" />
+ </marshal>
+ <convertBodyTo type="java.lang.String" />
+ <to uri="mock:result2" />
+ </route>
+
+ </camelContext>
+</beans>
\ No newline at end of file
Propchange:
camel/trunk/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvMarshalAutogenColumnsSpringTest-context.xml
------------------------------------------------------------------------------
svn:eol-style = native