Repository: camel Updated Branches: refs/heads/master 1b155f65a -> 012666bbd
CAMEL-7621 - new(named as function)data field annotation added as described in the issue as the following https://issues.apache.org/jira/browse/CAMEL-7621?focusedCommentId=16023475&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-16023475 Renaming package name Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/012666bb Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/012666bb Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/012666bb Branch: refs/heads/master Commit: 012666bbdbff475f3513b5ee410abbd8c6f3f36b Parents: 1b155f6 Author: onders86 <ondersez...@gmail.com> Authored: Sun Sep 24 19:40:21 2017 +0300 Committer: onders86 <ondersez...@gmail.com> Committed: Tue Sep 26 10:20:59 2017 +0300 ---------------------------------------------------------------------- .../dataformat/bindy/BindyAbstractFactory.java | 5 +- .../camel/dataformat/bindy/BindyCsvFactory.java | 34 ++- .../camel/dataformat/bindy/BindyFactory.java | 6 +- .../bindy/BindyFixedLengthFactory.java | 34 ++- .../bindy/BindyKeyValuePairFactory.java | 9 +- .../dataformat/bindy/annotation/DataField.java | 8 + .../bindy/csv/BindyCsvDataFormat.java | 4 +- .../bindy/fixed/BindyFixedLengthDataFormat.java | 8 +- .../bindy/kvp/BindyKeyValuePairDataFormat.java | 4 +- ...ndySimpleCsvFunctionWithClassMethodTest.java | 123 ++++++++++ ...SimpleCsvFunctionWithExternalMethodTest.java | 127 +++++++++++ .../BindyComplexKeyValuePairStandaloneTest.java | 11 +- ...pleFixedLengthUnmarshallClassMethodTest.java | 222 ++++++++++++++++++ ...FixedLengthUnmarshallExternalMethodTest.java | 226 +++++++++++++++++++ 14 files changed, 797 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java index 1108a68..8a7ca73 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.camel.CamelContext; import org.apache.camel.dataformat.bindy.annotation.Link; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; @@ -99,9 +100,9 @@ public abstract class BindyAbstractFactory implements BindyFactory { */ public abstract void initAnnotatedFields() throws Exception; - public abstract void bind(List<String> data, Map<String, Object> model, int line) throws Exception; + public abstract void bind(CamelContext camelContext, List<String> data, Map<String, Object> model, int line) throws Exception; - public abstract String unbind(Map<String, Object> model) throws Exception; + public abstract String unbind(CamelContext camelContext, Map<String, Object> model) throws Exception; /** * Link objects together http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java index cb5dd3a..994bed0 100755 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java @@ -17,6 +17,7 @@ package org.apache.camel.dataformat.bindy; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -27,6 +28,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; +import org.apache.camel.CamelContext; import org.apache.camel.dataformat.bindy.annotation.BindyConverter; import org.apache.camel.dataformat.bindy.annotation.CsvRecord; import org.apache.camel.dataformat.bindy.annotation.DataField; @@ -35,7 +37,10 @@ import org.apache.camel.dataformat.bindy.annotation.OneToMany; import org.apache.camel.dataformat.bindy.annotation.Section; import org.apache.camel.dataformat.bindy.format.FormatException; import org.apache.camel.dataformat.bindy.util.ConverterUtils; +import org.apache.camel.impl.DefaultClassResolver; +import org.apache.camel.spi.ClassResolver; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.ReflectionHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -159,7 +164,7 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor } @Override - public void bind(List<String> tokens, Map<String, Object> model, int line) throws Exception { + public void bind(CamelContext camelContext, List<String> tokens, Map<String, Object> model, int line) throws Exception { int pos = 1; int counterMandatoryFields = 0; @@ -225,6 +230,31 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor value = getDefaultValueForPrimitive(field.getType()); } } + + if (value != null && !dataField.method().isEmpty()) { + Class<?> clazz; + if (dataField.method().contains(".")) { + clazz = camelContext.getClassResolver().resolveMandatoryClass(dataField.method().substring(0, dataField.method().lastIndexOf("."))); + } else { + clazz = field.getType(); + } + + String methodName = dataField.method().substring(dataField.method().lastIndexOf(".") + 1, + dataField.method().length()); + + Method m = ReflectionHelper.findMethod(clazz, methodName, field.getType()); + if (m != null) { + // this method must be static and return type + // must be the same as the datafield and + // must receive only the datafield value + // as the method argument + value = ObjectHelper.invokeMethod(m, null, value); + } else { + // fallback to method without parameter, that is on the value itself + m = ReflectionHelper.findMethod(clazz, methodName); + value = ObjectHelper.invokeMethod(m, value); + } + } field.set(modelField, value); @@ -245,7 +275,7 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor } @Override - public String unbind(Map<String, Object> model) throws Exception { + public String unbind(CamelContext camelContext, Map<String, Object> model) throws Exception { StringBuilder buffer = new StringBuilder(); Map<Integer, List<String>> results = new HashMap<Integer, List<String>>(); http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java index 8ffc1b7..26e12bb 100755 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java @@ -19,6 +19,8 @@ package org.apache.camel.dataformat.bindy; import java.util.List; import java.util.Map; +import org.apache.camel.CamelContext; + /** * The bindy factory is a factory used to create the POJO models and bind or * unbind the data to and from the record (CSV, ...) @@ -44,7 +46,7 @@ public interface BindyFactory { * @param line is the position of the record into the file * @throws Exception can be thrown */ - void bind(List<String> data, Map<String, Object> model, int line) throws Exception; + void bind(CamelContext camelContext, List<String> data, Map<String, Object> model, int line) throws Exception; /** * The unbind is used to transform the content of the classes model objects @@ -56,6 +58,6 @@ public interface BindyFactory { * class link to POJO objects * @throws Exception can be thrown */ - String unbind(Map<String, Object> model) throws Exception; + String unbind(CamelContext camelContext, Map<String, Object> model) throws Exception; } http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java index 1a0b523..78c9ed6 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java @@ -17,6 +17,7 @@ package org.apache.camel.dataformat.bindy; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -27,6 +28,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; +import org.apache.camel.CamelContext; import org.apache.camel.dataformat.bindy.annotation.BindyConverter; import org.apache.camel.dataformat.bindy.annotation.DataField; import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord; @@ -34,6 +36,7 @@ import org.apache.camel.dataformat.bindy.annotation.Link; import org.apache.camel.dataformat.bindy.format.FormatException; import org.apache.camel.dataformat.bindy.util.ConverterUtils; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.ReflectionHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -154,11 +157,11 @@ public class BindyFixedLengthFactory extends BindyAbstractFactory implements Bin // as we provide the content of the record and // we don't split it as this is the case for a CSV record @Override - public void bind(List<String> data, Map<String, Object> model, int line) throws Exception { + public void bind(CamelContext camelContext, List<String> data, Map<String, Object> model, int line) throws Exception { // noop } - public void bind(String record, Map<String, Object> model, int line) throws Exception { + public void bind(CamelContext camelContext, String record, Map<String, Object> model, int line) throws Exception { int pos = 1; int counterMandatoryFields = 0; @@ -277,6 +280,31 @@ public class BindyFixedLengthFactory extends BindyAbstractFactory implements Bin } else { value = getDefaultValueForPrimitive(field.getType()); } + + if (value != null && !dataField.method().isEmpty()) { + Class<?> clazz; + if (dataField.method().contains(".")) { + clazz = camelContext.getClassResolver().resolveMandatoryClass(dataField.method().substring(0, dataField.method().lastIndexOf("."))); + } else { + clazz = field.getType(); + } + + String methodName = dataField.method().substring(dataField.method().lastIndexOf(".") + 1, + dataField.method().length()); + + Method m = ReflectionHelper.findMethod(clazz, methodName, field.getType()); + if (m != null) { + // this method must be static and return type + // must be the same as the datafield and + // must receive only the datafield value + // as the method argument + value = ObjectHelper.invokeMethod(m, null, value); + } else { + // fallback to method without parameter, that is on the value itself + m = ReflectionHelper.findMethod(clazz, methodName); + value = ObjectHelper.invokeMethod(m, value); + } + } field.set(modelField, value); @@ -337,7 +365,7 @@ public class BindyFixedLengthFactory extends BindyAbstractFactory implements Bin } @Override - public String unbind(Map<String, Object> model) throws Exception { + public String unbind(CamelContext camelContext, Map<String, Object> model) throws Exception { StringBuilder buffer = new StringBuilder(); Map<Integer, List<String>> results = new HashMap<Integer, List<String>>(); http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java index b7ee119..30af7b0 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; +import org.apache.camel.CamelContext; import org.apache.camel.dataformat.bindy.annotation.BindyConverter; import org.apache.camel.dataformat.bindy.annotation.KeyValuePairField; import org.apache.camel.dataformat.bindy.annotation.Link; @@ -116,15 +117,15 @@ public class BindyKeyValuePairFactory extends BindyAbstractFactory implements Bi } @Override - public void bind(List<String> data, Map<String, Object> model, int line) throws Exception { + public void bind(CamelContext camelContext, List<String> data, Map<String, Object> model, int line) throws Exception { // Map to hold the model @OneToMany classes while binding Map<String, List<Object>> lists = new HashMap<String, List<Object>>(); - bind(data, model, line, lists); + bind(camelContext, data, model, line, lists); } - public void bind(List<String> data, Map<String, Object> model, int line, Map<String, List<Object>> lists) throws Exception { + public void bind(CamelContext camelContext, List<String> data, Map<String, Object> model, int line, Map<String, List<Object>> lists) throws Exception { Map<Integer, List<String>> results = new HashMap<Integer, List<String>>(); @@ -412,7 +413,7 @@ public class BindyKeyValuePairFactory extends BindyAbstractFactory implements Bi * */ @Override - public String unbind(Map<String, Object> model) throws Exception { + public String unbind(CamelContext camelContext, Map<String, Object> model) throws Exception { StringBuilder builder = new StringBuilder(); http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java index f7b2c8e..d4002ce 100755 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java @@ -146,4 +146,12 @@ public @interface DataField { */ String rounding() default "CEILING"; + /** + * Method name to call to apply such customization + * on DataField. This must be the method on the datafield + * itself or you must provide static fully qualified name of + * the class's method e.g: see unit test + * org.apache.camel.dataformat.bindy.csv.BindySimpleCsvFunctionWithExternalMethodTest.replaceToBar + */ + String method() default ""; } http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java index d56f243..ad24a99 100755 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java @@ -97,7 +97,7 @@ public class BindyCsvDataFormat extends BindyAbstractDataFormat { for (Map<String, Object> model : models) { - String result = factory.unbind(model); + String result = factory.unbind(getCamelContext(), model); byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result); outputStream.write(bytes); @@ -195,7 +195,7 @@ public class BindyCsvDataFormat extends BindyAbstractDataFormat { } // Bind data from CSV record with model classes - factory.bind(result, model, count); + factory.bind(getCamelContext(), result, model, count); // Link objects together factory.link(model); http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java index 23fce52..64f0973 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java @@ -119,7 +119,7 @@ public class BindyFixedLengthDataFormat extends BindyAbstractDataFormat { LOG.info("Skipping marshal of header row; 'skipHeader=true'"); continue; } else { - result = headerFactory.unbind(model); + result = headerFactory.unbind(getCamelContext(), model); } } } else if (row == models.size() && footerFactory != null) { @@ -131,14 +131,14 @@ public class BindyFixedLengthDataFormat extends BindyAbstractDataFormat { LOG.info("Skipping marshal of footer row; 'skipFooter=true'"); continue; } else { - result = footerFactory.unbind(model); + result = footerFactory.unbind(getCamelContext(), model); } } } if (result == null) { // marshal as a normal / default row - result = factory.unbind(model); + result = factory.unbind(getCamelContext(), model); } byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result); @@ -291,7 +291,7 @@ public class BindyFixedLengthDataFormat extends BindyAbstractDataFormat { Map<String, Object> model = factory.factory(); // Bind data from Fixed record with model classes - factory.bind(myLine, model, count); + factory.bind(getCamelContext(), myLine, model, count); // Link objects together factory.link(model); http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java index 1d35957..bbac5da 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java @@ -80,7 +80,7 @@ public class BindyKeyValuePairDataFormat extends BindyAbstractDataFormat { row = Collections.singletonMap(model.getClass().getName(), model); } - String result = factory.unbind(row); + String result = factory.unbind(getCamelContext(), row); outputStream.write(converter.convertTo(byte[].class, exchange, result)); outputStream.write(crlf); @@ -136,7 +136,7 @@ public class BindyKeyValuePairDataFormat extends BindyAbstractDataFormat { if (result.size() > 0) { // Bind data from message with model classes // Counter is used to detect line where error occurs - factory.bind(result, model, count, lists); + factory.bind(getCamelContext(), result, model, count, lists); // Link objects together factory.link(model); http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvFunctionWithClassMethodTest.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvFunctionWithClassMethodTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvFunctionWithClassMethodTest.java new file mode 100644 index 0000000..4f5b811 --- /dev/null +++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvFunctionWithClassMethodTest.java @@ -0,0 +1,123 @@ +/** + * 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 java.io.Serializable; +import java.math.BigDecimal; + +import org.apache.camel.EndpointInject; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.dataformat.bindy.annotation.CsvRecord; +import org.apache.camel.dataformat.bindy.annotation.DataField; +import org.apache.camel.dataformat.bindy.util.ConverterUtils; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class BindySimpleCsvFunctionWithClassMethodTest extends CamelTestSupport { + + @EndpointInject(uri = "mock:resultMarshal1") + private MockEndpoint mockEndPointMarshal1; + + @EndpointInject(uri = "mock:resultUnMarshal1") + private MockEndpoint mockEndPointUnMarshal1; + + @EndpointInject(uri = "mock:resultMarshal2") + private MockEndpoint mockEndPointMarshal2; + + @EndpointInject(uri = "mock:resultUnMarshal2") + private MockEndpoint mockEndPointUnMarshal2; + + @Test + public void testUnMarshallMessage() throws Exception { + + mockEndPointMarshal1.expectedMessageCount(1); + mockEndPointMarshal1.expectedBodiesReceived("\"123\",\"\"\"foo\"\"\",\"10\"" + ConverterUtils.getStringCarriageReturn("WINDOWS")); + + BindyCsvRowFormat7621 body = new BindyCsvRowFormat7621(); + body.setFirstField("123"); + body.setSecondField("\"\"foo\"\""); + body.setNumber(new BigDecimal(10)); + template.sendBody("direct:startMarshal1", body); + + assertMockEndpointsSatisfied(); + + BindyCsvRowFormat7621 model = mockEndPointUnMarshal1.getReceivedExchanges().get(0).getIn().getBody(BindyCsvRowFormat7621.class); + + assertEquals("123", model.getFirstField()); + assertEquals("\"\"FOO\"\"", model.getSecondField()); + assertEquals(new BigDecimal(10), model.getNumber()); + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + BindyCsvDataFormat camelDataFormat1 = + new BindyCsvDataFormat(BindyCsvRowFormat7621.class); + + from("direct:startMarshal1") + .marshal(camelDataFormat1) + .to("mock:resultMarshal1") + .to("direct:middle1"); + + from("direct:middle1") + .unmarshal(camelDataFormat1) + .to("mock:resultUnMarshal1"); + } + }; + } + + @CsvRecord(separator = ",", quote = "\"", quoting = true, quotingEscaped = false) + public static class BindyCsvRowFormat7621 implements Serializable { + + @DataField(pos = 1) + private String firstField; + + @DataField(pos = 2, method = "toUpperCase") + private String secondField; + + @DataField(pos = 3, pattern = "########.##") + private BigDecimal number; + + public String getFirstField() { + return firstField; + } + + public void setFirstField(String firstField) { + this.firstField = firstField; + } + + public String getSecondField() { + return secondField; + } + + public void setSecondField(String secondField) { + this.secondField = secondField; + } + + public BigDecimal getNumber() { + return number; + } + + public void setNumber(BigDecimal number) { + this.number = number; + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvFunctionWithExternalMethodTest.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvFunctionWithExternalMethodTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvFunctionWithExternalMethodTest.java new file mode 100644 index 0000000..ebc03a7 --- /dev/null +++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvFunctionWithExternalMethodTest.java @@ -0,0 +1,127 @@ +/** + * 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 java.io.Serializable; +import java.math.BigDecimal; + +import org.apache.camel.EndpointInject; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.dataformat.bindy.annotation.CsvRecord; +import org.apache.camel.dataformat.bindy.annotation.DataField; +import org.apache.camel.dataformat.bindy.util.ConverterUtils; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class BindySimpleCsvFunctionWithExternalMethodTest extends CamelTestSupport { + + @EndpointInject(uri = "mock:resultMarshal1") + private MockEndpoint mockEndPointMarshal1; + + @EndpointInject(uri = "mock:resultUnMarshal1") + private MockEndpoint mockEndPointUnMarshal1; + + @EndpointInject(uri = "mock:resultMarshal2") + private MockEndpoint mockEndPointMarshal2; + + @EndpointInject(uri = "mock:resultUnMarshal2") + private MockEndpoint mockEndPointUnMarshal2; + + public static String replaceToBar(String fooString) { + return fooString.replaceAll("foo", "bar"); + } + + @Test + public void testUnMarshallMessage() throws Exception { + + mockEndPointMarshal1.expectedMessageCount(1); + mockEndPointMarshal1.expectedBodiesReceived("\"123\",\"\"\"foo\"\"\",\"10\"" + ConverterUtils.getStringCarriageReturn("WINDOWS")); + + BindyCsvRowFormat7621 body = new BindyCsvRowFormat7621(); + body.setFirstField("123"); + body.setSecondField("\"\"foo\"\""); + body.setNumber(new BigDecimal(10)); + template.sendBody("direct:startMarshal1", body); + + assertMockEndpointsSatisfied(); + + BindyCsvRowFormat7621 model = mockEndPointUnMarshal1.getReceivedExchanges().get(0).getIn().getBody(BindyCsvRowFormat7621.class); + + assertEquals("123", model.getFirstField()); + assertEquals("\"\"bar\"\"", model.getSecondField()); + assertEquals(new BigDecimal(10), model.getNumber()); + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + BindyCsvDataFormat camelDataFormat1 = + new BindyCsvDataFormat(BindyCsvRowFormat7621.class); + + from("direct:startMarshal1") + .marshal(camelDataFormat1) + .to("mock:resultMarshal1") + .to("direct:middle1"); + + from("direct:middle1") + .unmarshal(camelDataFormat1) + .to("mock:resultUnMarshal1"); + } + }; + } + + @CsvRecord(separator = ",", quote = "\"", quoting = true, quotingEscaped = false) + public static class BindyCsvRowFormat7621 implements Serializable { + + @DataField(pos = 1) + private String firstField; + + @DataField(pos = 2, method = "org.apache.camel.dataformat.bindy.csv.BindySimpleCsvFunctionWithExternalMethodTest.replaceToBar") + private String secondField; + + @DataField(pos = 3, pattern = "########.##") + private BigDecimal number; + + public String getFirstField() { + return firstField; + } + + public void setFirstField(String firstField) { + this.firstField = firstField; + } + + public String getSecondField() { + return secondField; + } + + public void setSecondField(String secondField) { + this.secondField = secondField; + } + + public BigDecimal getNumber() { + return number; + } + + public void setNumber(BigDecimal number) { + this.number = number; + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindyComplexKeyValuePairStandaloneTest.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindyComplexKeyValuePairStandaloneTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindyComplexKeyValuePairStandaloneTest.java index f6a8ee9..39bd902 100644 --- a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindyComplexKeyValuePairStandaloneTest.java +++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fix/BindyComplexKeyValuePairStandaloneTest.java @@ -23,11 +23,13 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.camel.CamelContext; import org.apache.camel.dataformat.bindy.BindyAbstractFactory; import org.apache.camel.dataformat.bindy.kvp.BindyKeyValuePairDataFormat; import org.apache.camel.dataformat.bindy.model.fix.complex.onetomany.Header; import org.apache.camel.dataformat.bindy.model.fix.complex.onetomany.Order; import org.apache.camel.dataformat.bindy.model.fix.complex.onetomany.Trailer; +import org.apache.camel.impl.DefaultCamelContext; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -76,7 +78,8 @@ public class BindyComplexKeyValuePairStandaloneTest { List<String> data = Arrays.asList(message.split("\\u0001")); - factory.bind(data, model, counter); + CamelContext camelContext = new DefaultCamelContext(); + factory.bind(camelContext, data, model, counter); LOG.info(">>> Model : " + model.toString()); @@ -94,7 +97,8 @@ public class BindyComplexKeyValuePairStandaloneTest { List<String> data = Arrays.asList(message.split("\\u0001")); - factory.bind(data, model, counter); + CamelContext camelContext = new DefaultCamelContext(); + factory.bind(camelContext, data, model, counter); LOG.info(">>> Model : " + model.toString()); @@ -112,7 +116,8 @@ public class BindyComplexKeyValuePairStandaloneTest { List<String> data = Arrays.asList(message.split("\\u0001")); - factory.bind(data, model, counter); + CamelContext camelContext = new DefaultCamelContext(); + factory.bind(camelContext, data, model, counter); LOG.info(">>> Model : " + model.toString()); http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/unmarshall/simple/method/BindySimpleFixedLengthUnmarshallClassMethodTest.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/unmarshall/simple/method/BindySimpleFixedLengthUnmarshallClassMethodTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/unmarshall/simple/method/BindySimpleFixedLengthUnmarshallClassMethodTest.java new file mode 100644 index 0000000..1817ebe --- /dev/null +++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/unmarshall/simple/method/BindySimpleFixedLengthUnmarshallClassMethodTest.java @@ -0,0 +1,222 @@ +/** + * 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.fixed.unmarshall.simple.method; + +import java.math.BigDecimal; +import java.util.Date; + +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.annotation.DataField; +import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord; +import org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat; +import org.junit.Assert; +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 BindySimpleFixedLengthUnmarshallClassMethodTest extends AbstractJUnit4SpringContextTests { + + private static final String URI_MOCK_RESULT = "mock:result"; + private static final String URI_DIRECT_START = "direct:start"; + + @Produce(uri = URI_DIRECT_START) + private ProducerTemplate template; + + @EndpointInject(uri = URI_MOCK_RESULT) + private MockEndpoint result; + + private String expected; + + @Test + @DirtiesContext + public void testUnMarshallMessage() throws Exception { + + expected = "10A9 PaulineM ISINXD12345678BUYShare000002500.45USD01-08-2009Hello "; + + template.sendBody(expected); + + result.expectedMessageCount(1); + result.assertIsSatisfied(); + + // check the model + BindySimpleFixedLengthUnmarshallClassMethodTest.Order order = result.getReceivedExchanges().get(0).getIn().getBody(BindySimpleFixedLengthUnmarshallClassMethodTest.Order.class); + Assert.assertEquals(10, order.getOrderNr()); + // the field is not trimmed + Assert.assertEquals(" PAULINE", order.getFirstName()); + Assert.assertEquals("M ", order.getLastName()); + Assert.assertEquals("Hello ", order.getComment()); + } + + public static class ContextConfig extends RouteBuilder { + BindyFixedLengthDataFormat camelDataFormat = new BindyFixedLengthDataFormat(Order.class); + + public void configure() { + from(URI_DIRECT_START).unmarshal(camelDataFormat).to(URI_MOCK_RESULT); + } + + } + + @FixedLengthRecord(length = 75) + public static class Order { + + @DataField(pos = 1, length = 2) + private int orderNr; + + @DataField(pos = 3, length = 2) + private String clientNr; + + @DataField(pos = 5, length = 9, method = "toUpperCase") + private String firstName; + + @DataField(pos = 14, length = 5, align = "L") + private String lastName; + + @DataField(pos = 19, length = 4) + private String instrumentCode; + + @DataField(pos = 23, length = 10) + private String instrumentNumber; + + @DataField(pos = 33, length = 3) + private String orderType; + + @DataField(pos = 36, length = 5) + private String instrumentType; + + @DataField(pos = 41, precision = 2, length = 12, paddingChar = '0') + private BigDecimal amount; + + @DataField(pos = 53, length = 3) + private String currency; + + @DataField(pos = 56, length = 10, pattern = "dd-MM-yyyy") + private Date orderDate; + + @DataField(pos = 66, length = 10) + private String comment; + + public int getOrderNr() { + return orderNr; + } + + public void setOrderNr(int orderNr) { + this.orderNr = orderNr; + } + + public String getClientNr() { + return clientNr; + } + + public void setClientNr(String clientNr) { + this.clientNr = clientNr; + } + + 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 getInstrumentCode() { + return instrumentCode; + } + + public void setInstrumentCode(String instrumentCode) { + this.instrumentCode = instrumentCode; + } + + public String getInstrumentNumber() { + return instrumentNumber; + } + + public void setInstrumentNumber(String instrumentNumber) { + this.instrumentNumber = instrumentNumber; + } + + public String getOrderType() { + return orderType; + } + + public void setOrderType(String orderType) { + this.orderType = orderType; + } + + public String getInstrumentType() { + return instrumentType; + } + + public void setInstrumentType(String instrumentType) { + this.instrumentType = instrumentType; + } + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public Date getOrderDate() { + return orderDate; + } + + public void setOrderDate(Date orderDate) { + this.orderDate = orderDate; + } + + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + + @Override + public String toString() { + return "Model : " + Order.class.getName() + " : " + this.orderNr + ", " + this.orderType + ", " + String.valueOf(this.amount) + ", " + this.instrumentCode + ", " + + this.instrumentNumber + ", " + this.instrumentType + ", " + this.currency + ", " + this.clientNr + ", " + this.firstName + ", " + this.lastName + ", " + + String.valueOf(this.orderDate); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/012666bb/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/unmarshall/simple/method/BindySimpleFixedLengthUnmarshallExternalMethodTest.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/unmarshall/simple/method/BindySimpleFixedLengthUnmarshallExternalMethodTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/unmarshall/simple/method/BindySimpleFixedLengthUnmarshallExternalMethodTest.java new file mode 100644 index 0000000..0bb38af --- /dev/null +++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/unmarshall/simple/method/BindySimpleFixedLengthUnmarshallExternalMethodTest.java @@ -0,0 +1,226 @@ +/** + * 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.fixed.unmarshall.simple.method; + +import java.math.BigDecimal; +import java.util.Date; + +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.annotation.DataField; +import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord; +import org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat; +import org.junit.Assert; +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 BindySimpleFixedLengthUnmarshallExternalMethodTest extends AbstractJUnit4SpringContextTests { + + private static final String URI_MOCK_RESULT = "mock:result"; + private static final String URI_DIRECT_START = "direct:start"; + + @Produce(uri = URI_DIRECT_START) + private ProducerTemplate template; + + @EndpointInject(uri = URI_MOCK_RESULT) + private MockEndpoint result; + + private String expected; + + public static String replaceToBar(String fooString) { + return fooString.replaceAll("Pauline", "Bar"); + } + + @Test + @DirtiesContext + public void testUnMarshallMessage() throws Exception { + + expected = "10A9 PaulineM ISINXD12345678BUYShare000002500.45USD01-08-2009Hello "; + + template.sendBody(expected); + + result.expectedMessageCount(1); + result.assertIsSatisfied(); + + // check the model + BindySimpleFixedLengthUnmarshallExternalMethodTest.Order order = result.getReceivedExchanges().get(0).getIn().getBody(BindySimpleFixedLengthUnmarshallExternalMethodTest.Order.class); + Assert.assertEquals(10, order.getOrderNr()); + // the field is not trimmed + Assert.assertEquals(" Bar", order.getFirstName()); + Assert.assertEquals("M ", order.getLastName()); + Assert.assertEquals("Hello ", order.getComment()); + } + + public static class ContextConfig extends RouteBuilder { + BindyFixedLengthDataFormat camelDataFormat = new BindyFixedLengthDataFormat(Order.class); + + public void configure() { + from(URI_DIRECT_START).unmarshal(camelDataFormat).to(URI_MOCK_RESULT); + } + + } + + @FixedLengthRecord(length = 75) + public static class Order { + + @DataField(pos = 1, length = 2) + private int orderNr; + + @DataField(pos = 3, length = 2) + private String clientNr; + + @DataField(pos = 5, length = 9, method = "org.apache.camel.dataformat.bindy.fixed.unmarshall.simple.function.BindySimpleFixedLengthUnmarshallExternalMethodTest.replaceToBar") + private String firstName; + + @DataField(pos = 14, length = 5, align = "L") + private String lastName; + + @DataField(pos = 19, length = 4) + private String instrumentCode; + + @DataField(pos = 23, length = 10) + private String instrumentNumber; + + @DataField(pos = 33, length = 3) + private String orderType; + + @DataField(pos = 36, length = 5) + private String instrumentType; + + @DataField(pos = 41, precision = 2, length = 12, paddingChar = '0') + private BigDecimal amount; + + @DataField(pos = 53, length = 3) + private String currency; + + @DataField(pos = 56, length = 10, pattern = "dd-MM-yyyy") + private Date orderDate; + + @DataField(pos = 66, length = 10) + private String comment; + + public int getOrderNr() { + return orderNr; + } + + public void setOrderNr(int orderNr) { + this.orderNr = orderNr; + } + + public String getClientNr() { + return clientNr; + } + + public void setClientNr(String clientNr) { + this.clientNr = clientNr; + } + + 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 getInstrumentCode() { + return instrumentCode; + } + + public void setInstrumentCode(String instrumentCode) { + this.instrumentCode = instrumentCode; + } + + public String getInstrumentNumber() { + return instrumentNumber; + } + + public void setInstrumentNumber(String instrumentNumber) { + this.instrumentNumber = instrumentNumber; + } + + public String getOrderType() { + return orderType; + } + + public void setOrderType(String orderType) { + this.orderType = orderType; + } + + public String getInstrumentType() { + return instrumentType; + } + + public void setInstrumentType(String instrumentType) { + this.instrumentType = instrumentType; + } + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public Date getOrderDate() { + return orderDate; + } + + public void setOrderDate(Date orderDate) { + this.orderDate = orderDate; + } + + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + + @Override + public String toString() { + return "Model : " + Order.class.getName() + " : " + this.orderNr + ", " + this.orderType + ", " + String.valueOf(this.amount) + ", " + this.instrumentCode + ", " + + this.instrumentNumber + ", " + this.instrumentType + ", " + this.currency + ", " + this.clientNr + ", " + this.firstName + ", " + this.lastName + ", " + + String.valueOf(this.orderDate); + } + } + +}