Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 57475200C24 for ; Thu, 23 Feb 2017 19:20:24 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 5621D160B67; Thu, 23 Feb 2017 18:20:24 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 7696B160B3E for ; Thu, 23 Feb 2017 19:20:23 +0100 (CET) Received: (qmail 79915 invoked by uid 500); 23 Feb 2017 18:20:22 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 79905 invoked by uid 99); 23 Feb 2017 18:20:22 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Feb 2017 18:20:22 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7A6CFDFDAC; Thu, 23 Feb 2017 18:20:22 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Thu, 23 Feb 2017 18:20:22 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] camel git commit: CAMEL-10802: java.lang.ClassCastException when using FlexibleAggregationStrategy archived-at: Thu, 23 Feb 2017 18:20:24 -0000 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 Authored: Thu Feb 23 19:16:31 2017 +0100 Committer: Claus Ibsen 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 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 implements Aggregatio injector.setValue(oldExchange, picked); } - private void injectAsCollection(Exchange oldExchange, E picked) { - Collection col = injector.getValueAsCollection(oldExchange); + private void injectAsCollection(Exchange oldExchange, E picked, Class collectionType) { + Collection col = injector.getValueAsCollection(oldExchange, collectionType); col = safeInsertIntoCollection(oldExchange, col, picked); injector.setValueAsCollection(oldExchange, col); } @@ -315,7 +315,7 @@ public class FlexibleAggregationStrategy 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 getValueAsCollection(Exchange exchange); + public abstract Collection getValueAsCollection(Exchange exchange, Class type); public abstract void setValueAsCollection(Exchange exchange, Collection obj); } @@ -343,8 +343,14 @@ public class FlexibleAggregationStrategy implements Aggregatio } @Override @SuppressWarnings("unchecked") - public Collection getValueAsCollection(Exchange exchange) { - return exchange.getProperty(propertyName, Collection.class); + public Collection getValueAsCollection(Exchange exchange, Class 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 implements Aggregatio } @Override @SuppressWarnings("unchecked") - public Collection getValueAsCollection(Exchange exchange) { - return exchange.getIn().getHeader(headerName, Collection.class); + public Collection getValueAsCollection(Exchange exchange, Class 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 implements Aggregatio } @Override @SuppressWarnings("unchecked") - public Collection getValueAsCollection(Exchange exchange) { - return exchange.getIn().getBody(Collection.class); + public Collection getValueAsCollection(Exchange exchange, Class 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 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()}"); } }; }