camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r791092 - /camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceJavaDSLBuilderTest.java
Date Sat, 04 Jul 2009 10:04:21 GMT
Author: davsclaus
Date: Sat Jul  4 10:04:21 2009
New Revision: 791092

URL: http://svn.apache.org/viewvc?rev=791092&view=rev
Log:
A crude Java DSL emitter based on two different strategies to traverse the route path

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceJavaDSLBuilderTest.java
      - copied, changed from r791059, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceJavaDSLBuilderTest.java
(from r791059, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceJavaDSLBuilderTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceJavaDSLBuilderTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java&r1=791059&r2=791092&rev=791092&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceJavaDSLBuilderTest.java
Sat Jul  4 10:04:21 2009
@@ -16,44 +16,99 @@
  */
 package org.apache.camel.processor;
 
-import org.apache.camel.ContextTestSupport;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
-
-public class RandomLoadBalanceTest extends ContextTestSupport {
-    protected MockEndpoint x;
-    protected MockEndpoint y;
-    protected MockEndpoint z;
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        x = getMockEndpoint("mock:x");
-        y = getMockEndpoint("mock:y");
-        z = getMockEndpoint("mock:z");
+import java.util.List;
+
+import org.apache.camel.Channel;
+import org.apache.camel.Navigate;
+import org.apache.camel.Processor;
+import org.apache.camel.Route;
+import org.apache.camel.model.LoadBalanceDefinition;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.model.SendDefinition;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.processor.loadbalancer.LoadBalancer;
+import org.apache.camel.processor.loadbalancer.RandomLoadBalancer;
+
+/**
+ * A crude unit test to navigate the route and build a Java DSL from the route definition
+ */
+public class RandomLoadBalanceJavaDSLBuilderTest extends RandomLoadBalanceTest {
+
+    public void testNavigateRouteAsJavaDSLWithNavigate() throws Exception {
+        // this one navigate using the runtime route using the Navigate<Processor>
+
+        StringBuilder sb = new StringBuilder();
+
+        Route route = context.getRoutes().get(0);
+
+        // the start of the route
+        sb.append("from(\"" + route.getEndpoint().getEndpointUri() + "\")");
+
+        // navigate the route and add Java DSL to the sb
+        Navigate<Processor> nav = route.navigate();
+        navigateRoute(nav, sb);
+
+        // output the Java DSL
+        assertEquals("from(\"direct:start\").loadBalance().random().to(\"mock:x\").to(\"mock:y\").to(\"mock:z\")",
sb.toString());
     }
 
-    protected RouteBuilder createRouteBuilder() {
-        return new RouteBuilder() {
-            public void configure() {
-                from("direct:start").loadBalance().
-                random().to("mock:x", "mock:y", "mock:z");
+    public void testNavigateRouteAsJavaDSL() throws Exception {
+        // this one navigate using the route definiton
+
+        StringBuilder sb = new StringBuilder();
+
+        RouteDefinition route = context.getRouteDefinitions().get(0);
+
+        // the start of the route
+        sb.append("from(\"" + route.getInputs().get(0).getEndpoint().getEndpointUri() + "\")");
+
+        // navigate the route and add Java DSL to the sb
+        navigateDefinition(route, sb);
+
+        // output the Java DSL
+        assertEquals("from(\"direct:start\").loadBalance().random().to(\"mock:x\").to(\"mock:y\").to(\"mock:z\")",
sb.toString());
+    }
+
+    private void navigateRoute(Navigate<Processor> nav, StringBuilder sb) {
+        if (!nav.hasNext()) {
+            return;
+        }
+
+        for (Processor child : nav.next()) {
+
+            if (child instanceof Channel) {
+                Channel channel = (Channel) child;
+                ProcessorDefinition def = channel.getProcessorDefinition();
+                navigateDefinition(def, sb);
             }
-        };
+        }
     }
 
-    public void testRandom() throws Exception {
-        // it should be safe to assume that they should at least each get > 5 messages
-        x.expectedMinimumMessageCount(5);
-        y.expectedMinimumMessageCount(5);
-        z.expectedMinimumMessageCount(5);
+    private void navigateDefinition(ProcessorDefinition def, StringBuilder sb) {
+
+        if (def instanceof LoadBalanceDefinition) {
+            sb.append(".loadBalance()");
 
-        for (int i = 0; i < 100; i++) {
-            template.sendBody("direct:start", "Hello World");
+            LoadBalanceDefinition lbd = (LoadBalanceDefinition) def;
+            LoadBalancer balancer = lbd.getLoadBalancerType().getLoadBalancer(null);
+            if (balancer instanceof RandomLoadBalancer) {
+                sb.append(".random()");
+            }
+        }
+
+        if (def instanceof SendDefinition) {
+            SendDefinition send = (SendDefinition) def;
+            sb.append(".to(\"" + send.getEndpoint().getEndpointUri() + "\")");
         }
 
-        assertMockEndpointsSatisfied();
+        List<ProcessorDefinition> children = def.getOutputs();
+        if (children == null || children.isEmpty()) {
+            return;
+        }
+
+        for (ProcessorDefinition child : children) {
+            navigateDefinition(child, sb);
+        }
     }
 
 }
\ No newline at end of file



Mime
View raw message