001: package org.vraptor.reflection;
002:
003: import java.lang.annotation.Annotation;
004: import java.lang.reflect.Field;
005: import java.lang.reflect.Method;
006: import java.util.List;
007: import java.util.Map;
008:
009: import junit.framework.TestCase;
010:
011: import org.vraptor.VRaptorException;
012: import org.vraptor.VRaptorServlet;
013: import org.vraptor.annotations.In;
014: import org.vraptor.annotations.Out;
015: import org.vraptor.annotations.Read;
016: import org.vraptor.component.ComponentInstantiationException;
017: import org.vraptor.component.FieldOutjecter;
018: import org.vraptor.component.GetterOutjecter;
019: import org.vraptor.component.Outjecter;
020:
021: /**
022: * Tests the reflection util class.
023: *
024: * @author Guilherme Silveira
025: */
026: public class ReflectionUtilTest extends TestCase {
027:
028: public void testCompleteInstantiate()
029: throws ComponentInstantiationException {
030: ReflectionUtil.instantiate(VRaptorServlet.class);
031: }
032:
033: @SuppressWarnings("unchecked")
034: public void testInstantiateNullPointer()
035: throws ComponentInstantiationException {
036: try {
037: ReflectionUtil.instantiate((Class) null);
038: fail();
039: } catch (Exception ex) {
040: // ok
041: }
042: }
043:
044: public void instantiateArithmeticException() {
045: try {
046: ReflectionUtil
047: .instantiate(WrongReflectionUtilTestClass.class);
048: fail();
049: } catch (ComponentInstantiationException e) {
050: // ok
051: }
052: }
053:
054: public static class WrongReflectionUtilTestClass {
055: public WrongReflectionUtilTestClass() throws VRaptorException {
056: throw new VRaptorException("test exception");
057: }
058: }
059:
060: public void testMethodInvocation()
061: throws MethodInvocationException, SecurityException,
062: NoSuchMethodException {
063: ReflectionUtilTestClass obj = new ReflectionUtilTestClass();
064: ReflectionUtil.invoke(obj, obj.getClass().getMethod(
065: "simpleMethod"));
066: }
067:
068: public void testWrongMethodInvocation() throws SecurityException,
069: NoSuchMethodException {
070: ReflectionUtilTestClass obj = new ReflectionUtilTestClass();
071: try {
072: ReflectionUtil.invoke(obj, obj.getClass().getMethod(
073: "simpleWrongMethod"));
074: fail();
075: } catch (MethodInvocationException e) {
076: // ok
077: }
078: }
079:
080: public void testWrongArgumentMethodInvocation()
081: throws SecurityException, NoSuchMethodException {
082: ReflectionUtilTestClass obj = new ReflectionUtilTestClass();
083: try {
084: ReflectionUtil.invoke(obj, obj.getClass().getMethod(
085: "argumentMethod", int.class));
086: fail();
087: } catch (MethodInvocationException e) {
088: // ok
089: }
090: }
091:
092: public void testArgumentMethodInvocation()
093: throws MethodInvocationException, SecurityException,
094: NoSuchMethodException {
095: ReflectionUtilTestClass obj = new ReflectionUtilTestClass();
096: ReflectionUtil.invoke(obj, obj.getClass().getMethod(
097: "argumentMethod", int.class), 1);
098: }
099:
100: public void testSettingIllegalAccess() throws SettingException,
101: SecurityException, NoSuchFieldException {
102: try {
103: ReflectionUtilTestClass obj = new ReflectionUtilTestClass();
104: Field f = obj.getClass().getDeclaredField("internal");
105: ReflectionUtil.setField(obj, f, 1);
106: assert false : "Illegal Access.";
107: } catch (SettingException e) {
108: // ok
109: }
110: }
111:
112: public void testSettingInvalidType() throws SettingException,
113: SecurityException, NoSuchFieldException {
114: try {
115: ReflectionUtilTestClass obj = new ReflectionUtilTestClass();
116: Field f = obj.getClass().getDeclaredField("internal");
117: boolean accessible = f.isAccessible();
118: f.setAccessible(true);
119: ReflectionUtil.setField(obj, f, "1");
120: f.setAccessible(accessible);
121: fail("Its an invalid type, should throw an exception.");
122: } catch (SettingException e) {
123: // ok
124: }
125: }
126:
127: public void testSetting() throws SettingException,
128: SecurityException, NoSuchFieldException {
129: ReflectionUtilTestClass obj = new ReflectionUtilTestClass();
130: Field f = obj.getClass().getDeclaredField("internal");
131: boolean accessible = f.isAccessible();
132: f.setAccessible(true);
133: ReflectionUtil.setField(obj, f, 1);
134: f.setAccessible(accessible);
135: }
136:
137: public void testFindSetter() throws SecurityException,
138: NoSuchMethodException {
139: Method setter = ReflectionUtil.findSetter(
140: new ReflectionUtilTestClass(), "value");
141: assertEquals(ReflectionUtilTestClass.class.getMethod(
142: "setValue", String.class), setter);
143: }
144:
145: @SuppressWarnings("unused")
146: @Read
147: @Out
148: @In
149: private String annotatedField;
150:
151: public void testCannotFindAnnotation() {
152: Annotation[] annotations = new Annotation[] {};
153: assertNull(ReflectionUtil.findAnnotation(annotations,
154: Read.class));
155: }
156:
157: public void testChecksIfGetIsAGetter() throws SecurityException,
158: NoSuchMethodException {
159: Method get = ReflectionUtilTestClass.class.getMethod("get",
160: new Class[0]);
161: assertFalse(ReflectionUtil.isGetter(get));
162: }
163:
164: public void testChecksIfIsIsAGetter() throws SecurityException,
165: NoSuchMethodException {
166: Method is = ReflectionUtilTestClass.class.getMethod("is",
167: new Class[0]);
168: assertFalse(ReflectionUtil.isGetter(is));
169: }
170:
171: public void testChecksIfANonReturnMethodIsAGetter()
172: throws SecurityException, NoSuchMethodException {
173: Method getVoidProperty = ReflectionUtilTestClass.class
174: .getMethod("getVoidProperty", new Class[0]);
175: assertFalse(ReflectionUtil.isGetter(getVoidProperty));
176: }
177:
178: public void testChecksIfAMethodWhichReceivesAParameterIsAGetter()
179: throws SecurityException, NoSuchMethodException {
180: Method getBizarre = ReflectionUtilTestClass.class.getMethod(
181: "getBizarre", new Class[] { Integer.TYPE });
182: assertFalse(ReflectionUtil.isGetter(getBizarre));
183: }
184:
185: public void testChecksIfAMethodNotStartingWithGetIsAGetter()
186: throws SecurityException, NoSuchMethodException {
187: Method bizarreGetter3 = ReflectionUtilTestClass.class
188: .getMethod("bizarreGetter3", new Class[0]);
189: assertFalse(ReflectionUtil.isGetter(bizarreGetter3));
190: }
191:
192: public void testChecksIfAnIsMethodReturningStringIsAGetter()
193: throws SecurityException, NoSuchMethodException {
194: Method isBizarre = ReflectionUtilTestClass.class.getMethod(
195: "isBizarre", new Class[0]);
196: assertFalse(ReflectionUtil.isGetter(isBizarre));
197: }
198:
199: public void testChecksForAValidGetter() throws SecurityException,
200: NoSuchMethodException {
201: Method getInternal = ReflectionUtilTestClass.class.getMethod(
202: "getInternal", new Class[0]);
203: assertTrue(ReflectionUtil.isGetter(getInternal));
204: }
205:
206: public void testChecksForAValidIs() throws SecurityException,
207: NoSuchMethodException {
208: Method isClosed = ReflectionUtilTestClass.class.getMethod(
209: "isClosed", new Class[0]);
210: assertTrue(ReflectionUtil.isGetter(isClosed));
211: }
212:
213: public void testChecksForAStaticMethodGetter()
214: throws SecurityException, NoSuchMethodException {
215: Method getStatic = ReflectionUtilTestClass.class.getMethod(
216: "getStatic", new Class[0]);
217: assertFalse(ReflectionUtil.isGetter(getStatic));
218: }
219:
220: static class NonPublicClass {
221: }
222:
223: public void testGetGettersWithNonPublicClass() {
224: try {
225: ReflectionUtil.getGetters(NonPublicClass.class);
226: fail();
227: } catch (IllegalArgumentException e) {
228: // ok
229: }
230: }
231:
232: public void testGetGettersIgnoresGetClass() {
233: Map<String, Method> x = ReflectionUtil
234: .getGetters(ReflectionUtilTestClass.class);
235: assertFalse(x.containsKey("class"));
236: }
237:
238: public void testGetGettersIgnoresGettersAndIsersWithoutAName() {
239: Map<String, Method> x = ReflectionUtil
240: .getGetters(ReflectionUtilTestClass.class);
241: assertFalse(x.containsKey(""));
242: }
243:
244: public void testGetGettersIgnoresGettersReturningVoid() {
245: Map<String, Method> x = ReflectionUtil
246: .getGetters(ReflectionUtilTestClass.class);
247: assertFalse(x.containsKey("voidProperty"));
248: }
249:
250: public void testGetGettersFindsIs() {
251: Map<String, Method> x = ReflectionUtil
252: .getGetters(ReflectionUtilTestClass.class);
253: assertTrue(x.containsKey("closed"));
254: }
255:
256: public void testGetGettersForCapsPROPERTIES() {
257: Map<String, Method> x = ReflectionUtil
258: .getGetters(ReflectionUtilTestClass.class);
259: assertTrue(x.containsKey("URLocationFoo"));
260: }
261:
262: public void testGetGettersForFieldWithLiength1() {
263: Map<String, Method> x = ReflectionUtil
264: .getGetters(ReflectionUtilTestClass.class);
265: assertTrue(x.containsKey("a"));
266: }
267:
268: public static class ReflectionUtilTestClass {
269:
270: @SuppressWarnings("unused")
271: private int internal;
272:
273: private boolean closed;
274:
275: public int getA() {
276: return 0;
277: }
278:
279: public void getVoidProperty() {
280: }
281:
282: public void simpleMethod() {
283: }
284:
285: public String getURLocationFoo() {
286: return "";
287: }
288:
289: public String is() {
290: return null;
291: }
292:
293: public void simpleWrongMethod() {
294: @SuppressWarnings("unused")
295: int i = 1 / 0;
296: }
297:
298: public void argumentMethod(int i) {
299: }
300:
301: public String isBizarre() {
302: return null;
303: }
304:
305: @SuppressWarnings("unused")
306: private String value;
307:
308: public void setValue(String value) {
309: this .value = value;
310: }
311:
312: public static int getStatic() {
313: return 0;
314: }
315:
316: protected int getProtected() {
317: return 0;
318: }
319:
320: public int getInternal() {
321: return internal;
322: }
323:
324: public boolean isClosed() {
325: return closed;
326: }
327:
328: public void bizarreGetter1() {
329: }
330:
331: public int bizarreGetter2(int x) {
332: return x;
333: }
334:
335: public int bizarreGetter3() {
336: return 0;
337: }
338:
339: public int getBizarre(int x) {
340: return x;
341: }
342:
343: public void get() {
344:
345: }
346:
347: }
348:
349: public static class AnnotatedField {
350: @Out
351: @SuppressWarnings("unused")
352: private String field;
353: }
354:
355: public void testLoadsAnnotatedFieldOutjecters() {
356: List<Outjecter> outjecters = ReflectionUtil
357: .loadOutjecters(AnnotatedField.class);
358: assertEquals(1, outjecters.size());
359: assertEquals(FieldOutjecter.class, outjecters.get(0).getClass());
360: }
361:
362: public static class AnnotatedGetter {
363: @Out
364: public String getX() {
365: return "";
366: }
367: }
368:
369: public void testLoadsAnnotatedGetterOutjecters() {
370: List<Outjecter> outjecters = ReflectionUtil
371: .loadOutjecters(AnnotatedGetter.class);
372: assertEquals(1, outjecters.size());
373: assertEquals(GetterOutjecter.class, outjecters.get(0)
374: .getClass());
375: }
376:
377: public static class NotAnnotatedGetter {
378: public String getX() {
379: return "";
380: }
381: }
382:
383: public void testLoadsNotAnnotatedGetterOutjecters() {
384: List<Outjecter> outjecters = ReflectionUtil
385: .loadOutjecters(NotAnnotatedGetter.class);
386: assertEquals(1, outjecters.size());
387: assertEquals(GetterOutjecter.class, outjecters.get(0)
388: .getClass());
389: }
390:
391: }
|