Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id AAE89200AE1 for ; Mon, 23 May 2016 08:10:20 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A9738160A28; Mon, 23 May 2016 06:10:20 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id CCCE4160A06 for ; Mon, 23 May 2016 08:10:19 +0200 (CEST) Received: (qmail 55064 invoked by uid 500); 23 May 2016 06:10:18 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 55055 invoked by uid 99); 23 May 2016 06:10:18 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 May 2016 06:10:18 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id AEBD1DFF12; Mon, 23 May 2016 06:10:18 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Message-Id: <1fd7396712104737a75448f97c575842@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: camel git commit: CAMEL-9982 Date: Mon, 23 May 2016 06:10:18 +0000 (UTC) archived-at: Mon, 23 May 2016 06:10:20 -0000 Repository: camel Updated Branches: refs/heads/master c44e7dca7 -> 739a750c0 CAMEL-9982 Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/739a750c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/739a750c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/739a750c Branch: refs/heads/master Commit: 739a750c0b37a5244d459e601903d0491feace3c Parents: c44e7dc Author: Arno Noordover Authored: Sun May 22 20:59:10 2016 +0200 Committer: Arno Noordover Committed: Sun May 22 20:59:10 2016 +0200 ---------------------------------------------------------------------- .../bindy/BindyAbstractDataFormat.java | 17 +++++++++++ .../bindy/csv/BindyCsvDataFormat.java | 11 +------ .../bindy/fixed/BindyFixedLengthDataFormat.java | 1 + .../BindySimpleFixedLengthWithLinkTest.java | 32 ++++++++++++++++++++ 4 files changed, 51 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/739a750c/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java index 9099a46..c55084e 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java @@ -16,9 +16,11 @@ */ package org.apache.camel.dataformat.bindy; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,6 +30,7 @@ import java.util.function.Function; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.dataformat.bindy.annotation.FormatFactories; +import org.apache.camel.dataformat.bindy.annotation.Link; import org.apache.camel.dataformat.bindy.format.factories.DefaultFactoryRegistry; import org.apache.camel.dataformat.bindy.format.factories.FactoryRegistry; import org.apache.camel.dataformat.bindy.format.factories.FormatFactoryInterface; @@ -129,6 +132,20 @@ public abstract class BindyAbstractDataFormat extends ServiceSupport implements this.modelFactory = modelFactory; } + protected Map createLinkedFieldsModel(Object model) throws IllegalAccessException { + Map row = new HashMap<>(); + for (Field field : model.getClass().getDeclaredFields()) { + Link linkField = field.getAnnotation(Link.class); + if (linkField != null) { + boolean accessible = field.isAccessible(); + field.setAccessible(true); + row.put(field.getType().getName(), field.get(model)); + field.setAccessible(accessible); + } + } + return row; + } + protected abstract BindyAbstractFactory createModelFactory(FormatFactory formatFactory) throws Exception; protected Object extractUnmarshalResult(List> models) { http://git-wip-us.apache.org/repos/asf/camel/blob/739a750c/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 500afe3..8f986ba 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 @@ -91,16 +91,7 @@ public class BindyCsvDataFormat extends BindyAbstractDataFormat { String name = model.getClass().getName(); Map row = new HashMap(1); row.put(name, model); - // search for @Link-ed fields and add them to the model - for (Field field : model.getClass().getDeclaredFields()) { - Link linkField = field.getAnnotation(Link.class); - if (linkField != null) { - boolean accessible = field.isAccessible(); - field.setAccessible(true); - row.put(field.getType().getName(), field.get(model)); - field.setAccessible(accessible); - } - } + row.putAll(createLinkedFieldsModel(model)); models.add(row); } } http://git-wip-us.apache.org/repos/asf/camel/blob/739a750c/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 317c659..22efd73 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 @@ -85,6 +85,7 @@ public class BindyFixedLengthDataFormat extends BindyAbstractDataFormat { String name = model.getClass().getName(); Map row = new HashMap(); row.put(name, model); + row.putAll(createLinkedFieldsModel(model)); models.add(row); } } else { http://git-wip-us.apache.org/repos/asf/camel/blob/739a750c/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/link/BindySimpleFixedLengthWithLinkTest.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/link/BindySimpleFixedLengthWithLinkTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/link/BindySimpleFixedLengthWithLinkTest.java index 323befd..e38b06c 100644 --- a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/link/BindySimpleFixedLengthWithLinkTest.java +++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/link/BindySimpleFixedLengthWithLinkTest.java @@ -28,6 +28,8 @@ import org.apache.camel.model.dataformat.BindyType; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; +import static org.hamcrest.core.Is.is; + /** * This test validates that header and footer records are successfully * marshalled / unmarshalled in conjunction with the primary data records @@ -37,11 +39,15 @@ public class BindySimpleFixedLengthWithLinkTest extends CamelTestSupport { public static final String URI_DIRECT_UNMARSHALL = "direct:unmarshall"; public static final String URI_MOCK_UNMARSHALL_RESULT = "mock:unmarshall-result"; + public static final String URI_DIRECT_MARSHALL = "direct:marshall"; + public static final String URI_MOCK_MARSHALL_RESULT = "mock:marshall-result"; private static final String TEST_RECORD = "AAABBBCCC\r\n"; @EndpointInject(uri = URI_MOCK_UNMARSHALL_RESULT) private MockEndpoint unmarshallResult; + @EndpointInject(uri = URI_MOCK_MARSHALL_RESULT) + private MockEndpoint marshallResult; // ************************************************************************* // TESTS @@ -65,6 +71,28 @@ public class BindySimpleFixedLengthWithLinkTest extends CamelTestSupport { assertEquals("BBB", order.subRec.fieldB); } + @Test + public void testMarshallMessage() throws Exception { + + marshallResult.expectedMessageCount(1); + + Order order = new Order(); + order.setFieldA("AAA"); + order.setFieldC("CCC"); + SubRec subRec = new SubRec(); + subRec.setFieldB("BBB"); + order.setSubRec(subRec); + + template.sendBody(URI_DIRECT_MARSHALL, order); + + marshallResult.assertIsSatisfied(); + + // check the model + Exchange exchange = marshallResult.getReceivedExchanges().get(0); + String asString = exchange.getIn().getBody(String.class); + assertThat(asString, is("AAABBBCCC\r\n")); + } + // ************************************************************************* // ROUTES // ************************************************************************* @@ -83,6 +111,10 @@ public class BindySimpleFixedLengthWithLinkTest extends CamelTestSupport { from(URI_DIRECT_UNMARSHALL) .unmarshal(bindy) .to(URI_MOCK_UNMARSHALL_RESULT); + + from(URI_DIRECT_MARSHALL) + .marshal(bindy) + .to(URI_MOCK_MARSHALL_RESULT); } };