View Javadoc

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