camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] git commit: CAMEL-6962 Allow binding name to be specified in JibxDataFormat with thanks to Peter
Date Thu, 14 Nov 2013 07:31:09 GMT
Updated Branches:
  refs/heads/camel-2.11.x 0b54e7418 -> f1a530b83
  refs/heads/camel-2.12.x bf95b99b2 -> 7bebdc9f2


CAMEL-6962 Allow binding name to be specified in JibxDataFormat with thanks to Peter


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7bebdc9f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7bebdc9f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7bebdc9f

Branch: refs/heads/camel-2.12.x
Commit: 7bebdc9f26d3533c62ef8565672cdbe9b650514f
Parents: bf95b99
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Thu Nov 14 14:48:38 2013 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Thu Nov 14 15:26:24 2013 +0800

----------------------------------------------------------------------
 .../camel/dataformat/jibx/JibxDataFormat.java   | 26 ++++++-
 ...bxDataFormatMarshallWithBindingNameTest.java | 79 ++++++++++++++++++++
 ...DataFormatUnmarshallWithBindingNameTest.java | 66 ++++++++++++++++
 3 files changed, 169 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7bebdc9f/components/camel-jibx/src/main/java/org/apache/camel/dataformat/jibx/JibxDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-jibx/src/main/java/org/apache/camel/dataformat/jibx/JibxDataFormat.java
b/components/camel-jibx/src/main/java/org/apache/camel/dataformat/jibx/JibxDataFormat.java
index 4adb014..3ba125f 100644
--- a/components/camel-jibx/src/main/java/org/apache/camel/dataformat/jibx/JibxDataFormat.java
+++ b/components/camel-jibx/src/main/java/org/apache/camel/dataformat/jibx/JibxDataFormat.java
@@ -26,9 +26,11 @@ import org.jibx.runtime.BindingDirectory;
 import org.jibx.runtime.IBindingFactory;
 import org.jibx.runtime.IMarshallingContext;
 import org.jibx.runtime.IUnmarshallingContext;
+import org.jibx.runtime.JiBXException;
 
 public class JibxDataFormat implements DataFormat {
     private Class<?> unmarshallClass;
+    private String bindingName;
 
     public JibxDataFormat() {
     }
@@ -37,15 +39,20 @@ public class JibxDataFormat implements DataFormat {
         this.setUnmarshallClass(unmarshallClass);
     }
 
+    public JibxDataFormat(Class<?> unmarshallClass, String bindingName) {
+        this.setUnmarshallClass(unmarshallClass);
+        this.setBindingName(bindingName);
+    }
+
     public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception
{
-        IBindingFactory bindingFactory = BindingDirectory.getFactory(body.getClass());
+        IBindingFactory bindingFactory = createBindingFactory(body.getClass(), bindingName);
         IMarshallingContext marshallingContext = bindingFactory.createMarshallingContext();
         marshallingContext.marshalDocument(body, null, null, stream);
     }
 
     public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
         ObjectHelper.notNull(getUnmarshallClass(), "unmarshallClass");
-        IBindingFactory bindingFactory = BindingDirectory.getFactory(getUnmarshallClass());
+        IBindingFactory bindingFactory = createBindingFactory(getUnmarshallClass(), bindingName);
         IUnmarshallingContext unmarshallingContext = bindingFactory.createUnmarshallingContext();
         return unmarshallingContext.unmarshalDocument(stream, null);
     }
@@ -58,4 +65,19 @@ public class JibxDataFormat implements DataFormat {
         this.unmarshallClass = unmarshallClass;
     }
 
+    public String getBindingName() {
+        return bindingName;
+    }
+
+    public void setBindingName(String bindingName) {
+        this.bindingName = bindingName;
+    }
+
+    private IBindingFactory createBindingFactory(Class<?> clazz, String bindingName)
throws JiBXException {
+        if (bindingName == null) {
+            return BindingDirectory.getFactory(clazz);
+        } else {
+            return BindingDirectory.getFactory(bindingName, clazz);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/7bebdc9f/components/camel-jibx/src/test/java/org/apache/camel/dataformat/jibx/JibxDataFormatMarshallWithBindingNameTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jibx/src/test/java/org/apache/camel/dataformat/jibx/JibxDataFormatMarshallWithBindingNameTest.java
b/components/camel-jibx/src/test/java/org/apache/camel/dataformat/jibx/JibxDataFormatMarshallWithBindingNameTest.java
new file mode 100644
index 0000000..2dd7bcb
--- /dev/null
+++ b/components/camel-jibx/src/test/java/org/apache/camel/dataformat/jibx/JibxDataFormatMarshallWithBindingNameTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.jibx;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Element;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+
+/**
+ * @version
+ */
+public class JibxDataFormatMarshallWithBindingNameTest extends CamelTestSupport {
+    private static final String BINDING_NAME = "purchaseOrder-jibx";
+
+    @Test
+    public void testMarshall() throws InterruptedException, ParserConfigurationException,
IOException, SAXException {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        PurchaseOrder purchaseOrder = new PurchaseOrder();
+        String name = "foo";
+        purchaseOrder.setName(name);
+        double price = 49;
+        purchaseOrder.setPrice(price);
+        double amount = 3;
+        purchaseOrder.setAmount(amount);
+
+        template.sendBody("direct:start", purchaseOrder);
+
+        assertMockEndpointsSatisfied();
+
+        String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
+        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        Element root = builder.parse(new InputSource(new StringReader(body))).getDocumentElement();
+
+        assertEquals(name, root.getAttribute("name"));
+        assertEquals(price + "", root.getAttribute("price"));
+        assertEquals(amount + "", root.getAttribute("amount"));
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                JibxDataFormat jibxDataFormat = new JibxDataFormat(PurchaseOrder.class, BINDING_NAME);
+
+                from("direct:start").marshal(jibxDataFormat).convertBodyTo(String.class).to("mock:result");
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/7bebdc9f/components/camel-jibx/src/test/java/org/apache/camel/dataformat/jibx/JibxDataFormatUnmarshallWithBindingNameTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jibx/src/test/java/org/apache/camel/dataformat/jibx/JibxDataFormatUnmarshallWithBindingNameTest.java
b/components/camel-jibx/src/test/java/org/apache/camel/dataformat/jibx/JibxDataFormatUnmarshallWithBindingNameTest.java
new file mode 100644
index 0000000..6709fa8
--- /dev/null
+++ b/components/camel-jibx/src/test/java/org/apache/camel/dataformat/jibx/JibxDataFormatUnmarshallWithBindingNameTest.java
@@ -0,0 +1,66 @@
+/**
+ * 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.jibx;
+
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.xml.sax.SAXException;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+
+public class JibxDataFormatUnmarshallWithBindingNameTest extends CamelTestSupport {
+    private static final String BINDING_NAME = "purchaseOrder-jibx";
+
+    @Test
+    public void testUnmarshallWithBindingName() throws InterruptedException, ParserConfigurationException,
IOException,
+            SAXException {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        String name = "foo";
+        double price = 1;
+        double amount = 2;
+        String purchaseOrderXml = String.format("<order name='%s' price='%s' amount='%s'
/>", name, price + "", amount
+                + "");
+
+        template.sendBody("direct:start", purchaseOrderXml);
+
+        assertMockEndpointsSatisfied();
+
+        PurchaseOrder body = mock.getReceivedExchanges().get(0).getIn().getBody(PurchaseOrder.class);
+        assertEquals(name, body.getName());
+        assertEquals(price, body.getPrice(), 1);
+        assertEquals(amount, body.getAmount(), 1);
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                JibxDataFormat jibxDataFormat = new JibxDataFormat(PurchaseOrder.class, BINDING_NAME);
+
+                from("direct:start").unmarshal(jibxDataFormat).convertBodyTo(PurchaseOrder.class).to("mock:result");
+            }
+        };
+    }
+
+}


Mime
View raw message