View Javadoc

1   package test.crispy.impl;
2   
3   import java.util.Date;
4   import java.util.Properties;
5   
6   import junit.framework.TestCase;
7   import net.sf.crispy.Property;
8   import net.sf.crispy.impl.RestExecutor;
9   import net.sf.crispy.impl.ServiceManager;
10  import net.sf.crispy.impl.rest.MiniRestServer;
11  import net.sf.crispy.interceptor.StopWatchInterceptor;
12  import test.crispy.compatibility.CompatibilityKit;
13  import test.crispy.concurrent.AsynchronousCallbackForTests;
14  import test.crispy.example.interceptor.WaitInterceptor;
15  import test.crispy.example.model.Kunde;
16  import test.crispy.example.model.Primitive;
17  import test.crispy.example.service.Calculator;
18  import test.crispy.example.service.Echo;
19  
20  public class RestServiceTest extends TestCase {
21  
22  	private static MiniRestServer miniRestServer = null;
23  	private CompatibilityKit compatibilityKit = new CompatibilityKit();
24  	
25  	/**
26  	 * Init <code>static</code> MiniXmlRpcServer for all tests.
27  	 */
28  	public RestServiceTest() {
29  		if (miniRestServer == null) {
30  			miniRestServer = new MiniRestServer();
31  			
32  			miniRestServer.addService("test.crispy.example.service.Echo", "test.crispy.example.service.EchoImpl");
33  			miniRestServer.addService("service.echo", "test.crispy.example.service.EchoImpl");
34  			miniRestServer.addService("test.crispy.example.service.Calculator", "test.crispy.example.service.CalculatorImpl");
35  			miniRestServer.addService("service.calculator", "test.crispy.example.service.CalculatorImpl");
36  			
37  			miniRestServer.start();
38  		}
39  		compatibilityKit.addProperty(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
40  	}
41  	
42  	/**
43  	 * Started MiniRestServer.
44  	 * 
45  	 * @see junit.framework.TestCase#setUp()
46  	 */
47  	protected void setUp() throws Exception {
48  		super.setUp();
49  		compatibilityKit.removeProperty(Property.DYNAMIC_PROXY_CLASS);
50  	}
51  		
52      public void testEchoPrimitive() throws Exception {
53      	Properties prop = new Properties();
54      	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
55          
56          ServiceManager lvServiceManager = new ServiceManager(prop);
57          Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class);
58          Primitive lvPrimitive = Primitive.createPrimitiveExample();
59          
60          Primitive lvPrimitiveAfter = lvEcho.echoPrimitive(lvPrimitive);
61          assertEquals(lvPrimitive, lvPrimitiveAfter);
62          assertNotSame(lvPrimitive, lvPrimitiveAfter);
63  	}
64  
65  	public void testSimpleRemotePing() throws Exception {
66  		Properties prop = new Properties();
67      	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
68      			
69  		ServiceManager manager = new ServiceManager(prop);
70  		Echo echo =  (Echo) manager.createService(Echo.class);
71  		String lvPing = echo.ping();
72  		assertEquals(lvPing, Echo.PING_STRING);
73  	}
74  	
75      public void testSimpleRemoteInvocation() throws Exception {
76          compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager());
77  	}
78      
79   
80      public void testSimpleRemoteInvocationWithDynamicCglibProxy() throws Exception {
81      	compatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY);
82      	compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager());
83  	}
84  	
85      public void testSimpleRemoteInvocationWithDynamicJdkProxy() throws Exception {
86      	compatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
87      	compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager());
88  	}
89      
90      public void testSimpleDoubleRemoteInvocation() throws Exception {
91      	compatibilityKit.makeMultipleServiceTest();
92      }
93  
94      public void testComplexRemoteInvocation() throws Exception {
95      	compatibilityKit.makeComplexEchoTest(compatibilityKit.createServiceManager());
96      }
97  
98      public void testCycleDetectionTest() throws Exception {
99  		compatibilityKit.makeCycleDetectionTest(compatibilityKit.createServiceManager());
100 	}    
101     
102     public void testAddNode() throws Exception {
103     	compatibilityKit.makeAddNodeTest(compatibilityKit.createServiceManager());
104 	}    
105 
106     public void testProxyInterceptor() throws Exception {
107         Properties prop = new Properties();
108         prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
109         prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
110         prop.put(Property.INTERCEPTOR_CLASS, StopWatchInterceptor.class.getName());
111         prop.put(Property.INTERCEPTOR_CLASS_2, WaitInterceptor.class.getName());
112         
113         ServiceManager lvServiceManager = new ServiceManager(prop);
114         StopWatchInterceptor lvStopWatch = (StopWatchInterceptor) lvServiceManager.getInterceptorByPos(0);
115 
116 		Calculator lvCalculator = (Calculator) lvServiceManager.createService(Calculator.class);
117 		assertNotNull(lvCalculator);
118 		int lvResult = lvCalculator.add(2, 3);
119 		assertTrue("StopWatch: " + lvStopWatch.getStopTimeInvokeMethod(), lvStopWatch.getStopTimeInvokeMethod() >= 0);
120 		assertEquals(lvResult, 5);
121 		lvResult = lvCalculator.subtract(8, 2);
122 		assertTrue("StopWatch: " + lvStopWatch.getStopTimeInvokeMethod(), lvStopWatch.getStopTimeInvokeMethod() >= 0);
123 		assertEquals(lvResult, 6);		
124         
125         
126 		Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class);
127 		assertNotNull(lvEcho);
128 		String lvEchoStr = "Hallo Echo";
129 		assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr);		
130 		assertTrue("StopWatch: " + lvStopWatch.getStopTimeInvokeMethod(), lvStopWatch.getStopTimeInvokeMethod() >= 0);
131 	}
132 
133     public void testModifyService() throws Exception {
134 		compatibilityKit.makeModifyServiceTest(compatibilityKit.createServiceManager());
135 	}
136     
137     public void testAddLongs() throws Exception {
138     	Properties prop = new Properties();
139     	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
140     	
141 		ServiceManager manager = new ServiceManager(prop);
142 		Calculator calc =  (Calculator) manager.createService(Calculator.class);
143 		assertEquals(calc.addLong(new Long(123l), new Long(124l)), new Long(247));
144 	}    
145 
146     public void testOverloading() throws Exception {
147 		Properties prop = new Properties();
148     	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());		
149 		
150 		ServiceManager manager = new ServiceManager(prop);
151 		Calculator lvCalculator = (Calculator) manager.createService(Calculator.class);
152 		int lvIntResult = lvCalculator.add(2, 3);
153 		assertEquals(lvIntResult, 5);
154 		long lvLongResult = lvCalculator.add(21, 32);
155 		assertEquals(lvLongResult, 53);
156 		double lvDoubleResult = lvCalculator.add(2.3, 3.2);
157 		assertEquals(lvDoubleResult, 5.5, 0);
158     }
159     
160     public void testCreateService() throws Exception {
161         Properties prop = new Properties();
162     	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
163     	
164         ServiceManager lvManager = new ServiceManager(prop);
165         Calculator lvCalculator1 = (Calculator) lvManager.createService(Calculator.class);
166         Calculator lvCalculator2 = (Calculator) lvManager.createService(Calculator.class);
167         
168         assertNotNull(lvCalculator1);
169         assertNotNull(lvCalculator2);
170         assertNotSame(lvCalculator1, lvCalculator2);
171 	}
172 
173     public void testThrowException() throws Exception {
174         Properties prop = new Properties();
175         prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
176         
177 		ServiceManager lvManager = new ServiceManager(prop);
178 
179 		Echo lvEcho = (Echo) lvManager.createService(Echo.class);
180 		boolean throwException = false;
181 		try {
182 			lvEcho.throwException("A Test-Excption.");
183 		} catch (Exception e) {
184 			throwException = true;
185 		}
186 		assertTrue("No Exception thrown: " + throwException, throwException);
187 	}
188     
189     public void _testOverloadingInteger() throws Exception {
190 		Properties prop = new Properties();
191     	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());		
192 		
193 		ServiceManager manager = new ServiceManager(prop);
194 		Echo echo =  (Echo) manager.createService(Echo.class);
195 		Integer i[] = echo.echoArray(new Integer[] {new Integer(1), new Integer(3), new Integer(5)});
196 		assertTrue(i.length == 3);
197 		assertEquals(i[0], new Integer(1));
198 		assertEquals(i[1], new Integer(3));
199 		assertEquals(i[2], new Integer(5));
200 	}
201     
202     public void _testOverloadingLong() throws Exception {
203 		Properties prop = new Properties();
204     	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
205     	
206 		ServiceManager manager = new ServiceManager(prop);
207 		Echo echo =  (Echo) manager.createService(Echo.class);
208 		Long l[] = echo.echoArray(new Long[] {new Long(1), new Long(5)});
209 		assertTrue(l.length == 2);
210 		assertEquals(l[0], new Long(1));
211 		assertEquals(l[1], new Long(5));
212 	}
213 
214     public void _testOverloadingObject() throws Exception {
215 		Properties prop = new Properties();
216     	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
217     	
218 		ServiceManager manager = new ServiceManager(prop);
219 		Echo echo =  (Echo) manager.createService(Echo.class);
220 		Object o[] = echo.echoArray(new Object[] {"a", new Integer(3), new Long(5)});
221 		assertTrue(o.length == 3);
222 		assertEquals(o[0], "a");
223 		assertEquals(o[1], new Integer(3));
224 //		assertEquals(o[2], new Long(5)); is Integer
225 	}
226 
227 	public void testTransferDate() throws Exception {
228 		Properties prop = new Properties();
229     	prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
230 		
231 		ServiceManager manager = new ServiceManager(prop);
232 		Echo lvEcho =  (Echo) manager.createService(Echo.class);
233 		
234 		Kunde k = new Kunde("JUnit-Test-Name");
235 		Date d = new Date();
236 		k.setDate(d);
237 		Kunde k2 = lvEcho.renameKunde(k, "Rename-Test");
238 		assertNotNull(k2.getDate());
239 		// the dates are different after transfer with Xml_Rpc
240 		// cut the last 3 position
241 		String s1 = Long.toString(d.getTime());
242 		s1 = s1.substring(0, (s1.length() - 3)) + "000";
243 		String s2 = Long.toString(k2.getDate().getTime());
244 		assertEquals(s1, s2);
245 
246 	}
247 
248     
249     
250 	public void testAsynchronousInvocation() throws Exception {
251 		Properties prop = new Properties();
252 		prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
253 		prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName());
254 		
255 		ServiceManager manager = new ServiceManager(prop);
256 		Echo echo =  (Echo) manager.createService(Echo.class);
257 		String lvPing = echo.ping();
258 		assertNull(lvPing);
259 		
260         assertTrue(manager.isInvocationAsynchronous(Echo.class));
261         
262         Thread.sleep(300);
263         AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(Echo.class);
264         assertNotNull(lvAsynchronousCallbackForTests);
265         
266         assertEquals("ping", lvAsynchronousCallbackForTests.getMethodName());
267         assertEquals(Echo.PING_STRING, lvAsynchronousCallbackForTests.getResult());
268         assertNull(lvAsynchronousCallbackForTests.getThrowable());
269 	}
270 	
271 	public void testAsynchronousInvocationWithMultyThreaded() throws Exception {
272 		Properties prop = new Properties();
273 		prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
274 		prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName());
275 		prop.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "3");
276 		
277 		ServiceManager manager = new ServiceManager(prop);
278 		Echo echo =  (Echo) manager.createService(Echo.class);
279 		String lvPing = echo.ping();
280 		assertNull(lvPing);
281 		
282         assertTrue(manager.isInvocationAsynchronous(Echo.class));
283         
284         Thread.sleep(300);
285         AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(Echo.class);
286         assertNotNull(lvAsynchronousCallbackForTests);
287         
288         assertEquals("ping", lvAsynchronousCallbackForTests.getMethodName());
289         assertEquals(Echo.PING_STRING, lvAsynchronousCallbackForTests.getResult());
290         assertNull(lvAsynchronousCallbackForTests.getThrowable());
291 	}
292 
293 	public void testIndivualAsynchronousInvocationWithMultyThreaded() throws Exception {
294 		Properties prop = new Properties();
295 		prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
296 
297 		ServiceManager manager = new ServiceManager(prop);
298 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
299 		Echo e = (Echo) manager.createService(Echo.class, lvCallback, null, 8);
300 		
301 		String lvEchoStr = null;
302 		String lvLongExecution = null;
303 		for (int i=0;i<3;i++) {
304 			lvLongExecution = e.doLongExecution("Hello");
305 			assertNull(lvLongExecution);
306 			lvEchoStr = e.echo("Hello");
307 			assertNull(lvEchoStr);
308 		}
309 	}
310 	
311 	public void testIndivualAsynchronousInvocationWithMultyThreadedWith2Services() throws Exception {
312 		Properties prop = new Properties();
313 		prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
314 
315 		ServiceManager manager = new ServiceManager(prop);
316 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
317 		Echo e = (Echo) manager.createService(Echo.class, lvCallback, null, 8);
318 		Calculator c =  (Calculator) manager.createService(Calculator.class, lvCallback, null, 8);
319 		
320 		String lvLongExecution = null;
321 		Long lvAddResult = null;
322 		for (int i=0;i<3;i++) {
323 			lvLongExecution = e.doLongExecution("Hello");
324 			assertNull(lvLongExecution);
325 			lvAddResult = c.addLong(new Long(123), new Long(456));
326 			assertNull(lvAddResult);
327 		}
328 	}
329 	
330 	public void testIndivualAsynchronousInvocationWithMethodFilter() throws Exception {
331 		Properties prop = new Properties();
332 		prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
333 
334 		ServiceManager manager = new ServiceManager(prop);
335 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
336 		Echo e = (Echo) manager.createService(Echo.class, lvCallback, new String[] { "doLongExecution" }, 4);
337 		
338 		String lvEchoStr = null;
339 		String lvLongExecution = null;
340 		for (int i=0;i<3;i++) {
341 			lvLongExecution = e.doLongExecution("Hello");
342 			assertNotNull(lvLongExecution);
343 			assertEquals("Hello", lvLongExecution);
344 			lvEchoStr = e.echo("Hello");
345 			assertNull(lvEchoStr);
346 		}
347 	}
348 
349 	public void testAddAndRemoveAsynchronousCallback() throws Exception {
350 		Properties prop = new Properties();
351 		prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
352 
353 		ServiceManager manager = new ServiceManager(prop);
354 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
355 		Echo e = (Echo) manager.createService(Echo.class, lvCallback, new String[] { "doLongExecution" }, 4);
356 		Calculator c =  (Calculator) manager.createService(Calculator.class, lvCallback, null, 4);
357 				
358 		String lvEchoStr = e.echo("Hello");
359 		assertNull(lvEchoStr);
360 		
361 		Long lvAddResult = c.addLong(new Long(1), new Long(3));
362 		assertNull(lvAddResult);
363 		
364 		assertTrue(manager.isInvocationAsynchronous(Calculator.class));
365 		manager.removeAsynchronousCallback(Calculator.class);
366 		assertFalse(manager.isInvocationAsynchronous(Calculator.class));
367 		
368 		Thread.sleep(200);
369 		
370 		lvEchoStr = e.echo("Hello2");
371 		assertNull(lvEchoStr);
372 		
373 		lvAddResult = c.addLong(new Long(2), new Long(3));
374 		assertNotNull(lvAddResult);
375 		assertEquals(new Long(5), lvAddResult);
376 		
377 		
378 		manager.removeAsynchronousCallback(Echo.class);
379 		
380 		lvEchoStr = e.echo("Hello3");
381 		assertNotNull(lvEchoStr);
382 		assertEquals("Hello3", lvEchoStr);
383 	}
384 
385 }