001: /*
002: * Created on 29.04.2005 from Linke
003: *
004: */
005: package test.crispy.impl;
006:
007: import java.util.Date;
008: import java.util.Properties;
009:
010: import javax.naming.InitialContext;
011:
012: import junit.framework.TestCase;
013: import net.sf.crispy.Property;
014: import net.sf.crispy.impl.ServiceManager;
015: import net.sf.crispy.impl.StaticEjbProxy;
016: import net.sf.crispy.interceptor.StopWatchInterceptor;
017:
018: import org.mockejb.MockContainer;
019: import org.mockejb.SessionBeanDescriptor;
020: import org.mockejb.jndi.MockContextFactory;
021:
022: import test.crispy.concurrent.AsynchronousCallbackForTests;
023: import test.crispy.example.ejb.EjbService;
024: import test.crispy.example.ejb.EjbServiceBean;
025: import test.crispy.example.ejb.EjbServiceHome;
026: import test.crispy.example.interceptor.WaitInterceptor;
027: import test.crispy.example.model.Kunde;
028: import test.crispy.example.service.Calculator;
029:
030: /**
031: * @author Linke
032: *
033: */
034: public class EjbServiceTest extends TestCase {
035:
036: /**
037: * @see junit.framework.TestCase#setUp()
038: */
039: protected void setUp() throws Exception {
040: super .setUp();
041: MockContextFactory.setAsInitial();
042: MockContainer lvMockContainer = new MockContainer(
043: new InitialContext());
044: SessionBeanDescriptor lvBeanDescriptor = new SessionBeanDescriptor(
045: EjbService.JNDI_NAME, EjbServiceHome.class,
046: EjbService.class, EjbServiceBean.class);
047: lvMockContainer.deploy(lvBeanDescriptor);
048:
049: }
050:
051: public void testSimpleEjbTest() {
052: Properties prop = new Properties();
053: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
054: prop.put(EjbService.class.getName()
055: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
056: .getName());
057: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
058: .getName());
059:
060: ServiceManager lvServiceManager = new ServiceManager(prop);
061:
062: EjbService lvEjbService = (EjbService) lvServiceManager
063: .createService(EjbService.class);
064: assertNotNull(lvEjbService);
065: String lvEchoStr = "BlaBla...";
066: assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: "
067: + lvEchoStr);
068: assertEquals(lvEjbService.add(5, 7), 12);
069: }
070:
071: public void testEjbTestWithDynamicJdkProxy() {
072: Properties prop = new Properties();
073: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
074: prop.put(EjbService.class.getName()
075: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
076: .getName());
077: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
078: .getName());
079: prop.put(Property.DYNAMIC_PROXY_CLASS,
080: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
081:
082: ServiceManager lvServiceManager = new ServiceManager(prop);
083:
084: EjbService lvEjbService = (EjbService) lvServiceManager
085: .createService(EjbService.class);
086: assertNotNull(lvEjbService);
087: String lvEchoStr = "BlaBla...";
088: assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: "
089: + lvEchoStr);
090: assertEquals(lvEjbService.add(5, 7), 12);
091: }
092:
093: public void testSimpleRemoteInvocationWithOutDynamicJdkProxy()
094: throws Exception {
095: Properties prop = new Properties();
096: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
097: prop.put(EjbService.class.getName()
098: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
099: .getName());
100: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
101: .getName());
102: prop.put(Property.INTERCEPTOR_CLASS, WaitInterceptor.class
103: .getName());
104: prop.put(Property.INTERCEPTOR_CLASS_2,
105: StopWatchInterceptor.class.getName());
106:
107: ServiceManager lvManager = new ServiceManager(prop);
108:
109: EjbService lvEjbService = (EjbService) lvManager
110: .createService(EjbService.class);
111: assertNotNull(lvEjbService);
112: String lvEchoStr = "BlaBla...";
113: assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: "
114: + lvEchoStr);
115: assertEquals(lvEjbService.add(5, 7), 12);
116:
117: StopWatchInterceptor lvStopWatchInterceptor = (StopWatchInterceptor) lvManager
118: .getInterceptorByPos(1);
119: long lvStopTime = lvStopWatchInterceptor
120: .getStopTimeInvokeMethod();
121: assertTrue("StopTime is: " + lvStopTime, lvStopTime > 10);
122:
123: lvManager.removeInterceptorByPos(0);
124: lvManager.removeInterceptorByPos(0);
125: assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: "
126: + lvEchoStr);
127: assertEquals(lvStopTime, lvStopWatchInterceptor
128: .getStopTimeInvokeMethod());
129:
130: lvStopWatchInterceptor.clearStopTimeInvokeMethod();
131: assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: "
132: + lvEchoStr);
133: lvStopTime = lvStopWatchInterceptor.getStopTimeInvokeMethod();
134: assertEquals(lvStopTime, 0);
135: }
136:
137: /** Ejb und Cglib-Proxy funnktioniert nicht, weil bei Ejb's die Klasse final ist! */
138: public void _testEjbTestWithDynamicCglibProxy() {
139: Properties prop = new Properties();
140: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
141: prop.put(EjbService.class.getName()
142: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
143: .getName());
144: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
145: .getName());
146: prop.put(Property.DYNAMIC_PROXY_CLASS,
147: Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY);
148:
149: ServiceManager lvServiceManager = new ServiceManager(prop);
150:
151: EjbService lvEjbService = (EjbService) lvServiceManager
152: .createService(EjbService.class);
153: assertNotNull(lvEjbService);
154: String lvEchoStr = "BlaBla...";
155: assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: "
156: + lvEchoStr);
157: assertEquals(lvEjbService.add(5, 7), 12);
158: }
159:
160: public void testNoHomeInterface() throws Exception {
161: Properties prop = new Properties();
162: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
163: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
164: .getName());
165:
166: ServiceManager lvServiceManager = new ServiceManager(prop);
167: try {
168: EjbService lvEjbService = (EjbService) lvServiceManager
169: .createService(EjbService.class);
170: assertNotNull(lvEjbService);
171: fail("Without HomeInterface no EjbService!");
172: } catch (Exception e) {
173: assertTrue(true);
174: }
175: }
176:
177: public void testNoJndiEntry() throws Exception {
178: Properties prop = new Properties();
179: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
180: .getName());
181:
182: ServiceManager lvServiceManager = new ServiceManager(prop);
183: try {
184: EjbService lvEjbService = (EjbService) lvServiceManager
185: .createService(EjbService.class);
186: assertNotNull(lvEjbService);
187: fail("Without JNDI-Name no EjbService!");
188: } catch (Exception e) {
189: assertTrue(true);
190: }
191: }
192:
193: public void testInvalidJndiName() throws Exception {
194: Properties prop = new Properties();
195: prop
196: .put(EjbService.class.getName(), EjbService.JNDI_NAME
197: + "_");
198: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
199: .getName());
200:
201: ServiceManager lvServiceManager = new ServiceManager(prop);
202: try {
203: EjbService lvEjbService = (EjbService) lvServiceManager
204: .createService(EjbService.class);
205: assertNotNull(lvEjbService);
206: fail("Invalide JNDI-Name!");
207: } catch (Exception e) {
208: assertTrue(true);
209: }
210: }
211:
212: public void testProxyInterceptor() throws Exception {
213: Properties prop = new Properties();
214: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
215: prop.put(EjbService.class.getName()
216: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
217: .getName());
218: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
219: .getName());
220: prop.put(Property.INTERCEPTOR_CLASS, StopWatchInterceptor.class
221: .getName());
222:
223: ServiceManager lvServiceManager = new ServiceManager(prop);
224:
225: EjbService lvEjbService = (EjbService) lvServiceManager
226: .createService(EjbService.class);
227: assertNotNull(lvEjbService);
228: String lvEchoStr = "BlaBla...";
229: assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: "
230: + lvEchoStr);
231: assertEquals(lvEjbService.add(5, 7), 12);
232:
233: // StopWatchInterceptor lvProxyInterceptor = (StopWatchInterceptor) lvServiceManager.getInterceptorIterator().next();
234: // System.out.println("EjbTest-StopTimeNewInstance: " + lvProxyInterceptor.getStopTimeNewInstance());
235: // System.out.println("EjbTest-StopTimeInvokeMethod: " + lvProxyInterceptor.getStopTimeInvokeMethod());
236: }
237:
238: public void testCreateService() throws Exception {
239: Properties prop = new Properties();
240: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
241: prop.put(EjbService.class.getName()
242: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
243: .getName());
244: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
245: .getName());
246:
247: ServiceManager lvManager = new ServiceManager(prop);
248: EjbService lvEjbService1 = (EjbService) lvManager
249: .createService(EjbService.class);
250: EjbService lvEjbService2 = (EjbService) lvManager
251: .createService(EjbService.class);
252:
253: assertNotNull(lvEjbService1);
254: assertNotNull(lvEjbService2);
255: assertNotSame(lvEjbService1, lvEjbService2);
256: }
257:
258: public void testEjbTestWithoutHomeInterface() {
259: Properties prop = new Properties();
260: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
261: // prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
262: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
263: .getName());
264:
265: ServiceManager lvServiceManager = new ServiceManager(prop);
266:
267: try {
268: lvServiceManager.createService(EjbService.class);
269: fail("Must thrown Exception: No Home-Interface");
270: } catch (Exception e) {
271: assertTrue(true);
272: }
273: }
274:
275: public void testNullValueParamsWithException() throws Exception {
276: Properties prop = new Properties();
277: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
278: prop.put(EjbService.class.getName()
279: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
280: .getName());
281: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
282: .getName());
283:
284: ServiceManager lvManager = new ServiceManager(prop);
285:
286: EjbService lvEjbService = (EjbService) lvManager
287: .createService(EjbService.class);
288: String s = lvEjbService.echo(null);
289: assertNull(s);
290: }
291:
292: public void testNullLongValueParams() throws Exception {
293: Properties prop = new Properties();
294: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
295: prop.put(EjbService.class.getName()
296: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
297: .getName());
298: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
299: .getName());
300:
301: ServiceManager lvManager = new ServiceManager(prop);
302:
303: EjbService lvEjbService = (EjbService) lvManager
304: .createService(EjbService.class);
305: Long lvLong = lvEjbService.addLong(null, null);
306: assertNull(lvLong);
307: }
308:
309: public void testNullComplexValueParams() throws Exception {
310: Properties prop = new Properties();
311: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
312: prop.put(EjbService.class.getName()
313: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
314: .getName());
315: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
316: .getName());
317:
318: ServiceManager lvManager = new ServiceManager(prop);
319:
320: EjbService lvEjbService = (EjbService) lvManager
321: .createService(EjbService.class);
322: Kunde k = lvEjbService.renameKunde(null, null);
323: assertNull(k);
324: }
325:
326: public void testTransferDate() throws Exception {
327: Properties prop = new Properties();
328: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
329: prop.put(EjbService.class.getName()
330: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
331: .getName());
332: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
333: .getName());
334:
335: ServiceManager manager = new ServiceManager(prop);
336: EjbService lvEcho = (EjbService) manager
337: .createService(EjbService.class);
338:
339: Kunde k = new Kunde("JUnit-Test-Name");
340: Date d = new Date();
341: k.setDate(d);
342: Kunde k2 = lvEcho.renameKunde(k, "Rename-Test");
343: assertNotNull(k2.getDate());
344: assertEquals(k.getDate(), k2.getDate());
345: }
346:
347: public void testAsynchronousInvocation() throws Exception {
348: Properties prop = new Properties();
349: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
350: prop.put(EjbService.class.getName()
351: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
352: .getName());
353: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
354: .getName());
355: prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS,
356: AsynchronousCallbackForTests.class.getName());
357: prop.put(Property.DYNAMIC_PROXY_CLASS,
358: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
359:
360: ServiceManager manager = new ServiceManager(prop);
361: EjbService lvEjbService = (EjbService) manager
362: .createService(EjbService.class);
363: String lvEchoString = lvEjbService.echo("Hello EJB-Test echo");
364: assertNull(lvEchoString);
365:
366: assertTrue(manager.isInvocationAsynchronous(EjbService.class));
367:
368: Thread.sleep(300);
369: AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager
370: .getAsynchronousCallback(EjbService.class);
371: assertNotNull(lvAsynchronousCallbackForTests);
372:
373: assertEquals("echo", lvAsynchronousCallbackForTests
374: .getMethodName());
375: assertEquals("Return Echo: Hello EJB-Test echo",
376: lvAsynchronousCallbackForTests.getResult());
377: assertNull(lvAsynchronousCallbackForTests.getThrowable());
378: }
379:
380: public void testAsynchronousInvocationWithMultyThreaded()
381: throws Exception {
382: Properties prop = new Properties();
383: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
384: prop.put(EjbService.class.getName()
385: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
386: .getName());
387: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
388: .getName());
389: prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS,
390: AsynchronousCallbackForTests.class.getName());
391: prop.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "3");
392: prop.put(Property.DYNAMIC_PROXY_CLASS,
393: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
394:
395: ServiceManager manager = new ServiceManager(prop);
396: EjbService lvEjbService = (EjbService) manager
397: .createService(EjbService.class);
398: String lvEchoString = lvEjbService.echo("Hello EJB-Test echo");
399: assertNull(lvEchoString);
400:
401: assertTrue(manager.isInvocationAsynchronous(EjbService.class));
402:
403: Thread.sleep(300);
404: AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager
405: .getAsynchronousCallback(EjbService.class);
406: assertNotNull(lvAsynchronousCallbackForTests);
407:
408: assertEquals("echo", lvAsynchronousCallbackForTests
409: .getMethodName());
410: assertEquals("Return Echo: Hello EJB-Test echo",
411: lvAsynchronousCallbackForTests.getResult());
412: assertNull(lvAsynchronousCallbackForTests.getThrowable());
413: }
414:
415: public void testIndivualAsynchronousInvocationWithMultyThreaded()
416: throws Exception {
417: Properties prop = new Properties();
418: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
419: prop.put(EjbService.class.getName()
420: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
421: .getName());
422: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
423: .getName());
424: prop.put(Property.DYNAMIC_PROXY_CLASS,
425: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
426:
427: ServiceManager manager = new ServiceManager(prop);
428: AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
429: EjbService lvEjbService = (EjbService) manager.createService(
430: EjbService.class, lvCallback, null, 4);
431:
432: String lvEchoStr = null;
433: for (int i = 0; i < 3; i++) {
434: lvEchoStr = lvEjbService.echo("Hello");
435: assertNull(lvEchoStr);
436: }
437:
438: Thread.sleep(500);
439: assertEquals("echo", lvCallback.getMethodName());
440: assertEquals("Return Echo: Hello", lvCallback.getResult());
441: }
442:
443: public void testIndivualAsynchronousInvocationWithMethodFilter()
444: throws Exception {
445: Properties prop = new Properties();
446: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
447: prop.put(EjbService.class.getName()
448: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
449: .getName());
450: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
451: .getName());
452: prop.put(Property.DYNAMIC_PROXY_CLASS,
453: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
454:
455: ServiceManager manager = new ServiceManager(prop);
456: AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
457: EjbService lvEjbService = (EjbService) manager
458: .createService(EjbService.class, lvCallback,
459: new String[] { "add" }, 4);
460:
461: String lvEchoStr = null;
462: int lvAddValue = 0;
463: for (int i = 0; i < 3; i++) {
464: lvAddValue = lvEjbService.add(3, 2);
465: assertEquals(5, lvAddValue);
466: lvEchoStr = lvEjbService.echo("Hello");
467: assertNull(lvEchoStr);
468: }
469: }
470:
471: public void testAddAndRemoveAsynchronousCallback() throws Exception {
472: Properties prop = new Properties();
473: prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
474: prop.put(EjbService.class.getName()
475: + Property.EJB_HOME_INTERFACE, EjbServiceHome.class
476: .getName());
477: prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class
478: .getName());
479: prop.put(Property.DYNAMIC_PROXY_CLASS,
480: Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
481:
482: ServiceManager manager = new ServiceManager(prop);
483: AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
484: EjbService e = (EjbService) manager
485: .createService(EjbService.class, lvCallback,
486: new String[] { "add" }, 4);
487:
488: String lvEchoStr = e.echo("Hello");
489: assertNull(lvEchoStr);
490:
491: assertFalse(manager.isInvocationAsynchronous(Calculator.class));
492:
493: lvEchoStr = e.echo("Hello2");
494: assertNull(lvEchoStr);
495:
496: manager.removeAsynchronousCallback(EjbService.class);
497:
498: lvEchoStr = e.echo("Hello3");
499: assertNotNull(lvEchoStr);
500: assertEquals("Return Echo: Hello3", lvEchoStr);
501: }
502:
503: }
|