View Javadoc

1   package test.crispy.impl;
2   
3   import java.util.Properties;
4   
5   import junit.framework.TestCase;
6   import net.sf.crispy.Property;
7   import net.sf.crispy.impl.MiniServer;
8   import net.sf.crispy.impl.ServiceManager;
9   import net.sf.crispy.impl.StaticCorbaProxy;
10  import net.sf.crispy.impl.corba.MiniCorbaServer;
11  import net.sf.crispy.interceptor.StopWatchInterceptor;
12  import test.crispy.concurrent.AsynchronousCallbackForTests;
13  import test.crispy.example.interceptor.WaitInterceptor;
14  import test.crispy.example.service.corba.Calculator;
15  import test.crispy.example.service.corba.Customer;
16  import test.crispy.example.service.corba.Echo;
17  
18  public class StaticCorbaProxyTest extends TestCase {
19  
20  	private static MiniServer miniServer = null;
21  	
22  	public StaticCorbaProxyTest() {
23  		if (miniServer == null) {
24  			miniServer = new MiniCorbaServer();
25  			miniServer.addService("test.crispy.example.service.corba.Echo", "test.crispy.example.service.CorbaEcho");
26  			miniServer.addService("test.crispy.example.service.corba.Calculator", "test.crispy.example.service.CorbaCalculator");
27  			miniServer.start();
28  		}
29  	}
30  	
31  	public void testSimpleRemotePing() throws Exception {
32    	  Properties props = new Properties();
33  	  props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
34  
35  	  ServiceManager lvManager = new ServiceManager(props);
36  	  Echo lvEcho = (Echo) lvManager.createService(Echo.class);
37  	  String lvPingString = lvEcho.ping();
38  	  assertNotNull(lvPingString);
39  	  assertEquals("PING", lvPingString);
40  	}
41  
42  	public void testSimpleMultiServices() throws Exception {
43    	  Properties props = new Properties();
44  	  props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
45  
46  	  ServiceManager lvManager = new ServiceManager(props);
47  	  Echo lvEcho = (Echo) lvManager.createService(Echo.class);
48  	  String lvPingString = lvEcho.ping();
49  	  assertNotNull(lvPingString);
50  		  
51  	  Calculator lvCalculator = (Calculator) lvManager.createService(Calculator.class);
52  	  assertNotNull(lvCalculator);
53  	  assertEquals(5, lvCalculator.add(2, 3));
54  	  assertEquals(7, lvCalculator.subtract(8, 1));
55  	}
56  
57  	public void testSimpleRemotePingWithUrlProperties() throws Exception {
58    	  Properties props = new Properties();
59    	  props.put(Property.REMOTE_URL_AND_PORT, "localhost:1057");
60  	  props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
61  
62  	  ServiceManager lvManager = new ServiceManager(props);
63  	  Echo lvEcho = (Echo) lvManager.createService(Echo.class);
64  	  String lvPingString = lvEcho.ping();
65  	  assertNotNull(lvPingString);
66  		}
67  	
68  	public void testEchoTest() throws Exception {
69    	  Properties props = new Properties();
70  	  props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
71  
72  	  ServiceManager lvManager = new ServiceManager(props);
73  	  Echo lvEcho = (Echo) lvManager.createService(Echo.class);
74  	  String lvEchoString = "Hello Crispy";
75  	  String lvEchoStringAfter = lvEcho.echo(lvEchoString);
76  	  assertEquals("CORBA-Echo: " + lvEchoString, lvEchoStringAfter);
77  	}
78  	
79  	
80  	public void testNullValueParameter() throws Exception {
81    	  Properties props = new Properties();
82  	  props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
83  
84  	  ServiceManager lvManager = new ServiceManager(props);
85  	  Echo lvEcho = (Echo) lvManager.createService(Echo.class);
86  
87  	  try {
88  		  lvEcho.echo(null);
89  		  fail("CORBA not supported null values, must thrown Exception.");
90  	  } catch (Exception e) {
91  		assertTrue(true);
92  	  }
93  	}
94  	
95  	public void testTestWithDynamicJdkProxy() {
96    	  Properties props = new Properties();
97  	  props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
98  	  props.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
99  
100 	  ServiceManager lvManager = new ServiceManager(props);
101 	  Echo lvEcho = (Echo) lvManager.createService(Echo.class);
102 	  String lvEchoString = "Hello Crispy";
103 	  String lvEchoStringAfter = lvEcho.echo(lvEchoString);
104 	  assertEquals("CORBA-Echo: " + lvEchoString, lvEchoStringAfter);	  
105 	  
106 	  Calculator lvCalculator = (Calculator) lvManager.createService(Calculator.class);
107 	  assertNotNull(lvCalculator);
108 	  assertEquals(5, lvCalculator.add(2, 3));
109 	  assertEquals(7, lvCalculator.subtract(8, 1));
110 	}
111 	
112 	public void testTestWithDynamicJdkProxyWithStopWatch() {
113   	  Properties props = new Properties();
114 	  props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
115 	  props.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
116       props.put(Property.INTERCEPTOR_CLASS, WaitInterceptor.class.getName());
117       props.put(Property.INTERCEPTOR_CLASS_2, StopWatchInterceptor.class.getName());		
118 
119 	  ServiceManager lvManager = new ServiceManager(props);
120 	  Echo lvEcho = (Echo) lvManager.createService(Echo.class);
121 	  String lvEchoString = "Hello Crispy";
122 	  String lvEchoStringAfter = lvEcho.echo(lvEchoString);
123 	  assertEquals("CORBA-Echo: " + lvEchoString, lvEchoStringAfter);	  
124 
125 	  StopWatchInterceptor lvStopWatchInterceptor = (StopWatchInterceptor) lvManager.getInterceptorByPos(1);
126 	  long lvStopTime = lvStopWatchInterceptor.getStopTimeInvokeMethod();
127 	  assertTrue("StopTime is: " + lvStopTime, lvStopTime > 10);		  
128 	}
129 	
130     public void testCreateService() throws Exception {
131     	Properties props = new Properties();
132     	props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
133     	
134         ServiceManager lvManager = new ServiceManager(props);
135         Echo lvEcho1 = (Echo) lvManager.createService(Echo.class);
136         Echo lvEcho2 = (Echo) lvManager.createService(Echo.class);
137         
138         assertNotNull(lvEcho1);
139         assertNotNull(lvEcho2);
140         assertNotSame(lvEcho1, lvEcho2);
141 	}
142 
143     public void testMiniCorbaServer() throws Exception {
144     	int lvPort = 12345;
145 		MiniCorbaServer lvCorbaServer = new MiniCorbaServer(lvPort);
146 		assertEquals(lvPort, lvCorbaServer.getPort());
147 		
148 		lvCorbaServer = new MiniCorbaServer(null, lvPort);
149 		assertEquals("127.0.0.1", lvCorbaServer.getHost());
150 
151 		String lvHost = "MyHost";
152 		lvCorbaServer = new MiniCorbaServer(lvHost, lvPort);
153 		assertEquals(lvPort, lvCorbaServer.getPort());
154 		assertEquals(lvHost, lvCorbaServer.getHost());
155 				
156 		miniServer.start();
157 		miniServer.start();
158 	}
159     
160     
161 	public void testComplexArgsServices() throws Exception {
162   	  Properties props = new Properties();
163 	  props.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
164 
165 	  ServiceManager lvManager = new ServiceManager(props);
166 	  Echo lvEcho = (Echo) lvManager.createService(Echo.class);
167 	  Customer lvCustomer = new Customer("TestFirstName", "JUnitLastName", 47.11);
168 	  String lvTestRename = "NewTestName";
169 	  Customer lvCustomerAfterRename = lvEcho.rename(lvCustomer, lvTestRename);
170 	  assertNotNull(lvCustomerAfterRename);
171 	  assertEquals(lvCustomerAfterRename.lastName, lvTestRename);
172 	}
173 
174 	
175 	
176 	
177 	public void testAsynchronousInvocation() throws Exception {
178 		Properties prop = new Properties();
179     	prop.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
180     	prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
181 		prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName());
182 		
183 		ServiceManager manager = new ServiceManager(prop);
184 		Echo echo =  (Echo) manager.createService(Echo.class);
185 		String lvPing = echo.ping();
186 		assertNull(lvPing);
187 		
188         assertTrue(manager.isInvocationAsynchronous(Echo.class));
189         
190         Thread.sleep(300);
191         AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(Echo.class);
192         assertNotNull(lvAsynchronousCallbackForTests);
193         
194         assertEquals("ping", lvAsynchronousCallbackForTests.getMethodName());
195         assertEquals("PING", lvAsynchronousCallbackForTests.getResult());
196         assertNull(lvAsynchronousCallbackForTests.getThrowable());
197 	}
198 	
199 	public void testAsynchronousInvocationWithMultyThreaded() throws Exception {
200 		Properties prop = new Properties();
201 		prop.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
202 		prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
203 		prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName());
204 		prop.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "3");
205 		
206 		ServiceManager manager = new ServiceManager(prop);
207 		Echo echo =  (Echo) manager.createService(Echo.class);
208 		String lvPing = echo.ping();
209 		assertNull(lvPing);
210 		
211         assertTrue(manager.isInvocationAsynchronous(Echo.class));
212         
213         Thread.sleep(500);
214         AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(Echo.class);
215         assertNotNull(lvAsynchronousCallbackForTests);
216         
217         assertEquals("ping", lvAsynchronousCallbackForTests.getMethodName());
218         assertEquals("PING", lvAsynchronousCallbackForTests.getResult());
219         assertNull(lvAsynchronousCallbackForTests.getThrowable());
220 	}
221 
222 	public void testIndivualAsynchronousInvocationWithMultyThreaded() throws Exception {
223 		Properties prop = new Properties();
224 		prop.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
225         prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
226 
227 		ServiceManager manager = new ServiceManager(prop);
228 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
229 		Echo e = (Echo) manager.createService(Echo.class, lvCallback, null, 8);
230 		
231 		String lvEchoStr = null;
232 		for (int i=0;i<3;i++) {
233 			lvEchoStr = e.echo("Hello");
234 			assertNull(lvEchoStr);
235 		}
236 		
237 		Thread.sleep(500);
238         assertEquals("echo", lvCallback.getMethodName());
239         assertEquals("CORBA-Echo: Hello", lvCallback.getResult());
240         assertNull(lvCallback.getThrowable());
241 		
242 	}
243 	
244 	public void testIndivualAsynchronousInvocationWithMultyThreadedWith2Services() throws Exception {
245 		Properties prop = new Properties();
246 		prop.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
247         prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
248 
249 		ServiceManager manager = new ServiceManager(prop);
250 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
251 		Echo lvEcho = (Echo) manager.createService(Echo.class, lvCallback, null, 8);
252 		Calculator lvCalculator =  (Calculator) manager.createService(Calculator.class, lvCallback, null, 8);
253 		
254 		assertNotNull(lvEcho);
255 		assertNotNull(lvCalculator);
256 		
257 		String lvEchoStr = null;
258 		int lvAddResult = -1;
259 		for (int i=0;i<3;i++) {
260 			lvEchoStr = lvEcho.echo("Hello");
261 			assertNull(lvEchoStr);
262 			lvAddResult = lvCalculator.add(123, 456);
263 			assertEquals(0, lvAddResult);
264 		}
265 	}
266 
267 	public void testIndivualAsynchronousInvocationWithMethodFilter() throws Exception {
268 		Properties prop = new Properties();
269 		prop.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
270         prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
271 
272 		ServiceManager manager = new ServiceManager(prop);
273 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
274 		Echo e = (Echo) manager.createService(Echo.class, lvCallback, new String[] { "ping" }, 4);
275 		
276 		String lvEchoStr = null;
277 		String lvPing = null;
278 		for (int i=0;i<3;i++) {
279 			lvPing = e.ping();
280 			assertNotNull(lvPing);
281 			assertEquals("PING", lvPing);
282 			lvEchoStr = e.echo("Hello");
283 			assertNull(lvEchoStr);
284 		}
285 	}
286 	
287 	
288 	public void testAddAndRemoveAsynchronousCallback() throws Exception {
289 		Properties prop = new Properties();
290 		prop.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
291         prop.put(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
292 
293 
294 		ServiceManager manager = new ServiceManager(prop);
295 		AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
296 		Echo e = (Echo) manager.createService(Echo.class, lvCallback, new String[] { "ping" }, 4);
297 		Calculator c =  (Calculator) manager.createService(Calculator.class, lvCallback, null, 4);
298 				
299 		String lvEchoStr = e.echo("Hello");
300 		assertNull(lvEchoStr);
301 		
302 		int lvAddResult = c.add(1, 3);
303 		assertEquals(0, lvAddResult);
304 		
305 		assertTrue(manager.isInvocationAsynchronous(Calculator.class));
306 		manager.removeAsynchronousCallback(Calculator.class);
307 		assertFalse(manager.isInvocationAsynchronous(Calculator.class));
308 		
309 		lvEchoStr = e.echo("Hello2");
310 		assertNull(lvEchoStr);
311 		
312 		lvAddResult = c.add(2, 3);
313 		assertEquals(5, lvAddResult);
314 		
315 		
316 		manager.removeAsynchronousCallback(Echo.class);
317 		
318 		lvEchoStr = e.echo("Hello3");
319 		assertNotNull(lvEchoStr);
320 		assertEquals("CORBA-Echo: Hello3", lvEchoStr);
321 	}
322 
323 
324 }