View Javadoc

1   /*
2    * $Id: MulticastRouterTestCase.java 22419 2011-07-15 03:41:06Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.routing.outbound;
11  
12  import org.mule.api.MuleEventContext;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.lifecycle.Callable;
15  import org.mule.message.ExceptionMessage;
16  import org.mule.module.client.MuleClient;
17  import org.mule.tck.junit4.FunctionalTestCase;
18  import org.mule.util.IOUtils;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.InputStream;
22  import java.util.concurrent.atomic.AtomicInteger;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertNull;
29  import static org.junit.Assert.assertTrue;
30  
31  public class MulticastRouterTestCase extends FunctionalTestCase
32  {
33      private static AtomicInteger errorCounter = new AtomicInteger(0);
34      
35      
36      @Override
37      protected String getConfigResources()
38      {
39          return "org/mule/test/integration/routing/outbound/multicasting-router-config.xml";
40      }
41  
42      @Test
43      public void testAll() throws Exception
44      {
45          ByteArrayInputStream bis = new ByteArrayInputStream("Hello, world".getBytes("UTF-8"));
46          MuleClient client = new MuleClient(muleContext);
47          client.dispatch("vm://inbound1", bis, null);
48          MuleMessage response = client.request("vm://output1", 2000);
49          assertNull(response);
50          MuleMessage error = client.request("vm://errors", 2000);
51          assertNotNull(error);
52          Object payload = error.getPayload();
53          assertNotNull(payload);
54          assertTrue(payload instanceof ExceptionMessage);
55      }
56  
57      @Test
58      public void testFirstSuccessful() throws Exception
59      {
60          ByteArrayInputStream bis = new ByteArrayInputStream("Hello, world".getBytes("UTF-8"));
61          MuleClient client = new MuleClient(muleContext);
62          MuleMessage response = client.send("vm://inbound2", bis, null);
63          assertNotNull(response);
64          Object payload = response.getPayload();
65          assertNotNull(payload);
66          assertEquals("Hello, world", response.getPayload());
67          assertEquals(errorCounter.get(), 2);
68          MuleMessage error = client.request("vm://errors2", 2000);
69          assertNull(error);
70      }
71  
72      public static class Fail implements Callable
73      {
74          @Override
75          public Object onCall(MuleEventContext eventContext) throws Exception
76          {
77              errorCounter.incrementAndGet();
78              eventContext.getMessage().setPayload("Exception was thrown");
79              throw new Exception();
80          }
81      }
82  
83      public static class Echo
84      {
85          public  String process(Object message) throws Exception
86          {
87              if (message instanceof String)
88              {
89                  return (String) message;
90              }
91              else if (message instanceof byte[])
92              {
93                  return new String((byte[])message);
94              }
95              else if (message instanceof InputStream)
96              {
97                  return IOUtils.toString((InputStream) message);
98              }
99              else
100             {
101                 return message.toString();
102             }
103         }
104     }
105 }