camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] camel git commit: CAMEL-10802: java.lang.ClassCastException when using FlexibleAggregationStrategy
Date Thu, 23 Feb 2017 18:20:22 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.18.x 4532655a7 -> 452ebd7d5
  refs/heads/master 4d74b8ab7 -> e7690fce6


CAMEL-10802: java.lang.ClassCastException when using FlexibleAggregationStrategy


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e7690fce
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e7690fce
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e7690fce

Branch: refs/heads/master
Commit: e7690fce641127e4cc75cf3fa596f86cdf44098e
Parents: 4d74b8a
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Feb 23 19:16:31 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Feb 23 19:16:31 2017 +0100

----------------------------------------------------------------------
 .../toolbox/FlexibleAggregationStrategy.java    | 38 ++++++++++----
 .../FlexibleAggregationStrategiesTest.java      | 53 +++++++++++++++++++-
 2 files changed, 79 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e7690fce/camel-core/src/main/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategy.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategy.java
b/camel-core/src/main/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategy.java
index a8c433a..5fe86b1 100644
--- a/camel-core/src/main/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategy.java
+++ b/camel-core/src/main/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategy.java
@@ -232,7 +232,7 @@ public class FlexibleAggregationStrategy<E extends Object> implements
Aggregatio
         if (collectionType == null) {
             injectAsRawValue(exchange, picked);
         } else {
-            injectAsCollection(exchange, picked);
+            injectAsCollection(exchange, picked, collectionType);
         }
 
         return exchange;
@@ -259,8 +259,8 @@ public class FlexibleAggregationStrategy<E extends Object> implements
Aggregatio
         injector.setValue(oldExchange, picked);
     }
 
-    private void injectAsCollection(Exchange oldExchange, E picked) {
-        Collection<E> col = injector.getValueAsCollection(oldExchange);
+    private void injectAsCollection(Exchange oldExchange, E picked, Class<? extends Collection>
collectionType) {
+        Collection<E> col = injector.getValueAsCollection(oldExchange, collectionType);
         col = safeInsertIntoCollection(oldExchange, col, picked);
         injector.setValueAsCollection(oldExchange, col);
     }
@@ -315,7 +315,7 @@ public class FlexibleAggregationStrategy<E extends Object> implements
Aggregatio
         public abstract void prepareAggregationExchange(Exchange exchange);
         public abstract E getValue(Exchange exchange);
         public abstract void setValue(Exchange exchange, E obj);
-        public abstract Collection<E> getValueAsCollection(Exchange exchange);
+        public abstract Collection<E> getValueAsCollection(Exchange exchange, Class<?
extends Collection> type);
         public abstract void setValueAsCollection(Exchange exchange, Collection<E>
obj);
     }
     
@@ -343,8 +343,14 @@ public class FlexibleAggregationStrategy<E extends Object> implements
Aggregatio
         }
 
         @Override @SuppressWarnings("unchecked")
-        public Collection<E> getValueAsCollection(Exchange exchange) {
-            return exchange.getProperty(propertyName, Collection.class);
+        public Collection<E> getValueAsCollection(Exchange exchange, Class<? extends
Collection> type) {
+            Object value = exchange.getProperty(propertyName);
+            if (value == null) {
+                // empty so create a new collection to host this
+                return exchange.getContext().getInjector().newInstance(type);
+            } else {
+                return exchange.getProperty(propertyName, type);
+            }
         }
 
         @Override
@@ -378,8 +384,14 @@ public class FlexibleAggregationStrategy<E extends Object> implements
Aggregatio
         }
 
         @Override @SuppressWarnings("unchecked")
-        public Collection<E> getValueAsCollection(Exchange exchange) {
-            return exchange.getIn().getHeader(headerName, Collection.class);
+        public Collection<E> getValueAsCollection(Exchange exchange, Class<? extends
Collection> type) {
+            Object value = exchange.getIn().getHeader(headerName);
+            if (value == null) {
+                // empty so create a new collection to host this
+                return exchange.getContext().getInjector().newInstance(type);
+            } else {
+                return exchange.getIn().getHeader(headerName, type);
+            }
         }
         
         @Override
@@ -409,8 +421,14 @@ public class FlexibleAggregationStrategy<E extends Object> implements
Aggregatio
         }
 
         @Override @SuppressWarnings("unchecked")
-        public Collection<E> getValueAsCollection(Exchange exchange) {
-            return exchange.getIn().getBody(Collection.class);
+        public Collection<E> getValueAsCollection(Exchange exchange, Class<? extends
Collection> type) {
+            Object value = exchange.getIn().getBody();
+            if (value == null) {
+                // empty so create a new collection to host this
+                return exchange.getContext().getInjector().newInstance(type);
+            } else {
+                return exchange.getIn().getBody(type);
+            }
         }
         
         @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/e7690fce/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
b/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
index e062dab..789d458 100644
--- a/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
@@ -17,8 +17,11 @@
 package org.apache.camel.util.toolbox;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -26,6 +29,8 @@ import org.w3c.dom.Node;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.builder.NotifyBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.processor.aggregate.AggregationStrategy;
 import org.apache.camel.util.toolbox.FlexibleAggregationStrategy.CompletionAwareMixin;
@@ -200,7 +205,30 @@ public class FlexibleAggregationStrategiesTest extends ContextTestSupport
{
         assertEquals("ok", list.get(0).getTextContent());
         assertEquals("error", list.get(1).getTextContent());
     }
-    
+
+    @Test
+    public void testLinkedList() throws Exception {
+        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).and().whenExactlyFailed(0).create();
+
+        template.sendBody("direct:linkedlist", Arrays.asList("FIRST", "SECOND"));
+
+        assertTrue(notify.matches(10, TimeUnit.SECONDS));
+    }
+
+    @Test
+    public void testHashSet() throws Exception {
+        HashSet<String> r = new HashSet<>();
+        r.add("FIRST");
+        r.add("SECOND");
+
+        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).and().whenExactlyFailed(0).create();
+
+        Set result = template.requestBody("direct:hashset", Arrays.asList("FIRST", "SECOND"),
Set.class);
+
+        assertTrue(notify.matches(10, TimeUnit.SECONDS));
+        assertEquals(r, result);
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
@@ -274,7 +302,28 @@ public class FlexibleAggregationStrategiesTest extends ContextTestSupport
{
                             .accumulateInCollection(ArrayList.class))
                         .constant(true).completionSize(3)
                     .to("mock:result.xpath1");
-                
+
+                from("direct:linkedlist")
+                    .log(LoggingLevel.INFO, "Before the first split the body is ${body} and
has class ${body.getClass()}")
+                        .split(body(), AggregationStrategies.flexible().pick(body()).accumulateInCollection(LinkedList.class))
+                    .log(LoggingLevel.INFO, "During the first split the body is ${body} and
has class ${body.getClass()}")
+                    .end()
+                    .log(LoggingLevel.INFO, "Before the second split the body is ${body}
and has class ${body.getClass()}")
+                        .split(body(), AggregationStrategies.flexible().pick(body()).accumulateInCollection(LinkedList.class))
+                    .log(LoggingLevel.INFO, "During the second split the body is ${body}
and has class ${body.getClass()}")
+                    .end()
+                    .log(LoggingLevel.INFO, "After the second split the body is ${body} and
has class ${body.getClass()}");
+
+                from("direct:hashset")
+                    .log(LoggingLevel.INFO, "Before the first split the body is ${body} and
has class ${body.getClass()}")
+                        .split(body(), AggregationStrategies.flexible().pick(body()).accumulateInCollection(HashSet.class))
+                    .log(LoggingLevel.INFO, "During the first split the body is ${body} and
has class ${body.getClass()}")
+                    .end()
+                    .log(LoggingLevel.INFO, "Before the second split the body is ${body}
and has class ${body.getClass()}")
+                        .split(body(), AggregationStrategies.flexible().pick(body()).accumulateInCollection(HashSet.class))
+                    .log(LoggingLevel.INFO, "During the second split the body is ${body}
and has class ${body.getClass()}")
+                    .end()
+                    .log(LoggingLevel.INFO, "After the second split the body is ${body} and
has class ${body.getClass()}");
             }
         };
     }


Mime
View raw message