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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/socket/shutdown/ProcessInvocationShutdownTestCase.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
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2009, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* 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
package org.jboss.test.remoting.transport.socket.shutdown;
 
23
 
 
24
import java.io.IOException;
 
25
import java.net.InetAddress;
 
26
import java.security.AccessController;
 
27
import java.security.PrivilegedAction;
 
28
import java.util.HashMap;
 
29
import java.util.Map;
 
30
 
 
31
import javax.management.MBeanServer;
 
32
 
 
33
import junit.framework.TestCase;
 
34
 
 
35
import org.apache.log4j.ConsoleAppender;
 
36
import org.apache.log4j.Level;
 
37
import org.apache.log4j.Logger;
 
38
import org.apache.log4j.PatternLayout;
 
39
import org.jboss.remoting.Client;
 
40
import org.jboss.remoting.InvocationRequest;
 
41
import org.jboss.remoting.InvokerLocator;
 
42
import org.jboss.remoting.InvokerRegistry;
 
43
import org.jboss.remoting.ServerInvocationHandler;
 
44
import org.jboss.remoting.ServerInvoker;
 
45
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
46
import org.jboss.remoting.transport.Connector;
 
47
import org.jboss.remoting.transport.PortUtil;
 
48
import org.jboss.remoting.transport.ServerFactory;
 
49
import org.jboss.remoting.transport.socket.LRUPool;
 
50
import org.jboss.remoting.transport.socket.SocketServerInvoker;
 
51
import org.jboss.remoting.transport.socket.TransportClientFactory;
 
52
 
 
53
 
 
54
/**
 
55
 * Unit test for JBREM-1076.
 
56
 * 
 
57
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
58
 * @version $Rev$
 
59
 * <p>
 
60
 * Copyright Apr 12, 2009
 
61
 * </p>
 
62
 */
 
63
public class ProcessInvocationShutdownTestCase extends TestCase
 
64
{
 
65
   private static Logger log = Logger.getLogger(ProcessInvocationShutdownTestCase.class);
 
66
   
 
67
   private static boolean firstTime = true;
 
68
   
 
69
   protected String host;
 
70
   protected int port;
 
71
   protected String locatorURI;
 
72
   protected InvokerLocator serverLocator;
 
73
   protected Connector connector;
 
74
   protected TestInvocationHandler invocationHandler;
 
75
   protected SocketServerInvoker socketServerInvoker;
 
76
 
 
77
   
 
78
   public void setUp() throws Exception
 
79
   {
 
80
      if (firstTime)
 
81
      {
 
82
         firstTime = false;
 
83
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
 
84
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
85
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
86
         PatternLayout layout = new PatternLayout(pattern);
 
87
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
88
         Logger.getRootLogger().addAppender(consoleAppender);  
 
89
      }
 
90
   }
 
91
 
 
92
   
 
93
   public void tearDown()
 
94
   {
 
95
   }
 
96
   
 
97
   
 
98
   public void testShutdown() throws Throwable
 
99
   {
 
100
      log.info("entering " + getName());
 
101
      
 
102
      // Start server.
 
103
      setupServer();
 
104
      assertTrue(connector.getServerInvoker() instanceof TestServerInvoker);
 
105
      log.info("using a TestServerInvoker");
 
106
      
 
107
      // Create client.
 
108
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
109
      HashMap clientConfig = new HashMap();
 
110
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
111
      addExtraClientConfig(clientConfig);
 
112
      final Client client = new Client(clientLocator, clientConfig);
 
113
      client.connect();
 
114
      log.info("client is connected");
 
115
      
 
116
      // Start SocketServerInvoker.processInvocation().
 
117
      new Thread()
 
118
      {
 
119
         public void run()
 
120
         {
 
121
            try
 
122
            {
 
123
               client.invoke("abc");
 
124
            }
 
125
            catch (Throwable e)
 
126
            {
 
127
               e.printStackTrace();
 
128
            }
 
129
         }
 
130
      }.start();
 
131
      
 
132
      Thread.sleep(4000);
 
133
      shutdownServer();
 
134
      Thread.sleep(4000);
 
135
      assertFalse(TestLRUPool.called);
 
136
      log.info(getName() + " PASSES");
 
137
   }
 
138
   
 
139
   
 
140
   protected String getTransport()
 
141
   {
 
142
      return "test";
 
143
   }
 
144
   
 
145
   
 
146
   protected void addExtraClientConfig(Map config) {}
 
147
   protected void addExtraServerConfig(Map config) {}
 
148
   
 
149
 
 
150
   protected void setupServer() throws Exception
 
151
   {
 
152
      AccessController.doPrivileged( new PrivilegedAction()
 
153
      {
 
154
         public Object run()
 
155
         {
 
156
            InvokerRegistry.registerInvokerFactories(getTransport(), TransportClientFactory.class, TestTransportServerFactory.class);
 
157
            return null;
 
158
         }
 
159
      });
 
160
      
 
161
      host = InetAddress.getLocalHost().getHostAddress();
 
162
      port = PortUtil.findFreePort(host);
 
163
      locatorURI = getTransport() + "://" + host + ":" + port;
 
164
      String metadata = System.getProperty("remoting.metadata");
 
165
      if (metadata != null)
 
166
      {
 
167
         locatorURI += "/?" + metadata;
 
168
      }
 
169
      serverLocator = new InvokerLocator(locatorURI);
 
170
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
171
      HashMap config = new HashMap();
 
172
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
173
      addExtraServerConfig(config);
 
174
      connector = new Connector(serverLocator, config);
 
175
      connector.create();
 
176
      invocationHandler = new TestInvocationHandler();
 
177
      connector.addInvocationHandler("test", invocationHandler);
 
178
      connector.start();
 
179
      socketServerInvoker = (SocketServerInvoker) connector.getServerInvoker();
 
180
      socketServerInvoker.setMaxPoolSize(0);
 
181
   }
 
182
   
 
183
   
 
184
   protected void shutdownServer() throws Exception
 
185
   {
 
186
      if (connector != null)
 
187
         connector.stop();
 
188
   }
 
189
   
 
190
   
 
191
   static class TestInvocationHandler implements ServerInvocationHandler
 
192
   {
 
193
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
194
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
195
      {
 
196
         return invocation.getParameter();
 
197
      }
 
198
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
199
      public void setMBeanServer(MBeanServer server) {}
 
200
      public void setInvoker(ServerInvoker invoker) {}
 
201
   }
 
202
 
 
203
   static class TestLRUPool extends LRUPool
 
204
   {
 
205
      static public boolean called;
 
206
      
 
207
      public TestLRUPool(int min, int max)
 
208
      {
 
209
         super(min, max);
 
210
      }  
 
211
      public void insert(Object key, Object o)
 
212
      {
 
213
         log.info(this + ".insert() called");
 
214
         called = true;
 
215
      }
 
216
   }
 
217
   
 
218
   static class TestServerInvoker extends SocketServerInvoker
 
219
   {
 
220
      public TestServerInvoker(InvokerLocator locator, Map configuration)
 
221
      {
 
222
         super(locator, configuration);
 
223
      }
 
224
      public TestServerInvoker(InvokerLocator locator)
 
225
      {
 
226
         super(locator);
 
227
      }
 
228
      public synchronized void start() throws IOException
 
229
      {
 
230
         super.start();
 
231
         clientpool = new TestLRUPool(2, maxPoolSize);
 
232
         clientpool.create(); 
 
233
      }
 
234
   }
 
235
   
 
236
   static public class TestTransportServerFactory implements ServerFactory
 
237
   {
 
238
      public boolean called;
 
239
      
 
240
      public ServerInvoker createServerInvoker(InvokerLocator locator, Map config)
 
241
      {
 
242
         called = true;
 
243
         log.info(this + ".createServerInvoker() called");
 
244
         return new TestServerInvoker(locator, config);
 
245
      }
 
246
      public boolean supportsSSL()
 
247
      {
 
248
         return false;
 
249
      }
 
250
   }
 
251
}
 
 
b'\\ No newline at end of file'