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