activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r519941 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/builder/ test/java/org/apache/camel/
Date Mon, 19 Mar 2007 13:18:38 GMT
Author: jstrachan
Date: Mon Mar 19 06:18:37 2007
New Revision: 519941

URL: http://svn.apache.org/viewvc?view=rev&rev=519941
Log:
tidied up the code and added test case for ChoiceProcessor

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ChoiceProcessor.java  
(with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CompositeProcessor.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Processors.java   (with
props)
Removed:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/OldProcessorBuilder.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/SendProcessor.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoiceBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoicePredicateBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ChoiceProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ChoiceProcessor.java?view=auto&rev=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ChoiceProcessor.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ChoiceProcessor.java Mon
Mar 19 06:18:37 2007
@@ -0,0 +1,82 @@
+/**
+ *
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Implements a Choice structure where one or more predicates are used which if they are
true their processors
+ * are used, with a default otherwise clause used if none match.
+ *
+ * @version $Revision$
+ */
+public class ChoiceProcessor<E> implements Processor<E> {
+    private List<FilterProcessor<E>> filters = new ArrayList<FilterProcessor<E>>();
+    private Processor<E> otherwise;
+
+    public ChoiceProcessor(List<FilterProcessor<E>> filters, Processor<E>
otherwise) {
+        this.filters = filters;
+        this.otherwise = otherwise;
+    }
+
+    public void onExchange(E exchange) {
+        for (FilterProcessor<E> filterProcessor : filters) {
+            Predicate<E> predicate = filterProcessor.getPredicate();
+            if (predicate != null) {
+                filterProcessor.getProcessor().onExchange(exchange);
+                return;
+            }
+        }
+        if (otherwise != null) {
+            otherwise.onExchange(exchange);
+        }
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder builder = new StringBuilder("choice{");
+        boolean first = true;
+        for (FilterProcessor<E> processor : filters) {
+            if (first) {
+                first = false;
+            }
+            else {
+                builder.append(", ");
+            }
+            builder.append("when ");
+            builder.append(processor.getPredicate().toString());
+            builder.append(": ");
+            builder.append(processor.getProcessor());
+        }
+        if (otherwise != null) {
+            builder.append(", otherwise: ");
+            builder.append(otherwise);
+        }
+        builder.append("}");
+        return builder.toString();
+    }
+
+    public List<FilterProcessor<E>> getFilters() {
+        return filters;
+    }
+
+    public Processor<E> getOtherwise() {
+        return otherwise;
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ChoiceProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ChoiceProcessor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ChoiceProcessor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CompositeProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CompositeProcessor.java?view=auto&rev=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CompositeProcessor.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CompositeProcessor.java
Mon Mar 19 06:18:37 2007
@@ -0,0 +1,56 @@
+/**
+ *
+ * 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;
+
+import java.util.Collection;
+
+/**
+ * Represents a composite pattern, aggregating a collection of processors together as a single
processor
+ *
+ * @version $Revision$
+ */
+public class CompositeProcessor<E> implements Processor<E> {
+    private final Collection<Processor<E>> processors;
+
+    public CompositeProcessor(Collection<Processor<E>> processors) {
+        this.processors = processors;
+    }
+
+    public void onExchange(E exchange) {
+        for (Processor<E> processor : processors) {
+            processor.onExchange(exchange);
+        }
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder builder = new StringBuilder("[ ");
+        boolean first = true;
+        for (Processor<E> processor : processors) {
+            if (first) {
+                first = false;
+            }
+            else {
+                builder.append(", ");
+            }
+            builder.append(processor.toString());
+        }
+        builder.append(" ]");
+        return builder.toString();
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CompositeProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CompositeProcessor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CompositeProcessor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Processors.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Processors.java?view=auto&rev=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Processors.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Processors.java Mon Mar
19 06:18:37 2007
@@ -0,0 +1,25 @@
+/**
+ *
+ * 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;
+
+/**
+ * @version $Revision$
+ */
+public class Processors {
+    
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Processors.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Processors.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Processors.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/SendProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/SendProcessor.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/SendProcessor.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/SendProcessor.java Mon
Mar 19 06:18:37 2007
@@ -37,6 +37,6 @@
 
     @Override
     public String toString() {
-        return "sendTo (" + destination + ")";
+        return "sendTo(" + destination + ")";
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoiceBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoiceBuilder.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoiceBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoiceBuilder.java
Mon Mar 19 06:18:37 2007
@@ -18,6 +18,9 @@
 
 import org.apache.camel.Predicate;
 import org.apache.camel.Exchange;
+import org.apache.camel.ChoiceProcessor;
+import org.apache.camel.FilterProcessor;
+import org.apache.camel.Processor;
 
 import java.util.List;
 import java.util.ArrayList;
@@ -29,7 +32,7 @@
 
     private final DestinationBuilder<E> parent;
     private List<ChoicePredicateBuilder<E>> predicateBuilders = new ArrayList<ChoicePredicateBuilder<E>>();
-    private DestinationBuilder otherwise;
+    private DestinationBuilder<E> otherwise;
 
     public ChoiceBuilder(DestinationBuilder<E> parent) {
         super(parent);
@@ -38,6 +41,8 @@
 
     /**
      * Adds a predicate which if it is true then the message exchange is sent to the given
destination
+     *
+     * @return a builder for creating a when predicate clause and action
      */
     public ChoicePredicateBuilder<E> when(Predicate<E> predicate) {
         ChoicePredicateBuilder<E> answer = new ChoicePredicateBuilder<E>(this,
predicate);
@@ -46,15 +51,28 @@
     }
 
     public DestinationBuilder<E> otherwise() {
-        this.otherwise = new DestinationBuilder(parent);
-        return parent;
+        this.otherwise = new DestinationBuilder<E>(parent);
+        return otherwise;
     }
 
     public List<ChoicePredicateBuilder<E>> getPredicateBuilders() {
         return predicateBuilders;
     }
 
-    public DestinationBuilder getOtherwise() {
+    public DestinationBuilder<E> getOtherwise() {
         return otherwise;
+    }
+
+    @Override
+    public Processor<E> createProcessor() {
+        List<FilterProcessor<E>> filters = new ArrayList<FilterProcessor<E>>();
+        for (ChoicePredicateBuilder<E> predicateBuilder : predicateBuilders) {
+            filters.add(predicateBuilder.createProcessor());
+        }
+        Processor<E> otherwiseProcessor = null;
+        if (otherwise != null) {
+            otherwiseProcessor = otherwise.createProcessor();
+        }
+        return new ChoiceProcessor<E>(filters, otherwiseProcessor);
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoicePredicateBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoicePredicateBuilder.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoicePredicateBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ChoicePredicateBuilder.java
Mon Mar 19 06:18:37 2007
@@ -43,4 +43,6 @@
         super.to(uri);
         return parent;
     }
+
+    
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java
Mon Mar 19 06:18:37 2007
@@ -34,6 +34,11 @@
     }
 
     @Override
+    public Processor<E> createProcessor() {
+        return new SendProcessor<E>(destination);
+    }
+
+    @Override
     public void createProcessors() {
         getParent().addProcessor(new SendProcessor<E>(destination));
     }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
Mon Mar 19 06:18:37 2007
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.builder;
 
+import org.apache.camel.CompositeProcessor;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Predicate;
@@ -23,7 +24,6 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
  * @version $Revision$
@@ -35,7 +35,7 @@
     private List<Processor<E>> processors = new ArrayList<Processor<E>>();
     private List<ProcessorBuilder<E>> processBuilders = new ArrayList<ProcessorBuilder<E>>();
 
-    public DestinationBuilder(RouteBuilder builder, Endpoint<E> from) {
+    public DestinationBuilder(RouteBuilder<E> builder, Endpoint<E> from) {
         this.builder = builder;
         this.from = from;
         this.parent = this;
@@ -70,23 +70,31 @@
      */
     public ProcessorBuilder<E> to(Endpoint<E> endpoint) {
         ConfiguredDestinationBuilder<E> answer = new ConfiguredDestinationBuilder<E>(this,
endpoint);
-        parent.addProcessBuilder(answer);
+        addProcessBuilder(answer);
         return answer;
     }
 
     /**
      * Creates a predicate which is applied and only if it is true then
      * the exchange is forwarded to the destination
+     *
+     * @return the builder for a predicate
      */
-    public PredicateBuilder<E> filter(Predicate predicate) {
-        return new PredicateBuilder<E>(this, predicate);
+    public PredicateBuilder<E> filter(Predicate<E> predicate) {
+        PredicateBuilder<E> answer = new PredicateBuilder<E>(this, predicate);
+        addProcessBuilder(answer);
+        return answer;
     }
 
     /**
      * Creates a choice of one or more predicates with an otherwise clause
+     *
+     * @return the builder for a choice expression
      */
     public ChoiceBuilder<E> choice() {
-        return new ChoiceBuilder<E>(this);
+        ChoiceBuilder<E> answer = new ChoiceBuilder<E>(this);
+        addProcessBuilder(answer);
+        return answer;
     }
 
     public RouteBuilder<E> getBuilder() {
@@ -101,12 +109,29 @@
         processBuilders.add(processBuilder);
     }
 
-    public Processor<E> createProcessor() {
-        throw new UndefinedDestinationException();
-    }
-
     public void addProcessor(Processor<E> processor) {
         processors.add(processor);
+    }
+
+    public Processor<E> createProcessor() {
+        List<Processor<E>> answer = new ArrayList<Processor<E>>();
+
+        for (ProcessorBuilder<E> processBuilder : processBuilders) {
+            Processor<E> processor = processBuilder.createProcessor();
+            if (processor == null) {
+                throw new IllegalArgumentException("No processor created for processBuilder:
" + processBuilder);
+            }
+            answer.add(processor);
+        }
+        if (answer.size() == 0) {
+            return null;
+        }
+        if (answer.size() == 1) {
+            return answer.get(0);
+        }
+        else {
+            return new CompositeProcessor<E>(answer);
+        }
     }
 
     public void createProcessors() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
Mon Mar 19 06:18:37 2007
@@ -53,7 +53,13 @@
         return predicate;
     }
 
+    public FilterProcessor<E> createProcessor() {
+        // lets create a single processor for all child predicates
+        Processor<E> childProcessor = super.createProcessor();
+        return new FilterProcessor<E>(predicate, childProcessor);
+    }
+
     public void addProcessor(Processor<E> processor) {
-        getParent().addProcessor(new FilterProcessor(predicate, processor));
+        getParent().addProcessor(new FilterProcessor<E>(predicate, processor));
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
Mon Mar 19 06:18:37 2007
@@ -17,10 +17,14 @@
 package org.apache.camel.builder;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
 
 /**
  * @version $Revision$
  */
 public interface ProcessorBuilder<E extends Exchange> {
     public void createProcessors();
+
+    public Processor<E> createProcessor();
+
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
Mon Mar 19 06:18:37 2007
@@ -39,7 +39,7 @@
     private EndpointResolver<E> endpointResolver;
     private List<DestinationBuilder<E>> destinationBuilders = new ArrayList<DestinationBuilder<E>>();
     private AtomicBoolean initalized = new AtomicBoolean(false);
-    private Map<Endpoint<E>, List<Processor<E>>> routeMap = new HashMap<Endpoint<E>,
List<Processor<E>>>();
+    private Map<Endpoint<E>, Processor<E>> routeMap = new HashMap<Endpoint<E>,
Processor<E>>();
 
     /**
      * Called on initialisation to to build the required destinationBuilders
@@ -84,7 +84,7 @@
     /**
      * Returns the routing map from inbound endpoints to processors
      */
-    public Map<Endpoint<E>, List<Processor<E>>> getRouteMap() {
+    public Map<Endpoint<E>, Processor<E>> getRouteMap() {
         checkInitialized();
         return routeMap;
     }
@@ -121,12 +121,14 @@
         }
     }
 
-    protected void populateRouteMap(Map<Endpoint<E>, List<Processor<E>>>
routeMap) {
+    protected void populateRouteMap(Map<Endpoint<E>, Processor<E>> routeMap)
{
         for (DestinationBuilder<E> destinationBuilder : destinationBuilders) {
             Endpoint<E> from = destinationBuilder.getFrom();
-            destinationBuilder.createProcessors();
-            List<Processor<E>> processors = destinationBuilder.getProcessors();
-            routeMap.put(from, processors);
+            Processor<E> processor = destinationBuilder.createProcessor();
+            if (processor == null) {
+                throw new IllegalArgumentException("No processor created for DestinationBuilder:
" + destinationBuilder);
+            }
+            routeMap.put(from, processor);
         }
     }
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java?view=diff&rev=519941&r1=519940&r2=519941
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java Mon
Mar 19 06:18:37 2007
@@ -17,7 +17,6 @@
 package org.apache.camel;
 
 import junit.framework.TestCase;
-import org.apache.camel.builder.DestinationBuilder;
 import org.apache.camel.builder.RouteBuilder;
 
 import java.util.List;
@@ -28,7 +27,6 @@
  * @version $Revision$
  */
 public class RouteBuilderTest extends TestCase {
-
     public void testSimpleRoute() throws Exception {
         RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() {
             public void configure() {
@@ -36,16 +34,13 @@
             }
         };
 
-        Map<Endpoint<Exchange>, List<Processor<Exchange>>> routeMap
= builder.getRouteMap();
-        Set<Map.Entry<Endpoint<Exchange>, List<Processor<Exchange>>>>
routes = routeMap.entrySet();
+        Map<Endpoint<Exchange>, Processor<Exchange>> routeMap = builder.getRouteMap();
+        Set<Map.Entry<Endpoint<Exchange>, Processor<Exchange>>> routes
= routeMap.entrySet();
         assertEquals("Number routes created", 1, routes.size());
-        for (Map.Entry<Endpoint<Exchange>, List<Processor<Exchange>>>
route : routes) {
+        for (Map.Entry<Endpoint<Exchange>, Processor<Exchange>> route :
routes) {
             Endpoint<Exchange> key = route.getKey();
             assertEquals("From endpoint", "seda://a", key.getEndpointUri());
-            List<Processor<Exchange>> processors = route.getValue();
-
-            assertEquals("Number of processors", 1, processors.size());
-            Processor processor = processors.get(0);
+            Processor processor = route.getValue();
 
             assertTrue("Processor should be a SendProcessor but was: " + processor + " with
type: " + processor.getClass().getName(), processor instanceof SendProcessor);
             SendProcessor sendProcessor = (SendProcessor) processor;
@@ -53,37 +48,33 @@
         }
     }
 
-
     public void testSimpleRouteWithHeaderPredicate() throws Exception {
-        RouteBuilder builder = new RouteBuilder() {
+        RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() {
             public void configure() {
                 from("seda://a").filter(headerEquals("foo", "bar")).to("seda://b");
             }
         };
 
-        Map<Endpoint<Exchange>, List<Processor<Exchange>>> routeMap
= builder.getRouteMap();
-        Set<Map.Entry<Endpoint<Exchange>, List<Processor<Exchange>>>>
routes = routeMap.entrySet();
+        Map<Endpoint<Exchange>, Processor<Exchange>> routeMap = builder.getRouteMap();
+        System.out.println("Created map: " + routeMap);
+
+        Set<Map.Entry<Endpoint<Exchange>, Processor<Exchange>>> routes
= routeMap.entrySet();
         assertEquals("Number routes created", 1, routes.size());
-        for (Map.Entry<Endpoint<Exchange>, List<Processor<Exchange>>>
route : routes) {
+        for (Map.Entry<Endpoint<Exchange>, Processor<Exchange>> route :
routes) {
             Endpoint<Exchange> key = route.getKey();
             assertEquals("From endpoint", "seda://a", key.getEndpointUri());
-            List<Processor<Exchange>> processors = route.getValue();
-
-            assertEquals("Number of processors", 1, processors.size());
-            Processor processor = processors.get(0);
+            Processor processor = route.getValue();
 
             assertTrue("Processor should be a FilterProcessor but was: " + processor + "
with type: " + processor.getClass().getName(), processor instanceof FilterProcessor);
             FilterProcessor filterProcessor = (FilterProcessor) processor;
-        
+
             SendProcessor sendProcessor = (SendProcessor) filterProcessor.getProcessor();
             assertEquals("Endpoint URI", "seda://b", sendProcessor.getDestination().getEndpointUri());
         }
-
-        System.out.println("Created map: " + routeMap);
     }
 
     public void testSimpleRouteWithChoice() throws Exception {
-        RouteBuilder builder = new RouteBuilder() {
+        RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() {
             public void configure() {
                 from("seda://a").choice()
                         .when(headerEquals("foo", "bar")).to("seda://b")
@@ -91,5 +82,40 @@
                         .otherwise().to("seda://d");
             }
         };
+
+        Map<Endpoint<Exchange>, Processor<Exchange>> routeMap = builder.getRouteMap();
+        System.out.println("Created map: " + routeMap);
+
+        Set<Map.Entry<Endpoint<Exchange>, Processor<Exchange>>> routes
= routeMap.entrySet();
+        assertEquals("Number routes created", 1, routes.size());
+        for (Map.Entry<Endpoint<Exchange>, Processor<Exchange>> route :
routes) {
+            Endpoint<Exchange> key = route.getKey();
+            assertEquals("From endpoint", "seda://a", key.getEndpointUri());
+            Processor processor = route.getValue();
+
+            assertTrue("Processor should be a ChoiceProcessor but was: " + processor + "
with type: " + processor.getClass().getName(), processor instanceof ChoiceProcessor);
+            ChoiceProcessor<Exchange> choiceProcessor = (ChoiceProcessor<Exchange>)
processor;
+
+            List<FilterProcessor<Exchange>> filters = choiceProcessor.getFilters();
+            assertEquals("Should be two when clauses", 2, filters.size());
+
+            FilterProcessor<Exchange> filter1 = filters.get(0);
+            assertSendTo(filter1.getProcessor(), "seda://b");
+
+            FilterProcessor<Exchange> filter2 = filters.get(1);
+            assertSendTo(filter2.getProcessor(), "seda://c");
+
+
+            assertSendTo(choiceProcessor.getOtherwise(), "seda://d");
+        }
+
+    }
+
+
+    protected void assertSendTo(Processor processor, String uri) {
+        assertTrue("Processor should be a SendProcessor but was: " + processor + " with type:
" + processor.getClass().getName(), processor instanceof SendProcessor);
+
+        SendProcessor sendProcessor = (SendProcessor) processor;
+        assertEquals("Endpoint URI", uri, sendProcessor.getDestination().getEndpointUri());
     }
 }



Mime
View raw message