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 <a...@noordover.net>
Authored: Sun May 22 20:59:10 2016 +0200
Committer: Arno Noordover <a...@noordover.net>
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<String, Object> createLinkedFieldsModel(Object model) throws 
IllegalAccessException {
+        Map<String, Object> 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<Map<String, Object>> 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<String, Object> row = new HashMap<String, Object>(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<String, Object> row = new HashMap<String, Object>();
                 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);
             }
         };
 

Reply via email to