View Javadoc

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