1 45 package org.exolab.jms.net.invoke; 46 47 import java.util.Arrays ; 48 import java.util.Map ; 49 import java.util.Properties ; 50 import java.util.HashMap ; 51 import java.io.IOException ; 52 53 import org.apache.commons.logging.Log; 54 import org.apache.commons.logging.LogFactory; 55 import junit.framework.TestCase; 56 57 import org.exolab.jms.net.EchoServer; 58 import org.exolab.jms.net.EchoService; 59 import org.exolab.jms.net.EchoServiceImpl; 60 import org.exolab.jms.net.jvm.JVM; 61 import org.exolab.jms.net.orb.ORB; 62 import org.exolab.jms.net.orb.ORBFactory; 63 import org.exolab.jms.net.proxy.Proxy; 64 import org.exolab.jms.net.registry.Registry; 65 import org.exolab.jms.net.util.SSLUtil; 66 67 68 74 public abstract class InvokeTestCase extends TestCase { 75 76 79 private final String _uri; 80 81 84 private final String _routeURI; 85 86 89 private final boolean _embeddedService; 90 91 95 private final Map _connectionProps; 96 97 101 private final Map _acceptorProps; 102 103 106 private ORB _orb; 107 108 111 private Throwable _failure; 112 113 117 private JVM _jvm; 118 119 122 private static final Log _log = LogFactory.getLog(InvokeTestCase.class); 123 124 127 private static final String ECHO_SERVICE = "echo"; 128 129 130 138 public InvokeTestCase(String name, String uri, boolean embeddedService) { 139 this(name, uri, embeddedService, null); 140 } 141 142 151 public InvokeTestCase(String name, String uri, boolean embeddedService, 152 Map properties) { 153 this(name, uri, null, embeddedService, properties); 154 } 155 156 165 public InvokeTestCase(String name, String uri, String routeURI, 166 boolean embeddedService) { 167 this(name, uri, routeURI, embeddedService, null); 168 } 169 170 180 public InvokeTestCase(String name, String uri, String routeURI, 181 boolean embeddedService, Map properties) { 182 this(name, uri, routeURI, embeddedService, properties, properties); 183 } 184 185 196 public InvokeTestCase(String name, String uri, String routeURI, 197 boolean embeddedService, 198 Map connectionProps, Map acceptorProps) { 199 super(name); 200 _uri = uri; 201 _routeURI = routeURI; 202 _embeddedService = embeddedService; 203 _connectionProps = connectionProps; 204 _acceptorProps = acceptorProps; 205 } 206 207 212 public void testPrimitives() throws Exception { 213 Registry registry = _orb.getRegistry(getConnectionProperties()); 214 EchoService echo = (EchoService) registry.lookup(ECHO_SERVICE); 215 216 assertEquals(true, echo.echoBoolean(true)); 217 assertEquals(false, echo.echoBoolean(false)); 218 219 assertEquals(Byte.MIN_VALUE, echo.echoByte(Byte.MIN_VALUE)); 220 assertEquals(Byte.MAX_VALUE, echo.echoByte(Byte.MAX_VALUE)); 221 222 assertEquals(Character.MIN_VALUE, echo.echoChar(Character.MIN_VALUE)); 223 assertEquals(Character.MAX_VALUE, echo.echoChar(Character.MAX_VALUE)); 224 225 assertEquals(Short.MIN_VALUE, echo.echoShort(Short.MIN_VALUE)); 226 assertEquals(Short.MAX_VALUE, echo.echoShort(Short.MAX_VALUE)); 227 228 assertEquals(Integer.MIN_VALUE, echo.echoInt(Integer.MIN_VALUE)); 229 assertEquals(Integer.MAX_VALUE, echo.echoInt(Integer.MAX_VALUE)); 230 231 assertEquals(Long.MIN_VALUE, echo.echoLong(Long.MIN_VALUE)); 232 assertEquals(Long.MAX_VALUE, echo.echoLong(Long.MAX_VALUE)); 233 234 assertEquals(Float.MIN_VALUE, echo.echoFloat(Float.MIN_VALUE), 0.0f); 235 assertEquals(Float.MAX_VALUE, echo.echoFloat(Float.MAX_VALUE), 0.0f); 236 237 assertEquals(Double.MIN_VALUE, echo.echoDouble(Double.MIN_VALUE), 0.0); 238 assertEquals(Double.MAX_VALUE, echo.echoDouble(Double.MAX_VALUE), 0.0); 239 } 240 241 246 public void testPrimitiveArrays() throws Exception { 247 final int size = 4096; 248 249 Registry registry = _orb.getRegistry(getConnectionProperties()); 250 EchoService echo = (EchoService) registry.lookup(ECHO_SERVICE); 251 252 byte[] bytes = new byte[size]; 254 for (int i = 0; i < bytes.length; ++i) { 255 bytes[i] = (byte) i; 256 } 257 byte[] bytesResult = (byte[]) echo.echoObject(bytes); 258 assertTrue(Arrays.equals(bytes, bytesResult)); 259 260 int[] ints = new int[size]; 262 for (int i = 0; i < ints.length; ++i) { 263 ints[i] = (int) i; 264 } 265 int[] intsResult = (int[]) echo.echoObject(ints); 266 assertTrue(Arrays.equals(ints, intsResult)); 267 268 float[] floats = new float[size]; 270 for (int i = 0; i < floats.length; ++i) { 271 floats[i] = i; 272 } 273 float[] floatsResult = (float[]) echo.echoObject(floats); 274 assertTrue(Arrays.equals(floats, floatsResult)); 275 } 276 277 282 public void testConcurrency() throws Exception { 283 Thread [] threads = new Thread [10]; 284 285 Registry registry = _orb.getRegistry(getConnectionProperties()); 286 EchoService echo = (EchoService) registry.lookup(ECHO_SERVICE); 287 288 for (int i = 0; i < threads.length; ++i) { 289 threads[i] = new Thread (new IntInvoker(echo, i, 1000)); 290 } 291 292 for (int i = 0; i < threads.length; ++i) { 293 threads[i].start(); 294 } 295 296 for (int i = 0; i < threads.length; ++i) { 297 try { 298 threads[i].join(); 299 } catch (InterruptedException ignore) { 300 } 301 } 302 if (_failure != null) { 303 if (_failure instanceof Error ) { 304 throw (Error ) _failure; 305 } else if (_failure instanceof Exception ) { 306 throw (Exception ) _failure; 307 } else { 308 throw new Exception ("testConcurrency failed: " 309 + _failure.getMessage()); 310 } 311 } 312 } 313 314 322 protected Map getConnectionProperties() throws IOException { 323 Map properties = new HashMap (); 324 properties.put(ORB.PROVIDER_URI, getServerURI()); 325 if (_connectionProps != null) { 326 properties.putAll(_connectionProps); 327 } 328 return properties; 329 } 330 331 338 protected Map getAcceptorProperties() throws Exception { 339 Map properties = new HashMap (); 340 properties.put(ORB.PROVIDER_URI, _uri); 341 if (_acceptorProps != null) { 342 properties.putAll(_acceptorProps); 343 } 344 return properties; 345 } 346 347 352 protected String getServerURI() { 353 return (_routeURI != null) ? _routeURI : _uri; 354 } 355 356 361 protected void setUp() throws Exception { 362 _log.debug("setUp() [test=" + getName() + ", uri=" + _uri + "]"); 363 _orb = ORBFactory.createORB(getAcceptorProperties()); 364 if (_routeURI != null) { 365 _orb.addRoute(_uri, _routeURI); 366 } 367 368 if (_embeddedService) { 369 Registry serverRegistry = _orb.getRegistry(); 370 371 Proxy proxy = _orb.exportObject(new EchoServiceImpl()); 372 serverRegistry.bind(ECHO_SERVICE, proxy); 373 } else { 374 Properties props = new Properties (); 375 final String key = "log4j.configuration"; 376 String log4j = System.getProperty(key); 377 if (log4j != null) { 378 props.setProperty("log4j.configuration", key); 379 } 380 _jvm = new JVM(EchoServer.class.getName(), null, props, 381 getServerURI()); 382 _jvm.start(); 383 Thread.sleep(2000); 385 } 386 } 387 388 393 protected void tearDown() throws Exception { 394 _log.debug("tearDown() [test=" + getName() + ", uri=" + _uri + "]"); 395 _orb.shutdown(); 396 if (!_embeddedService && _jvm != null) { 397 _jvm.stop(); 398 _jvm.waitFor(); 399 } 400 401 SSLUtil.clearProperties(); 403 } 404 405 408 private class IntInvoker implements Runnable { 409 410 413 private final EchoService _echo; 414 415 418 private final int _value; 419 420 423 private final int _count; 424 425 426 433 public IntInvoker(EchoService echo, int value, int count) { 434 _echo = echo; 435 _value = value; 436 _count = count; 437 } 438 439 442 public void run() { 443 try { 444 for (int i = 0; i < _count; ++i) { 445 assertEquals(_value, _echo.echoInt(_value)); 446 } 447 } catch (Throwable exception) { 448 _failure = exception; 449 } 450 } 451 } 452 } 453 | Popular Tags |