You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dk...@apache.org on 2012/08/21 21:28:25 UTC

svn commit: r1375730 - in /camel/branches/camel-2.10.x/camel-core/src: main/java/org/apache/camel/converter/jaxp/XmlConverter.java test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java

Author: dkulp
Date: Tue Aug 21 19:28:25 2012
New Revision: 1375730

URL: http://svn.apache.org/viewvc?rev=1375730&view=rev
Log:
Merged revisions 1371571 via  git cherry-pick from
https://svn.apache.org/repos/asf/camel/trunk

........
  r1371571 | ningjiang | 2012-08-10 00:46:30 -0400 (Fri, 10 Aug 2012) | 2 lines

  CAMEL-5403 Type converter for NodeList with size 1 to Node

........

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

Modified: camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java?rev=1375730&r1=1375729&r2=1375730&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java (original)
+++ camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java Tue Aug 21 19:28:25 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/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java?rev=1375730&view=auto
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java (added)
+++ camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/language/XPathLanguageSingleNodeListTest.java Tue Aug 21 19:28:25 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");
+            }
+        };
+    }
+
+}