View Javadoc

1   package test.crispy.util;
2   
3   import java.lang.reflect.Method;
4   import java.util.Vector;
5   
6   import junit.framework.TestCase;
7   import net.sf.crispy.util.Converter;
8   import net.sf.crispy.util.Invoker;
9   import test.crispy.example.model.Kunde;
10  import test.crispy.example.service.Calculator;
11  import test.crispy.example.service.CalculatorImpl;
12  import test.crispy.example.service.Echo;
13  import test.crispy.example.service.EchoImpl;
14  
15  public class InvokerTest extends TestCase {
16  
17  	
18  	public void testInvokeWithConverter() throws Exception {
19  		Echo lvEcho = new EchoImpl();
20  		String lvEchoStr = "Hello Echo ...";
21  		Vector lvParam = new Vector();
22  		lvParam.add(lvEchoStr);
23  		String lvRetEchoStr = (String) Invoker.invokeWithConverter(lvEcho, "echo", lvParam, new Converter());
24  		assertTrue(lvRetEchoStr.equals(lvEchoStr));
25  		
26  		Calculator lvCalculator = new CalculatorImpl();
27  		lvParam = new Vector();
28  		lvParam.add(new Integer(5));
29  		lvParam.add(new Integer(3));
30  		Integer lvRetCalc = (Integer) Invoker.invokeWithConverter(lvCalculator, "add", lvParam, new Converter());
31  		assertTrue(lvRetCalc.intValue() == 8);
32  		
33  		lvRetCalc = (Integer) Invoker.invokeWithConverter(lvCalculator, "subtract", lvParam, new Converter());
34  		assertTrue(lvRetCalc.intValue() == 2);
35  		
36  		try {
37  			lvRetCalc = (Integer) Invoker.invokeWithConverter(lvCalculator, "xyz", lvParam, new Converter());
38  			fail("NoSuchMethodException is NOT thrown");			
39  		} catch (NoSuchMethodException e) { assertTrue(true); }
40  	}
41  	
42  	public void testInvoke() throws Exception {
43  		String lvEchoStr = "Hello Echo ...";
44  		Vector lvParam = new Vector();
45  		lvParam.add(lvEchoStr);
46  		Object o = Invoker.invoke(EchoImpl.class.getName(), "echo", lvParam, false, new Converter());
47  		assertTrue(o.toString().equals(lvEchoStr));
48  	}
49  
50  	public void testInvokeIntegerArray() throws Exception {
51  		Vector lvParam = new Vector();
52  		lvParam.add(new Integer[] {new Integer(1), new Integer(2), new Integer(3)});
53  		Object o = Invoker.invoke(EchoImpl.class.getName(), "echoArray", lvParam, false, new Converter());
54  		assertTrue(o.getClass().isArray());
55  		assertTrue(o instanceof Integer[]);
56  		Integer i[] = (Integer[]) o;
57  		assertTrue(i.length == 3);
58  		assertEquals(i[1], new Integer(2));
59  	}
60  
61  	public void testInvokeLongArray() throws Exception {
62  		Vector lvParam = new Vector();
63  		lvParam.add(new Long[] {new Long(1), new Long(2), new Long(3)});
64  		Object o = Invoker.invoke(EchoImpl.class.getName(), "echoArray", lvParam, false, new Converter());
65  		assertTrue(o.getClass().isArray());
66  		assertTrue(o instanceof Long[]);
67  		Long l[] = (Long[]) o;
68  		assertTrue(l.length == 3);
69  		assertEquals(l[1], new Long(2));
70  	}
71  
72  	public void testInvokeObjectArray() throws Exception {
73  		Vector lvParam = new Vector();
74  		lvParam.add(new Object[] {"a", new Long(3)});
75  		Object o = Invoker.invoke(EchoImpl.class.getName(), "echoArray", lvParam, false, new Converter());
76  		assertTrue(o.getClass().isArray());
77  		assertTrue(o instanceof Object[]);
78  		Object obj[] = (Object[]) o;
79  		assertTrue(obj.length == 2);
80  		assertEquals(obj[0], "a");
81  		assertEquals(obj[1], new Long(3));
82  	}
83  	
84  	public void testFindMethod() throws Exception {
85  		String lvEchoStr = "Hello Echo ...";
86  		Vector lvParam = new Vector();
87  		lvParam.add(lvEchoStr);
88  		Method lvMethod = Invoker.findMethod(Echo.class, "echo", lvParam);
89  		assertTrue(lvMethod.getName().equals("echo"));
90  		assertTrue(lvMethod.getParameterTypes().length == 1);
91  		Class lvClass = lvMethod.getParameterTypes()[0];
92  		assertTrue(lvClass.getName().equals(String.class.getName()));
93  		
94  		lvParam = new Vector();
95  		lvParam.add(new Integer(5));
96  		lvParam.add(new Integer(3));
97  		lvMethod = Invoker.findMethod(Calculator.class, "add", lvParam);
98  		assertTrue(lvMethod.getName().equals("add"));
99  		assertTrue(lvMethod.getParameterTypes().length == 2);
100 		Class lvClass0 = lvMethod.getParameterTypes()[0];
101 		Class lvClass1 = lvMethod.getParameterTypes()[0];
102 		assertTrue(lvClass0.getName().equals(int.class.getName()));		
103 		assertTrue(lvClass1.getName().equals(int.class.getName()));
104 	}
105 	
106 	public void testFindMethod2() throws Exception {
107 		String lvMethodName = "setName";
108 		Method lvMethods[] = Invoker.findAllMethods(Kunde.class, lvMethodName);
109 		assertEquals(lvMethods[0].getName(), lvMethodName);
110 	}
111 	
112 	public void testFindMethodIntegerOverloading() throws Exception {
113 		Vector lvParam = new Vector();
114 		lvParam.add(new Integer[]{ new Integer(1), new Integer(2), new Integer(3)});
115 		Method lvMethod = Invoker.findMethod(Echo.class, "echoArray", lvParam);
116 		assertTrue(lvMethod.getName().equals("echoArray"));
117 		assertTrue(lvMethod.getParameterTypes().length == 1);
118 		Class lvClass = lvMethod.getParameterTypes()[0];
119 		assertTrue(lvClass.isArray());
120 		assertEquals(lvClass.getComponentType(), Integer.class);
121 	}
122 
123 	public void testFindMethodLongOverloading() throws Exception {
124 		Vector lvParam = new Vector();
125 		lvParam.add(new Long[]{ new Long(1), new Long(2)});
126 		Method lvMethod = Invoker.findMethod(Echo.class, "echoArray", lvParam);
127 		assertTrue(lvMethod.getName().equals("echoArray"));
128 		assertTrue(lvMethod.getParameterTypes().length == 1);
129 		Class lvClass = lvMethod.getParameterTypes()[0];
130 		assertTrue(lvClass.isArray());
131 		assertEquals(lvClass.getComponentType(), Long.class);
132 	}
133 
134 	public void testFindMethodObjectOverloading() throws Exception {
135 		Vector lvParam = new Vector();
136 		lvParam.add(new Object[]{ "a", new Long(2)});
137 		Method lvMethod = Invoker.findMethod(Echo.class, "echoArray", lvParam);
138 		assertTrue(lvMethod.getName().equals("echoArray"));
139 		assertTrue(lvMethod.getParameterTypes().length == 1);
140 		Class lvClass = lvMethod.getParameterTypes()[0];
141 		assertTrue(lvClass.isArray());
142 		assertEquals(lvClass.getComponentType(), Object.class);
143 	}
144 	
145 	public void testArray2Vector() throws Exception {
146 		Integer lvInteger[] = new Integer[] {new Integer(5), new Integer(8)};
147 		Vector v = Invoker.array2Vector(lvInteger);
148 		assertTrue(v.size() == lvInteger.length);
149 		assertTrue(v.get(0).equals(lvInteger[0]));
150 	}
151 	
152 	public void testArray2SimpleVector() throws Exception {
153 		Integer lvInteger[] = new Integer[] {new Integer(5), new Integer(8)};
154 		Vector v = Invoker.array2SimpleVector(lvInteger);
155 		assertTrue(v.size() == lvInteger.length);
156 		assertTrue(v.get(0).equals(lvInteger[0]));		
157 	}
158 	
159 	public void testArray2SimpleArray() throws Exception {
160 		Integer lvInteger[] = new Integer[] {new Integer(5), new Integer(8)};
161 		Object o[] = Invoker.array2SimpleArray(lvInteger, new Converter());
162 		assertTrue(o.length == lvInteger.length);
163 		assertTrue(o[0].equals(lvInteger[0]));				
164 	}
165 	
166 	public void testArrayStringComponentType() throws Exception {
167 		Vector v = new Vector();
168 		v.add(new String[]{"a", "b"});
169 		Object o = Invoker.invoke(new EchoImpl(), "echoArray", v, true, new Converter());
170 		assertTrue(o instanceof Vector);
171 		Vector v2 = (Vector) o;
172 		assertTrue(v2.size() == 2);
173 		assertEquals(v2.get(0), "a");
174 		assertEquals(v2.get(1), "b");
175 	}
176 	
177 	public void testArrayLongComponentType() throws Exception {
178 		Vector v = new Vector();
179 		v.add(new Long[]{new Long(1), new Long(3)});
180 		Object o = Invoker.invoke(new EchoImpl(), "echoArray", v, true, new Converter());
181 		assertTrue(o instanceof Vector);
182 		Vector v2 = (Vector) o;
183 		assertTrue(v2.size() == 2);
184 		assertEquals(v2.get(0), new Integer(1));
185 		assertEquals(v2.get(1), new Integer(3));
186 	}
187 
188 	public void testArrayObjectComponentType() throws Exception {
189 		Vector v = new Vector();
190 		v.add(new Object[]{"a", new Long(3)});
191 		Object o = Invoker.invoke(new EchoImpl(), "echoArray", v, true, new Converter());
192 		assertTrue(o instanceof Vector);
193 		Vector v2 = (Vector) o;
194 		assertTrue(v2.size() == 2);
195 		assertEquals(v2.get(0), "a");
196 		assertEquals(v2.get(1), new Integer(3));
197 	}
198 
199 	public void testArray2SimpleVectorWithNullArray() throws Exception {
200 		Vector lvVector = Invoker.array2SimpleVector(null);
201 		assertNotNull(lvVector);
202 		assertEquals(lvVector.size(), 0);
203 	}
204 	
205 	public void testFindMethodNotFound() throws Exception {
206 		Method lvMethods[] = Invoker.findAllMethods(Echo.class, "add");
207 		assertEquals(0, lvMethods.length);
208 	}
209 	
210 	public void testFindMethodWithParamsNull() throws Exception {
211 		Method lvMethod = Invoker.findMethod(Echo.class, "ping", null);
212 		assertNotNull(lvMethod);
213 		assertEquals(lvMethod.getName(), "ping");
214 	}
215 
216 	public void testFindMethodWithParamIsNull() throws Exception {
217 		Vector lvParam = new Vector();
218 		lvParam.add(null);
219 		Method lvMethod = Invoker.findMethod(Echo.class, "echo", lvParam);
220 		assertTrue(lvMethod.getName().equals("echo"));
221 		assertTrue(lvMethod.getParameterTypes().length == 1);
222 		Class lvClass = lvMethod.getParameterTypes()[0];
223 		assertEquals(lvClass, String.class);
224 	}
225 	
226 	public void testFindMethodWithNoCorrectSizeOfParams() throws Exception {
227 		Vector lvParam = new Vector();
228 		lvParam.add("Echo-String");
229 		lvParam.add("NOT VALID PARAM");
230 		Method lvMethod = Invoker.findMethod(Echo.class, "echo", lvParam);
231 		assertTrue(lvMethod.getName().equals("echo"));
232 		assertTrue(lvMethod.getParameterTypes().length == 1);
233 		Class lvClass = lvMethod.getParameterTypes()[0];
234 		assertEquals(lvClass, String.class);
235 	}
236 
237 
238 	public void testFindAllMethods() throws Exception {
239 		int lvCountMethods = Invoker.findAllMethods(Echo.class, "echoArray").length;
240 		assertEquals(4, lvCountMethods);
241 		
242 		lvCountMethods = Invoker.findAllMethods(Echo.class, null).length;
243 		assertEquals(0, lvCountMethods);
244 		
245 		lvCountMethods = Invoker.findAllMethods(Echo.class, "").length;
246 		assertEquals(0, lvCountMethods);
247 
248 		lvCountMethods = Invoker.findAllMethods(Echo.class, "ping").length;
249 		assertEquals(1, lvCountMethods);
250 	}
251 	
252 	public void testVector2ArrayWithOneArgsToMuch() throws Exception {
253 		Method lvMethods[] = Invoker.findAllMethods(Echo.class, "echo");
254 		Vector lvParams = new Vector();
255 		lvParams.add("Echo1");
256 		lvParams.add("Echo2");
257 		Object[] lvObjects = Invoker.vector2Array(lvParams, lvMethods[0], new Converter());
258 		assertEquals(2, lvObjects.length);
259 		assertEquals("Echo1", lvObjects[0]);
260 		assertEquals("Echo2", lvObjects[1]);
261 	}
262 	
263 	public void testVector2ArrayWithOneArgsToMuchWithOutConverter() throws Exception {
264 		Method lvMethods[] = Invoker.findAllMethods(Echo.class, "echo");
265 		Vector lvParams = new Vector();
266 		lvParams.add("Echo1");
267 		lvParams.add("Echo2");
268 		// Converter is NULL
269 		Object[] lvObjects = Invoker.vector2Array(lvParams, lvMethods[0], null);
270 		assertEquals(2, lvObjects.length);
271 		assertEquals("Echo1", lvObjects[0]);
272 		assertEquals("Echo2", lvObjects[1]);
273 	}
274 	
275 	public void testVector2ArrayWithLongArray() throws Exception {
276 		Method lvMethods[] = Invoker.findAllMethods(Echo.class, "echoArray");
277 		Vector lvParams = new Vector();
278 		lvParams.add(new Long(3));
279 		lvParams.add(new Long(5));
280 		Object[] lvObjects = Invoker.vector2Array(lvParams, lvMethods[0], new Converter());
281 		assertEquals(2, lvObjects.length);
282 		assertEquals(new Long(3), lvObjects[0]);
283 		assertEquals(new Long(5), lvObjects[1]);		
284 	}
285 	
286 	public void testVector2ArrayWithIntegerArray() throws Exception {
287 		Method lvMethods[] = Invoker.findAllMethods(Echo.class, "echoArray");
288 		Vector lvParams = new Vector();
289 		lvParams.add(new Integer(3));
290 		lvParams.add(new Integer(5));
291 		Object[] lvObjects = Invoker.vector2Array(lvParams, lvMethods[0], new Converter());
292 		assertEquals(2, lvObjects.length);
293 		assertEquals(new Integer(3), lvObjects[0]);
294 		assertEquals(new Integer(5), lvObjects[1]);		
295 	}
296 
297 	public void testVector2ArrayWithCorrectParameterAndOverloading() throws Exception {
298 		Vector lvParamTypes = new Vector();
299 		lvParamTypes.add(Integer.class);
300 		lvParamTypes.add(Integer.class);
301 		Method lvMethod = Invoker.findMethod(Calculator.class, "add", lvParamTypes);
302 		Vector lvParams = new Vector();
303 		lvParams.add(new Integer(3));
304 		lvParams.add(new Integer(5));
305 		Object[] lvObjects = Invoker.vector2Array(lvParams, lvMethod, new Converter());
306 
307 		assertEquals(2, lvObjects.length);
308 		assertEquals(new Integer(3), lvObjects[0]);
309 		assertEquals(new Integer(5), lvObjects[1]);		
310 	}
311 
312 	public void testVector2ArrayWithToMuchParameterAndOverloading() throws Exception {
313 		Vector lvParamTypes = new Vector();
314 		lvParamTypes.add(Integer.class);
315 		lvParamTypes.add(Integer.class);
316 		Method lvMethod = Invoker.findMethod(Calculator.class, "add", lvParamTypes);
317 		Vector lvParams = new Vector();
318 		lvParams.add(new Integer(3));
319 		lvParams.add(new Integer(5));
320 		lvParams.add(new Integer(7));
321 		Object[] lvObjects = Invoker.vector2Array(lvParams, lvMethod, new Converter());
322 
323 		assertEquals(3, lvObjects.length);
324 		assertEquals(new Integer(3), lvObjects[0]);
325 		assertEquals(new Integer(5), lvObjects[1]);		
326 		assertEquals(new Integer(7), lvObjects[2]);
327 	}
328 	
329 	public void testVector2ArrayWithToMuchParameterAndOverloading2() throws Exception {
330 		Vector lvParamTypes = new Vector();
331 		lvParamTypes.add(Long.class);
332 		lvParamTypes.add(Long.class);
333 		Method lvMethod = Invoker.findMethod(Calculator.class, "add", lvParamTypes);
334 		Vector lvParams = new Vector();
335 		lvParams.add(new Long(3));
336 		lvParams.add(new Long(5));
337 		lvParams.add(new Long(7));
338 		Object[] lvObjects = Invoker.vector2Array(lvParams, lvMethod, new Converter());
339 
340 		assertEquals(3, lvObjects.length);
341 		assertEquals(new Long(3), lvObjects[0]);
342 		assertEquals(new Long(5), lvObjects[1]);		
343 		assertEquals(new Long(7), lvObjects[2]);
344 	}
345 
346 	public void testIllegalArgumentException() throws Exception {
347 		try {
348 			Invoker.invoke(new EchoImpl(), "echo", new Vector() , false, null);
349 			fail("Must thrown IllegalArgumentException, echo-method has one arg.");
350 		} catch (Exception e) {
351 			assertTrue(true);
352 		}
353 	}
354 }