camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r744881 - /camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
Date Mon, 16 Feb 2009 10:33:31 GMT
Author: davsclaus
Date: Mon Feb 16 10:33:30 2009
New Revision: 744881

URL: http://svn.apache.org/viewvc?rev=744881&view=rev
Log:
Splitter with pojo wiki example

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
      - copied, changed from r744867, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterTest.java

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
(from r744867, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterTest.java&r1=744867&r2=744881&rev=744881&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitterPojoTest.java
Mon Feb 16 10:33:30 2009
@@ -16,217 +16,71 @@
  */
 package org.apache.camel.processor;
 
+import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
-import java.util.TreeSet;
 
-import org.apache.camel.CamelException;
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Exchange;
-import org.apache.camel.Message;
-import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
+import org.apache.camel.impl.JndiRegistry;
 
 /**
  * @version $Revision$
  */
-public class SplitterTest extends ContextTestSupport {
+public class SplitterPojoTest extends ContextTestSupport {
 
-    public void testSendingAMessageUsingMulticastReceivesItsOwnExchange() throws Exception
{
-        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
-        resultEndpoint.expectedBodiesReceived("James", "Guillaume", "Hiram", "Rob");
-
-        // InOnly
-        template.send("direct:seqential", new Processor() {
-            public void process(Exchange exchange) {
-                Message in = exchange.getIn();
-                in.setBody("James,Guillaume,Hiram,Rob");
-                in.setHeader("foo", "bar");
-            }
-        });
-
-        assertMockEndpointsSatisfied();
-
-        List<Exchange> list = resultEndpoint.getReceivedExchanges();
-        for (int i = 0; i < 4; i++) {
-            Exchange exchange = list.get(i);
-            Message in = exchange.getIn();
-            assertMessageHeader(in, Splitter.SPLIT_COUNTER, i);
-            assertMessageHeader(in, Splitter.SPLIT_SIZE, 4);
-        }
-    }
-
-    public void testSplitterWithAggregationStrategy() throws Exception {
-        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
-        resultEndpoint.expectedBodiesReceived("James", "Guillaume", "Hiram", "Rob", "Roman");
-
-        Exchange result = template.request("direct:seqential", new Processor() {
-            public void process(Exchange exchange) {
-                Message in = exchange.getIn();
-                in.setBody("James,Guillaume,Hiram,Rob,Roman");
-                in.setHeader("foo", "bar");
-            }
-        });
-
-        assertMockEndpointsSatisfied();
-        Message out = result.getOut();
-        assertEquals("Roman", out.getBody());
-        assertMessageHeader(out, "foo", "bar");
-        assertMessageHeader(out, Splitter.SPLIT_COUNTER, 4);
-    }
-
-    public void testEmptyBody() {
-        Exchange result = template.request("direct:seqential", new Processor() {
-            public void process(Exchange exchange) throws Exception {
-                exchange.getIn().setHeader("foo", "bar");
-            }
-        });
-
-        assertNull(result.getOut(false));
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("mySplitterBean", new MySplitterBean());
+        return jndi;
     }
 
-    public void testSendingAMessageUsingMulticastReceivesItsOwnExchangeParallel() throws
Exception {
-        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
+    public void testSplitWithPojoBean() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("James", "Jonathan", "Hadrian", "Claus", "Willem");
 
-        resultEndpoint.expectsNoDuplicates(body());
-        resultEndpoint.expectedMessageCount(4);
-
-        // InOnly
-        template.send("direct:parallel", new Processor() {
-            public void process(Exchange exchange) {
-                Message in = exchange.getIn();
-                in.setBody("James,Guillaume,Hiram,Rob");
-                in.setHeader("foo", "bar");
-            }
-        });
+        template.sendBody("direct:start", "James,Jonathan,Hadrian,Claus,Willem");
 
         assertMockEndpointsSatisfied();
-
-        List<Exchange> list = resultEndpoint.getReceivedExchanges();
-
-        Set<Integer> numbersFound = new TreeSet<Integer>();
-
-        final String[] names = {"James", "Guillaume", "Hiram", "Rob"};
-
-        for (int i = 0; i < 4; i++) {
-            Exchange exchange = list.get(i);
-            Message in = exchange.getIn();
-            Integer splitCounter = in.getHeader(Splitter.SPLIT_COUNTER, Integer.class);
-            numbersFound.add(splitCounter);
-            assertEquals(names[splitCounter], in.getBody());
-            assertMessageHeader(in, Splitter.SPLIT_SIZE, 4);
-        }
-
-        assertEquals(4, numbersFound.size());
     }
 
-    public void testSplitterWithAggregationStrategyParallel() throws Exception {
-        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
-        resultEndpoint.expectedMessageCount(5);
-
-        Exchange result = template.request("direct:parallel", new Processor() {
-            public void process(Exchange exchange) {
-                Message in = exchange.getIn();
-                in.setBody("James,Guillaume,Hiram,Rob,Roman");
-                in.setHeader("foo", "bar");
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // START SNIPPET: e1
+                from("direct:start")
+                        // here we use a POJO bean mySplitterBean to do the split of the
payload
+                        .split().method("mySplitterBean")
+                        .to("mock:result");
+                // END SNIPPET: e1
             }
-        });
-
-        assertMockEndpointsSatisfied();
-        Message out = result.getOut();
-
-        assertMessageHeader(out, "foo", "bar");
-        assertEquals((Integer)5, result.getProperty("aggregated", Integer.class));
+        };
     }
-    
-    public void testSplitterWithAggregationStrategyParallelStreaming() throws Exception {
-        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
-        resultEndpoint.expectedMessageCount(5);
-        resultEndpoint.expectedBodiesReceivedInAnyOrder("James", "Guillaume", "Hiram", "Rob",
"Roman");
-
-        Exchange result = template.request("direct:parallel-streaming", new Processor() {
-            public void process(Exchange exchange) {
-                Message in = exchange.getIn();
-                in.setBody("James,Guillaume,Hiram,Rob,Roman");
-                in.setHeader("foo", "bar");
-            }
-        });
 
-        assertMockEndpointsSatisfied();
-        Message out = result.getOut();
+    // START SNIPPET: e2
+    public class MySplitterBean {
 
-        assertMessageHeader(out, "foo", "bar");
-        assertEquals((Integer)5, result.getProperty("aggregated", Integer.class));
-    }
-    
-    public void testSplitterWithStreaming() throws Exception {
-        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
-        resultEndpoint.expectedMessageCount(5);
-        resultEndpoint.expectedHeaderReceived("foo", "bar");
-
-        template.request("direct:streaming", new Processor() {
-            public void process(Exchange exchange) {
-                Message in = exchange.getIn();
-                in.setBody("James,Guillaume,Hiram,Rob,Roman");
-                in.setHeader("foo", "bar");
+        /**
+         * The split method returns something that is iteratable such as a java.util.List.
+         *
+         * @param body the payload of the incoming message
+         * @return a list containing each part splitted
+         */
+        public List split(String body) {
+            // since this is based on an unit test you can of couse
+            // use different logic for splitting as Camel have out
+            // of the box support for splitting a String based on comma
+            // but this is for show and tell, since this is java code
+            // you have the full power how you like to split your messages
+            List answer = new ArrayList();
+            String[] parts = body.split(",");
+            for (String part : parts) {
+                answer.add(part);
             }
-        });
-        
-        assertMockEndpointsSatisfied();
-        for (Exchange exchange : resultEndpoint.getReceivedExchanges()) {
-            assertNotNull(exchange.getIn().getHeader(Splitter.SPLIT_COUNTER));
-            //this header cannot be set when streaming is used
-            assertNull(exchange.getIn().getHeader(Splitter.SPLIT_SIZE));
+            return answer;
         }
-
-    }
-    
-    public void testSplitterWithException() throws Exception {
-        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
-        resultEndpoint.expectedMessageCount(4);
-        resultEndpoint.expectedHeaderReceived("foo", "bar");
-        
-        MockEndpoint failedEndpoint = getMockEndpoint("mock:failed");
-        failedEndpoint.expectedMessageCount(1);
-        failedEndpoint.expectedHeaderReceived("foo", "bar");
-        
-        Exchange result = template.request("direct:exception", new Processor() {
-            public void process(Exchange exchange) {
-                Message in = exchange.getIn();
-                in.setBody("James,Guillaume,Hiram,Rob,Exception");
-                in.setHeader("foo", "bar");
-            }
-        });
-        
-        assertTrue("The result exchange should have a camel exception", result.getException()
instanceof CamelException);
-
-        assertMockEndpointsSatisfied();
     }
+    // END SNIPPET: e2
 
-    protected RouteBuilder createRouteBuilder() {
-        return new RouteBuilder() {
-            public void configure() {
-                errorHandler(deadLetterChannel("mock:failed").maximumRedeliveries(0));
-                from("direct:seqential").split(body().tokenize(","), new UseLatestAggregationStrategy()).to("mock:result");
-                from("direct:parallel").split(body().tokenize(","), new MyAggregationStrategy()).parallelProcessing(true).to("mock:result");
-                from("direct:streaming").split(body().tokenize(",")).streaming().to("mock:result");
-                from("direct:parallel-streaming").split(body().tokenize(","), new MyAggregationStrategy()).parallelProcessing().streaming().to("mock:result");
-                from("direct:exception")
-                    .split(body().tokenize(","))
-                    .aggregationStrategy(new MyAggregationStrategy())
-                    .parallelProcessing(true).streaming()
-                    .process(new Processor() {
-                        public void process(Exchange exchange) throws Exception {
-                            String string = exchange.getIn().getBody(String.class);
-                            if ("Exception".equals(string)) {
-                                throw new CamelException("Just want to throw exception here");
-                            }
-                        
-                        }                    
-                    }).to("mock:result");
-            }
-        };
-    }
-}
+}
\ No newline at end of file



Mime
View raw message