1 package test.crispy.impl; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.List; 6 import java.util.Properties; 7 8 import junit.framework.TestCase; 9 import net.sf.crispy.Property; 10 import net.sf.crispy.impl.JBossRemotingExecutor; 11 import net.sf.crispy.impl.MiniServer; 12 import net.sf.crispy.impl.ServiceManager; 13 import net.sf.crispy.impl.jboss.MiniJBossRemotingServer; 14 import net.sf.crispy.interceptor.StopWatchInterceptor; 15 import net.sf.crispy.strategy.NameSpacePlusMethodInvocationStrategy; 16 import test.crispy.JUnitTestException2; 17 import test.crispy.compatibility.CompatibilityKit; 18 import test.crispy.concurrent.AsynchronousCallbackForTests; 19 import test.crispy.example.model.Kunde; 20 import test.crispy.example.model.Primitive; 21 import test.crispy.example.model.ValidationError; 22 import test.crispy.example.service.Calculator; 23 import test.crispy.example.service.Echo; 24 25 public class JBossRemotingServiceTest extends TestCase { 26 27 private static MiniServer miniServer = null; 28 private CompatibilityKit compatibilityKit = new CompatibilityKit(); 29 30 /** 31 * Init <code>static</code> MiniServer for all tests. 32 */ 33 public JBossRemotingServiceTest() { 34 if (miniServer == null) { 35 miniServer = new MiniJBossRemotingServer(); 36 miniServer.addService("test.crispy.example.service.Echo", "test.crispy.example.service.EchoImpl"); 37 miniServer.addService("test.crispy.example.service.Calculator", "test.crispy.example.service.CalculatorImpl"); 38 39 miniServer.start(); 40 } 41 compatibilityKit.addProperty(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 42 } 43 44 protected void setUp() throws Exception { 45 super.setUp(); 46 compatibilityKit.removeProperty(Property.DYNAMIC_PROXY_CLASS); 47 } 48 49 public void testEchoPrimitive() throws Exception { 50 Properties prop = new Properties(); 51 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 52 53 ServiceManager lvServiceManager = new ServiceManager(prop); 54 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 55 Primitive lvPrimitive = Primitive.createPrimitiveExample(); 56 57 Primitive lvPrimitiveAfter = lvEcho.echoPrimitive(lvPrimitive); 58 assertEquals(lvPrimitive, lvPrimitiveAfter); 59 assertNotSame(lvPrimitive, lvPrimitiveAfter); 60 } 61 62 public void testSimpleRemotePing() throws Exception { 63 Properties prop = new Properties(); 64 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 65 66 ServiceManager manager = new ServiceManager(prop); 67 Echo echo = (Echo) manager.createService(Echo.class); 68 String lvPing = echo.ping(); 69 assertEquals(lvPing, Echo.PING_STRING); 70 } 71 72 public void testSimpleRemoteInvocation() throws Exception { 73 compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager()); 74 } 75 76 77 public void testSimpleRemoteInvocationWithDynamicCglibProxy() throws Exception { 78 compatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY); 79 compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager()); 80 } 81 82 public void testSimpleRemoteInvocationWithDynamicJdkProxy() throws Exception { 83 compatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY); 84 compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager()); 85 } 86 87 public void testSimpleDoubleRemoteInvocation() throws Exception { 88 compatibilityKit.makeMultipleServiceTest(); 89 } 90 91 public void testComplexRemoteInvocation() throws Exception { 92 compatibilityKit.makeComplexEchoTest(compatibilityKit.createServiceManager()); 93 } 94 95 public void testCycleDetectionTest() throws Exception { 96 compatibilityKit.makeCycleDetectionTest(compatibilityKit.createServiceManager()); 97 } 98 99 public void testAddNode() throws Exception { 100 compatibilityKit.makeAddNodeTest(compatibilityKit.createServiceManager()); 101 } 102 103 public void testProxyInterceptor() throws Exception { 104 Properties prop = new Properties(); 105 prop.put(Property.INTERCEPTOR_CLASS, StopWatchInterceptor.class.getName()); 106 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 107 108 ServiceManager lvServiceManager = new ServiceManager(prop); 109 110 Calculator lvCalculator = (Calculator) lvServiceManager.createService(Calculator.class); 111 assertNotNull(lvCalculator); 112 int lvResult = lvCalculator.add(2, 3); 113 assertEquals(lvResult, 5); 114 lvResult = lvCalculator.subtract(8, 2); 115 assertEquals(lvResult, 6); 116 117 118 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 119 assertNotNull(lvEcho); 120 String lvEchoStr = "Hallo Echo"; 121 assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr); 122 } 123 124 public void testModifyService() throws Exception { 125 compatibilityKit.makeModifyServiceTest(compatibilityKit.createServiceManager()); 126 } 127 128 public void testAddLongs() throws Exception { 129 Properties prop = new Properties(); 130 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 131 132 ServiceManager manager = new ServiceManager(prop); 133 Calculator calc = (Calculator) manager.createService(Calculator.class); 134 assertEquals(calc.addLong(new Long(123l), new Long(124l)), new Long(247)); 135 } 136 137 public void testOverloading() throws Exception { 138 Properties prop = new Properties(); 139 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 140 141 ServiceManager manager = new ServiceManager(prop); 142 Calculator lvCalculator = (Calculator) manager.createService(Calculator.class); 143 int lvIntResult = lvCalculator.add(2, 3); 144 assertEquals(lvIntResult, 5); 145 long lvLongResult = lvCalculator.add(21, 32); 146 assertEquals(lvLongResult, 53); 147 double lvDoubleResult = lvCalculator.add(2.3, 3.2); 148 assertEquals(lvDoubleResult, 5.5, 0); 149 } 150 151 public void testOverloadingInteger() throws Exception { 152 Properties prop = new Properties(); 153 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 154 155 ServiceManager manager = new ServiceManager(prop); 156 Echo echo = (Echo) manager.createService(Echo.class); 157 Integer i[] = echo.echoArray(new Integer[] {new Integer(1), new Integer(3), new Integer(5)}); 158 assertTrue(i.length == 3); 159 assertEquals(i[0], new Integer(1)); 160 assertEquals(i[1], new Integer(3)); 161 assertEquals(i[2], new Integer(5)); 162 } 163 164 public void testOverloadingLong() throws Exception { 165 Properties prop = new Properties(); 166 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 167 168 ServiceManager manager = new ServiceManager(prop); 169 Echo echo = (Echo) manager.createService(Echo.class); 170 Long l[] = echo.echoArray(new Long[] {new Long(1), new Long(5)}); 171 assertTrue(l.length == 2); 172 assertEquals(l[0], new Long(1)); 173 assertEquals(l[1], new Long(5)); 174 } 175 176 public void testOverloadingObject() throws Exception { 177 Properties prop = new Properties(); 178 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 179 180 ServiceManager manager = new ServiceManager(prop); 181 Echo echo = (Echo) manager.createService(Echo.class); 182 Object o[] = echo.echoArray(new Object[] {"a", new Integer(3), new Long(5)}); 183 assertTrue(o.length == 3); 184 assertEquals(o[0], "a"); 185 assertEquals(o[1], new Integer(3)); 186 assertEquals(o[2], new Long(5)); 187 } 188 189 public void testCreateService() throws Exception { 190 Properties prop = new Properties(); 191 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 192 193 ServiceManager lvManager = new ServiceManager(prop); 194 Calculator lvCalculator1 = (Calculator) lvManager.createService(Calculator.class); 195 Calculator lvCalculator2 = (Calculator) lvManager.createService(Calculator.class); 196 197 assertNotNull(lvCalculator1); 198 assertNotNull(lvCalculator2); 199 assertNotSame(lvCalculator1, lvCalculator2); 200 } 201 202 public void testThrowException() throws Exception { 203 Properties prop = new Properties(); 204 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 205 ServiceManager lvManager = new ServiceManager(prop); 206 207 Echo lvEcho = (Echo) lvManager.createService(Echo.class); 208 boolean throwException = false; 209 try { 210 lvEcho.throwException("A Test-Excption."); 211 } catch (Exception e) { 212 throwException = true; 213 } 214 assertTrue("No Exception thrown: " + throwException, throwException); 215 } 216 217 public void testNullValueParamsWithException() throws Exception { 218 Properties prop = new Properties(); 219 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 220 221 ServiceManager lvManager = new ServiceManager(prop); 222 223 Echo lvEcho = (Echo) lvManager.createService(Echo.class); 224 String s = lvEcho.echo(null); 225 assertNull(s); 226 } 227 228 public void testNullLongValueParams() throws Exception { 229 Properties prop = new Properties(); 230 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 231 232 ServiceManager lvManager = new ServiceManager(prop); 233 234 Calculator lvCalculator = (Calculator) lvManager.createService(Calculator.class); 235 Long lvLong = lvCalculator.addLong(null, null); 236 assertNull(lvLong); 237 } 238 239 public void testNullComplexValueParams() throws Exception { 240 Properties prop = new Properties(); 241 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 242 243 ServiceManager lvManager = new ServiceManager(prop); 244 245 Echo lvEcho = (Echo) lvManager.createService(Echo.class); 246 Kunde k = lvEcho.renameKunde(null, null); 247 assertNull(k); 248 } 249 250 public void testTransferDate() throws Exception { 251 Properties prop = new Properties(); 252 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 253 254 ServiceManager manager = new ServiceManager(prop); 255 Echo lvEcho = (Echo) manager.createService(Echo.class); 256 257 Kunde k = new Kunde("JUnit-Test-Name"); 258 Date d = new Date(); 259 k.setDate(d); 260 Kunde k2 = lvEcho.renameKunde(k, "Rename-Test"); 261 assertNotNull(k2.getDate()); 262 assertEquals(k.getDate(), k2.getDate()); 263 } 264 265 public void testInvalidInvocationStrategy() throws Exception { 266 Properties prop = new Properties(); 267 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 268 prop.put(Property.INVOCATION_STRATEGY, NameSpacePlusMethodInvocationStrategy.class.getName()); 269 ServiceManager lvManager = new ServiceManager(prop); 270 271 Echo lvEcho = (Echo) lvManager.createService(Echo.class); 272 try { 273 lvEcho.echo("Bla"); 274 fail("Must be thrown Exception. It is set a bad InvocationStrategy"); 275 } catch (Exception e) { 276 assertTrue(true); 277 } 278 } 279 280 281 282 public void testAsynchronousInvocation() throws Exception { 283 Properties prop = new Properties(); 284 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 285 prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName()); 286 287 ServiceManager manager = new ServiceManager(prop); 288 Echo echo = (Echo) manager.createService(Echo.class); 289 String lvPing = echo.ping(); 290 assertNull(lvPing); 291 292 assertTrue(manager.isInvocationAsynchronous(Echo.class)); 293 294 Thread.sleep(300); 295 AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(Echo.class); 296 assertNotNull(lvAsynchronousCallbackForTests); 297 298 assertEquals("ping", lvAsynchronousCallbackForTests.getMethodName()); 299 assertEquals(Echo.PING_STRING, lvAsynchronousCallbackForTests.getResult()); 300 assertNull(lvAsynchronousCallbackForTests.getThrowable()); 301 } 302 303 public void testAsynchronousInvocationWithMultyThreaded() throws Exception { 304 Properties prop = new Properties(); 305 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 306 prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName()); 307 prop.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "3"); 308 309 ServiceManager manager = new ServiceManager(prop); 310 Echo echo = (Echo) manager.createService(Echo.class); 311 String lvPing = echo.ping(); 312 assertNull(lvPing); 313 314 assertTrue(manager.isInvocationAsynchronous(Echo.class)); 315 316 Thread.sleep(300); 317 AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(Echo.class); 318 assertNotNull(lvAsynchronousCallbackForTests); 319 320 assertEquals("ping", lvAsynchronousCallbackForTests.getMethodName()); 321 assertEquals(Echo.PING_STRING, lvAsynchronousCallbackForTests.getResult()); 322 assertNull(lvAsynchronousCallbackForTests.getThrowable()); 323 } 324 325 public void testIndivualAsynchronousInvocationWithMultyThreaded() throws Exception { 326 Properties prop = new Properties(); 327 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 328 329 ServiceManager manager = new ServiceManager(prop); 330 AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests(); 331 Echo e = (Echo) manager.createService(Echo.class, lvCallback, null, 8); 332 333 String lvEchoStr = null; 334 String lvLongExecution = null; 335 for (int i=0;i<3;i++) { 336 lvLongExecution = e.doLongExecution("Hello"); 337 assertNull(lvLongExecution); 338 lvEchoStr = e.echo("Hello"); 339 assertNull(lvEchoStr); 340 } 341 } 342 343 public void testIndivualAsynchronousInvocationWithMultyThreadedWith2Services() throws Exception { 344 Properties prop = new Properties(); 345 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 346 347 ServiceManager manager = new ServiceManager(prop); 348 AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests(); 349 Echo e = (Echo) manager.createService(Echo.class, lvCallback, null, 8); 350 Calculator c = (Calculator) manager.createService(Calculator.class, lvCallback, null, 8); 351 352 String lvLongExecution = null; 353 Long lvAddResult = null; 354 for (int i=0;i<3;i++) { 355 lvLongExecution = e.doLongExecution("Hello"); 356 assertNull(lvLongExecution); 357 lvAddResult = c.addLong(new Long(123), new Long(456)); 358 assertNull(lvAddResult); 359 } 360 } 361 362 public void testIndivualAsynchronousInvocationWithMethodFilter() throws Exception { 363 Properties prop = new Properties(); 364 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 365 366 ServiceManager manager = new ServiceManager(prop); 367 AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests(); 368 Echo e = (Echo) manager.createService(Echo.class, lvCallback, new String[] { "doLongExecution" }, 4); 369 370 String lvEchoStr = null; 371 String lvLongExecution = null; 372 for (int i=0;i<3;i++) { 373 lvLongExecution = e.doLongExecution("Hello"); 374 assertNotNull(lvLongExecution); 375 assertEquals("Hello", lvLongExecution); 376 lvEchoStr = e.echo("Hello"); 377 assertNull(lvEchoStr); 378 } 379 } 380 381 public void testAddAndRemoveAsynchronousCallback() throws Exception { 382 Properties prop = new Properties(); 383 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 384 385 ServiceManager manager = new ServiceManager(prop); 386 AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests(); 387 Echo e = (Echo) manager.createService(Echo.class, lvCallback, new String[] { "doLongExecution" }, 4); 388 Calculator c = (Calculator) manager.createService(Calculator.class, lvCallback, null, 4); 389 390 String lvEchoStr = e.echo("Hello"); 391 assertNull(lvEchoStr); 392 393 Long lvAddResult = c.addLong(new Long(1), new Long(3)); 394 assertNull(lvAddResult); 395 396 assertTrue(manager.isInvocationAsynchronous(Calculator.class)); 397 manager.removeAsynchronousCallback(Calculator.class); 398 assertFalse(manager.isInvocationAsynchronous(Calculator.class)); 399 400 lvEchoStr = e.echo("Hello2"); 401 assertNull(lvEchoStr); 402 403 lvAddResult = c.addLong(new Long(2), new Long(3)); 404 assertNotNull(lvAddResult); 405 assertEquals(new Long(5), lvAddResult); 406 407 408 manager.removeAsynchronousCallback(Echo.class); 409 410 lvEchoStr = e.echo("Hello3"); 411 assertNotNull(lvEchoStr); 412 assertEquals("Hello3", lvEchoStr); 413 } 414 415 public void testRemoteMethodWithThrownExceptionWithValidationErrors() throws Exception { 416 Properties prop = new Properties(); 417 prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName()); 418 419 ServiceManager manager = new ServiceManager(prop); 420 Echo lvEcho = (Echo) manager.createService(Echo.class); 421 422 List lvValidationErrors = new ArrayList(); 423 ValidationError lvError1 = new ValidationError(1, "Error 1"); 424 ValidationError lvError2 = new ValidationError(2, "Error 2"); 425 lvValidationErrors.add(lvError1); 426 lvValidationErrors.add(lvError2); 427 try { 428 lvEcho.throwComplexException("JUnit-Test", lvValidationErrors); 429 fail("The method throwComplexEception must be thrown a Exception"); 430 } catch (JUnitTestException2 e) { 431 assertEquals(lvValidationErrors.size(), e.getValidationErrors().size()); 432 } 433 } 434 435 }