View Javadoc

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