View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.tcp.integration;
8   
9   import static org.junit.Assert.assertEquals;
10  
11  import org.mule.DefaultMuleMessage;
12  import org.mule.api.MuleException;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.client.LocalMuleClient;
15  import org.mule.api.transformer.wire.WireFormat;
16  import org.mule.tck.junit4.FunctionalTestCase;
17  import org.mule.tck.junit4.rule.DynamicPort;
18  import org.mule.transformer.wire.SerializedMuleMessageWireFormat;
19  import org.mule.transport.tcp.TcpProtocol;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  import java.net.Socket;
25  
26  import org.junit.Rule;
27  import org.junit.Test;
28  
29  public abstract class AbstractMuleMessageProtocolReadTestCase extends FunctionalTestCase
30  {
31  
32      @Rule
33      public DynamicPort port = new DynamicPort("port");
34  
35  
36      @Test
37      public void testServer() throws Exception
38      {
39          LocalMuleClient client = muleContext.getClient();
40          safeProtocolSend("localhost", port.getNumber(), new DefaultMuleMessage(TEST_MESSAGE, muleContext));
41          MuleMessage response = client.request("vm://testOut", RECEIVE_TIMEOUT);
42          assertEquals(TEST_MESSAGE, response.getPayload());
43      }
44  
45      private void safeProtocolSend(String host, int port, DefaultMuleMessage msg) throws IOException, MuleException
46      {
47          Socket clientSocket = new Socket(host, port);
48          DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
49  
50          ByteArrayOutputStream baos = new ByteArrayOutputStream();
51          WireFormat wireFormat = new SerializedMuleMessageWireFormat();
52          wireFormat.setMuleContext(muleContext);
53          wireFormat.write(baos, msg, msg.getEncoding());
54          TcpProtocol delegate = createMuleMessageProtocol();
55          delegate.write(outToServer, baos.toByteArray());
56          clientSocket.close();
57      }
58  
59      protected abstract TcpProtocol createMuleMessageProtocol();
60  
61  }