camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hekon...@apache.org
Subject git commit: [CAMEL-7175] Created FnAggregationStrategy wrapping Any result into Exchange.
Date Wed, 05 Feb 2014 21:49:20 GMT
Updated Branches:
  refs/heads/master 88798a612 -> 66e6b1f45


[CAMEL-7175] Created FnAggregationStrategy wrapping Any result into Exchange.


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

Branch: refs/heads/master
Commit: 66e6b1f45e1536f69a4fd2235f8c90e0871be1a1
Parents: 88798a6
Author: Henryk Konsek <hekonsek@gmail.com>
Authored: Wed Feb 5 22:49:02 2014 +0100
Committer: Henryk Konsek <hekonsek@gmail.com>
Committed: Wed Feb 5 22:49:02 2014 +0100

----------------------------------------------------------------------
 .../scala/org/apache/camel/scala/Preamble.scala | 18 +++++++-
 .../apache/camel/scala/dsl/AggregatorTest.scala | 45 +++++++++++++++++++-
 2 files changed, 60 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/66e6b1f4/components/camel-scala/src/main/scala/org/apache/camel/scala/Preamble.scala
----------------------------------------------------------------------
diff --git a/components/camel-scala/src/main/scala/org/apache/camel/scala/Preamble.scala b/components/camel-scala/src/main/scala/org/apache/camel/scala/Preamble.scala
index d6ba6ee..c7f6558 100644
--- a/components/camel-scala/src/main/scala/org/apache/camel/scala/Preamble.scala
+++ b/components/camel-scala/src/main/scala/org/apache/camel/scala/Preamble.scala
@@ -18,6 +18,8 @@ package org.apache.camel
 package scala
 
 import processor.aggregate.AggregationStrategy
+import org.apache.camel.builder.ExchangeBuilder.anExchange
+import org.apache.camel.scala.Preamble.FnAggregationStrategy.exchangeWrappingAggregator
 
 object Preamble extends Preamble
 /**
@@ -33,6 +35,7 @@ trait Preamble {
 
   implicit def enrichFnAny(f: Exchange => Any) = new ScalaPredicate(f)
   implicit def enrichAggr(f: (Exchange, Exchange) => Exchange) = new FnAggregationStrategy(f)
+  implicit def enrichWrappingAggregator[T <: Any](f: (Exchange, Exchange) => T) = exchangeWrappingAggregator(f)
 
   /**
    * process { in(classOf[String]) { _+"11" } .toIn }
@@ -124,4 +127,17 @@ trait Preamble {
     override def aggregate(original: Exchange, resource: Exchange): Exchange = aggregator(original,
resource)
   }
 
-}
+  object FnAggregationStrategy {
+
+    def exchangeWrappingAggregator[T <: Any](aggregator: (Exchange, Exchange) => T)
= {
+      val wrappingAggregator =
+        (oldExch: Exchange, newExch: Exchange) => newExch match {
+          case null => oldExch
+          case _ => anExchange(newExch.getContext).withBody(aggregator(oldExch, newExch)).build
+      }
+      new FnAggregationStrategy(wrappingAggregator)
+    }
+
+  }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/66e6b1f4/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/AggregatorTest.scala
----------------------------------------------------------------------
diff --git a/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/AggregatorTest.scala
b/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/AggregatorTest.scala
index f03ec7a..b826be4 100644
--- a/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/AggregatorTest.scala
+++ b/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/AggregatorTest.scala
@@ -19,6 +19,8 @@ package org.apache.camel.scala.dsl
 import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy
 import org.junit.Test
 import builder.RouteBuilder
+import org.apache.camel.Exchange
+import org.apache.camel.builder.ExchangeBuilder.anExchange
 
 /**
  * Test case for message aggregator
@@ -47,6 +49,28 @@ class AggregatorTest extends ScalaTestSupport {
     }
   }
 
+  @Test
+  def testWrappingFunctionalAggregator() {
+    "mock:c" expect {
+      _.received("foobar")
+    }
+    test {
+      "direct:c" ! "foo"
+      "direct:c" ! "bar"
+    }
+  }
+
+  @Test
+  def testFunctionalAggregator() {
+    "mock:d" expect {
+      _.received("foobar")
+    }
+    test {
+      "direct:d" ! "foo"
+      "direct:d" ! "bar"
+    }
+  }
+
   val builder =
     new RouteBuilder {
        "direct:a" ==> {
@@ -59,5 +83,22 @@ class AggregatorTest extends ScalaTestSupport {
          }
        }
        //END SNIPPET: block
-    }  
-}
+
+      "direct:c" ==> {
+        val aggregator = (oldEx: Exchange, newEx: Exchange) => oldEx match {
+          case null => newEx.in[String]
+          case _ => oldEx.in[String] + newEx.in[String]
+        }
+        aggregate ("constant", aggregator) completionSize 2 to "mock:c"
+      }
+
+      "direct:d" ==> {
+        val aggregator = (oldEx: Exchange, newEx: Exchange) => oldEx match {
+          case null => newEx
+          case _ => anExchange(newEx.getContext).withBody(oldEx.in[String] + newEx.in[String]).build
+        }
+        aggregate ("constant", aggregator) completionSize 2 to "mock:d"
+      }
+    }
+
+}
\ No newline at end of file


Mime
View raw message