View Javadoc

1   /*
2    * Created on 29.04.2005 from Linke
3    *
4    */
5   package test.crispy.impl;
6   
7   import java.util.Date;
8   import java.util.Properties;
9   
10  import javax.naming.InitialContext;
11  
12  import junit.framework.TestCase;
13  import net.sf.crispy.Property;
14  import net.sf.crispy.impl.ServiceManager;
15  import net.sf.crispy.impl.StaticEjbProxy;
16  import net.sf.crispy.interceptor.StopWatchInterceptor;
17  
18  import org.mockejb.MockContainer;
19  import org.mockejb.SessionBeanDescriptor;
20  import org.mockejb.jndi.MockContextFactory;
21  
22  import test.crispy.concurrent.AsynchronousCallbackForTests;
23  import test.crispy.example.ejb.EjbService;
24  import test.crispy.example.ejb.EjbServiceBean;
25  import test.crispy.example.ejb.EjbServiceHome;
26  import test.crispy.example.interceptor.WaitInterceptor;
27  import test.crispy.example.model.Kunde;
28  import test.crispy.example.service.Calculator;
29  
30  /**
31   * @author Linke
32   *
33   */
34  public class EjbServiceTest extends TestCase {
35  	
36  	
37  	/**
38  	 * @see junit.framework.TestCase#setUp()
39  	 */
40  	protected void setUp() throws Exception {
41  		super.setUp();
42      	MockContextFactory.setAsInitial();
43      	MockContainer lvMockContainer = new MockContainer(new InitialContext ());
44      	SessionBeanDescriptor lvBeanDescriptor = new SessionBeanDescriptor(
45      							EjbService.JNDI_NAME, EjbServiceHome.class, 
46  								EjbService.class, EjbServiceBean.class);
47      	lvMockContainer.deploy(lvBeanDescriptor);
48  		
49  	}
50  	
51  	public void testSimpleEjbTest() {
52      	Properties prop = new Properties();
53          prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
54          prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
55          prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
56          
57          ServiceManager lvServiceManager = new ServiceManager(prop);       
58  
59      	EjbService lvEjbService = (EjbService) lvServiceManager.createService(EjbService.class);
60      	assertNotNull(lvEjbService);
61      	String lvEchoStr = "BlaBla...";
62      	assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: " + lvEchoStr);
63      	assertEquals(lvEjbService.add(5, 7), 12);
64  	}
65  
66  	public void testEjbTestWithDynamicJdkProxy() {
67      	Properties prop = new Properties();
68          prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
69          prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
70          prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
71          prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
72          
73          ServiceManager lvServiceManager = new ServiceManager(prop);       
74  
75      	EjbService lvEjbService = (EjbService) lvServiceManager.createService(EjbService.class);
76      	assertNotNull(lvEjbService);
77      	String lvEchoStr = "BlaBla...";
78      	assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: " + lvEchoStr);
79      	assertEquals(lvEjbService.add(5, 7), 12);
80  	}
81  
82      public void testSimpleRemoteInvocationWithOutDynamicJdkProxy() throws Exception {
83  		Properties prop = new Properties ();
84          prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
85          prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
86          prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());		
87          prop.put(Property.INTERCEPTOR_CLASS, WaitInterceptor.class.getName());
88          prop.put(Property.INTERCEPTOR_CLASS_2, StopWatchInterceptor.class.getName());		
89  		
90  		ServiceManager lvManager = new ServiceManager(prop);
91  
92  		EjbService lvEjbService = (EjbService) lvManager.createService(EjbService.class);
93      	assertNotNull(lvEjbService);
94      	String lvEchoStr = "BlaBla...";
95      	assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: " + lvEchoStr);
96      	assertEquals(lvEjbService.add(5, 7), 12);
97  
98  		StopWatchInterceptor lvStopWatchInterceptor = (StopWatchInterceptor) lvManager.getInterceptorByPos(1);
99  		long lvStopTime = lvStopWatchInterceptor.getStopTimeInvokeMethod();
100 		assertTrue("StopTime is: " + lvStopTime, lvStopTime > 10);
101 
102 		lvManager.removeInterceptorByPos(0);
103 		lvManager.removeInterceptorByPos(0);
104 		assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: " + lvEchoStr);
105 		assertEquals(lvStopTime, lvStopWatchInterceptor.getStopTimeInvokeMethod());
106 		
107 		lvStopWatchInterceptor.clearStopTimeInvokeMethod();
108 		assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: " + lvEchoStr);
109 		lvStopTime = lvStopWatchInterceptor.getStopTimeInvokeMethod();
110 		assertEquals(lvStopTime, 0);
111     }
112 
113 	/** Ejb und Cglib-Proxy funnktioniert nicht, weil bei Ejb's die Klasse final ist! */
114 	public void _testEjbTestWithDynamicCglibProxy() {
115     	Properties prop = new Properties();
116         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
117         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
118         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
119         prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY);
120         
121         ServiceManager lvServiceManager = new ServiceManager(prop);       
122 
123     	EjbService lvEjbService = (EjbService) lvServiceManager.createService(EjbService.class);
124     	assertNotNull(lvEjbService);
125     	String lvEchoStr = "BlaBla...";
126     	assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: " + lvEchoStr);
127     	assertEquals(lvEjbService.add(5, 7), 12);
128 	}
129 
130 	public void testNoHomeInterface() throws Exception {
131     	Properties prop = new Properties();
132         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
133         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
134 
135         ServiceManager lvServiceManager = new ServiceManager(prop);       
136         try {
137         	EjbService lvEjbService = (EjbService) lvServiceManager.createService(EjbService.class);
138         	assertNotNull(lvEjbService);
139         	fail("Without HomeInterface no EjbService!");
140 		} catch (Exception e) { assertTrue(true); }
141 	}
142 	
143 	public void testNoJndiEntry() throws Exception {
144     	Properties prop = new Properties();
145         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
146 
147         ServiceManager lvServiceManager = new ServiceManager(prop);       
148         try {
149         	EjbService lvEjbService = (EjbService) lvServiceManager.createService(EjbService.class);
150         	assertNotNull(lvEjbService);
151         	fail("Without JNDI-Name no EjbService!");
152 		} catch (Exception e) { assertTrue(true);}		
153 	}
154 
155 	public void testInvalidJndiName() throws Exception {
156     	Properties prop = new Properties();
157         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME + "_");
158         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
159 
160         ServiceManager lvServiceManager = new ServiceManager(prop);       
161         try {
162         	EjbService lvEjbService = (EjbService) lvServiceManager.createService(EjbService.class);
163         	assertNotNull(lvEjbService);
164         	fail("Invalide JNDI-Name!");
165 		} catch (Exception e) { assertTrue(true); }
166 	}
167 	
168     public void testProxyInterceptor() throws Exception {
169         Properties prop = new Properties();
170         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
171         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
172         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
173         prop.put(Property.INTERCEPTOR_CLASS, StopWatchInterceptor.class.getName());
174         
175         ServiceManager lvServiceManager = new ServiceManager(prop);       
176 
177     	EjbService lvEjbService = (EjbService) lvServiceManager.createService(EjbService.class);
178     	assertNotNull(lvEjbService);
179     	String lvEchoStr = "BlaBla...";
180     	assertEquals(lvEjbService.echo(lvEchoStr), "Return Echo: " + lvEchoStr);
181     	assertEquals(lvEjbService.add(5, 7), 12);
182     	
183 //    	StopWatchInterceptor lvProxyInterceptor = (StopWatchInterceptor) lvServiceManager.getInterceptorIterator().next();
184 //    	System.out.println("EjbTest-StopTimeNewInstance: " + lvProxyInterceptor.getStopTimeNewInstance());
185 //    	System.out.println("EjbTest-StopTimeInvokeMethod: " + lvProxyInterceptor.getStopTimeInvokeMethod());
186     }
187 
188     public void testCreateService() throws Exception {
189         Properties prop = new Properties();
190         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
191         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
192         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
193 
194     	
195         ServiceManager lvManager = new ServiceManager(prop);
196         EjbService lvEjbService1 = (EjbService) lvManager.createService(EjbService.class);
197         EjbService lvEjbService2 = (EjbService) lvManager.createService(EjbService.class);
198         
199         assertNotNull(lvEjbService1);
200         assertNotNull(lvEjbService2);
201         assertNotSame(lvEjbService1, lvEjbService2);
202 	}
203 
204 	public void testEjbTestWithoutHomeInterface() {
205     	Properties prop = new Properties();
206         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
207 //        prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
208         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
209         
210         ServiceManager lvServiceManager = new ServiceManager(prop);       
211 
212         try {
213         	lvServiceManager.createService(EjbService.class);
214         	fail("Must thrown Exception: No Home-Interface");
215 		} catch (Exception e) {
216 			assertTrue(true);
217 		}
218 	}
219 
220 	
221     public void testNullValueParamsWithException() throws Exception {
222         Properties prop = new Properties();
223         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
224         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
225         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
226         
227 		ServiceManager lvManager = new ServiceManager(prop);
228 
229     	EjbService lvEjbService = (EjbService) lvManager.createService(EjbService.class);
230 		String s = lvEjbService.echo(null);
231 		assertNull(s);
232 	}
233 
234     public void testNullLongValueParams() throws Exception {
235         Properties prop = new Properties();
236         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
237         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
238         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
239         
240 		ServiceManager lvManager = new ServiceManager(prop);
241 
242 		EjbService lvEjbService = (EjbService) lvManager.createService(EjbService.class);
243 		Long lvLong = lvEjbService.addLong(null, null);
244 		assertNull(lvLong);
245 	}
246 
247     public void testNullComplexValueParams() throws Exception {
248         Properties prop = new Properties();
249         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
250         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
251         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
252 		
253 		ServiceManager lvManager = new ServiceManager(prop);
254 
255 		EjbService lvEjbService = (EjbService) lvManager.createService(EjbService.class);
256 		Kunde k = lvEjbService.renameKunde(null, null);
257 		assertNull(k);
258 	}
259     
260 	public void testTransferDate() throws Exception {
261 		Properties prop = new Properties();
262         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
263         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
264         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
265 
266 		
267 		ServiceManager manager = new ServiceManager(prop);
268 		EjbService lvEcho =  (EjbService) manager.createService(EjbService.class);
269 		
270 		Kunde k = new Kunde("JUnit-Test-Name");
271 		Date d = new Date();
272 		k.setDate(d);
273 		Kunde k2 = lvEcho.renameKunde(k, "Rename-Test");
274 		assertNotNull(k2.getDate());
275 		assertEquals(k.getDate(), k2.getDate());
276 	}
277 
278     
279     
280     
281 	public void testAsynchronousInvocation() throws Exception {
282 		Properties prop = new Properties();
283         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
284         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
285         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
286 		prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName());
287 		prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
288 		
289 		ServiceManager manager = new ServiceManager(prop);
290 		EjbService lvEjbService = (EjbService) manager.createService(EjbService.class);
291 		String lvEchoString = lvEjbService.echo("Hello EJB-Test echo");
292 		assertNull(lvEchoString);
293 		
294         assertTrue(manager.isInvocationAsynchronous(EjbService.class));
295         
296         Thread.sleep(300);
297         AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(EjbService.class);
298         assertNotNull(lvAsynchronousCallbackForTests);
299         
300         assertEquals("echo", lvAsynchronousCallbackForTests.getMethodName());
301         assertEquals("Return Echo: Hello EJB-Test echo", lvAsynchronousCallbackForTests.getResult());
302         assertNull(lvAsynchronousCallbackForTests.getThrowable());
303 	}
304 	
305 	public void testAsynchronousInvocationWithMultyThreaded() throws Exception {
306 		Properties prop = new Properties();
307         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
308         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
309         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
310 		prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName());
311 		prop.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "3");
312 		prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
313 		
314 		ServiceManager manager = new ServiceManager(prop);
315 		EjbService lvEjbService = (EjbService) manager.createService(EjbService.class);
316 		String lvEchoString = lvEjbService.echo("Hello EJB-Test echo");
317 		assertNull(lvEchoString);
318 		
319         assertTrue(manager.isInvocationAsynchronous(EjbService.class));
320         
321         Thread.sleep(300);
322         AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(EjbService.class);
323         assertNotNull(lvAsynchronousCallbackForTests);
324         
325         assertEquals("echo", lvAsynchronousCallbackForTests.getMethodName());
326         assertEquals("Return Echo: Hello EJB-Test echo", lvAsynchronousCallbackForTests.getResult());
327         assertNull(lvAsynchronousCallbackForTests.getThrowable());
328 	}
329 
330 	public void testIndivualAsynchronousInvocationWithMultyThreaded() throws Exception {
331 		Properties prop = new Properties();
332         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
333         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
334         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
335 		prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
336 
337 		ServiceManager manager = new ServiceManager(prop);
338 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
339 		EjbService lvEjbService = (EjbService) manager.createService(EjbService.class, lvCallback, null, 4);
340 		
341 		String lvEchoStr = null;
342 		for (int i=0;i<3;i++) {
343 			lvEchoStr = lvEjbService.echo("Hello");
344 			assertNull(lvEchoStr);
345 		}
346 		
347 		Thread.sleep(500);
348 		assertEquals("echo", lvCallback.getMethodName());
349 		assertEquals("Return Echo: Hello", lvCallback.getResult());
350 	}
351 	
352 
353 	public void testIndivualAsynchronousInvocationWithMethodFilter() throws Exception {
354 		Properties prop = new Properties();
355         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
356         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
357         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
358 		prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
359 
360 
361 		ServiceManager manager = new ServiceManager(prop);
362 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
363 		EjbService lvEjbService = (EjbService) manager.createService(EjbService.class, lvCallback, new String[] { "add" }, 4);
364 		
365 		String lvEchoStr = null;
366 		int lvAddValue = 0;
367 		for (int i=0;i<3;i++) {
368 			lvAddValue = lvEjbService.add(3, 2);
369 			assertEquals(5, lvAddValue);
370 			lvEchoStr = lvEjbService.echo("Hello");
371 			assertNull(lvEchoStr);
372 		}
373 	}
374 	
375 	public void testAddAndRemoveAsynchronousCallback() throws Exception {
376 		Properties prop = new Properties();
377         prop.put(EjbService.class.getName(), EjbService.JNDI_NAME);
378         prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName());
379         prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
380 		prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
381 
382 
383 		ServiceManager manager = new ServiceManager(prop);
384 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
385 		EjbService e = (EjbService) manager.createService(EjbService.class, lvCallback, new String[] { "add" }, 4);
386 		
387 		String lvEchoStr = e.echo("Hello");
388 		assertNull(lvEchoStr);
389 				
390 		assertFalse(manager.isInvocationAsynchronous(Calculator.class));
391 		
392 		lvEchoStr = e.echo("Hello2");
393 		assertNull(lvEchoStr);
394 
395 		manager.removeAsynchronousCallback(EjbService.class);
396 		
397 		lvEchoStr = e.echo("Hello3");
398 		assertNotNull(lvEchoStr);
399 		assertEquals("Return Echo: Hello3", lvEchoStr);
400 	}
401 
402 
403 }