camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [06/13] camel git commit: Camel java dsl parser prototype
Date Sun, 08 Oct 2017 12:02:49 GMT
Camel java dsl parser prototype


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

Branch: refs/heads/parser2
Commit: e9d6f0bf843718b4099fd2af1a631ad6d5a3d11d
Parents: cb55f5a
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Oct 7 18:27:47 2017 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Oct 7 18:27:47 2017 +0200

----------------------------------------------------------------------
 .../apache/camel/parser/RouteBuilderParser.java |  9 ++-
 .../helper/CamelJavaTreeParserHelper.java       | 34 +++++----
 .../camel/parser/java/RoasterJavaDslTest.java   |  4 -
 .../java/RoasterJavaDslTwoRoutesTest.java       | 79 ++++++++++++++++++++
 .../parser/java/TwoRoutesRouteBuilder.java      | 33 ++++++++
 5 files changed, 138 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
index aa54cb3..f525a56 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
@@ -20,6 +20,7 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.camel.parser.helper.CamelJavaTreeParserHelper;
@@ -75,10 +76,12 @@ public final class RouteBuilderParser {
         CamelJavaTreeParserHelper parser = new CamelJavaTreeParserHelper();
         List<CamelNodeDetails> list = new ArrayList<>();
         for (MethodSource<JavaClassSource> configureMethod : methods) {
-            // TODO: list of details, on per route
-            CamelNodeDetails details = parser.parseCamelRoute(clazz, baseDir, fullyQualifiedFileName,
configureMethod);
-            list.add(details);
+            // there may be multiple route builder configure methods
+            List<CamelNodeDetails> details = parser.parseCamelRouteTree(clazz, baseDir,
fullyQualifiedFileName, configureMethod);
+            list.addAll(details);
         }
+        // we end up parsing bottom->up so reverse list
+        Collections.reverse(list);
 
         return list;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
index af19acd..2f81d80 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
@@ -19,13 +19,13 @@ package org.apache.camel.parser.helper;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.camel.catalog.CamelCatalog;
 import org.apache.camel.catalog.DefaultCamelCatalog;
 import org.apache.camel.catalog.JSonSchemaHelper;
-import org.apache.camel.parser.RouteBuilderParser;
 import org.apache.camel.parser.model.CamelNodeDetails;
 import org.apache.camel.parser.model.CamelNodeDetailsFactory;
 import org.apache.camel.parser.roaster.StatementFieldSource;
@@ -66,10 +66,8 @@ public final class CamelJavaTreeParserHelper {
 
     private CamelCatalog camelCatalog = new DefaultCamelCatalog(true);
 
-    // TODO: add support for multiple routes
-    // TODO: rename this method
-    public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, String baseDir, String
fullyQualifiedFileName,
-                                            MethodSource<JavaClassSource> method) {
+    public List<CamelNodeDetails> parseCamelRouteTree(JavaClassSource clazz, String
baseDir, String fullyQualifiedFileName,
+                                                      MethodSource<JavaClassSource>
method) {
 
         // find any from which is the start of the route
         CamelNodeDetailsFactory nodeFactory = CamelNodeDetailsFactory.newInstance();
@@ -91,24 +89,32 @@ public final class CamelJavaTreeParserHelper {
             }
         }
 
+        List<CamelNodeDetails> answer = new ArrayList<>();
+
+        if (route.getOutputs().isEmpty()) {
+            // okay no routes found
+            return answer;
+        }
+
         // now parse the route node and build the correct model/tree structure of the EIPs
 
         // re-create factory as we rebuild the tree
         nodeFactory = CamelNodeDetailsFactory.newInstance();
+        CamelNodeDetails parent = route.getOutputs().get(0);
 
-        CamelNodeDetails from = route.getOutputs().get(0);
-        CamelNodeDetails answer = nodeFactory.copyNode(null, "from", from);
-        answer.setFileName(fullyQualifiedFileName);
-
-        CamelNodeDetails parent = answer;
-        for (int i = 1; i < route.getOutputs().size(); i++) {
-
+        for (int i = 0; i < route.getOutputs().size(); i++) {
             CamelNodeDetails node = route.getOutputs().get(i);
             String name = node.getName();
 
             // TODO: use camel catalog to know about these types and when to do what
-            // special for some EIPs
-            if ("choice".equals(name)) {
+            // special for some EIPs such as choice
+
+            if ("from".equals(name)) {
+                CamelNodeDetails from = nodeFactory.copyNode(null, "from", node);
+                from.setFileName(fullyQualifiedFileName);
+                answer.add(from);
+                parent = from;
+            } else if ("choice".equals(name)) {
                 CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
                 parent.addOutput(output);
                 parent = output;

http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
index 9041e2f..6378f41 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
@@ -55,10 +55,6 @@ public class RoasterJavaDslTest extends CamelTestSupport {
         assertTrue(tree.contains("30\t      toD"));
         assertTrue(tree.contains("33\t    otherwise"));
         assertTrue(tree.contains("36\t  to"));
-
-        String name = details.getFileName();
-        System.out.println(name);
-        System.out.println(tree);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
new file mode 100644
index 0000000..6c80003
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.parser.java;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.camel.parser.RouteBuilderParser;
+import org.apache.camel.parser.model.CamelNodeDetails;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.jboss.forge.roaster.Roaster;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RoasterJavaDslTwoRoutesTest extends CamelTestSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RoasterJavaDslTwoRoutesTest.class);
+
+    @Override
+    public boolean isDumpRouteCoverage() {
+        return true;
+    }
+
+    @Test
+    public void parseTree() throws Exception {
+        JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java"));
+
+        List<CamelNodeDetails> list = RouteBuilderParser.parseRouteBuilderTree(clazz,
".",
+            "src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java",true);
+        assertEquals(2, list.size());
+
+        CamelNodeDetails details = list.get(0);
+        CamelNodeDetails details2 = list.get(1);
+        assertEquals("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java",
details.getFileName());
+        assertEquals("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java",
details2.getFileName());
+
+        String tree = details.dump(0);
+        LOG.info("\n" + tree);
+
+        String tree2 = details2.dump(0);
+        LOG.info("\n" + tree2);
+
+        assertTrue(tree.contains("25\tfrom"));
+        assertTrue(tree.contains("26\t  log"));
+        assertTrue(tree.contains("27\t  to"));
+
+        assertTrue(tree2.contains("29\tfrom"));
+        assertTrue(tree2.contains("30\t  transform"));
+        assertTrue(tree2.contains("31\t  to"));
+    }
+
+    @Test
+    public void testRouteCoverage() throws Exception {
+        context.addRoutes(new TwoRoutesRouteBuilder());
+
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+
+        template.sendBody("direct:foo", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java
new file mode 100644
index 0000000..55a8b08
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java
@@ -0,0 +1,33 @@
+/**
+ * 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.parser.java;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class TwoRoutesRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:foo").routeId("foo")
+            .log("I was here")
+            .to("mock:foo");
+
+        from("direct:bar").routeId("bar")
+            .transform(simple("Someone was here"))
+            .to("mock:bar");
+    }
+}


Mime
View raw message