1   /*
2    * $Id: TcpConnectorFunctionalTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.tcp;
12  
13  import org.mule.MuleManager;
14  import org.mule.impl.ResponseOutputStream;
15  import org.mule.impl.endpoint.MuleEndpointURI;
16  import org.mule.tck.functional.AbstractProviderFunctionalTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalTestComponent;
19  import org.mule.umo.UMOEventContext;
20  import org.mule.umo.endpoint.MalformedEndpointException;
21  import org.mule.umo.endpoint.UMOEndpointURI;
22  import org.mule.umo.provider.UMOConnector;
23  
24  import java.io.BufferedInputStream;
25  import java.io.BufferedOutputStream;
26  import java.io.DataInputStream;
27  import java.io.DataOutputStream;
28  import java.io.IOException;
29  import java.net.Socket;
30  import java.net.URI;
31  
32  public class TcpConnectorFunctionalTestCase extends AbstractProviderFunctionalTestCase
33  {
34      private Socket s;
35      private int port = 61675;
36  
37      protected void doTearDown() throws Exception
38      {
39          if (s != null)
40          {
41              s.close();
42          }
43      }
44  
45      protected void sendTestData(int iterations) throws Exception
46      {
47          MuleManager.getConfiguration().setSynchronous(false);
48          URI uri = getInDest().getUri();
49          for (int i = 0; i < iterations; i++)
50          {
51              s = createSocket(uri);
52              DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
53              dos.write("Hello".getBytes());
54              dos.flush();
55              logger.info("Sent message: " + i);
56              dos.close();
57          }
58      }
59  
60      protected Socket createSocket(URI uri) throws IOException
61      {
62          return new Socket(uri.getHost(), uri.getPort());
63      }
64  
65      protected void receiveAndTestResults() throws Exception
66      {
67          Thread.sleep(3000);
68          assertEquals(100, callbackCount);
69      }
70  
71      protected UMOEndpointURI getInDest()
72      {
73          try
74          {
75              return new MuleEndpointURI("tcp://localhost:" + port);
76          }
77          catch (MalformedEndpointException e)
78          {
79              fail(e.getMessage());
80              return null;
81          }
82      }
83  
84      protected UMOEndpointURI getOutDest()
85      {
86          return null;
87      }
88  
89      public UMOConnector createConnector() throws Exception
90      {
91          TcpConnector connector = new TcpConnector();
92          connector.setName("testTcp");
93          connector.getDispatcherThreadingProfile().setDoThreading(false);
94  
95          return connector;
96      }
97  
98      public void testDispatchAndReply() throws Exception
99      {
100         MuleManager.getConfiguration().setSynchronous(false);
101         descriptor = getTestDescriptor("testComponent", FunctionalTestComponent.class.getName());
102 
103         initialiseComponent(descriptor, new EventCallback()
104         {
105             public void eventReceived(UMOEventContext context, Object component) throws Exception
106             {
107                 callbackCount++;
108                 String result = "Received Async event: " + context.getMessageAsString();
109                 assertNotNull(context.getOutputStream());
110 
111                 if (!((ResponseOutputStream)context.getOutputStream()).getSocket().isClosed())
112                 {
113                     context.getOutputStream().write(result.getBytes());
114                     context.getOutputStream().flush();
115                 }
116 
117                 callbackCalled = true;
118             }
119         });
120         // Start the server
121         MuleManager.getInstance().start();
122 
123         URI uri = getInDest().getUri();
124         s = createSocket(uri);
125         DataOutputStream dos = new DataOutputStream((s.getOutputStream()));
126         dos.write("Hello".getBytes());
127         dos.flush();
128 
129         afterInitialise();
130 
131         DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
132         byte[] buf = new byte[32];
133         int x = dis.read(buf);
134         assertTrue(x > -1);
135         assertTrue(new String(buf, 0, x).startsWith("Received Async event"));
136         assertEquals(1, callbackCount);
137 
138     }
139 }