camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r1371571 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/converter/jaxp/XmlConverter.java test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java
Date Fri, 10 Aug 2012 04:46:31 GMT
Author: ningjiang
Date: Fri Aug 10 04:46:30 2012
New Revision: 1371571

URL: http://svn.apache.org/viewvc?rev=1371571&view=rev
Log:
CAMEL-5403 Type converter for NodeList with size 1 to Node

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java?rev=1371571&r1=1371570&r2=1371571&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
Fri Aug 10 04:46:30 2012
@@ -693,6 +693,27 @@ public class XmlConverter {
         toResult(source, result);
         return result.getNode();
     }
+    
+    /**
+     * Convert a NodeList consisting of just 1 node to a DOM Node.
+     * @param nl the NodeList
+     * @return the DOM Node
+     */
+    @Converter
+    public Node toDOMNodeFromSingleNodeList(NodeList nl) {
+        return nl.getLength() == 1 ? nl.item(0) : null;
+    }
+    
+    /**
+     * Convert a NodeList consisting of just 1 node to a DOM Document.
+     * Cannot convert NodeList with length > 1 because they require a root node.
+     * @param nl the NodeList
+     * @return the DOM Document
+     */
+    @Converter
+    public Document toDOMDocumentFromSingleNodeList(NodeList nl) throws ParserConfigurationException,
TransformerException {
+        return nl.getLength() == 1 ? toDOMDocument(nl.item(0)) : null;
+    }
 
     /**
      * Converts the given TRaX Source into a W3C DOM node

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java?rev=1371571&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java
Fri Aug 10 04:46:30 2012
@@ -0,0 +1,91 @@
+/**
+ * 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.language;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.NoTypeConversionAvailableException;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+/**
+ * Tests new converters added to XmlConverters to make Camel intelligent when needing to
convert 
+ * a NodeList of length 1 into a Document or a Node.
+ *
+ */
+public class XPathLanguageSingleNodeListTest extends ContextTestSupport {
+    
+    private static final String XML_INPUT_SINGLE = "<root><name>Raul</name><surname>Kripalani</surname></root>";
+    private static final String XML_INPUT_MULTIPLE = "<root><name>Raul</name><name>Raul</name><surname>Kripalani</surname></root>";
+    
+    /**
+     * A single node XPath selection that internally returns a DTMNodeList of length 1 can
now be automatically
+     * converted to a Document/Node.
+     * @throws Exception
+     */
+    @Test
+    public void testSingleNodeList() throws Exception {
+        getMockEndpoint("mock:found").expectedMessageCount(1);
+        getMockEndpoint("mock:found").setResultWaitTime(500);
+        getMockEndpoint("mock:notfound").expectedMessageCount(0);
+        getMockEndpoint("mock:notfound").setResultWaitTime(500);
+
+        template.requestBody("direct:doTest", XML_INPUT_SINGLE, String.class);
+        assertMockEndpointsSatisfied();
+        
+    }
+    
+    /**
+     * Regression test to ensure that a NodeList of length > 1 is not processed by the
new converters.
+     * @throws Exception
+     */
+    @Test
+    public void testMultipleNodeList() throws Exception {
+        getMockEndpoint("mock:found").expectedMessageCount(0);
+        getMockEndpoint("mock:found").setResultWaitTime(500);
+        getMockEndpoint("mock:notfound").expectedMessageCount(0);
+        getMockEndpoint("mock:notfound").setResultWaitTime(500);
+
+        try {
+            template.requestBody("direct:doTest", XML_INPUT_MULTIPLE, String.class);
+            fail("NoTypeConversionAvailableException expected");
+        } catch (CamelExecutionException ex) {
+            assertEquals(RuntimeCamelException.class, ex.getCause().getClass());
+            assertEquals(NoTypeConversionAvailableException.class, ex.getCause().getCause().getClass());
+        }
+        
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:doTest")
+                    .transform().xpath("/root/name")
+                    .choice()
+                        .when().xpath("/name")
+                            .to("mock:found")
+                        .otherwise()
+                            .to("mock:notfound");
+            }
+        };
+    }
+
+}



Mime
View raw message