Author: davsclaus Date: Tue Aug 16 08:05:45 2011 New Revision: 1158143 URL: http://svn.apache.org/viewvc?rev=1158143&view=rev Log: CAMEL-4337: Bindy in CSV format now uses trim, clip, and length attributes on @DataField when marshalling. Thanks to Reinhard Luft for the patch.
Added: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest.java (with props) camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclassandtrimandclip/ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclassandtrimandclip/Customer.java (with props) camel/trunk/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest-context.xml (with props) Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java?rev=1158143&r1=1158142&r2=1158143&view=diff ============================================================================== --- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java (original) +++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java Tue Aug 16 08:05:45 2011 @@ -388,6 +388,14 @@ public class BindyCsvFactory extends Bin result = formatString(format, value); + if (datafield.trim()) { + result = result.trim(); + } + + if (datafield.clip() && result.length() > datafield.length()) { + result = result.substring(0, datafield.length()); + } + if (LOG.isDebugEnabled()) { LOG.debug("Value to be formatted: {}, position: {}, and its formatted value: {}", new Object[]{value, datafield.pos(), result}); } Added: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest.java?rev=1158143&view=auto ============================================================================== --- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest.java (added) +++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest.java Tue Aug 16 08:05:45 2011 @@ -0,0 +1,74 @@ +/** + * 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.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.dataformat.bindy.model.simple.oneclassandtrimandclip.Customer; +import org.junit.Test; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +@ContextConfiguration +public class BindySimpleCsvMarshallTrimClipTest extends AbstractJUnit4SpringContextTests { + + private static final String URI_MOCK_RESULT = "mock:result"; + private static final String URI_MOCK_ERROR = "mock:error"; + private static final String URI_DIRECT_START = "direct:start"; + + private String expected; + + @Produce(uri = URI_DIRECT_START) + private ProducerTemplate template; + + @EndpointInject(uri = URI_MOCK_RESULT) + private MockEndpoint result; + + @Test + @DirtiesContext + public void testMarshallMessage() throws Exception { + expected = "Mr,John,Doeeeeeeee,Cityyyyyyy\r\n"; + result.expectedBodiesReceived(expected); + + template.sendBody(generateModel()); + + result.assertIsSatisfied(); + } + + public Object generateModel() { + Customer customer = new Customer(); + customer.setSalutation("Mr"); + customer.setFirstName(" John "); + customer.setLastName("Doeeeeeeeeeeeee"); + customer.setCity("Cityyyyyyy "); + return customer; + } + + public static class ContextConfig extends RouteBuilder { + public void configure() { + BindyCsvDataFormat camelDataFormat = new BindyCsvDataFormat( + "org.apache.camel.dataformat.bindy.model.simple.oneclassandtrimandclip"); + + from(URI_DIRECT_START).marshal(camelDataFormat).to(URI_MOCK_RESULT); + } + } + +} Propchange: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclassandtrimandclip/Customer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclassandtrimandclip/Customer.java?rev=1158143&view=auto ============================================================================== --- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclassandtrimandclip/Customer.java (added) +++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclassandtrimandclip/Customer.java Tue Aug 16 08:05:45 2011 @@ -0,0 +1,69 @@ +/** + * 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.simple.oneclassandtrimandclip; + +import org.apache.camel.dataformat.bindy.annotation.CsvRecord; +import org.apache.camel.dataformat.bindy.annotation.DataField; + +@CsvRecord(separator = ",") +public class Customer { + + @DataField(pos = 1, trim = true, length = 2, clip = true) + private String salutation; + + @DataField(pos = 2, trim = true, length = 10, clip = true) + private String firstName; + + @DataField(pos = 3, trim = true, length = 10, clip = true) + private String lastName; + + @DataField(pos = 4, trim = true, length = 10, clip = true) + private String city; + + public String getSalutation() { + return salutation; + } + + public void setSalutation(String salutation) { + this.salutation = salutation; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + +} Propchange: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclassandtrimandclip/Customer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/oneclassandtrimandclip/Customer.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest-context.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest-context.xml?rev=1158143&view=auto ============================================================================== --- camel/trunk/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest-context.xml (added) +++ camel/trunk/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest-context.xml Tue Aug 16 08:05:45 2011 @@ -0,0 +1,32 @@ +<?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 xmlns="http://camel.apache.org/schema/spring"> + <routeBuilder ref="myBuilder"/> + </camelContext> + + <bean id="myBuilder" class="org.apache.camel.dataformat.bindy.csv.BindySimpleCsvMarshallTrimClipTest$ContextConfig"/> + +</beans> \ No newline at end of file Propchange: camel/trunk/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest-context.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest-context.xml ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTrimClipTest-context.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml