Author: davsclaus Date: Tue Apr 17 07:49:23 2012 New Revision: 1326975 URL: http://svn.apache.org/viewvc?rev=1326975&view=rev Log: CAMEL-5180: Added unit test for non required fields in csv. Thanks to Magnus Palmer for the test cases.
Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyTabSeparatorTest.java camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/tab/PurchaseOrder.java Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyTabSeparatorTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyTabSeparatorTest.java?rev=1326975&r1=1326974&r2=1326975&view=diff ============================================================================== --- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyTabSeparatorTest.java (original) +++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyTabSeparatorTest.java Tue Apr 17 07:49:23 2012 @@ -19,16 +19,16 @@ package org.apache.camel.dataformat.bind import java.util.List; import java.util.Map; +import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.dataformat.bindy.model.tab.PurchaseOrder; import org.apache.camel.test.junit4.CamelTestSupport; import org.apache.camel.util.CastUtils; - import org.junit.Test; /** - * @version + * @version */ public class BindyTabSeparatorTest extends CamelTestSupport { @@ -37,7 +37,7 @@ public class BindyTabSeparatorTest exten MockEndpoint mock = getMockEndpoint("mock:unmarshal"); mock.expectedMessageCount(1); - template.sendBody("direct:unmarshal", "123\tCamel in Action\t2"); + template.sendBody("direct:unmarshal", "123\tCamel in Action\t2\tPlease hurry\tJane Doe\tJohn Doe\n"); assertMockEndpointsSatisfied(); @@ -47,17 +47,63 @@ public class BindyTabSeparatorTest exten assertEquals(123, order.getId()); assertEquals("Camel in Action", order.getName()); assertEquals(2, order.getAmount()); + assertEquals("Please hurry", order.getOrderText()); + assertEquals("Jane Doe", order.getSalesRef()); + assertEquals("John Doe", order.getCustomerRef()); } @Test public void testMarshal() throws Exception { MockEndpoint mock = getMockEndpoint("mock:marshal"); - mock.expectedBodiesReceived("123\tCamel in Action\t2\n"); + mock.expectedBodiesReceived("123\tCamel in Action\t2\tPlease hurry\tJane Doe\tJohn Doe\n"); + + PurchaseOrder order = new PurchaseOrder(); + order.setId(123); + order.setName("Camel in Action"); + order.setAmount(2); + order.setOrderText("Please hurry"); + order.setSalesRef("Jane Doe"); + order.setCustomerRef("John Doe"); + + template.sendBody("direct:marshal", order); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testUnmarshalEmptyTrailingNoneRequiredFields() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:unmarshal"); + mock.expectedMessageCount(1); + + template.sendBodyAndHeader("direct:unmarshal", + "123\tCamel in Action\t2\t\t\n456\tCamel in Action\t1\t\t\t\n" + + "456\tCamel in Action\t2\t\t\n456\tCamel in Action\t1\t\t\t\n", Exchange.CONTENT_ENCODING, "iso8859-1"); + + assertMockEndpointsSatisfied(); + + List<Map<?, PurchaseOrder>> rows = CastUtils.cast(mock.getReceivedExchanges().get(0).getIn().getBody(List.class)); + PurchaseOrder order = rows.get(0).get(PurchaseOrder.class.getName()); + + assertEquals(123, order.getId()); + assertEquals("Camel in Action", order.getName()); + assertEquals(2, order.getAmount()); + assertNull(order.getOrderText()); + assertNull(order.getSalesRef()); + assertNull(order.getCustomerRef()); + } + + @Test + public void testMarshalEmptyTrailingNoneRequiredFields() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:marshal"); + mock.expectedBodiesReceived("123\tCamel in Action\t2\t\t\t\n"); PurchaseOrder order = new PurchaseOrder(); order.setId(123); order.setName("Camel in Action"); order.setAmount(2); + order.setOrderText(""); + order.setSalesRef(""); + order.setCustomerRef(""); template.sendBody("direct:marshal", order); @@ -72,13 +118,13 @@ public class BindyTabSeparatorTest exten BindyCsvDataFormat bindy = new BindyCsvDataFormat("org.apache.camel.dataformat.bindy.model.tab"); from("direct:marshal") - .marshal(bindy) - .convertBodyTo(String.class) - .to("mock:marshal"); + .marshal(bindy) + .convertBodyTo(String.class) + .to("mock:marshal"); from("direct:unmarshal") - .unmarshal(bindy) - .to("mock:unmarshal"); + .unmarshal(bindy) + .to("mock:unmarshal"); } }; } Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/tab/PurchaseOrder.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/tab/PurchaseOrder.java?rev=1326975&r1=1326974&r2=1326975&view=diff ============================================================================== --- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/tab/PurchaseOrder.java (original) +++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/tab/PurchaseOrder.java Tue Apr 17 07:49:23 2012 @@ -20,7 +20,7 @@ import org.apache.camel.dataformat.bindy import org.apache.camel.dataformat.bindy.annotation.DataField; /** - * @version + * @version */ @CsvRecord(separator = "\t", crlf = "UNIX") public class PurchaseOrder { @@ -34,6 +34,15 @@ public class PurchaseOrder { @DataField(pos = 3) private int amount; + @DataField(pos = 4, required = false) + private String orderText; + + @DataField(pos = 5, required = false) + private String salesRef; + + @DataField(pos = 6, required = false) + private String customerRef; + public int getId() { return id; } @@ -57,4 +66,28 @@ public class PurchaseOrder { public void setAmount(int amount) { this.amount = amount; } + + public String getOrderText() { + return orderText; + } + + public void setOrderText(String text) { + this.orderText = text; + } + + public String getSalesRef() { + return salesRef; + } + + public void setSalesRef(String salesRef) { + this.salesRef = salesRef; + } + + public String getCustomerRef() { + return customerRef; + } + + public void setCustomerRef(String customerRef) { + this.customerRef = customerRef; + } }