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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/socket/timeout/SocketDefaultTimeoutTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * JBoss, Home of Professional Open Source.
 
3
 * Copyright 2010, Red Hat Middleware LLC, and individual contributors
 
4
 * as indicated by the @author tags. See the copyright.txt file in the
 
5
 * distribution for a full listing of individual contributors.
 
6
 *
 
7
 * This is free software; you can redistribute it and/or modify it
 
8
 * under the terms of the GNU Lesser General Public License as
 
9
 * published by the Free Software Foundation; either version 2.1 of
 
10
 * the License, or (at your option) any later version.
 
11
 *
 
12
 * This software is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this software; if not, write to the Free
 
19
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
 */
 
22
 
 
23
package org.jboss.test.remoting.transport.socket.timeout;
 
24
 
 
25
import java.io.IOException;
 
26
import java.lang.reflect.Method;
 
27
import java.net.InetAddress;
 
28
import java.security.AccessController;
 
29
import java.security.PrivilegedAction;
 
30
import java.util.HashMap;
 
31
import java.util.Map;
 
32
 
 
33
import javax.management.MBeanServer;
 
34
 
 
35
import junit.framework.TestCase;
 
36
 
 
37
import org.apache.log4j.ConsoleAppender;
 
38
import org.apache.log4j.Level;
 
39
import org.apache.log4j.Logger;
 
40
import org.apache.log4j.PatternLayout;
 
41
import org.jboss.logging.XLevel;
 
42
import org.jboss.remoting.Client;
 
43
import org.jboss.remoting.InvocationRequest;
 
44
import org.jboss.remoting.InvokerLocator;
 
45
import org.jboss.remoting.InvokerRegistry;
 
46
import org.jboss.remoting.ServerInvocationHandler;
 
47
import org.jboss.remoting.ServerInvoker;
 
48
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
49
import org.jboss.remoting.transport.ClientFactory;
 
50
import org.jboss.remoting.transport.ClientInvoker;
 
51
import org.jboss.remoting.transport.Connector;
 
52
import org.jboss.remoting.transport.PortUtil;
 
53
import org.jboss.remoting.transport.ServerFactory;
 
54
import org.jboss.remoting.transport.socket.ServerAddress;
 
55
import org.jboss.remoting.transport.socket.SocketClientInvoker;
 
56
import org.jboss.remoting.transport.socket.SocketServerInvoker;
 
57
 
 
58
 
 
59
/**
 
60
 * Unit tests for JBREM-1188.
 
61
 * 
 
62
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
63
 * @version $Revision: 1.1 $
 
64
 * <p>
 
65
 * Copyright Feb 16, 2010
 
66
 */
 
67
public class SocketDefaultTimeoutTestCase extends TestCase
 
68
{
 
69
   private static Logger log = Logger.getLogger(SocketDefaultTimeoutTestCase.class);
 
70
   
 
71
   private static boolean firstTime = true;
 
72
   
 
73
   protected String host;
 
74
   protected int port;
 
75
   protected String locatorURI;
 
76
   protected InvokerLocator serverLocator;
 
77
   protected Connector connector;
 
78
   protected TestInvocationHandler invocationHandler;
 
79
 
 
80
   
 
81
   public void setUp() throws Exception
 
82
   {
 
83
      if (firstTime)
 
84
      {
 
85
         firstTime = false;
 
86
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
87
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
88
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
89
         PatternLayout layout = new PatternLayout(pattern);
 
90
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
91
         Logger.getRootLogger().addAppender(consoleAppender);  
 
92
      }
 
93
     
 
94
      AccessController.doPrivileged( new PrivilegedAction()
 
95
      {
 
96
         public Object run()
 
97
         {
 
98
            InvokerRegistry.registerInvokerFactories(getTransport(), getClientFactoryClass(), TestServerFactory.class);
 
99
            return null;
 
100
         }
 
101
      });
 
102
   }
 
103
 
 
104
   
 
105
   public void tearDown()
 
106
   {
 
107
   }
 
108
   
 
109
   
 
110
   public void testDefaultTimeout() throws Throwable
 
111
   {
 
112
      log.info("entering " + getName());
 
113
      
 
114
      // Start server.
 
115
      setupServer();
 
116
      
 
117
      // Create client.
 
118
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
119
      HashMap clientConfig = new HashMap();
 
120
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
121
      addExtraClientConfig(clientConfig);
 
122
      Client client = new Client(clientLocator, clientConfig);
 
123
      client.connect();
 
124
      log.info("client is connected");
 
125
      Method getServerAddress = getClientInvokerClass().getMethod("getServerAddress", new Class[]{});
 
126
      getServerAddress.setAccessible(true);
 
127
      ClientInvoker clientInvoker = client.getInvoker();
 
128
      ServerAddress address = (ServerAddress) getServerAddress.invoke(clientInvoker, new Object[]{});
 
129
      log.info("timeout in use: " + address.timeout);
 
130
      assertEquals(SocketClientInvoker.SO_TIMEOUT_DEFAULT, address.timeout);
 
131
      
 
132
      client.disconnect();
 
133
      shutdownServer();
 
134
      log.info(getName() + " PASSES");
 
135
   }
 
136
      
 
137
   
 
138
   protected String getTransport()
 
139
   {
 
140
      return "test";
 
141
   }
 
142
   
 
143
   
 
144
   protected void addExtraClientConfig(Map config) {}
 
145
   protected void addExtraServerConfig(Map config) {}
 
146
   
 
147
 
 
148
   protected void setupServer() throws Exception
 
149
   {
 
150
      host = InetAddress.getLocalHost().getHostAddress();
 
151
      port = PortUtil.findFreePort(host);
 
152
      locatorURI = getTransport() + "://" + host + ":" + port;
 
153
      String metadata = System.getProperty("remoting.metadata");
 
154
      if (metadata != null)
 
155
      {
 
156
         locatorURI += "/?" + metadata;
 
157
      }
 
158
      serverLocator = new InvokerLocator(locatorURI);
 
159
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
160
      HashMap config = new HashMap();
 
161
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
162
      addExtraServerConfig(config);
 
163
      connector = new Connector(serverLocator, config);
 
164
      connector.create();
 
165
      invocationHandler = new TestInvocationHandler();
 
166
      connector.addInvocationHandler("test", invocationHandler);
 
167
      connector.start();
 
168
   }
 
169
   
 
170
   
 
171
   protected void shutdownServer() throws Exception
 
172
   {
 
173
      if (connector != null)
 
174
         connector.stop();
 
175
   }
 
176
   
 
177
   
 
178
   protected Class getClientFactoryClass()
 
179
   {
 
180
      return TestSocketClientFactory.class;
 
181
   }
 
182
   
 
183
   
 
184
   protected Class getClientInvokerClass()
 
185
   {
 
186
      return TestSocketClientInvoker.class;
 
187
   }
 
188
   
 
189
   
 
190
   static class TestInvocationHandler implements ServerInvocationHandler
 
191
   {
 
192
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
193
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
194
      {
 
195
         return invocation.getParameter();
 
196
      }
 
197
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
198
      public void setMBeanServer(MBeanServer server) {}
 
199
      public void setInvoker(ServerInvoker invoker) {}
 
200
   }
 
201
   
 
202
   
 
203
   static public class TestSocketClientInvoker extends SocketClientInvoker
 
204
   {
 
205
      public TestSocketClientInvoker(InvokerLocator locator, Map configuration) throws IOException
 
206
      {
 
207
         super(locator, configuration);
 
208
      }
 
209
      public TestSocketClientInvoker(InvokerLocator locator)
 
210
      {
 
211
         super(locator);
 
212
      }
 
213
      public ServerAddress getServerAddress()
 
214
      {
 
215
         return address;
 
216
      }
 
217
      public String toString()
 
218
      {
 
219
         return "TestSocketClientInvoker";
 
220
      }
 
221
   }
 
222
   
 
223
   
 
224
   public static class TestSocketClientFactory implements ClientFactory
 
225
   {
 
226
      public ClientInvoker createClientInvoker(InvokerLocator locator, Map config) throws IOException
 
227
      {
 
228
         ClientInvoker clientInvoker = new TestSocketClientInvoker(locator, config);
 
229
         log.info("TestClientFaotory.createClientInvoker() returning " + clientInvoker);
 
230
         return clientInvoker;
 
231
      }
 
232
      public boolean supportsSSL()
 
233
      {
 
234
         return false;
 
235
      }  
 
236
   }
 
237
   
 
238
   
 
239
   public static class TestServerFactory implements ServerFactory
 
240
   {
 
241
      public ServerInvoker createServerInvoker(InvokerLocator locator, Map config) throws IOException
 
242
      {
 
243
         log.info("TestServerFactory.createServerInvoker() called");
 
244
         return new SocketServerInvoker(locator, config);
 
245
      }
 
246
      public boolean supportsSSL()
 
247
      {
 
248
         return false;
 
249
      }
 
250
   }
 
251
}
 
 
b'\\ No newline at end of file'