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