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