View Javadoc

1   /*
2    * $Id: MuleClientListenerTestCase.java 22421 2011-07-15 05:05: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  
11  package org.mule.test.integration.client;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.lifecycle.Startable;
15  import org.mule.api.lifecycle.Stoppable;
16  import org.mule.api.transport.DispatchException;
17  import org.mule.api.transport.NoReceiverForEndpointException;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.AbstractServiceAndFlowTestCase;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  
24  import org.junit.Test;
25  import org.junit.runners.Parameterized.Parameters;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertTrue;
30  
31  public class MuleClientListenerTestCase extends AbstractServiceAndFlowTestCase
32  {
33      @Parameters
34      public static Collection<Object[]> parameters()
35      {
36          return Arrays.asList(new Object[][]{
37              {ConfigVariant.SERVICE,
38                  "org/mule/test/integration/client/mule-client-listener-config-service.xml"},
39              {ConfigVariant.FLOW, "org/mule/test/integration/client/mule-client-listener-config-flow.xml"}});
40      }
41  
42      public MuleClientListenerTestCase(ConfigVariant variant, String configResources)
43      {
44          super(variant, configResources);
45      }
46  
47      public void doTestRegisterListener(String component, String endpoint, boolean canSendWithoutReceiver)
48          throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51  
52          try
53          {
54              client.send(endpoint, "Test Client Send message", null);
55          }
56          catch (DispatchException e)
57          {
58              if (!canSendWithoutReceiver)
59              {
60                  assertTrue(e.getCause() instanceof NoReceiverForEndpointException);
61              }
62          }
63  
64          Object c = muleContext.getRegistry().lookupObject(component);
65  
66          if (variant.equals(ConfigVariant.SERVICE))
67          {
68              ((Startable) c).start();
69          }
70  
71          MuleMessage message = client.send(endpoint, "Test Client Send message", null);
72          assertNotNull(message);
73          assertEquals("Received: Test Client Send message", message.getPayloadAsString());
74  
75          // The SpringRegistry is read-only so we can't unregister the service!
76          // muleContext.getRegistry().unregisterComponent("vmComponent");
77          ((Stoppable) c).stop();
78  
79          try
80          {
81              client.send(endpoint, "Test Client Send message", null);
82          }
83          catch (DispatchException e)
84          {
85              if (!canSendWithoutReceiver)
86              {
87                  assertTrue(e.getCause() instanceof NoReceiverForEndpointException);
88              }
89          }
90      }
91  
92      @Test
93      public void testRegisterListenerVm() throws Exception
94      {
95          doTestRegisterListener("vmComponent", "vm://test.queue", false);
96      }
97  
98      @Test
99      public void testRegisterListenerTcp() throws Exception
100     {
101         doTestRegisterListener("tcpComponent", "tcp://localhost:56324", true);
102     }
103 }