~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/bisocket/ServerTimerReuseTestCase.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 2005, 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.bisocket;
 
23
 
 
24
import java.lang.reflect.Field;
 
25
import java.net.InetAddress;
 
26
import java.util.HashMap;
 
27
import java.util.Map;
 
28
import java.util.Timer;
 
29
 
 
30
import javax.management.MBeanServer;
 
31
 
 
32
import junit.framework.TestCase;
 
33
 
 
34
import org.apache.log4j.ConsoleAppender;
 
35
import org.apache.log4j.Level;
 
36
import org.apache.log4j.Logger;
 
37
import org.apache.log4j.PatternLayout;
 
38
import org.jboss.remoting.Client;
 
39
import org.jboss.remoting.InvocationRequest;
 
40
import org.jboss.remoting.InvokerLocator;
 
41
import org.jboss.remoting.ServerInvocationHandler;
 
42
import org.jboss.remoting.ServerInvoker;
 
43
import org.jboss.remoting.callback.Callback;
 
44
import org.jboss.remoting.callback.HandleCallbackException;
 
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.bisocket.Bisocket;
 
49
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
 
50
 
 
51
 
 
52
/**
 
53
 * ServerTimerReuseTestCase verifies that BisocketServerInvoker.start() reuses an
 
54
 * existing static Timer if possible.  See JBREM-778.
 
55
 * 
 
56
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
57
 * @version $Revision: 2900 $
 
58
 * <p>
 
59
 * Copyright Jul 20, 2007
 
60
 * </p>
 
61
 */
 
62
public class ServerTimerReuseTestCase extends TestCase
 
63
{
 
64
   private static Logger log = Logger.getLogger(ServerTimerReuseTestCase.class);
 
65
   
 
66
   private static boolean firstTime = true;
 
67
   
 
68
   // remoting server connector
 
69
   private Connector connector;
 
70
   private InvokerLocator serverLocator;
 
71
   private TestInvocationHandler invocationHandler;
 
72
 
 
73
   
 
74
   /**
 
75
    * Sets up target remoting server.
 
76
    */
 
77
   public void setUp() throws Exception
 
78
   {
 
79
      if (firstTime)
 
80
      {
 
81
         firstTime = false;
 
82
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
 
83
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
84
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
85
         PatternLayout layout = new PatternLayout(pattern);
 
86
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
87
         Logger.getRootLogger().addAppender(consoleAppender);  
 
88
      }
 
89
   }
 
90
 
 
91
   
 
92
   public void tearDown()
 
93
   {
 
94
   }
 
95
   
 
96
   
 
97
   /**
 
98
    * Verifies that the Timer used by callback BisocketServerInvoker's is reused.
 
99
    */
 
100
   public void testTimerReuse() throws Throwable
 
101
   {
 
102
      log.info("entering " + getName());
 
103
      
 
104
      // Start server.
 
105
      String host = InetAddress.getLocalHost().getHostAddress();
 
106
      int port = PortUtil.findFreePort(host);
 
107
      String locatorURI = getTransport() + "://" + host + ":" + port; 
 
108
      serverLocator = new InvokerLocator(locatorURI);
 
109
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
110
      HashMap config = new HashMap();
 
111
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
112
      addExtraServerConfig(config);
 
113
      connector = new Connector(serverLocator, config);
 
114
      connector.create();
 
115
      invocationHandler = new TestInvocationHandler();
 
116
      connector.addInvocationHandler("sample", invocationHandler);
 
117
      connector.start();
 
118
      
 
119
      // Create client.
 
120
      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI + "/?timeout=5000");
 
121
      HashMap clientConfig = new HashMap();
 
122
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
123
      addExtraClientConfig(clientConfig);
 
124
      Client client = new Client(clientLocator1, clientConfig);
 
125
      client.connect();
 
126
      log.info("client is connected");
 
127
      
 
128
      // Test connections.
 
129
      assertEquals("abc", client.invoke("abc"));
 
130
      log.info("connection is good");
 
131
      
 
132
      // Add first callback handler and get Timer.
 
133
      TestCallbackHandler callbackHandler1 = new TestCallbackHandler();
 
134
      HashMap metadata = new HashMap();
 
135
      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
 
136
      client.addListener(callbackHandler1, metadata);
 
137
      Field field = BisocketServerInvoker.class.getDeclaredField("timer");
 
138
      field.setAccessible(true);
 
139
      Timer timer1 = (Timer) field.get(null);
 
140
      
 
141
      // Add second callback handler and verify Timer is unchanged.
 
142
      TestCallbackHandler callbackHandler2 = new TestCallbackHandler();
 
143
      metadata.clear();
 
144
      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
 
145
      client.addListener(callbackHandler2, metadata);
 
146
      Timer timer2 = (Timer) field.get(null);
 
147
      assertEquals(timer1, timer2);
 
148
      
 
149
      // Shut down.
 
150
      client.removeListener(callbackHandler1);
 
151
      client.removeListener(callbackHandler2);
 
152
      client.disconnect();
 
153
      connector.stop();
 
154
      log.info(getName() + " PASSES");
 
155
   }
 
156
   
 
157
   
 
158
   protected String getTransport()
 
159
   {
 
160
      return "bisocket";
 
161
   }
 
162
   
 
163
   
 
164
   protected void addExtraClientConfig(Map config) {}
 
165
   protected void addExtraServerConfig(Map config) {}
 
166
   
 
167
 
 
168
   static class TestInvocationHandler implements ServerInvocationHandler
 
169
   {
 
170
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
171
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
172
      {
 
173
         return invocation.getParameter();
 
174
      }
 
175
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
176
      public void setMBeanServer(MBeanServer server) {}
 
177
      public void setInvoker(ServerInvoker invoker) {}
 
178
   }
 
179
   
 
180
   
 
181
   static class TestCallbackHandler implements InvokerCallbackHandler
 
182
   {
 
183
      public void handleCallback(Callback callback) throws HandleCallbackException
 
184
      {
 
185
         log.info("received callback");
 
186
      }  
 
187
   }
 
188
}
 
 
b'\\ No newline at end of file'