View Javadoc

1   /*
2    * $Id: MulticastRouterTestCase.java 20690 2010-12-14 01:22:41Z dzapata $
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.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  public class MulticastRouterTestCase extends FunctionalTestCase
25  {
26      private static AtomicInteger errorCounter = new AtomicInteger(0);
27      
28      
29      @Override
30      protected String getConfigResources()
31      {
32          return "org/mule/test/integration/routing/outbound/multicasting-router-config.xml";
33      }
34  
35      public void testAll() throws Exception
36      {
37          ByteArrayInputStream bis = new ByteArrayInputStream("Hello, world".getBytes("UTF-8"));
38          MuleClient client = new MuleClient(muleContext);
39          client.dispatch("vm://inbound1", bis, null);
40          MuleMessage response = client.request("vm://output1", 2000);
41          assertNull(response);
42          MuleMessage error = client.request("vm://errors", 2000);
43          assertNotNull(error);
44          Object payload = error.getPayload();
45          assertNotNull(payload);
46          assertTrue(payload instanceof ExceptionMessage);
47      }
48  
49      public void testFirstSuccessful() throws Exception
50      {
51          ByteArrayInputStream bis = new ByteArrayInputStream("Hello, world".getBytes("UTF-8"));
52          MuleClient client = new MuleClient(muleContext);
53          MuleMessage response = client.send("vm://inbound2", bis, null);;
54          assertNotNull(response);
55          Object payload = response.getPayload();
56          assertNotNull(payload);
57          assertEquals("Hello, world", response.getPayload());
58          assertEquals(errorCounter.get(), 2);
59          MuleMessage error = client.request("vm://errors2", 2000);
60          assertNull(error);
61      }
62  
63      public static class Fail implements Callable
64      {
65          public Object onCall(MuleEventContext eventContext) throws Exception
66          {
67              errorCounter.incrementAndGet();
68              eventContext.getMessage().setPayload("Exception was thrown");
69              throw new Exception();
70          }
71      }
72  
73      public static class Echo
74      {
75          public  String process(Object message) throws Exception
76          {
77              if (message instanceof String)
78              {
79                  return (String) message;
80              }
81              else if (message instanceof byte[])
82              {
83                  return new String((byte[])message);
84              }
85              else if (message instanceof InputStream)
86              {
87                  return IOUtils.toString((InputStream) message);
88              }
89              else
90              {
91                  return message.toString();
92              }
93          }
94      }
95  }