Author: davsclaus
Date: Sun Nov 28 10:51:59 2010
New Revision: 1039865

URL: http://svn.apache.org/viewvc?rev=1039865&view=rev
Log:
CAMEL-3161: Check that load balancer is not misconfigured, eg having configured 
it double.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DoubleLoadBalancerMisconfigurationTest.java
   (with props)
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDoubleLoadBalancerMisconfigurationTest.java
   (with props)
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/DoubleLoadBalancerMisconfigurationTest.xml
      - copied, changed from r1039840, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopafter.xml
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalancerDefinition.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java?rev=1039865&r1=1039864&r2=1039865&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
 Sun Nov 28 10:51:59 2010
@@ -105,6 +105,9 @@ public class LoadBalanceDefinition exten
     }
 
     public void setLoadBalancerType(LoadBalancerDefinition loadbalancer) {
+        if (loadBalancerType != null) {
+            throw new IllegalArgumentException("Loadbalancer already 
configured to: " + loadBalancerType + ". Cannot set it to: " + loadbalancer);
+        }
         loadBalancerType = loadbalancer;
     }
 
@@ -121,7 +124,11 @@ public class LoadBalanceDefinition exten
     @Override
     public Processor createProcessor(RouteContext routeContext) throws 
Exception {
         LoadBalancer loadBalancer = 
LoadBalancerDefinition.getLoadBalancer(routeContext, loadBalancerType, ref);
-        for (ProcessorDefinition<?> processorType : getOutputs()) {            
+        for (ProcessorDefinition<?> processorType : getOutputs()) {
+            // output must not be another load balancer
+            if (processorType instanceof LoadBalanceDefinition) {
+                throw new IllegalArgumentException("Loadbalancer already 
configured to: " + loadBalancerType + ". Cannot set it to: " + processorType);
+            }
             Processor processor = processorType.createProcessor(routeContext);
             processor = wrapProcessor(routeContext, processor);
             loadBalancer.addProcessor(processor);
@@ -139,7 +146,7 @@ public class LoadBalanceDefinition exten
      * @return the builder
      */
     public LoadBalanceDefinition loadBalance(LoadBalancer loadBalancer) {
-        loadBalancerType = new LoadBalancerDefinition(loadBalancer);
+        setLoadBalancerType(new LoadBalancerDefinition(loadBalancer));
         return this;
     }
     
@@ -182,7 +189,7 @@ public class LoadBalanceDefinition exten
         FailOverLoadBalancer failover = new 
FailOverLoadBalancer(Arrays.asList(exceptions));
         failover.setMaximumFailoverAttempts(maximumFailoverAttempts);
         failover.setRoundRobin(roundRobin);
-        loadBalancerType = new LoadBalancerDefinition(failover);
+        setLoadBalancerType(new LoadBalancerDefinition(failover));
         this.setInheritErrorHandler(inheritErrorHandler);
         return this;
     }
@@ -220,7 +227,7 @@ public class LoadBalanceDefinition exten
         } else {
             weighted = new 
WeightedRoundRobinLoadBalancer(distributionRatioList);
         }
-        loadBalancerType = new LoadBalancerDefinition(weighted);
+        setLoadBalancerType(new LoadBalancerDefinition(weighted));
         return this;
     }
     
@@ -230,7 +237,7 @@ public class LoadBalanceDefinition exten
      * @return the builder
      */
     public LoadBalanceDefinition roundRobin() {
-        loadBalancerType = new LoadBalancerDefinition(new 
RoundRobinLoadBalancer());
+        setLoadBalancerType(new LoadBalancerDefinition(new 
RoundRobinLoadBalancer()));
         return this;
     }
 
@@ -239,7 +246,7 @@ public class LoadBalanceDefinition exten
      * @return the builder
      */
     public LoadBalanceDefinition random() {
-        loadBalancerType = new LoadBalancerDefinition(new 
RandomLoadBalancer());
+        setLoadBalancerType(new LoadBalancerDefinition(new 
RandomLoadBalancer()));
         return this;
     }
 
@@ -250,7 +257,7 @@ public class LoadBalanceDefinition exten
      * @return  the builder
      */
     public LoadBalanceDefinition sticky(Expression correlationExpression) {
-        loadBalancerType = new LoadBalancerDefinition(new 
StickyLoadBalancer(correlationExpression));
+        setLoadBalancerType(new LoadBalancerDefinition(new 
StickyLoadBalancer(correlationExpression)));
         return this;
     }
 
@@ -260,7 +267,7 @@ public class LoadBalanceDefinition exten
      * @return the builder
      */
     public LoadBalanceDefinition topic() {
-        loadBalancerType = new LoadBalancerDefinition(new TopicLoadBalancer());
+        setLoadBalancerType(new LoadBalancerDefinition(new 
TopicLoadBalancer()));
         return this;
     }
 

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalancerDefinition.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalancerDefinition.java?rev=1039865&r1=1039864&r2=1039865&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalancerDefinition.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalancerDefinition.java
 Sun Nov 28 10:51:59 2010
@@ -144,4 +144,12 @@ public class LoadBalancerDefinition exte
         });
     }
 
+    @Override
+    public String toString() {
+        if (loadBalancer != null) {
+            return loadBalancer.toString();
+        } else {
+            return loadBalancerTypeName;
+        }
+    }
 }

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DoubleLoadBalancerMisconfigurationTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DoubleLoadBalancerMisconfigurationTest.java?rev=1039865&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DoubleLoadBalancerMisconfigurationTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DoubleLoadBalancerMisconfigurationTest.java
 Sun Nov 28 10:51:59 2010
@@ -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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version $Revision$
+ */
+public class DoubleLoadBalancerMisconfigurationTest extends ContextTestSupport 
{
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    public void testMisconfiguration() throws Exception {
+        try {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    from("direct:start")
+                        .loadBalance().failover().roundRobin()
+                        .to("mock:a", "mock:b");
+                }
+            });
+            fail("Should have thrown an exception");
+        } catch (IllegalArgumentException e) {
+            assertEquals("Loadbalancer already configured to: 
FailoverLoadBalancer[[]]. Cannot set it to: RoundRobinLoadBalancer", 
e.getMessage());
+        }
+    }
+
+    public void testMisconfiguration2() throws Exception {
+        try {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    from("direct:start")
+                        .loadBalance().failover().random()
+                        .to("mock:a", "mock:b");
+                }
+            });
+            fail("Should have thrown an exception");
+        } catch (IllegalArgumentException e) {
+            assertEquals("Loadbalancer already configured to: 
FailoverLoadBalancer[[]]. Cannot set it to: RandomLoadBalancer", 
e.getMessage());
+        }
+    }
+
+    public void testMisconfiguration3() throws Exception {
+        try {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    from("direct:start")
+                        .loadBalance().random().failover()
+                        .to("mock:a", "mock:b");
+                }
+            });
+            fail("Should have thrown an exception");
+        } catch (IllegalArgumentException e) {
+            assertEquals("Loadbalancer already configured to: 
RandomLoadBalancer. Cannot set it to: FailoverLoadBalancer[[]]", 
e.getMessage());
+        }
+    }
+}

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DoubleLoadBalancerMisconfigurationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DoubleLoadBalancerMisconfigurationTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDoubleLoadBalancerMisconfigurationTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDoubleLoadBalancerMisconfigurationTest.java?rev=1039865&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDoubleLoadBalancerMisconfigurationTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDoubleLoadBalancerMisconfigurationTest.java
 Sun Nov 28 10:51:59 2010
@@ -0,0 +1,49 @@
+/**
+ * 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.spring.processor;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.FailedToCreateRouteException;
+
+import static 
org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringDoubleLoadBalancerMisconfigurationTest extends 
ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        try {
+            super.setUp();
+            fail("Should have thrown exception");
+        } catch (Exception e) {
+            FailedToCreateRouteException fe = 
assertIsInstanceOf(FailedToCreateRouteException.class, e.getCause());
+            IllegalArgumentException ie = 
assertIsInstanceOf(IllegalArgumentException.class, fe.getCause());
+            assertTrue(ie.getMessage().startsWith("Loadbalancer already 
configured to: RandomLoadBalancer. Cannot set it to: 
LoadBalanceType[RoundRobinLoadBalancer"));
+        }
+    }
+
+    public void testDummy() {
+        // noop
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        return createSpringCamelContext(this, 
"org/apache/camel/spring/processor/DoubleLoadBalancerMisconfigurationTest.xml");
+    }
+}
\ No newline at end of file

Propchange: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDoubleLoadBalancerMisconfigurationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDoubleLoadBalancerMisconfigurationTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/DoubleLoadBalancerMisconfigurationTest.xml
 (from r1039840, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopafter.xml)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/DoubleLoadBalancerMisconfigurationTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/DoubleLoadBalancerMisconfigurationTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopafter.xml&r1=1039840&r2=1039865&rev=1039865&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopafter.xml
 (original)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/DoubleLoadBalancerMisconfigurationTest.xml
 Sun Nov 28 10:51:59 2010
@@ -22,16 +22,19 @@
        http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <!-- START SNIPPET: e1 -->
     <camelContext xmlns="http://camel.apache.org/schema/spring";>
         <route>
             <from uri="direct:start"/>
-            <aop afterUri="mock:after">
-                <transform><constant>Bye World</constant></transform>
-                <to uri="mock:result"/>
-            </aop>
+            <loadBalance>
+                <random/>
+                <!-- invalid configuration with nested load balancer -->
+                <loadBalance>
+                    <roundRobin/>
+                    <to uri="mock:a"/>
+                    <to uri="mock:b"/>
+                </loadBalance>
+            </loadBalance>
         </route>
     </camelContext>
-    <!-- END SNIPPET: e1 -->
 
 </beans>


Reply via email to