camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hadr...@apache.org
Subject svn commit: r698682 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/processor/ camel-core/src/test/java/org/apache/camel/processor/ components/camel-spring/src/test/resources/org/apache/camel/spring/processor/
Date Wed, 24 Sep 2008 18:28:05 GMT
Author: hadrian
Date: Wed Sep 24 11:28:04 2008
New Revision: 698682

URL: http://svn.apache.org/viewvc?rev=698682&view=rev
Log:
CAMEL-325.  Add test for invalid expression and loop count/index properties.

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTestProcessor.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoopProcessor.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java
    activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/loop.xml

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoopProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoopProcessor.java?rev=698682&r1=698681&r2=698682&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoopProcessor.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoopProcessor.java
Wed Sep 24 11:28:04 2008
@@ -19,6 +19,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -29,6 +30,9 @@
  * @version $Revision: $
  */
 public class LoopProcessor extends DelegateProcessor {
+    public static final String PROP_ITER_COUNT = "CamelIterationCount";
+    public static final String PROP_ITER_INDEX = "CamelIterationIndex";
+    
     private static final Log LOG = LogFactory.getLog(LoopProcessor.class);
     
     private Expression<Exchange> expression;
@@ -44,9 +48,15 @@
         // but evaluation result is a textual representation of a numeric value.
         String text = ExchangeHelper.convertToType(exchange, String.class, expression.evaluate(exchange));
         Integer value = ExchangeHelper.convertToType(exchange, Integer.class, text);
-        int count = value != null ? value.intValue() : 0;
+        if (value == null) {
+        	// TODO: we should probably catch evaluate/convert exception an set is as fault
(after fix for CAMEL-316)
+        	throw new RuntimeCamelException("Expression \"" + expression + "\" does not evaluate
to an int.");
+        }
+        int count = value.intValue();
+        exchange.setProperty(PROP_ITER_COUNT, count);
         for (int i = 0; i < count; i++) {
             LOG.debug("LoopProcessor: iteration #" + i);
+            exchange.setProperty(PROP_ITER_INDEX, i);
             super.process(exchange);
         }
     }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java?rev=698682&r1=698681&r2=698682&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTest.java
Wed Sep 24 11:28:04 2008
@@ -17,6 +17,8 @@
 package org.apache.camel.processor;
 
 import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 
@@ -45,12 +47,29 @@
         lastEndpoint.assertIsSatisfied();
     }
 
-    private void performLoopTest(String endpointUri, int expectedIterations) throws InterruptedException
{
+    public void testLoopWithInvalidExpression() throws Exception {
+    	try {
+            performLoopTest("direct:b", 4, "invalid");
+            fail("Exception expected for invalid expression");
+    	} catch (RuntimeCamelException e) {
+    	    // expected
+        }
+    }
+
+    public void testLoopProperties() throws Exception {
+        performLoopTest("direct:e", 10);
+    }
+
+    private void performLoopTest(String endpointUri, int expectedIterations, String header)
throws InterruptedException {
         resultEndpoint.expectedMessageCount(expectedIterations);
-        template.sendBodyAndHeader(endpointUri, "<hello times='4'>world!</hello>",
"loop", "6");
+        template.sendBodyAndHeader(endpointUri, "<hello times='4'>world!</hello>",
"loop", header);
         resultEndpoint.assertIsSatisfied();
     }
 
+    private void performLoopTest(String endpointUri, int expectedIterations) throws InterruptedException
{
+        performLoopTest(endpointUri, expectedIterations, "6");
+    }
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -60,6 +79,8 @@
     }
 
     protected RouteBuilder createRouteBuilder() {
+        final Processor loopTest = new LoopTestProcessor(10);
+
         return new RouteBuilder() {
             public void configure() {
                 // START SNIPPET: ex
@@ -74,6 +95,9 @@
                 // START SNIPPET: ex4
                 from("direct:d").loop(2).to("mock:result").end().to("mock:last");
                 // END SNIPPET: ex4
+                // START SNIPPET: ex5
+                from("direct:e").loop(10).process(loopTest).to("mock:result");
+                // END SNIPPET: ex5
             }
         };
     }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTestProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTestProcessor.java?rev=698682&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTestProcessor.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/LoopTestProcessor.java
Wed Sep 24 11:28:04 2008
@@ -0,0 +1,55 @@
+/**
+ * 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.CamelException;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+public class LoopTestProcessor implements Processor {
+    private int count = 0;
+    private int index = 0;
+
+    public LoopTestProcessor() {
+    }
+
+    public LoopTestProcessor(int count) {
+        setCount(count);
+    }
+
+    public void setCount(int count) {
+        this.count = count;
+        reset();
+    }
+
+    public void reset() {
+        this.index = 0;
+    }
+
+    public void process(Exchange exchange) {
+    	Integer c = exchange.getProperty(LoopProcessor.PROP_ITER_COUNT, Integer.class);
+    	Integer i = exchange.getProperty(LoopProcessor.PROP_ITER_INDEX, Integer.class);
+    	if (c == null || c.intValue() != this.count) {
+    	    exchange.setException(new CamelException(
+                "Invalid count value.  Expected " + this.count + " but was " + c));
+        }
+        if (i == null || i.intValue() != this.index++) {
+            exchange.setException(new CamelException(
+                "Invalid index value.  Expected " + this.index + " but was " + i));
+        }
+    }
+}

Modified: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/loop.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/loop.xml?rev=698682&r1=698681&r2=698682&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/loop.xml
(original)
+++ activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/loop.xml
Wed Sep 24 11:28:04 2008
@@ -23,7 +23,7 @@
     ">
 
   <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
-    <!-- START SNIPPET: ex -->
+    <!-- START SNIPPET: ex1 -->
     <route>
       <from uri="direct:a"/>
       <loop>
@@ -31,7 +31,7 @@
         <to uri="mock:result"/>
       </loop>
     </route>
-    <!-- END SNIPPET: ex -->
+    <!-- END SNIPPET: ex1 -->
  
     <!-- START SNIPPET: ex2 -->
     <route>
@@ -62,6 +62,23 @@
       </loop>
       <to uri="mock:last"/>
     </route>
-    <!-- START SNIPPET: ex4 -->    
+    <!-- START SNIPPET: ex4 -->
+    
+    <!-- START SNIPPET: ex5 -->        
+    <route>
+      <from uri="direct:e"/>
+      <loop>
+        <constant>10</constant>
+        <process ref="myProcessor"/>
+        <to uri="mock:result"/>
+      </loop>
+    </route>
+    <!-- START SNIPPET: ex5 -->
+        
   </camelContext>
+  
+  <bean id="myProcessor" class="org.apache.camel.processor.LoopTestProcessor">
+    <constructor-arg index="0" value="10"/>
+  </bean>
+  
 </beans>



Mime
View raw message