You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/06/02 19:39:22 UTC

svn commit: r950686 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/ main/java/org/apache/camel/processor/ main/java/org/apache/camel/spi/ main/java/org/apache/camel/util/ test/java/org/apache/camel/processor/ test/java/org/apache/cam...

Author: davsclaus
Date: Wed Jun  2 17:39:21 2010
New Revision: 950686

URL: http://svn.apache.org/viewvc?rev=950686&view=rev
Log:
CAMEL-2769: OnCompletion is executed last when Exchange is done.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/Ordered.java   (with props)
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionShouldBeLastTest.java
      - copied, changed from r950541, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionGlobalTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SynchronizationAdapter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/SynchronizationVetoable.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java?rev=950686&r1=950685&r2=950686&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java Wed Jun  2 17:39:21 2010
@@ -33,6 +33,7 @@ import org.apache.camel.spi.Synchronizat
 import org.apache.camel.spi.TracedRouteNodes;
 import org.apache.camel.spi.UnitOfWork;
 import org.apache.camel.util.EventHelper;
+import org.apache.camel.util.OrderedComparator;
 import org.apache.camel.util.UuidGenerator;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -143,6 +144,7 @@ public class DefaultUnitOfWork implement
         }
     }
 
+    @SuppressWarnings("unchecked")
     public void done(Exchange exchange) {
         boolean failed = exchange.isFailed();
 
@@ -161,6 +163,8 @@ public class DefaultUnitOfWork implement
         if (synchronizations != null && !synchronizations.isEmpty()) {
             // reverse so we invoke it FILO style instead of FIFO
             Collections.reverse(synchronizations);
+            // and honor if any was ordered by sorting it accordingly
+            Collections.sort(synchronizations, new OrderedComparator());
             // invoke synchronization callbacks
             for (Synchronization synchronization : synchronizations) {
                 try {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SynchronizationAdapter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SynchronizationAdapter.java?rev=950686&r1=950685&r2=950686&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SynchronizationAdapter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SynchronizationAdapter.java Wed Jun  2 17:39:21 2010
@@ -19,6 +19,7 @@ package org.apache.camel.impl;
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.Synchronization;
 import org.apache.camel.spi.SynchronizationVetoable;
+import org.apache.camel.util.Ordered;
 
 /**
  * Simple {@link Synchronization} adapter with empty methods for easier overriding
@@ -26,7 +27,7 @@ import org.apache.camel.spi.Synchronizat
  *
  * @version $Revision$
  */
-public class SynchronizationAdapter implements SynchronizationVetoable {
+public class SynchronizationAdapter implements SynchronizationVetoable, Ordered {
 
     public void onComplete(Exchange exchange) {
         onDone(exchange);
@@ -45,4 +46,8 @@ public class SynchronizationAdapter impl
         return true;
     }
 
+    public int getOrder() {
+        // no particular order by default
+        return 0;
+    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java?rev=950686&r1=950685&r2=950686&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java Wed Jun  2 17:39:21 2010
@@ -28,6 +28,7 @@ import org.apache.camel.Processor;
 import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.impl.SynchronizationAdapter;
 import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.Ordered;
 import org.apache.camel.util.ServiceHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -81,70 +82,7 @@ public class OnCompletionProcessor exten
         }
 
         // register callback
-        exchange.getUnitOfWork().addSynchronization(new SynchronizationAdapter() {
-            @Override
-            public void onComplete(final Exchange exchange) {
-                if (onFailureOnly) {
-                    return;
-                }
-
-                if (onWhen != null && !onWhen.matches(exchange)) {
-                    // predicate did not match so do not route the onComplete
-                    return;
-                }
-
-                // must use a copy as we dont want it to cause side effects of the original exchange
-                final Exchange copy = prepareExchange(exchange);
-
-                executorService.submit(new Callable<Exchange>() {
-                    public Exchange call() throws Exception {
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Processing onComplete: " + copy);
-                        }
-                        doProcess(processor, copy);
-                        return copy;
-                    }
-                });
-            }
-
-            public void onFailure(final Exchange exchange) {
-                if (onCompleteOnly) {
-                    return;
-                }
-
-                if (onWhen != null && !onWhen.matches(exchange)) {
-                    // predicate did not match so do not route the onComplete
-                    return;
-                }
-
-                // must use a copy as we dont want it to cause side effects of the original exchange
-                final Exchange copy = prepareExchange(exchange);
-                // must remove exception otherwise onFailure routing will fail as well
-                // the caused exception is stored as a property (Exchange.EXCEPTION_CAUGHT) on the exchange
-                copy.setException(null);
-
-                executorService.submit(new Callable<Exchange>() {
-                    public Exchange call() throws Exception {
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Processing onFailure: " + copy);
-                        }
-                        doProcess(processor, copy);
-                        return null;
-                    }
-                });
-            }
-
-            @Override
-            public String toString() {
-                if (!onCompleteOnly && !onFailureOnly) {
-                    return "onCompleteOrFailure";
-                } else if (onCompleteOnly) {
-                    return "onCompleteOnly";
-                } else {
-                    return "onFailureOnly";
-                }
-            }
-        });
+        exchange.getUnitOfWork().addSynchronization(new OnCompletionSynchronization());
     }
 
     /**
@@ -197,6 +135,77 @@ public class OnCompletionProcessor exten
         return answer;
     }
 
+    private final class OnCompletionSynchronization extends SynchronizationAdapter implements Ordered {
+
+        public int getOrder() {
+            // we want to be last
+            return Ordered.LOWEST;
+        }
+
+        @Override
+        public void onComplete(final Exchange exchange) {
+            if (onFailureOnly) {
+                return;
+            }
+
+            if (onWhen != null && !onWhen.matches(exchange)) {
+                // predicate did not match so do not route the onComplete
+                return;
+            }
+
+            // must use a copy as we dont want it to cause side effects of the original exchange
+            final Exchange copy = prepareExchange(exchange);
+
+            executorService.submit(new Callable<Exchange>() {
+                public Exchange call() throws Exception {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Processing onComplete: " + copy);
+                    }
+                    doProcess(processor, copy);
+                    return copy;
+                }
+            });
+        }
+
+        public void onFailure(final Exchange exchange) {
+            if (onCompleteOnly) {
+                return;
+            }
+
+            if (onWhen != null && !onWhen.matches(exchange)) {
+                // predicate did not match so do not route the onComplete
+                return;
+            }
+
+            // must use a copy as we dont want it to cause side effects of the original exchange
+            final Exchange copy = prepareExchange(exchange);
+            // must remove exception otherwise onFailure routing will fail as well
+            // the caused exception is stored as a property (Exchange.EXCEPTION_CAUGHT) on the exchange
+            copy.setException(null);
+
+            executorService.submit(new Callable<Exchange>() {
+                public Exchange call() throws Exception {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Processing onFailure: " + copy);
+                    }
+                    doProcess(processor, copy);
+                    return null;
+                }
+            });
+        }
+
+        @Override
+        public String toString() {
+            if (!onCompleteOnly && !onFailureOnly) {
+                return "onCompleteOrFailure";
+            } else if (onCompleteOnly) {
+                return "onCompleteOnly";
+            } else {
+                return "onFailureOnly";
+            }
+        }
+    }
+
     @Override
     public String toString() {
         return "OnCompletionProcessor[" + processor + "]";

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/SynchronizationVetoable.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/SynchronizationVetoable.java?rev=950686&r1=950685&r2=950686&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/SynchronizationVetoable.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/SynchronizationVetoable.java Wed Jun  2 17:39:21 2010
@@ -23,10 +23,10 @@ package org.apache.camel.spi;
  * when the {@link org.apache.camel.Exchange} complete at the end. If the {@link org.apache.camel.Exchange}
  * is processed asynchronously the {@link org.apache.camel.spi.Synchronization} will be handed
  * over to the next thread. This ensures for example the file consumer will delete the processed file at the very
- * end, when the {@link org.apache.camel.Exchange} has been completed succesfully.
+ * end, when the {@link org.apache.camel.Exchange} has been completed successfully.
  * <p/>
- * However there may be situations where you do not want to handover certain {@link org.apache.camel.spi.Synchronization}
- * , such as when doing asynchronously request/reply over SEDA or VM endpoints.
+ * However there may be situations where you do not want to handover certain {@link org.apache.camel.spi.Synchronization},
+ * such as when doing asynchronously request/reply over SEDA or VM endpoints.
  *
  * @version $Revision$
  */
@@ -36,7 +36,7 @@ public interface SynchronizationVetoable
      * Whether or not handover this synchronization is allowed.
      * <p/>
      * For example when an {@link org.apache.camel.Exchange} is being routed
-     * from one thread to another thread, such as using toAsync etc.
+     * from one thread to another thread, such as using request/reply over SEDA
      *
      * @return <tt>true</tt> to allow handover, <tt>false</tt> to deny.
      */

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=950686&r1=950685&r2=950686&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Wed Jun  2 17:39:21 2010
@@ -220,19 +220,21 @@ public final class ObjectHelper {
         if (b == null) {
             return 1;
         }
+        if (a instanceof Ordered && b instanceof Ordered) {
+            return ((Ordered) a).getOrder() - ((Ordered) b).getOrder();
+        }
         if (ignoreCase && a instanceof String && b instanceof String) {
             return ((String) a).compareToIgnoreCase((String) b);
         }
         if (a instanceof Comparable) {
             Comparable comparable = (Comparable)a;
             return comparable.compareTo(b);
-        } else {
-            int answer = a.getClass().getName().compareTo(b.getClass().getName());
-            if (answer == 0) {
-                answer = a.hashCode() - b.hashCode();
-            }
-            return answer;
         }
+        int answer = a.getClass().getName().compareTo(b.getClass().getName());
+        if (answer == 0) {
+            answer = a.hashCode() - b.hashCode();
+        }
+        return answer;
     }
 
     public static Boolean toBoolean(Object value) {

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/util/Ordered.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/Ordered.java?rev=950686&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/Ordered.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/Ordered.java Wed Jun  2 17:39:21 2010
@@ -0,0 +1,46 @@
+/**
+ * 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.util;
+
+/**
+ * Interface to be implement by objects that should be orderable, such as in a {@link java.util.Collection}.
+ *
+ * @version $Revision$
+ */
+public interface Ordered {
+
+    /**
+     * The highest precedence
+     */
+    int HIGHEST = Integer.MIN_VALUE;
+
+    /**
+     * The lowest precedence
+     */
+    int LOWEST = Integer.MAX_VALUE;
+
+
+    /**
+     * Gets the order.
+     * <p/>
+     * Use low numbers for higher priority. Normally the sorting will start from 0 and move upwards.
+     * So if you want to be last then use {@link Integer#MAX_VALUE}.
+     *
+     * @return the order
+     */
+    int getOrder();
+}

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

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/util/Ordered.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java?rev=950686&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java Wed Jun  2 17:39:21 2010
@@ -0,0 +1,50 @@
+/**
+ * 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.util;
+
+import java.util.Comparator;
+
+/**
+ * A comparator to sort {@link Ordered}
+ *
+ * @version $Revision$
+ */
+public final class OrderedComparator implements Comparator {
+
+    private final boolean reverse;
+
+    public OrderedComparator() {
+        this(false);
+    }
+
+    public OrderedComparator(boolean reverse) {
+        this.reverse = reverse;
+    }
+
+    public int compare(Object o1, Object o2) {
+        Integer num1 = 0;
+        Integer num2 = 0;
+        if (o1 instanceof Ordered) {
+            num1 = ((Ordered) o1).getOrder();
+        }
+        if (o2 instanceof Ordered) {
+            num2 = ((Ordered) o2).getOrder();
+        }
+        int answer = num1.compareTo(num2);
+        return reverse ? -1 * answer : answer;
+    }
+}

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

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionShouldBeLastTest.java (from r950541, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionGlobalTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionShouldBeLastTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionShouldBeLastTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionGlobalTest.java&r1=950541&r2=950686&rev=950686&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionGlobalTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionShouldBeLastTest.java Wed Jun  2 17:39:21 2010
@@ -16,74 +16,76 @@
  */
 package org.apache.camel.processor;
 
-import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.SynchronizationAdapter;
 
 /**
  * @version $Revision$
  */
-public class OnCompletionGlobalTest extends ContextTestSupport {
+public class OnCompletionShouldBeLastTest extends ContextTestSupport {
 
-    public void testSynchronizeComplete() throws Exception {
-        getMockEndpoint("mock:sync").expectedBodiesReceived("Bye World");
-        getMockEndpoint("mock:sync").expectedPropertyReceived(Exchange.ON_COMPLETION, true);
-
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceived("Bye World");
+    public void testOnCompletionShouldBeLast() throws Exception {
+        getMockEndpoint("mock:sync").expectedBodiesReceived("C", "B", "A", "Hello World");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
 
         template.sendBody("direct:start", "Hello World");
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testSynchronizeFailure() throws Exception {
-        getMockEndpoint("mock:sync").expectedMessageCount(1);
-        getMockEndpoint("mock:sync").expectedPropertyReceived(Exchange.ON_COMPLETION, true);
-
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(0);
-
-        try {
-            template.sendBody("direct:start", "Kabom");
-            fail("Should throw exception");
-        } catch (CamelExecutionException e) {
-            assertEquals("Kabom", e.getCause().getMessage());
-        }
-
-        assertMockEndpointsSatisfied();
-    }
-
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                // START SNIPPET: e1
-                // define a global on completion that is invoked when the exchange is complete
-                onCompletion().to("log:global").to("mock:sync");
+                onCompletion().to("mock:sync");
 
                 from("direct:start")
-                    .process(new MyProcessor())
+                    .process(new Processor() {
+                        public void process(Exchange exchange) throws Exception {
+                            exchange.addOnCompletion(new SynchronizationAdapter() {
+                                @Override
+                                public void onDone(Exchange exchange) {
+                                    template.sendBody("mock:sync", "A");
+                                }
+
+                                @Override
+                                public String toString() {
+                                    return "A";
+                                }
+                            });
+
+                            exchange.addOnCompletion(new SynchronizationAdapter() {
+                                @Override
+                                public void onDone(Exchange exchange) {
+                                    template.sendBody("mock:sync", "B");
+                                }
+
+                                @Override
+                                public String toString() {
+                                    return "B";
+                                }
+                            });
+
+                            exchange.addOnCompletion(new SynchronizationAdapter() {
+                                @Override
+                                public void onDone(Exchange exchange) {
+                                    template.sendBody("mock:sync", "C");
+                                }
+
+                                @Override
+                                public String toString() {
+                                    return "C";
+                                }
+                            });
+                        }
+                    })
                     .to("mock:result");
-                // END SNIPPET: e1
             }
         };
     }
 
-    public static class MyProcessor implements Processor {
-
-        public MyProcessor() {
-        }
-
-        public void process(Exchange exchange) throws Exception {
-            if ("Kabom".equals(exchange.getIn().getBody())) {
-                throw new IllegalArgumentException("Kabom");
-            }
-            exchange.getIn().setBody("Bye World");
-        }
-    }
 }
\ No newline at end of file

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java?rev=950686&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java Wed Jun  2 17:39:21 2010
@@ -0,0 +1,163 @@
+/**
+ * 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.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Revision$
+ */
+public class OrderedComparatorTest extends TestCase {
+
+    @SuppressWarnings("unchecked")
+    public void testOrderedComparator() throws Exception {
+        List<Ordered> answer = new ArrayList<Ordered>();
+        answer.add(new MyOrder(0));
+        answer.add(new MyOrder(2));
+        answer.add(new MyOrder(1));
+        answer.add(new MyOrder(5));
+        answer.add(new MyOrder(4));
+
+        Collections.sort(answer, new OrderedComparator());
+
+        assertEquals(0, answer.get(0).getOrder());
+        assertEquals(1, answer.get(1).getOrder());
+        assertEquals(2, answer.get(2).getOrder());
+        assertEquals(4, answer.get(3).getOrder());
+        assertEquals(5, answer.get(4).getOrder());
+    }
+
+    @SuppressWarnings("unchecked")
+    public void testOrderedComparatorReverse() throws Exception {
+        List<Ordered> answer = new ArrayList<Ordered>();
+        answer.add(new MyOrder(0));
+        answer.add(new MyOrder(2));
+        answer.add(new MyOrder(1));
+        answer.add(new MyOrder(5));
+        answer.add(new MyOrder(4));
+
+        Collections.sort(answer, new OrderedComparator(true));
+
+        assertEquals(5, answer.get(0).getOrder());
+        assertEquals(4, answer.get(1).getOrder());
+        assertEquals(2, answer.get(2).getOrder());
+        assertEquals(1, answer.get(3).getOrder());
+        assertEquals(0, answer.get(4).getOrder());
+    }
+
+    @SuppressWarnings("unchecked")
+    public void testOrderedComparatorHigh() throws Exception {
+        List<Ordered> answer = new ArrayList<Ordered>();
+        answer.add(new MyOrder(0));
+        answer.add(new MyOrder(2));
+        answer.add(new MyOrder(200));
+        answer.add(new MyOrder(50));
+        answer.add(new MyOrder(Ordered.HIGHEST));
+        answer.add(new MyOrder(4));
+
+        Collections.sort(answer, new OrderedComparator());
+
+        assertEquals(Ordered.HIGHEST, answer.get(0).getOrder());
+        assertEquals(0, answer.get(1).getOrder());
+        assertEquals(2, answer.get(2).getOrder());
+        assertEquals(4, answer.get(3).getOrder());
+        assertEquals(50, answer.get(4).getOrder());
+        assertEquals(200, answer.get(5).getOrder());
+    }
+
+    @SuppressWarnings("unchecked")
+    public void testOrderedComparatorHighReverse() throws Exception {
+        List<Ordered> answer = new ArrayList<Ordered>();
+        answer.add(new MyOrder(0));
+        answer.add(new MyOrder(2));
+        answer.add(new MyOrder(200));
+        answer.add(new MyOrder(50));
+        answer.add(new MyOrder(Ordered.HIGHEST));
+        answer.add(new MyOrder(4));
+
+        Collections.sort(answer, new OrderedComparator(true));
+
+        assertEquals(200, answer.get(0).getOrder());
+        assertEquals(50, answer.get(1).getOrder());
+        assertEquals(4, answer.get(2).getOrder());
+        assertEquals(2, answer.get(3).getOrder());
+        assertEquals(0, answer.get(4).getOrder());
+        assertEquals(Ordered.HIGHEST, answer.get(5).getOrder());
+    }
+
+    @SuppressWarnings("unchecked")
+    public void testOrderedComparatorLow() throws Exception {
+        List<Ordered> answer = new ArrayList<Ordered>();
+        answer.add(new MyOrder(0));
+        answer.add(new MyOrder(-2));
+        answer.add(new MyOrder(200));
+        answer.add(new MyOrder(50));
+        answer.add(new MyOrder(Ordered.LOWEST));
+        answer.add(new MyOrder(-4));
+
+        Collections.sort(answer, new OrderedComparator());
+
+        assertEquals(-4, answer.get(0).getOrder());
+        assertEquals(-2, answer.get(1).getOrder());
+        assertEquals(0, answer.get(2).getOrder());
+        assertEquals(50, answer.get(3).getOrder());
+        assertEquals(200, answer.get(4).getOrder());
+        assertEquals(Ordered.LOWEST, answer.get(5).getOrder());
+    }
+
+    @SuppressWarnings("unchecked")
+    public void testOrderedComparatorLowReverse() throws Exception {
+        List<Ordered> answer = new ArrayList<Ordered>();
+        answer.add(new MyOrder(0));
+        answer.add(new MyOrder(-2));
+        answer.add(new MyOrder(200));
+        answer.add(new MyOrder(50));
+        answer.add(new MyOrder(Ordered.LOWEST));
+        answer.add(new MyOrder(-4));
+
+        Collections.sort(answer, new OrderedComparator(true));
+
+        assertEquals(Ordered.LOWEST, answer.get(0).getOrder());
+        assertEquals(200, answer.get(1).getOrder());
+        assertEquals(50, answer.get(2).getOrder());
+        assertEquals(0, answer.get(3).getOrder());
+        assertEquals(-2, answer.get(4).getOrder());
+        assertEquals(-4, answer.get(5).getOrder());
+    }
+
+    private static final class MyOrder implements Ordered {
+
+        private final int order;
+
+        private MyOrder(int order) {
+            this.order = order;
+        }
+
+        public int getOrder() {
+            return order;
+        }
+
+        @Override
+        public String toString() {
+            return "" + order;
+        }
+    }
+}

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

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