View Javadoc

1   /*
2    * $Id: FixedHostRmiClienSocketFactoryTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
11  package org.mule.management.agents;
12  
13  import org.mule.module.management.agent.FixedHostRmiClientSocketFactory;
14  import org.mule.tck.AbstractMuleTestCase;
15  
16  import java.net.InetAddress;
17  import java.net.InetSocketAddress;
18  import java.net.ServerSocket;
19  import java.net.Socket;
20  import java.nio.channels.ServerSocketChannel;
21  
22  public class FixedHostRmiClienSocketFactoryTestCase extends AbstractMuleTestCase
23  {
24      private static final int TEST_PORT = 60504;
25      protected volatile ServerSocket serverSocket;
26  
27      protected void doSetUp () throws Exception
28      {
29          super.doSetUp();
30          setupDummyServer();
31      }
32  
33      protected void doTearDown () throws Exception
34      {
35          super.doTearDown();
36          if (null != serverSocket)
37          {
38              serverSocket.close();
39          }
40      }
41  
42      public void testHostConstructorOverride () throws Exception
43      {
44          final String overrideHost = "127.0.0.1";
45          final FixedHostRmiClientSocketFactory factory =
46                  new FixedHostRmiClientSocketFactory(overrideHost);
47  
48          assertEquals(overrideHost, factory.getOverrideHost());
49          final Socket clientSocket = factory.createSocket("www.example.com", TEST_PORT);
50          final InetAddress address = clientSocket.getInetAddress();
51          final String socketHost = address.getHostAddress();
52          assertEquals(overrideHost, socketHost);
53      }
54  
55      /**
56       * Setter property may be used to dynamically switch the client socket host.
57       */
58      public void testHostSetterOverride () throws Exception
59      {
60          final String overrideHost = "127.0.0.1";
61          final FixedHostRmiClientSocketFactory factory =
62                  new FixedHostRmiClientSocketFactory();
63          factory.setOverrideHost(overrideHost);
64  
65          assertEquals(overrideHost, factory.getOverrideHost());
66          Socket clientSocket = null;
67          try
68          {
69              clientSocket = factory.createSocket("www.example.com", TEST_PORT);
70              final InetAddress address = clientSocket.getInetAddress();
71              final String socketHost = address.getHostAddress();
72              assertEquals(overrideHost, socketHost);
73          }
74          finally
75          {
76              if (null != clientSocket && !clientSocket.isClosed())
77              {
78                  clientSocket.close();
79              }
80          }
81      }
82  
83      /**
84       * Simple socket to have something to ping.
85       */
86      protected void setupDummyServer () throws Exception
87      {
88          ServerSocketChannel ssChannel = ServerSocketChannel.open();
89          ssChannel.configureBlocking(false);
90          serverSocket = ssChannel.socket();
91          serverSocket.bind(new InetSocketAddress(TEST_PORT));
92      }
93  }