camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r943161 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/converter/jaxp/ camel-core/src/test/java/org/apache/camel/builder/xml/ camel-core/src/test/java/org/apache/camel/component/bean/ camel-core/src/test/java/org/apache/camel/...
Date Tue, 11 May 2010 16:20:23 GMT
Author: davsclaus
Date: Tue May 11 16:20:23 2010
New Revision: 943161

URL: http://svn.apache.org/viewvc?rev=943161&view=rev
Log:
CAMEL-2669: Improved NodeList to String convertion. So it include the XML tags etc as you
would expect.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithXPathInjectionUsingResultTypeTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java
    camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregratedJmsRouteTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java?rev=943161&r1=943160&r2=943161&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
Tue May 11 16:20:23 2010
@@ -21,6 +21,7 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import javax.xml.transform.TransformerException;
 
 import org.w3c.dom.Attr;
 import org.w3c.dom.Element;
@@ -31,7 +32,6 @@ import org.w3c.dom.Text;
 import org.apache.camel.Converter;
 import org.apache.camel.util.ObjectHelper;
 
-
 /**
  * Converts from some DOM types to Java types
  *
@@ -39,20 +39,53 @@ import org.apache.camel.util.ObjectHelpe
  */
 @Converter
 public final class DomConverter {
+    private final XmlConverter xml;
 
-    private DomConverter() {
-        // Utility Class
+    public DomConverter() {
+        xml = new XmlConverter();
     }
 
     @Converter
-    public static String toString(NodeList nodeList) {
+    public String toString(NodeList nodeList) throws TransformerException {
+        // converting NodeList to String is more tricky
+        // sometimes the NodeList is a Node which we can then leverage
+        // the XML converter to turn into XML incl. tags
+
         StringBuilder buffer = new StringBuilder();
-        append(buffer, nodeList);
+
+        // use XML converter at first since it preserves tag names
+        boolean found = false;
+        if (nodeList instanceof Node) {
+            Node node = (Node) nodeList;
+            String s = xml.toString(node);
+            if (ObjectHelper.isNotEmpty(s)) {
+                found = true;
+                buffer.append(s);
+            }
+        } else {
+            // use XML converter at first since it preserves tag names
+            int size = nodeList.getLength();
+            for (int i = 0; i < size; i++) {
+                Node node = nodeList.item(i);
+                String s = xml.toString(node);
+                if (ObjectHelper.isNotEmpty(s)) {
+                    found = true;
+                    buffer.append(s);
+                }
+            }
+        }
+
+        // and eventually we must fallback to append without tags, such as when you have
+        // used an xpath to select an attribute or text() or something
+        if (!found) {
+            append(buffer, nodeList);
+        }
+
         return buffer.toString();
     }
 
     @Converter
-    public static Integer toInteger(NodeList nodeList) {
+    public Integer toInteger(NodeList nodeList) {
         StringBuilder buffer = new StringBuilder();
         append(buffer, nodeList);
         String s = buffer.toString();
@@ -60,7 +93,7 @@ public final class DomConverter {
     }
 
     @Converter
-    public static Long toLong(NodeList nodeList) {
+    public Long toLong(NodeList nodeList) {
         StringBuilder buffer = new StringBuilder();
         append(buffer, nodeList);
         String s = buffer.toString();
@@ -79,12 +112,12 @@ public final class DomConverter {
     }
 
     @Converter
-    public static InputStream toInputStream(NodeList nodeList) {
+    public InputStream toInputStream(NodeList nodeList) throws TransformerException {
         return new ByteArrayInputStream(toByteArray(nodeList));
     }
 
     @Converter
-    public static byte[] toByteArray(NodeList nodeList) {
+    public byte[] toByteArray(NodeList nodeList) throws TransformerException {
         String data = toString(nodeList);
         return data.getBytes();
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java?rev=943161&r1=943160&r2=943161&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathTest.java Tue May
11 16:20:23 2010
@@ -32,15 +32,17 @@ import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFunctionResolver;
 
-import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Exchange;
-import org.apache.camel.Expression;
-import org.apache.camel.Predicate;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
 
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import org.apache.camel.util.ObjectHelper;
+
 import static org.apache.camel.builder.xml.XPathBuilder.xpath;
 
 /**
@@ -59,6 +61,7 @@ public class XPathTest extends ContextTe
         assertExpression("foo/bar", "<foo><bar>cheese</bar></foo>",
"cheese");
         assertExpression("foo/bar/text()", "<foo><bar>cheese</bar></foo>",
"cheese");
         assertExpression("/foo/@id", "<foo id='cheese'>hey</foo>", "cheese");
+        assertExpression("/foo/@num", "<foo num='123'>hey</foo>", "123");
     }
 
     public void testXPathPredicates() throws Exception {
@@ -66,6 +69,7 @@ public class XPathTest extends ContextTe
         assertPredicate("$name = 'James'", "<foo><bar xyz='cheese'/></foo>",
true);
         assertPredicate("$name = 'Hiram'", "<foo><bar xyz='cheese'/></foo>",
false);
         assertPredicate("/foo/notExist", "<foo><bar xyz='cheese'/></foo>",
false);
+        assertPredicate("/foo[@num = '123']", "<foo num='123'>hey</foo>", true);
     }
 
     public void testXPathWithCustomVariable() throws Exception {
@@ -101,7 +105,7 @@ public class XPathTest extends ContextTe
         NodeList node = assertIsInstanceOf(NodeList.class, result);
         assertNotNull(node);
         String s = context.getTypeConverter().convertTo(String.class, node);
-        assertEquals("bar", s);
+        assertEquals("<foo>bar</foo>", s);
     }
 
     public void testXPathNumberResult() throws Exception {
@@ -135,7 +139,7 @@ public class XPathTest extends ContextTe
         Object result = xpath("/foo").evaluate(createExchange(doc));
         assertNotNull(result);
         String s = context.getTypeConverter().convertTo(String.class, result);
-        assertEquals("bar", s);
+        assertEquals("<foo>bar</foo>", s);
     }
 
     public void testXPathWithDocumentTypeDOMSource() throws Exception {
@@ -147,7 +151,7 @@ public class XPathTest extends ContextTe
         Object result = builder.evaluate(createExchange(doc));
         assertNotNull(result);
         String s = context.getTypeConverter().convertTo(String.class, result);
-        assertEquals("bar", s);
+        assertEquals("<foo>bar</foo>", s);
     }
 
     public void testXPathWithDocumentTypeInputSource() throws Exception {
@@ -160,7 +164,7 @@ public class XPathTest extends ContextTe
         Object result = builder.evaluate(createExchange(doc));
         assertNotNull(result);
         String s = context.getTypeConverter().convertTo(String.class, result);
-        assertEquals("bar", s);
+        assertEquals("<foo>bar</foo>", s);
     }
 
     public void testXPathWithDocumentTypeInputSourceFluentBuilder() throws Exception {
@@ -172,7 +176,7 @@ public class XPathTest extends ContextTe
         Object result = builder.evaluate(createExchange(doc));
         assertNotNull(result);
         String s = context.getTypeConverter().convertTo(String.class, result);
-        assertEquals("bar", s);
+        assertEquals("<foo>bar</foo>", s);
     }
 
     public void testXPathWithDocumentTypeInputSourceNoResultQName() throws Exception {
@@ -313,6 +317,9 @@ public class XPathTest extends ContextTe
 
     public void testXPathNotUsingExchangeEvaluate() throws Exception {
         String name = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>cheese</bar></foo>",
String.class);
+        assertEquals("<bar>cheese</bar>", name);
+
+        name = XPathBuilder.xpath("foo/bar/text()").evaluate(context, "<foo><bar>cheese</bar></foo>",
String.class);
         assertEquals("cheese", name);
 
         Integer number = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>123</bar></foo>",
Integer.class);
@@ -369,4 +376,38 @@ public class XPathTest extends ContextTe
         assertEquals(size, count);
     }
 
+    public void testXPathNodeListTest() throws Exception {
+        String xml = "<foo><person id=\"1\">Claus<country>SE</country></person>"
+                + "<person id=\"2\">Jonathan<country>CA</country></person></foo>";
+        Document doc = context.getTypeConverter().convertTo(Document.class, xml);
+
+        Object result = xpath("/foo/person").nodeSetResult().evaluate(createExchange(doc));
+        assertNotNull(result);
+
+        String s = context.getTypeConverter().convertTo(String.class, result);
+        assertEquals(ObjectHelper.between(xml, "<foo>", "</foo>"), s);
+    }
+
+    public void testXPathNodeListSimpleTest() throws Exception {
+        String xml = "<foo><person>Claus</person></foo>";
+        Document doc = context.getTypeConverter().convertTo(Document.class, xml);
+
+        Object result = xpath("/foo/person").nodeSetResult().evaluate(createExchange(doc));
+        assertNotNull(result);
+
+        String s = context.getTypeConverter().convertTo(String.class, result);
+        assertEquals("<person>Claus</person>", s);
+    }
+
+    public void testXPathNodeListSimpleTestText() throws Exception {
+        String xml = "<foo><person>Claus</person></foo>";
+        Document doc = context.getTypeConverter().convertTo(Document.class, xml);
+
+        Object result = xpath("/foo/person/text()").nodeSetResult().evaluate(createExchange(doc));
+        assertNotNull(result);
+
+        String s = context.getTypeConverter().convertTo(String.class, result);
+        assertEquals("Claus", s);
+    }
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithXPathInjectionUsingResultTypeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithXPathInjectionUsingResultTypeTest.java?rev=943161&r1=943160&r2=943161&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithXPathInjectionUsingResultTypeTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithXPathInjectionUsingResultTypeTest.java
Tue May 11 16:20:23 2010
@@ -43,8 +43,8 @@ public class BeanWithXPathInjectionUsing
         public String ab;
         public String abText;
 
-        public void read(@XPath("//a/b") String ab,
-                @XPath(value = "concat('a',//a/b)", resultType = String.class) String abText)
{
+        public void read(@XPath("//a/b/text()") String ab,
+                         @XPath(value = "concat('a',//a/b)", resultType = String.class) String
abText) {
             this.ab = ab;
             this.abText = abText;
         }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java?rev=943161&r1=943160&r2=943161&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java
Tue May 11 16:20:23 2010
@@ -42,7 +42,8 @@ public class XPathToFileTest extends Con
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(2);
 
-        String xml = "<foo><person>Claus</person><person>Jonathan</person></foo>";
+        String xml = "<foo><person id=\"1\">Claus<country>SE</country></person>"
+                + "<person id=\"2\">Jonathan<country>CA</country></person></foo>";
         Document doc = context.getTypeConverter().convertTo(Document.class, xml);
 
         template.sendBody("direct:start", doc);
@@ -51,11 +52,11 @@ public class XPathToFileTest extends Con
 
         File first = new File("target/xpath/xpath-0.xml").getAbsoluteFile();
         assertTrue("File xpath-0.xml should exists", first.exists());
-        assertEquals("Claus", context.getTypeConverter().convertTo(String.class, first));
+        assertEquals("<person id=\"1\">Claus<country>SE</country></person>",
context.getTypeConverter().convertTo(String.class, first));
 
         File second = new File("target/xpath/xpath-1.xml").getAbsoluteFile();
         assertTrue("File xpath-1.xml should exists", second.exists());
-        assertEquals("Jonathan", context.getTypeConverter().convertTo(String.class, second));
+        assertEquals("<person id=\"2\">Jonathan<country>CA</country></person>",
context.getTypeConverter().convertTo(String.class, second));
     }
 
     @Override
@@ -65,6 +66,7 @@ public class XPathToFileTest extends Con
             public void configure() throws Exception {
                 from("direct:start")
                     .split(xpath("/foo/person"))
+                        .log("${bodyAs(String)}")
                         .to("file://target/xpath?fileName=xpath-${property.CamelSplitIndex}.xml")
                         .to("mock:result");
             }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java?rev=943161&r1=943160&r2=943161&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java
Tue May 11 16:20:23 2010
@@ -33,28 +33,28 @@ public class DomConverterTest extends Co
     public void testDomConverterToString() throws Exception {
         Document document = context.getTypeConverter().convertTo(Document.class, "<?xml
version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>");
 
-        String s = DomConverter.toString(document.getChildNodes());
-        assertEquals("world!", s);
+        String s = new DomConverter().toString(document.getChildNodes());
+        assertEquals("<hello>world!</hello>", s);
     }
 
     public void testDomConverterToBytes() throws Exception {
         Document document = context.getTypeConverter().convertTo(Document.class, "<?xml
version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>");
 
-        byte[] bytes = DomConverter.toByteArray(document.getChildNodes());
-        assertTrue("Should be equal", ObjectHelper.equalByteArray("world!".getBytes(), bytes));
+        byte[] bytes = new DomConverter().toByteArray(document.getChildNodes());
+        assertTrue("Should be equal", ObjectHelper.equalByteArray("<hello>world!</hello>".getBytes(),
bytes));
     }
 
     public void testDomConverterToInteger() throws Exception {
         Document document = context.getTypeConverter().convertTo(Document.class, "<?xml
version=\"1.0\" encoding=\"UTF-8\"?><hello>47</hello>");
 
-        Integer number = DomConverter.toInteger(document.getChildNodes());
+        Integer number = new DomConverter().toInteger(document.getChildNodes());
         assertEquals(47, number.intValue());
     }
 
     public void testDomConverterToLong() throws Exception {
         Document document = context.getTypeConverter().convertTo(Document.class, "<?xml
version=\"1.0\" encoding=\"UTF-8\"?><hello>47</hello>");
 
-        Long number = DomConverter.toLong(document.getChildNodes());
+        Long number = new DomConverter().toLong(document.getChildNodes());
         assertEquals(47L, number.longValue());
     }
 
@@ -69,15 +69,15 @@ public class DomConverterTest extends Co
         List sub = DomConverter.toList(nl);
         assertEquals(2, sub.size());
 
-        assertEquals("Hello World", DomConverter.toString((NodeList) sub.get(0)));
-        assertEquals("Bye Camel", DomConverter.toString((NodeList) sub.get(1)));
+        assertEquals("<hello>Hello World</hello>", new DomConverter().toString((NodeList)
sub.get(0)));
+        assertEquals("<bye>Bye Camel</bye>", new DomConverter().toString((NodeList)
sub.get(1)));
     }
 
     public void testDomConverterToInputStream() throws Exception {
         Document document = context.getTypeConverter().convertTo(Document.class, "<?xml
version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>");
 
-        InputStream is = DomConverter.toInputStream(document.getChildNodes());
-        assertEquals("world!", context.getTypeConverter().convertTo(String.class, is));
+        InputStream is = new DomConverter().toInputStream(document.getChildNodes());
+        assertEquals("<hello>world!</hello>", context.getTypeConverter().convertTo(String.class,
is));
     }
 
 }

Modified: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregratedJmsRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregratedJmsRouteTest.java?rev=943161&r1=943160&r2=943161&view=diff
==============================================================================
--- camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregratedJmsRouteTest.java
(original)
+++ camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregratedJmsRouteTest.java
Tue May 11 16:20:23 2010
@@ -24,8 +24,8 @@ import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.processor.BatchProcessor;
 import org.apache.camel.processor.aggregate.AggregationStrategy;
+import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -44,7 +44,7 @@ public class AggregratedJmsRouteTest ext
     @Test
     public void testJmsBatchTimeoutExpiryWithAggregrationDelay() throws Exception {
         MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
-        resultEndpoint.setSleepForEmptyTest(3 * BatchProcessor.DEFAULT_BATCH_TIMEOUT);
+        resultEndpoint.setSleepForEmptyTest(3000);
         resultEndpoint.expectedMessageCount(1);
         for (int i = 1; i <= 2; i++) {
             String body = "message:" + i;
@@ -84,10 +84,11 @@ public class AggregratedJmsRouteTest ext
         return new RouteBuilder() {
             public void configure() throws Exception {
                 from(timeOutEndpointUri).to("jms:queue:test.b");
+
                 from("jms:queue:test.b").aggregate(header("cheese"), new AggregationStrategy()
{
                     public Exchange aggregate(Exchange oldExchange, Exchange newExchange)
{
                         try {
-                            Thread.sleep(2 * BatchProcessor.DEFAULT_BATCH_TIMEOUT);
+                            Thread.sleep(2000);
                         } catch (InterruptedException e) {
                             LOG.error("aggregration delay sleep inturrepted", e);
                             fail("aggregration delay sleep inturrepted");
@@ -100,22 +101,8 @@ public class AggregratedJmsRouteTest ext
                 from("jms:queue:point1").process(new MyProcessor()).to("jms:queue:reply");
                 from("jms:queue:point2").process(new MyProcessor()).to("jms:queue:reply");
                 from("jms:queue:point3").process(new MyProcessor()).to("jms:queue:reply");
-                from("jms:queue:reply").aggregate(header("cheese"), new AggregationStrategy()
{
-                    public Exchange aggregate(Exchange oldExchange, Exchange newExchange)
{
-                        if (oldExchange == null) {
-                            return newExchange;
-                        }
-
-                        LOG.info("try to aggregating the message ");
-                        Integer old = oldExchange.getProperty("aggregated", Integer.class);
-                        if (old == null) {
-                            old = 1;
-                        }
-                        oldExchange.setProperty("aggregated", old + 1);
-                        return oldExchange;
-                    }
-                }).completionPredicate(header("aggregated").isEqualTo(3))
-                .to("mock:reply");
+                from("jms:queue:reply").aggregate(header("cheese"), new UseLatestAggregationStrategy()).completionSize(3)
+                    .to("mock:reply");
             }
         };
     }



Mime
View raw message