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;
8   
9   import org.mule.ResponseOutputStream;
10  import org.mule.tck.junit4.FunctionalTestCase;
11  import org.mule.transport.tcp.protocols.AbstractByteProtocol;
12  import org.mule.transport.tcp.protocols.CustomClassLoadingLengthProtocol;
13  
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.io.OutputStream;
17  import java.net.Socket;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  public class TcpNamespaceHandlerTestCase extends FunctionalTestCase
27  {
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "tcp-namespace-config.xml";
33      }
34  
35      @Test
36      public void testConfig() throws Exception
37      {
38          TcpConnector c = lookupTcpConnector("tcpConnector");
39          assertNotNull(c);
40          assertEquals(1024, c.getReceiveBufferSize());
41          assertEquals(2048, c.getSendBufferSize());
42          assertEquals(50, c.getReceiveBacklog());
43          assertFalse(c.isReuseAddress().booleanValue());
44          // this is what we want - i was worried that the client was used as default if the server
45          // wasn't set, but that's not the case
46          assertEquals(-1, c.getServerSoTimeout());
47          assertEquals(3000, c.getClientSoTimeout());
48          assertEquals(3000, c.getSocketMaxWait());
49          assertTrue(c.isKeepAlive());
50          assertTrue(c.isConnected());
51          assertTrue(c.isStarted());
52  
53          assertEquals(c.getSocketFactory().getClass(), TcpSocketFactory.class);
54          assertFalse(((AbstractByteProtocol) c.getTcpProtocol()).isRethrowExceptionOnRead());
55      }
56      
57      @Test
58      public void testSeparateTimeouts() throws Exception
59      {
60          TcpConnector c = lookupTcpConnector("separateTimeouts");
61          assertNotNull(c);
62          assertEquals(4000, c.getServerSoTimeout());
63          assertEquals(3000, c.getClientSoTimeout());
64          assertEquals(-1, c.getSocketMaxWait());
65          assertTrue(c.isConnected());
66          assertTrue(c.isStarted());
67      }
68      
69      @Test
70      public void testTcpProtocolWithClass()
71      {
72          TcpConnector connector = lookupTcpConnector("connectorWithProtocolClass");
73          assertTrue(connector.getTcpProtocol() instanceof MockTcpProtocol);
74      }
75      
76      @Test
77      public void testTcpProtocolWithRef()
78      {
79          TcpConnector connector = lookupTcpConnector("connectorWithProtocolRef");
80          assertTrue(connector.getTcpProtocol() instanceof MockTcpProtocol);
81      }
82      
83      private TcpConnector lookupTcpConnector(String name)
84      {
85          TcpConnector connector = (TcpConnector)muleContext.getRegistry().lookupConnector(name);
86          assertNotNull(connector);
87          return connector;
88      }
89      
90      public static class MockTcpProtocol implements TcpProtocol
91      {
92          public ResponseOutputStream createResponse(Socket socket) throws IOException
93          {
94              throw new UnsupportedOperationException("createResponse");
95          }
96  
97          public Object read(InputStream is) throws IOException
98          {
99              throw new UnsupportedOperationException("read");
100         }
101 
102         public void write(OutputStream os, Object data) throws IOException
103         {
104             throw new UnsupportedOperationException("write");
105         }
106     }
107     
108     @Test
109     public void testPollingConnector()
110     {
111         PollingTcpConnector c = (PollingTcpConnector)muleContext.getRegistry().lookupConnector("pollingConnector");
112         assertNotNull(c);
113         assertEquals(4000, c.getPollingFrequency());
114         assertEquals(3000, c.getClientSoTimeout());
115         assertEquals(-1, c.getSocketMaxWait());
116         assertTrue(c.isConnected());
117         assertTrue(c.isStarted());
118     }
119     
120     @Test
121     public void testCustomClassLoadingProtocol() throws Exception
122     {
123         TcpConnector c = (TcpConnector)muleContext.getRegistry().lookupConnector("custom-class-loading-protocol-connector");
124         assertNotNull(c);
125         CustomClassLoadingLengthProtocol protocol = (CustomClassLoadingLengthProtocol) c.getTcpProtocol();
126         assertEquals(protocol.getClass(), CustomClassLoadingLengthProtocol.class);
127         assertEquals(protocol.getClassLoader(), muleContext.getRegistry().get("classLoader"));
128         assertTrue(((AbstractByteProtocol) c.getTcpProtocol()).isRethrowExceptionOnRead());
129     }
130     
131     @Test
132     public void testMessageDispatcherFactoryConnector() throws Exception {
133         TcpConnector c = (TcpConnector)muleContext.getRegistry().lookupConnector("messageDispatcherFactoryConnector");
134         assertNotNull(c);
135         assertEquals(LocalSocketTcpMessageDispatcherFactory.class, c.getDispatcherFactory().getClass());
136     }
137 }