View Javadoc

1   /*
2    * $Id: TcpRemoteSyncTestCase.java 22450 2011-07-19 08:20:41Z 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.transport.tcp;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.AbstractServiceAndFlowTestCase;
19  import org.mule.tck.junit4.rule.DynamicPort;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.junit.Rule;
27  import org.junit.Test;
28  import org.junit.runners.Parameterized.Parameters;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  
33  public class TcpRemoteSyncTestCase extends AbstractServiceAndFlowTestCase
34  {
35      public static final String message = "mule";
36  
37      @Rule
38      public DynamicPort dynamicPort1 = new DynamicPort("port1");
39  
40      @Rule
41      public DynamicPort dynamicPort2 = new DynamicPort("port2");
42  
43      @Rule
44      public DynamicPort dynamicPort3 = new DynamicPort("port3");
45  
46  
47      public TcpRemoteSyncTestCase(ConfigVariant variant, String configResources)
48      {
49          super(variant, configResources);
50      }
51  
52      @Parameters
53      public static Collection<Object[]> parameters()
54      {
55          return Arrays.asList(new Object[][]{{ConfigVariant.SERVICE, "tcp-remotesync-service.xml"},
56              {ConfigVariant.FLOW, "tcp-remotesync-flow.xml"}});
57      }
58  
59      @Test
60      public void testTcpTcpRemoteSync() throws Exception
61      {
62          MuleClient client = new MuleClient(muleContext);
63          Map<String, Object> props = new HashMap<String, Object>();
64  
65          // must notify the client to wait for a response from the server
66          props.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, Boolean.TRUE);
67          MuleMessage reply = client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("echoInTcp")).getAddress(), 
68              new DefaultMuleMessage(message, muleContext), props);
69  
70          assertNotNull(reply);
71          assertNotNull(reply.getPayload());
72          assertEquals("Received: " + message, reply.getPayloadAsString());
73      }
74  
75      @Test
76      public void testTcpVmRemoteSync() throws Exception
77      {
78          MuleClient client = new MuleClient(muleContext);
79          Map<String, Object> props = new HashMap<String, Object>();
80  
81          //must notify the client to wait for a response from the server
82          props.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, Boolean.TRUE);
83  
84          MuleMessage reply = client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("echo2InTcp")).getAddress(),
85              new DefaultMuleMessage(message, muleContext), props);
86  
87          assertNotNull(reply);
88          assertNotNull(reply.getPayload());
89          assertEquals("Received: " + message, reply.getPayloadAsString());
90      }
91  
92  }