~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/socket/ssl/builder/SSLSocketInvokerTestClient.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.jboss.test.remoting.transport.socket.ssl.builder;
2
 
 
3
 
import junit.framework.TestCase;
4
 
import org.apache.log4j.Level;
5
 
import org.jboss.logging.Logger;
6
 
import org.jboss.remoting.Client;
7
 
import org.jboss.remoting.InvocationRequest;
8
 
import org.jboss.remoting.InvokerLocator;
9
 
import org.jboss.remoting.ServerInvocationHandler;
10
 
import org.jboss.remoting.ServerInvoker;
11
 
import org.jboss.remoting.callback.Callback;
12
 
import org.jboss.remoting.callback.HandleCallbackException;
13
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
14
 
import org.jboss.remoting.invocation.NameBasedInvocation;
15
 
import org.jboss.remoting.security.SSLSocketBuilder;
16
 
import org.jboss.remoting.transport.Connector;
17
 
import org.jboss.test.remoting.transport.socket.ssl.SSLInvokerConstants;
18
 
 
19
 
import javax.management.MBeanServer;
20
 
import java.util.HashMap;
21
 
import java.util.Map;
22
 
 
23
 
/**
24
 
 * This is the client for the test to make regular ssl based invocation to the server and
25
 
 * have a ssl based callback server.  The special test in this case is want to have the callback
26
 
 * client that lives on the server to be in client mode so that this client test only has to have
27
 
 * a truststore locally, yet still use ssl.
28
 
 *
29
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
30
 
 */
31
 
public class SSLSocketInvokerTestClient extends TestCase implements SSLInvokerConstants
32
 
{
33
 
   private Client client;
34
 
   private Connector callbackConnector;
35
 
   private static final Logger log = Logger.getLogger(SSLSocketInvokerTestClient.class);
36
 
   private boolean gotCallback = false;
37
 
 
38
 
   public void init() throws Exception
39
 
   {
40
 
      // since doing basic (using default ssl server socket factory)
41
 
      // need to set the system properties to the truststore
42
 
      String trustStoreFilePath = this.getClass().getResource("../.truststore").getFile();
43
 
      System.setProperty("javax.net.ssl.trustStore", trustStoreFilePath);
44
 
      System.setProperty("javax.net.ssl.trustStorePassword", "unit-tests-client");
45
 
//         System.setProperty("javax.net.ssl.trustStorePassword", "secureexample");
46
 
 
47
 
      InvokerLocator locator = new InvokerLocator(getTransport() + "://" + host + ":" + getPort());
48
 
 
49
 
      // create callback connector
50
 
      Map config = new HashMap();
51
 
      config.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
52
 
      callbackConnector = new Connector(getTransport() + "://" + host + ":" + (getPort() + 5), config);
53
 
      callbackConnector.create();
54
 
      callbackConnector.addInvocationHandler("callback", new CallbackInvokerHandler());
55
 
      callbackConnector.start();
56
 
 
57
 
      client = new Client(locator, "mock");
58
 
      client.connect();
59
 
 
60
 
      try
61
 
      {
62
 
         client.addListener(new TestCallbackHandler(), callbackConnector.getLocator());
63
 
      }
64
 
      catch (Throwable throwable)
65
 
      {
66
 
         Exception e = new Exception(throwable.getMessage());
67
 
         e.initCause(throwable);
68
 
         e.printStackTrace();
69
 
         throw e;
70
 
      }
71
 
   }
72
 
 
73
 
   public void testRemoteCall() throws Throwable
74
 
   {
75
 
      log.debug("running testRemoteCall()");
76
 
 
77
 
      // simple invoke, should return bar
78
 
      Object ret = makeInvocation("foo", "bar");
79
 
      assertTrue("Result of testRemoteCall() invocation of foo.", "bar".equals(ret));
80
 
      assertEquals("bar", ret);
81
 
 
82
 
      // this will cause the server to start sending callbacks
83
 
      ret = makeInvocation("test", "foobar");
84
 
 
85
 
      System.out.println("ret = " + ret);
86
 
 
87
 
      Thread.sleep(5000);
88
 
 
89
 
      //DEBUG
90
 
//      Thread.sleep(600000);
91
 
 
92
 
      assertTrue(gotCallback);
93
 
 
94
 
 
95
 
   }
96
 
 
97
 
   protected String getTransport()
98
 
   {
99
 
      return transport;
100
 
   }
101
 
   
102
 
   protected int getPort()
103
 
   {
104
 
      return port;
105
 
   }
106
 
 
107
 
   private Object makeInvocation(String method, String param) throws Throwable
108
 
   {
109
 
      Object ret = client.invoke(new NameBasedInvocation(method,
110
 
                                                         new Object[]{param},
111
 
                                                         new String[]{String.class.getName()}),
112
 
                                 null);
113
 
 
114
 
      return ret;
115
 
   }
116
 
 
117
 
   public void setUp() throws Exception
118
 
   {
119
 
      org.apache.log4j.BasicConfigurator.configure();
120
 
      org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
121
 
      org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
122
 
      org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
123
 
      org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
124
 
 
125
 
      init();
126
 
   }
127
 
 
128
 
   public void tearDown() throws Exception
129
 
   {
130
 
      if (client != null)
131
 
      {
132
 
         client.disconnect();
133
 
         client = null;
134
 
      }
135
 
   }
136
 
 
137
 
   public class TestCallbackHandler implements InvokerCallbackHandler
138
 
   {
139
 
 
140
 
      public void handleCallback(Callback callback) throws HandleCallbackException
141
 
      {
142
 
         System.out.println("TestCallbackHandler.handleCallback() - " + callback);
143
 
         gotCallback = true;
144
 
      }
145
 
   }
146
 
 
147
 
   public class CallbackInvokerHandler implements ServerInvocationHandler
148
 
   {
149
 
      private InvokerCallbackHandler listener;
150
 
 
151
 
      public void setMBeanServer(MBeanServer server)
152
 
      {
153
 
      }
154
 
 
155
 
      public void setInvoker(ServerInvoker invoker)
156
 
      {
157
 
      }
158
 
 
159
 
      public Object invoke(InvocationRequest invocation) throws Throwable
160
 
      {
161
 
         System.out.println("CallbackInvokerHandler.invoke() called with " + invocation.getParameter());
162
 
         return null;
163
 
      }
164
 
 
165
 
      public void addListener(InvokerCallbackHandler callbackHandler)
166
 
      {
167
 
         listener = callbackHandler;
168
 
      }
169
 
 
170
 
      public void removeListener(InvokerCallbackHandler callbackHandler)
171
 
      {
172
 
         listener = null;
173
 
      }
174
 
   }
175
 
}