camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] camel git commit: CAMEL-8217: Fix typeHints option for camel-xmljson
Date Thu, 08 Jan 2015 07:15:28 GMT
Repository: camel
Updated Branches:
  refs/heads/master aad2fbd29 -> fe6220027


CAMEL-8217: Fix typeHints option for camel-xmljson

Fixed camel-core model to set the option on the data format. Changed the
behaviour of YES and WITH_PREFIX to reflect documentation and naming.


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

Branch: refs/heads/master
Commit: fb16ab629f5538cf93949d00b18700e1fd66f901
Parents: aad2fbd
Author: Toni Fadjukoff <lamperi@lamperi.name>
Authored: Thu Jan 8 01:36:54 2015 +0200
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Thu Jan 8 15:00:47 2015 +0800

----------------------------------------------------------------------
 .../model/dataformat/XmlJsonDataFormat.java     |  4 +-
 .../dataformat/xmljson/XmlJsonDataFormat.java   |  2 +
 .../dataformat/xmljson/XmlJsonOptionsTest.java  | 69 ++++++++++++++++++++
 .../xmljson/testMessage5-typeHints.json         |  1 +
 .../xmljson/testMessage5-typeHints.xml          |  6 ++
 .../xmljson/testMessage6-prefixedTypeHints.xml  |  5 ++
 6 files changed, 85 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/fb16ab62/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
b/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
index eda8481..fba0812 100644
--- a/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
@@ -171,7 +171,7 @@ public class XmlJsonDataFormat extends DataFormatDefinition {
 
         // will end up calling the setTypeHints(String s) which does the parsing from the
Enum String key to the Enum value
         if (typeHints != null) {
-            setProperty(camelContext, typeHints, TYPE_HINTS, typeHints);
+            setProperty(camelContext, dataFormat, TYPE_HINTS, typeHints);
         }
 
         //TODO: xmljson: element-namespace mapping is not implemented in the XML DSL
@@ -276,4 +276,4 @@ public class XmlJsonDataFormat extends DataFormatDefinition {
         this.typeHints = typeHints;
     }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/fb16ab62/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
b/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
index 99a91d7..81d9420 100644
--- a/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
+++ b/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
@@ -117,6 +117,8 @@ public class XmlJsonDataFormat extends ServiceSupport implements DataFormat
{
         if (typeHints == TypeHintsEnum.YES || typeHints == TypeHintsEnum.WITH_PREFIX) {
             serializer.setTypeHintsEnabled(true);
             if (typeHints == TypeHintsEnum.WITH_PREFIX) {
+                serializer.setTypeHintsCompatibility(false);
+            } else {
                 serializer.setTypeHintsCompatibility(true);
             }
         } else {

http://git-wip-us.apache.org/repos/asf/camel/blob/fb16ab62/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
b/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
index bfa2b7b..9242f0f 100644
--- a/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
+++ b/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
@@ -140,6 +140,61 @@ public class XmlJsonOptionsTest extends AbstractJsonTestSupport {
         assertEquals("Root element must have namespace attributes", 2, document.getDocumentElement().getAttributes().getLength());
         mockXML.assertIsSatisfied();
     }
+	
+	@Test
+	public void testTypeHintsToJSON() throws Exception {
+        InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml");
+        String in = context.getTypeConverter().convertTo(String.class, inStream);
+
+        MockEndpoint mockJSON = getMockEndpoint("mock:jsonTypeHints");
+        mockJSON.expectedMessageCount(1);
+        mockJSON.message(0).body().isInstanceOf(byte[].class);
+
+        Object json = template.requestBody("direct:marshalTypeHints", in);
+        String jsonString = context.getTypeConverter().convertTo(String.class, json);
+        JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
+        assertEquals("root.a must be number", Integer.valueOf(1), obj.getJSONObject("root").get("a"));
+        assertEquals("root.b must be boolean", Boolean.TRUE, obj.getJSONObject("root").get("b"));
+
+        mockJSON.assertIsSatisfied();
+	}
+	
+	@Test
+	public void testTypeHintsToXML() throws Exception {
+        InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json");
+        String in = context.getTypeConverter().convertTo(String.class, inStream);
+
+        MockEndpoint mockXML = getMockEndpoint("mock:xmlTypeHints");
+        mockXML.expectedMessageCount(1);
+        mockXML.message(0).body().isInstanceOf(String.class);
+
+        Object marshalled = template.requestBody("direct:unmarshalTypeHints", in);
+        Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
+        assertEquals("Element a should exists", 1, document.getDocumentElement().getElementsByTagName("a").getLength());
+        assertNotNull("Element a should have attribute type", document.getDocumentElement().getElementsByTagName("a").item(0).getAttributes().getNamedItem("type"));
+        assertEquals("Element a should have attribute type with value number", "number",
document.getDocumentElement().getElementsByTagName("a").item(0).getAttributes().getNamedItem("type").getTextContent());
+        assertEquals("Element b should have attribute type with value boolean", "boolean",
document.getDocumentElement().getElementsByTagName("b").item(0).getAttributes().getNamedItem("type").getTextContent());
+        mockXML.assertIsSatisfied();
+	}
+	
+	@Test
+	public void testPrefixedTypeHintsToJSON() throws Exception {
+        InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml");
+        String in = context.getTypeConverter().convertTo(String.class, inStream);
+
+        MockEndpoint mockJSON = getMockEndpoint("mock:jsonPrefixedTypeHints");
+        mockJSON.expectedMessageCount(1);
+        mockJSON.message(0).body().isInstanceOf(byte[].class);
+
+        Object json = template.requestBody("direct:marshalPrefixedTypeHints", in);
+        String jsonString = context.getTypeConverter().convertTo(String.class, json);
+        JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
+        assertEquals("root.a must be number", Integer.valueOf(1), obj.getJSONObject("root").get("a"));
+        assertEquals("root.b must be boolean", Boolean.TRUE, obj.getJSONObject("root").get("b"));
+
+        mockJSON.assertIsSatisfied();
+	}
+
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -171,6 +226,20 @@ public class XmlJsonOptionsTest extends AbstractJsonTestSupport {
                 // from JSON to XML
                 from("direct:unmarshalNS").unmarshal(namespacesFormat).to("mock:xmlNS");
 
+				XmlJsonDataFormat typeHintsFormat = new XmlJsonDataFormat();
+                typeHintsFormat.setForceTopLevelObject(true);
+				typeHintsFormat.setTypeHints("YES");
+				// from XML to JSON
+                from("direct:marshalTypeHints").marshal(typeHintsFormat).to("mock:jsonTypeHints");
+                // from JSON to XML
+                from("direct:unmarshalTypeHints").unmarshal(typeHintsFormat).to("mock:xmlTypeHints");
+				
+				XmlJsonDataFormat prefixedTypeHintsFormat = new XmlJsonDataFormat();
+                prefixedTypeHintsFormat.setForceTopLevelObject(true);
+				prefixedTypeHintsFormat.setTypeHints("WITH_PREFIX");
+				// from XML to JSON
+                from("direct:marshalPrefixedTypeHints").marshal(prefixedTypeHintsFormat).to("mock:jsonPrefixedTypeHints");
+
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/fb16ab62/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json
----------------------------------------------------------------------
diff --git a/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json
new file mode 100644
index 0000000..528b8c3
--- /dev/null
+++ b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json
@@ -0,0 +1 @@
+{ "a": 1, "b": true, "c": "foo" }

http://git-wip-us.apache.org/repos/asf/camel/blob/fb16ab62/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml
----------------------------------------------------------------------
diff --git a/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml
new file mode 100644
index 0000000..3052ba8
--- /dev/null
+++ b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml
@@ -0,0 +1,6 @@
+<!-- { "a": 1, "b": true, "c": "foo" } -->
+<root>
+	<a type="number">1</a>
+	<b type="boolean">true</b>
+	<c>foo</c>
+</root>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/fb16ab62/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml
----------------------------------------------------------------------
diff --git a/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml
new file mode 100644
index 0000000..66ff295
--- /dev/null
+++ b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml
@@ -0,0 +1,5 @@
+<!-- { "a": 1, "b": true, "c": "foo" } -->
+<root>
+	<a json_type="number">1</a>
+	<b json_type="boolean">true</b>
+</root>
\ No newline at end of file


Mime
View raw message