You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/12/05 11:29:46 UTC

[03/11] camel git commit: CAMEL-10559: route parser for java and xml to parse source code. Donated from fabric8 project.

http://git-wip-us.apache.org/repos/asf/camel/blob/556d58bd/tooling/route-parser/src/test/java/org/apache/camel/parser/RoasterSplitTokenizeTest.java
----------------------------------------------------------------------
diff --git a/tooling/route-parser/src/test/java/org/apache/camel/parser/RoasterSplitTokenizeTest.java b/tooling/route-parser/src/test/java/org/apache/camel/parser/RoasterSplitTokenizeTest.java
new file mode 100644
index 0000000..c12a5e3
--- /dev/null
+++ b/tooling/route-parser/src/test/java/org/apache/camel/parser/RoasterSplitTokenizeTest.java
@@ -0,0 +1,71 @@
+/**
+ *  Copyright 2005-2015 Red Hat, Inc.
+ *
+ *  Red Hat 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.parser;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.parser.model.CamelEndpointDetails;
+import org.jboss.forge.roaster.Roaster;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+import org.jboss.forge.roaster.model.source.MethodSource;
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RoasterSplitTokenizeTest {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RoasterSplitTokenizeTest.class);
+
+    @Test
+    public void parse() throws Exception {
+        JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/SplitTokenizeTest.java"));
+        MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
+
+        List<CamelEndpointDetails> details = new ArrayList<>();
+        RouteBuilderParser.parseRouteBuilderEndpoints(clazz, "src/test/java", "org/apache/camel/parser/SplitTokenizeTest.java", details);
+        LOG.info("{}", details);
+
+        List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
+        for (ParserResult result : list) {
+            LOG.info("Consumer: " + result.getElement());
+        }
+        Assert.assertEquals("direct:a", list.get(0).getElement());
+        Assert.assertEquals("direct:b", list.get(1).getElement());
+        Assert.assertEquals("direct:c", list.get(2).getElement());
+        Assert.assertEquals("direct:d", list.get(3).getElement());
+        Assert.assertEquals("direct:e", list.get(4).getElement());
+        Assert.assertEquals("direct:f", list.get(5).getElement());
+
+        list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
+        for (ParserResult result : list) {
+            LOG.info("Producer: " + result.getElement());
+        }
+        Assert.assertEquals("mock:split", list.get(0).getElement());
+        Assert.assertEquals("mock:split", list.get(1).getElement());
+        Assert.assertEquals("mock:split", list.get(2).getElement());
+        Assert.assertEquals("mock:split", list.get(3).getElement());
+        Assert.assertEquals("mock:split", list.get(4).getElement());
+        Assert.assertEquals("mock:split", list.get(5).getElement());
+
+        Assert.assertEquals(12, details.size());
+        Assert.assertEquals("direct:a", details.get(0).getEndpointUri());
+        Assert.assertEquals("mock:split", details.get(11).getEndpointUri());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/556d58bd/tooling/route-parser/src/test/java/org/apache/camel/parser/SimpleProcessorTest.java
----------------------------------------------------------------------
diff --git a/tooling/route-parser/src/test/java/org/apache/camel/parser/SimpleProcessorTest.java b/tooling/route-parser/src/test/java/org/apache/camel/parser/SimpleProcessorTest.java
new file mode 100644
index 0000000..3c5f7b3
--- /dev/null
+++ b/tooling/route-parser/src/test/java/org/apache/camel/parser/SimpleProcessorTest.java
@@ -0,0 +1,44 @@
+/**
+ *  Copyright 2005-2015 Red Hat, Inc.
+ *
+ *  Red Hat 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.parser;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+
+public class SimpleProcessorTest extends CamelTestSupport {
+
+    public void testProcess() throws Exception {
+        String out = template.requestBody("direct:start", "Hello World", String.class);
+        assertEquals("Bye World", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        exchange.getOut().setBody("Bye World");
+                    }
+                });
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/556d58bd/tooling/route-parser/src/test/java/org/apache/camel/parser/SplitTokenizeTest.java
----------------------------------------------------------------------
diff --git a/tooling/route-parser/src/test/java/org/apache/camel/parser/SplitTokenizeTest.java b/tooling/route-parser/src/test/java/org/apache/camel/parser/SplitTokenizeTest.java
new file mode 100644
index 0000000..251e531
--- /dev/null
+++ b/tooling/route-parser/src/test/java/org/apache/camel/parser/SplitTokenizeTest.java
@@ -0,0 +1,125 @@
+/**
+ *  Copyright 2005-2015 Red Hat, Inc.
+ *
+ *  Red Hat 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.parser;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+
+public class SplitTokenizeTest extends CamelTestSupport {
+
+    public void testSplitTokenizerA() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        mock.expectedBodiesReceived("Claus", "James", "Willem");
+
+        template.sendBody("direct:a", "Claus,James,Willem");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testSplitTokenizerB() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        mock.expectedBodiesReceived("Claus", "James", "Willem");
+
+        template.sendBodyAndHeader("direct:b", "Hello World", "myHeader", "Claus,James,Willem");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testSplitTokenizerC() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        mock.expectedBodiesReceived("Claus", "James", "Willem");
+
+        template.sendBody("direct:c", "Claus James Willem");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testSplitTokenizerD() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        mock.expectedBodiesReceived("[Claus]", "[James]", "[Willem]");
+
+        template.sendBody("direct:d", "[Claus][James][Willem]");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testSplitTokenizerE() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        mock.expectedBodiesReceived("<person>Claus</person>", "<person>James</person>", "<person>Willem</person>");
+
+        String xml = "<persons><person>Claus</person><person>James</person><person>Willem</person></persons>";
+        template.sendBody("direct:e", xml);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testSplitTokenizerEWithSlash() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        String xml = "<persons><person attr='/' /></persons>";
+        mock.expectedBodiesReceived("<person attr='/' />");
+        template.sendBody("direct:e", xml);
+        mock.assertIsSatisfied();
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testSplitTokenizerF() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:split");
+        mock.expectedBodiesReceived("<person name=\"Claus\"/>", "<person>James</person>", "<person>Willem</person>");
+
+        String xml = "<persons><person/><person name=\"Claus\"/><person>James</person><person>Willem</person></persons>";
+        template.sendBody("direct:f", xml);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+
+                from("direct:a")
+                        .split().tokenize(",")
+                        .to("mock:split");
+
+                from("direct:b")
+                        .split().tokenize(",", "myHeader")
+                        .to("mock:split");
+
+                from("direct:c")
+                        .split().tokenize("(\\W+)\\s*", null, true)
+                        .to("mock:split");
+
+                from("direct:d")
+                        .split().tokenizePair("[", "]", true)
+                        .to("mock:split");
+
+                from("direct:e")
+                        .split().tokenizeXML("person")
+                        .to("mock:split");
+
+                from("direct:f")
+                        .split().xpath("//person")
+                        // To test the body is not empty
+                        // it will call the ObjectHelper.evaluateValuePredicate()
+                        .filter().simple("${body}")
+                        .to("mock:split");
+
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/556d58bd/tooling/route-parser/src/test/resources/log4j2.properties
----------------------------------------------------------------------
diff --git a/tooling/route-parser/src/test/resources/log4j2.properties b/tooling/route-parser/src/test/resources/log4j2.properties
new file mode 100644
index 0000000..2f66eb5
--- /dev/null
+++ b/tooling/route-parser/src/test/resources/log4j2.properties
@@ -0,0 +1,30 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+appender.file.type = File
+appender.file.name = file
+appender.file.fileName = target/route-parser-test.log
+appender.file.layout.type = PatternLayout
+appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+appender.out.type = Console
+appender.out.name = out
+appender.out.layout.type = PatternLayout
+appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+logger.parser.name = org.apache.camel.parser
+logger.parser.level = DEBUG
+rootLogger.level = INFO
+rootLogger.appenderRef.file.ref = file