001: /*
002: * Created on 29.04.2005 from Linke
003: *
004: */
005: package test.crispy.impl;
006:
007: import java.math.BigDecimal;
008: import java.util.ArrayList;
009: import java.util.Date;
010: import java.util.List;
011: import java.util.Properties;
012:
013: import junit.framework.TestCase;
014: import net.sf.crispy.InvocationException;
015: import net.sf.crispy.Property;
016: import net.sf.crispy.impl.ServiceManager;
017: import net.sf.crispy.impl.StaticLocalObjectProxy;
018: import net.sf.crispy.impl.local.MiniLocalObjectServer;
019: import net.sf.crispy.interceptor.StopWatchInterceptor;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import test.crispy.JUnitTestException;
025: import test.crispy.JUnitTestException2;
026: import test.crispy.compatibility.CompatibilityKit;
027: import test.crispy.concurrent.AsynchronousCallbackForTests;
028: import test.crispy.example.interceptor.WaitInterceptor;
029: import test.crispy.example.model.Adresse;
030: import test.crispy.example.model.Kunde;
031: import test.crispy.example.model.Primitive;
032: import test.crispy.example.model.SubSubProblemNode;
033: import test.crispy.example.model.ValidationError;
034: import test.crispy.example.service.Calculator;
035: import test.crispy.example.service.CalculatorImpl;
036: import test.crispy.example.service.Echo;
037: import test.crispy.example.service.EchoImpl;
038: import test.crispy.example.service.MyService;
039: import test.crispy.example.service.MyServiceImpl;
040:
041: /**
042: * Simple local test from services.
043: *
044: * @author Linke
045: *
046: */
047: public class SimpleServiceTest extends TestCase {
048:
049: private static Log log = LogFactory.getLog(SimpleServiceTest.class);
050:
051: protected void tearDown() throws Exception {
052: super .tearDown();
053: MiniLocalObjectServer.removeService(Calculator.class);
054: MiniLocalObjectServer.removeService(Echo.class);
055: }
056:
057: private void makeEchoTest(ServiceManager pvServiceManager) {
058: Echo lvEcho = (Echo) pvServiceManager.createService(Echo.class);
059: assertNotNull(lvEcho);
060: String lvEchoStr = "Hallo Echo";
061: assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr);
062: }
063:
064: private void makeCalculatorTest(ServiceManager pvServiceManager) {
065: Calculator lvCalculator = (Calculator) pvServiceManager
066: .createService(Calculator.class);
067: assertNotNull(lvCalculator);
068: int lvResult = lvCalculator.add(2, 3);
069: assertEquals(lvResult, 5);
070: lvResult = lvCalculator.subtract(8, 2);
071: assertEquals(lvResult, 6);
072: }
073:
074: public void testSimpleRemotePing() throws Exception {
075: Properties prop = new Properties();
076: prop.put(Echo.class.getName(), EchoImpl.class.getName());
077:
078: ServiceManager manager = new ServiceManager(prop);
079: Echo echo = (Echo) manager.createService(Echo.class);
080: String lvPing = echo.ping();
081: assertEquals(lvPing, Echo.PING_STRING);
082: }
083:
084: /** The simpelst call from service. */
085: public void testSimpleService() {
086: ServiceManager lvServiceManager = new ServiceManager(
087: Echo.class, EchoImpl.class);
088: makeEchoTest(lvServiceManager);
089: }
090:
091: /** Test simple service with Properties. */
092: public void testSimplePropertyService() {
093: Properties prop = new Properties();
094: prop.put(Echo.class.getName(), EchoImpl.class.getName());
095:
096: ServiceManager lvServiceManager = new ServiceManager(prop);
097: makeEchoTest(lvServiceManager);
098: }
099:
100: /** Two ore more services are possible */
101: public void testMultipleSimpleService() {
102:
103: Properties prop = new Properties();
104: prop.put(Echo.class.getName(), EchoImpl.class.getName());
105: prop.put(Calculator.class.getName(), CalculatorImpl.class
106: .getName());
107:
108: ServiceManager lvServiceManager = new ServiceManager(prop);
109: makeEchoTest(lvServiceManager);
110: makeCalculatorTest(lvServiceManager);
111: }
112:
113: /** Try to call a service Echo without a registration from Echo. */
114: public void testServiceNotRegist() throws Exception {
115: Properties prop = new Properties();
116: prop.put(Property.STATIC_PROXY_CLASS,
117: StaticLocalObjectProxy.class.getName());
118: prop.put(Calculator.class.getName(), CalculatorImpl.class
119: .getName());
120:
121: ServiceManager lvServiceManager = new ServiceManager(prop);
122: makeCalculatorTest(lvServiceManager);
123: try {
124: // muss Fehler auftreten, da KEIN Echo-Service registriert ist
125: makeEchoTest(lvServiceManager);
126: fail("The Echo Service is not registered. This must throw Exception.");
127: } catch (InvocationException e) {
128: if (log.isDebugEnabled()) {
129: log.debug("NO Exception: " + e);
130: }
131: }
132: }
133:
134: /** This is not a correct case, but is not a problem */
135: public void testServiceWithInterface() {
136: Properties prop = new Properties();
137: prop.put(CalculatorImpl.class.getName(), CalculatorImpl.class
138: .getName());
139:
140: ServiceManager lvServiceManager = new ServiceManager(prop);
141: Calculator lvComputer = (Calculator) lvServiceManager
142: .createService(CalculatorImpl.class);
143: assertEquals(lvComputer.add(1, 1), 2);
144: assertEquals(lvComputer.subtract(1, 1), 0);
145:
146: try {
147: Calculator lvComputerError = (Calculator) lvServiceManager
148: .createService(Calculator.class);
149: lvComputerError.add(3, 4);
150: fail("The ComputerImpl and not Computer-Interfaces is registered");
151: } catch (Exception e) {
152: log.debug("No Error: ", e);
153: }
154:
155: }
156:
157: /** Call about the ProxyDelegate from Jdk-InvocationHandler. */
158: public void testJdkProxyDelegate() {
159: Properties prop = new Properties();
160: prop.put(Calculator.class.getName(), CalculatorImpl.class
161: .getName());
162: prop.put(Property.STATIC_PROXY_CLASS,
163: StaticLocalObjectProxy.class.getName());
164: prop.put(Property.DYNAMIC_PROXY_CLASS,
165: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
166: prop.put(Property.SECURITY_USER, "user");
167: prop.put(Property.SECURITY_PASSWD, "passwd");
168:
169: ServiceManager lvServiceManager = new ServiceManager(prop);
170: makeCalculatorTest(lvServiceManager);
171: }
172:
173: /** Call about the ProxyDelegate from CGLIB-InvocationHandler. */
174: public void testCglibProxyDelegate() {
175: Properties prop = new Properties();
176: prop.put(Calculator.class.getName(), CalculatorImpl.class
177: .getName());
178: prop.put(Property.STATIC_PROXY_CLASS,
179: StaticLocalObjectProxy.class.getName());
180: prop.put(Property.DYNAMIC_PROXY_CLASS,
181: Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY);
182:
183: ServiceManager lvServiceManager = new ServiceManager(prop);
184: // Calculator lvComputer = (Calculator) lvServiceManager.createService(Calculator.class);
185: makeCalculatorTest(lvServiceManager);
186:
187: }
188:
189: public void testMultiProxyInterceptor() throws Exception {
190: Properties prop = new Properties();
191: prop.put(Calculator.class.getName(), CalculatorImpl.class
192: .getName());
193: prop.put(Echo.class.getName(), EchoImpl.class.getName());
194: prop.put(Property.DYNAMIC_PROXY_CLASS,
195: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
196: prop.put(Property.INTERCEPTOR_CLASS, WaitInterceptor.class
197: .getName());
198: prop.put(Property.INTERCEPTOR_CLASS_2,
199: StopWatchInterceptor.class.getName());
200:
201: ServiceManager lvServiceManager = new ServiceManager(prop);
202: makeCalculatorTest(lvServiceManager);
203:
204: assertTrue(lvServiceManager.getInterceptorSize() == 2);
205:
206: WaitInterceptor lvWaitInterceptor = (WaitInterceptor) lvServiceManager
207: .getInterceptorByPos(0);
208: StopWatchInterceptor lvProxyInterceptor = (StopWatchInterceptor) lvServiceManager
209: .getInterceptorByPos(1);
210:
211: assertTrue(lvProxyInterceptor.getStopTimeNewInstance() >= lvWaitInterceptor
212: .getWaitTime());
213: assertTrue(lvProxyInterceptor.getStopTimeInvokeMethod() >= lvWaitInterceptor
214: .getWaitTime());
215:
216: makeEchoTest(lvServiceManager);
217: assertTrue(lvProxyInterceptor.getStopTimeNewInstance() >= lvWaitInterceptor
218: .getWaitTime());
219: assertTrue(lvProxyInterceptor.getStopTimeInvokeMethod() >= lvWaitInterceptor
220: .getWaitTime());
221:
222: }
223:
224: public void testSimpleRemoteInvocationWithOutDynamicJdkProxy()
225: throws Exception {
226: Properties prop = new Properties();
227: prop.put(Calculator.class.getName(), CalculatorImpl.class
228: .getName());
229: prop.put(Property.INTERCEPTOR_CLASS, WaitInterceptor.class
230: .getName());
231: prop.put(Property.INTERCEPTOR_CLASS_2,
232: StopWatchInterceptor.class.getName());
233:
234: ServiceManager lvManager = new ServiceManager(prop);
235: makeCalculatorTest(lvManager);
236: StopWatchInterceptor lvStopWatchInterceptor = (StopWatchInterceptor) lvManager
237: .getInterceptorByPos(1);
238: long lvStopTime = lvStopWatchInterceptor
239: .getStopTimeInvokeMethod();
240: assertTrue("StopTime is: " + lvStopTime, lvStopTime > 10);
241:
242: lvManager.removeInterceptorByPos(0);
243: lvManager.removeInterceptorByPos(0);
244: makeCalculatorTest(lvManager);
245: assertEquals(lvStopTime, lvStopWatchInterceptor
246: .getStopTimeInvokeMethod());
247:
248: lvStopWatchInterceptor.clearStopTimeInvokeMethod();
249: lvStopWatchInterceptor.clearStopTimeNewInstance();
250: makeCalculatorTest(lvManager);
251: lvStopTime = lvStopWatchInterceptor.getStopTimeInvokeMethod();
252: assertEquals(lvStopTime, 0);
253: }
254:
255: public void testModifyService() throws Exception {
256: CompatibilityKit compatibilityKit = new CompatibilityKit();
257: compatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS,
258: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
259: compatibilityKit.makeModifyServiceTest(compatibilityKit
260: .createServiceManager());
261: }
262:
263: public void testCycleDetectionTest() throws Exception {
264: Properties prop = new Properties();
265: prop.put(Echo.class.getName(), EchoImpl.class.getName());
266: prop.put(Property.DYNAMIC_PROXY_CLASS,
267: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
268:
269: ServiceManager lvServiceManager = new ServiceManager(prop);
270: Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class);
271:
272: Kunde k = new Kunde("Mario");
273: k.setGehalt(new BigDecimal(12.34));
274: Adresse a = new Adresse();
275: a.setOrt("Magdebrug");
276: a.setPlz("39104");
277: k.getAdressen().add(a);
278: a.setKunde(k);
279:
280: String lvRenameName = "Nadine";
281: Kunde kAfter = lvEcho.renameKunde(k, lvRenameName);
282: assertEquals(kAfter.getName(), lvRenameName);
283:
284: Kunde kTemp = ((Adresse) kAfter.getAdressen().get(0))
285: .getKunde();
286: assertEquals(kAfter, kTemp);
287: }
288:
289: public void testAddNode() throws Exception {
290: CompatibilityKit compatibilityKit = new CompatibilityKit();
291: compatibilityKit.makeAddNodeTest(compatibilityKit
292: .createServiceManager());
293: }
294:
295: public void testEchoPrimitive() throws Exception {
296: MiniLocalObjectServer server = new MiniLocalObjectServer();
297: server.addService(Echo.class, new EchoImpl());
298: server.start();
299:
300: Properties prop = new Properties();
301: prop.put(Property.STATIC_PROXY_CLASS,
302: StaticLocalObjectProxy.class.getName());
303:
304: ServiceManager lvServiceManager = new ServiceManager(prop);
305: Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class);
306: Primitive lvPrimitive = Primitive.createPrimitiveExample();
307:
308: Primitive lvPrimitiveAfter = lvEcho.echoPrimitive(lvPrimitive);
309: assertEquals(lvPrimitive, lvPrimitiveAfter);
310: assertSame(lvPrimitive, lvPrimitiveAfter);
311: }
312:
313: public void testInnerClassServiceImpl() throws Exception {
314: MiniLocalObjectServer server = new MiniLocalObjectServer();
315: server.addService(MyService.class, new MyService() {
316: MyServiceImpl myServiceImpl = new MyServiceImpl();
317:
318: public SubSubProblemNode transferSubSubProblemNode(
319: SubSubProblemNode pvNode) {
320: return myServiceImpl.transferSubSubProblemNode(pvNode);
321: }
322:
323: }
324:
325: );
326: server.start();
327:
328: Properties prop = new Properties();
329: prop.put(Property.STATIC_PROXY_CLASS,
330: StaticLocalObjectProxy.class.getName());
331:
332: ServiceManager lvServiceManager = new ServiceManager(prop);
333: MyService lvService = (MyService) lvServiceManager
334: .createService(MyService.class);
335:
336: SubSubProblemNode lvNode = new SubSubProblemNode();
337: Date d = new Date();
338: lvNode.setDate(d);
339:
340: SubSubProblemNode lvNodeAfter = lvService
341: .transferSubSubProblemNode(lvNode);
342: assertEquals(lvNodeAfter.getDate(), lvNode.getDate());
343: }
344:
345: public void testMiniLocalObjectServer() throws Exception {
346: MiniLocalObjectServer server = new MiniLocalObjectServer();
347: server.addService(Echo.class, new EchoImpl());
348: server.start();
349:
350: Properties prop = new Properties();
351: prop.put(Property.STATIC_PROXY_CLASS,
352: StaticLocalObjectProxy.class.getName());
353:
354: ServiceManager lvServiceManager = new ServiceManager(prop);
355: makeEchoTest(lvServiceManager);
356:
357: server.addService(Calculator.class, new CalculatorImpl());
358: makeCalculatorTest(lvServiceManager);
359: }
360:
361: public void testOverloading() throws Exception {
362: MiniLocalObjectServer server = new MiniLocalObjectServer();
363: server.start();
364:
365: server.addService(Calculator.class, new CalculatorImpl());
366:
367: Properties prop = new Properties();
368: prop.put(Property.STATIC_PROXY_CLASS,
369: StaticLocalObjectProxy.class.getName());
370:
371: ServiceManager manager = new ServiceManager(prop);
372: Calculator lvCalculator = (Calculator) manager
373: .createService(Calculator.class);
374: int lvIntResult = lvCalculator.add(2, 3);
375: assertEquals(lvIntResult, 5);
376: long lvLongResult = lvCalculator.add(21, 32);
377: assertEquals(lvLongResult, 53);
378: double lvDoubleResult = lvCalculator.add(2.3, 3.2);
379: assertEquals(lvDoubleResult, 5.5, 0);
380: }
381:
382: public void testOverloadingInteger() throws Exception {
383: MiniLocalObjectServer server = new MiniLocalObjectServer();
384: server.start();
385:
386: server.addService(Echo.class, new EchoImpl());
387:
388: Properties prop = new Properties();
389: prop.put(Property.STATIC_PROXY_CLASS,
390: StaticLocalObjectProxy.class.getName());
391:
392: ServiceManager manager = new ServiceManager(prop);
393: Echo echo = (Echo) manager.createService(Echo.class);
394: Integer i[] = echo.echoArray(new Integer[] { new Integer(1),
395: new Integer(3), new Integer(5) });
396: assertTrue(i.length == 3);
397: assertEquals(i[0], new Integer(1));
398: assertEquals(i[1], new Integer(3));
399: assertEquals(i[2], new Integer(5));
400: }
401:
402: public void testOverloadingLong() throws Exception {
403: MiniLocalObjectServer server = new MiniLocalObjectServer();
404: server.start();
405:
406: server.addService(Echo.class, new EchoImpl());
407:
408: Properties prop = new Properties();
409: prop.put(Property.STATIC_PROXY_CLASS,
410: StaticLocalObjectProxy.class.getName());
411:
412: ServiceManager manager = new ServiceManager(prop);
413: Echo echo = (Echo) manager.createService(Echo.class);
414: Long l[] = echo
415: .echoArray(new Long[] { new Long(1), new Long(5) });
416: assertTrue(l.length == 2);
417: assertEquals(l[0], new Long(1));
418: assertEquals(l[1], new Long(5));
419: }
420:
421: public void testOverloadingObject() throws Exception {
422: MiniLocalObjectServer server = new MiniLocalObjectServer();
423: server.start();
424:
425: server.addService(Echo.class, new EchoImpl());
426:
427: Properties prop = new Properties();
428: prop.put(Property.STATIC_PROXY_CLASS,
429: StaticLocalObjectProxy.class.getName());
430:
431: ServiceManager manager = new ServiceManager(prop);
432: Echo echo = (Echo) manager.createService(Echo.class);
433: Object o[] = echo.echoArray(new Object[] { "a", new Integer(3),
434: new Long(5) });
435: assertTrue(o.length == 3);
436: assertEquals(o[0], "a");
437: assertEquals(o[1], new Integer(3));
438: // assertEquals(o[2], new Long(5)); is Integer
439: }
440:
441: public void testCreateService() throws Exception {
442: Properties prop = new Properties();
443: prop.put(Property.STATIC_PROXY_CLASS,
444: StaticLocalObjectProxy.class.getName());
445: prop.put(Calculator.class.getName(), CalculatorImpl.class
446: .getName());
447:
448: ServiceManager lvManager = new ServiceManager(prop);
449: Calculator lvCalculator1 = (Calculator) lvManager
450: .createService(Calculator.class);
451: Calculator lvCalculator2 = (Calculator) lvManager
452: .createService(Calculator.class);
453:
454: assertNotNull(lvCalculator1);
455: assertNotNull(lvCalculator2);
456: assertNotSame(lvCalculator1, lvCalculator2);
457: }
458:
459: public void testThrowException() throws Exception {
460: Properties prop = new Properties();
461: prop.put(Property.STATIC_PROXY_CLASS,
462: StaticLocalObjectProxy.class.getName());
463: prop.put(Echo.class.getName(), EchoImpl.class.getName());
464:
465: ServiceManager lvManager = new ServiceManager(prop);
466:
467: Echo lvEcho = (Echo) lvManager.createService(Echo.class);
468: boolean throwException = false;
469: try {
470: lvEcho.throwException("A Test-Excption.");
471: } catch (JUnitTestException e) {
472: throwException = true;
473: }
474: assertTrue("No Exception thrown: " + throwException,
475: throwException);
476: }
477:
478: public void testNullValueParamsWithException() throws Exception {
479: Properties prop = new Properties();
480: prop.put(Property.STATIC_PROXY_CLASS,
481: StaticLocalObjectProxy.class.getName());
482: prop.put(Echo.class.getName(), EchoImpl.class.getName());
483:
484: ServiceManager lvManager = new ServiceManager(prop);
485:
486: Echo lvEcho = (Echo) lvManager.createService(Echo.class);
487: String s = lvEcho.echo(null);
488: assertNull(s);
489: }
490:
491: public void testNullLongValueParams() throws Exception {
492: Properties prop = new Properties();
493: prop.put(Property.STATIC_PROXY_CLASS,
494: StaticLocalObjectProxy.class.getName());
495: prop.put(Calculator.class.getName(), CalculatorImpl.class
496: .getName());
497:
498: ServiceManager lvManager = new ServiceManager(prop);
499:
500: Calculator lvCalculator = (Calculator) lvManager
501: .createService(Calculator.class);
502: Long lvLong = lvCalculator.addLong(null, null);
503: assertNull(lvLong);
504: }
505:
506: public void testNullComplexValueParams() throws Exception {
507: Properties prop = new Properties();
508: prop.put(Property.STATIC_PROXY_CLASS,
509: StaticLocalObjectProxy.class.getName());
510: prop.put(Echo.class.getName(), EchoImpl.class.getName());
511:
512: ServiceManager lvManager = new ServiceManager(prop);
513:
514: Echo lvEcho = (Echo) lvManager.createService(Echo.class);
515: Kunde k = lvEcho.renameKunde(null, null);
516: assertNull(k);
517: }
518:
519: public void testTransferDate() throws Exception {
520: Properties prop = new Properties();
521: prop.put(Property.STATIC_PROXY_CLASS,
522: StaticLocalObjectProxy.class.getName());
523: prop.put(Echo.class.getName(), EchoImpl.class.getName());
524:
525: ServiceManager manager = new ServiceManager(prop);
526: Echo lvEcho = (Echo) manager.createService(Echo.class);
527:
528: Kunde k = new Kunde("JUnit-Test-Name");
529: Date d = new Date();
530: k.setDate(d);
531: Kunde k2 = lvEcho.renameKunde(k, "Rename-Test");
532: assertNotNull(k2.getDate());
533: assertEquals(k.getDate(), k2.getDate());
534: }
535:
536: public void testAsynchronousInvocation() throws Exception {
537: Properties prop = new Properties();
538: prop.put(Property.STATIC_PROXY_CLASS,
539: StaticLocalObjectProxy.class.getName());
540: prop.put(Property.DYNAMIC_PROXY_CLASS,
541: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
542: prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS,
543: AsynchronousCallbackForTests.class.getName());
544: prop.put(Echo.class.getName(), EchoImpl.class.getName());
545:
546: ServiceManager manager = new ServiceManager(prop);
547: Echo echo = (Echo) manager.createService(Echo.class);
548: String lvPing = echo.ping();
549: assertNull(lvPing);
550:
551: assertTrue(manager.isInvocationAsynchronous(Echo.class));
552:
553: Thread.sleep(300);
554: AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager
555: .getAsynchronousCallback(Echo.class);
556: assertNotNull(lvAsynchronousCallbackForTests);
557:
558: assertEquals("ping", lvAsynchronousCallbackForTests
559: .getMethodName());
560: assertEquals(Echo.PING_STRING, lvAsynchronousCallbackForTests
561: .getResult());
562: assertNull(lvAsynchronousCallbackForTests.getThrowable());
563: }
564:
565: public void testAsynchronousInvocationWithMultyThreaded()
566: throws Exception {
567: Properties prop = new Properties();
568: prop.put(Property.STATIC_PROXY_CLASS,
569: StaticLocalObjectProxy.class.getName());
570: prop.put(Property.DYNAMIC_PROXY_CLASS,
571: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
572: prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS,
573: AsynchronousCallbackForTests.class.getName());
574: prop.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "3");
575: prop.put(Echo.class.getName(), EchoImpl.class.getName());
576:
577: ServiceManager manager = new ServiceManager(prop);
578: Echo echo = (Echo) manager.createService(Echo.class);
579: String lvPing = echo.ping();
580: assertNull(lvPing);
581:
582: assertTrue(manager.isInvocationAsynchronous(Echo.class));
583:
584: Thread.sleep(300);
585: AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager
586: .getAsynchronousCallback(Echo.class);
587: assertNotNull(lvAsynchronousCallbackForTests);
588:
589: assertEquals("ping", lvAsynchronousCallbackForTests
590: .getMethodName());
591: assertEquals(Echo.PING_STRING, lvAsynchronousCallbackForTests
592: .getResult());
593: assertNull(lvAsynchronousCallbackForTests.getThrowable());
594: }
595:
596: public void testIndivualAsynchronousInvocationWithMultyThreaded()
597: throws Exception {
598: Properties prop = new Properties();
599: prop.put(Property.STATIC_PROXY_CLASS,
600: StaticLocalObjectProxy.class.getName());
601: prop.put(Property.DYNAMIC_PROXY_CLASS,
602: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
603: prop.put(Echo.class.getName(), EchoImpl.class.getName());
604:
605: ServiceManager manager = new ServiceManager(prop);
606: AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
607: Echo e = (Echo) manager.createService(Echo.class, lvCallback,
608: null, 8);
609:
610: String lvEchoStr = null;
611: String lvLongExecution = null;
612: for (int i = 0; i < 3; i++) {
613: lvLongExecution = e.doLongExecution("Hello");
614: assertNull(lvLongExecution);
615: lvEchoStr = e.echo("Hello");
616: assertNull(lvEchoStr);
617: }
618: }
619:
620: public void testIndivualAsynchronousInvocationWithMultyThreadedWith2Services()
621: throws Exception {
622: Properties prop = new Properties();
623: prop.put(Property.STATIC_PROXY_CLASS,
624: StaticLocalObjectProxy.class.getName());
625: prop.put(Property.DYNAMIC_PROXY_CLASS,
626: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
627: prop.put(Echo.class.getName(), EchoImpl.class.getName());
628: prop.put(Calculator.class.getName(), CalculatorImpl.class
629: .getName());
630:
631: ServiceManager manager = new ServiceManager(prop);
632: AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
633: Echo e = (Echo) manager.createService(Echo.class, lvCallback,
634: null, 8);
635: Calculator c = (Calculator) manager.createService(
636: Calculator.class, lvCallback, null, 8);
637:
638: String lvLongExecution = null;
639: Long lvAddResult = null;
640: for (int i = 0; i < 3; i++) {
641: lvLongExecution = e.doLongExecution("Hello");
642: assertNull(lvLongExecution);
643: lvAddResult = c.addLong(new Long(123), new Long(456));
644: assertNull(lvAddResult);
645: }
646: }
647:
648: public void testIndivualAsynchronousInvocationWithMethodFilter()
649: throws Exception {
650: Properties prop = new Properties();
651: prop.put(Property.STATIC_PROXY_CLASS,
652: StaticLocalObjectProxy.class.getName());
653: prop.put(Property.DYNAMIC_PROXY_CLASS,
654: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
655: prop.put(Echo.class.getName(), EchoImpl.class.getName());
656:
657: ServiceManager manager = new ServiceManager(prop);
658: AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
659: Echo e = (Echo) manager.createService(Echo.class, lvCallback,
660: new String[] { "doLongExecution" }, 4);
661:
662: String lvEchoStr = null;
663: String lvLongExecution = null;
664: for (int i = 0; i < 3; i++) {
665: lvLongExecution = e.doLongExecution("Hello");
666: assertNotNull(lvLongExecution);
667: assertEquals("Hello", lvLongExecution);
668: lvEchoStr = e.echo("Hello");
669: assertNull(lvEchoStr);
670: }
671: }
672:
673: public void testAddAndRemoveAsynchronousCallback() throws Exception {
674: Properties prop = new Properties();
675: prop.put(Property.STATIC_PROXY_CLASS,
676: StaticLocalObjectProxy.class.getName());
677: prop.put(Property.DYNAMIC_PROXY_CLASS,
678: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
679: prop.put(Echo.class.getName(), EchoImpl.class.getName());
680: prop.put(Calculator.class.getName(), CalculatorImpl.class
681: .getName());
682:
683: ServiceManager manager = new ServiceManager(prop);
684: AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
685: Echo e = (Echo) manager.createService(Echo.class, lvCallback,
686: new String[] { "doLongExecution" }, 4);
687: Calculator c = (Calculator) manager.createService(
688: Calculator.class, lvCallback, null, 4);
689:
690: String lvEchoStr = e.echo("Hello");
691: assertNull(lvEchoStr);
692:
693: Long lvAddResult = c.addLong(new Long(1), new Long(3));
694: assertNull(lvAddResult);
695:
696: assertTrue(manager.isInvocationAsynchronous(Calculator.class));
697: manager.removeAsynchronousCallback(Calculator.class);
698: assertFalse(manager.isInvocationAsynchronous(Calculator.class));
699:
700: lvEchoStr = e.echo("Hello2");
701: assertNull(lvEchoStr);
702:
703: lvAddResult = c.addLong(new Long(2), new Long(3));
704: assertNotNull(lvAddResult);
705: assertEquals(new Long(5), lvAddResult);
706:
707: manager.removeAsynchronousCallback(Echo.class);
708:
709: lvEchoStr = e.echo("Hello3");
710: assertNotNull(lvEchoStr);
711: assertEquals("Hello3", lvEchoStr);
712: }
713:
714: public void testRemoteMethodWithThrownExceptionWithValidationErrors()
715: throws Exception {
716: Properties prop = new Properties();
717: prop.put(Property.STATIC_PROXY_CLASS,
718: StaticLocalObjectProxy.class.getName());
719: prop.put(Echo.class.getName(), EchoImpl.class.getName());
720:
721: ServiceManager manager = new ServiceManager(prop);
722: Echo lvEcho = (Echo) manager.createService(Echo.class);
723:
724: List lvValidationErrors = new ArrayList();
725: ValidationError lvError1 = new ValidationError(1, "Error 1");
726: ValidationError lvError2 = new ValidationError(2, "Error 2");
727: lvValidationErrors.add(lvError1);
728: lvValidationErrors.add(lvError2);
729: try {
730: lvEcho.throwComplexException("JUnit-Test",
731: lvValidationErrors);
732: fail("The method throwComplexEception must be thrown a Exception");
733: } catch (Exception e) {
734: assertTrue(e instanceof JUnitTestException2);
735: assertEquals(lvValidationErrors.size(),
736: ((JUnitTestException2) e).getValidationErrors()
737: .size());
738: }
739: }
740:
741: }
|