1   package org.org.usurper.utils;
2   
3   import java.lang.reflect.Field;
4   import java.lang.reflect.InvocationTargetException;
5   import java.lang.reflect.Type;
6   import java.util.Arrays;
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import org.org.usurper.utils.ReflectionUtils;
11  
12  import junit.framework.TestCase;
13  
14  public class ReflectionUtilsTest extends TestCase {
15      private class FixtureVO {
16          private final Map<String, Integer> attr;
17  
18          private Map<String, Integer> attr2;
19  
20          public Map<String, Integer> getAttr2() {
21              return attr2;
22          }
23  
24          public void setAttr2(Map<String, Integer> attr2) {
25              this.attr2 = attr2;
26          }
27  
28          public FixtureVO(final Map<String, Integer> attr) {
29              this.attr = attr;
30          }
31  
32          public Map<String, Integer> getAttr() {
33              return attr;
34          }
35      }
36  
37      private class FixtureVO2 {
38          private String attr;
39  
40          private Integer attr2;
41  
42          private String[] attr3;
43  
44          public FixtureVO2() {
45          }
46  
47          public String getAttr() {
48              return attr;
49          }
50  
51          public void setAttr(String attr) {
52              this.attr = attr;
53          }
54  
55          public Integer getAttr2() {
56              return attr2;
57          }
58  
59          public void setAttr2(Integer attr2) {
60              this.attr2 = attr2;
61          }
62  
63          public String[] getAttr3() {
64              return attr3;
65          }
66  
67          public void setAttr3(String[] attr3) {
68              this.attr3 = attr3;
69          }
70  
71      }
72  
73      public void testGetGenericTypeOfConstructor() {
74          Type[] types = ReflectionUtils.getGenericTypes(FixtureVO.class.getConstructors()[0], 1);
75          assertEquals(types[0], String.class);
76          assertEquals(types[1], Integer.class);
77      }
78  
79      public void testGetGenericTypesField() {
80          FixtureVO fixtureVO = new FixtureVO(new HashMap<String, Integer>());
81          Field field = null;
82          try {
83              field = fixtureVO.getClass().getDeclaredField("attr2");
84          } catch (SecurityException e) {
85              fail("INCOMPLETE FIXTURE, Field needed.");
86          } catch (NoSuchFieldException e) {
87              fail("INCOMPLETE FIXTURE, Field needed.");
88          }
89          Type[] types = ReflectionUtils.getGenericTypes(field);
90          assertEquals(types[0], String.class);
91          assertEquals(types[1], Integer.class);
92      }
93  
94      public void testGetField() {
95          Field field = null;
96          try {
97              field = ReflectionUtils.getField(new FixtureVO2(), "attr");
98          } catch (SecurityException e) {
99              fail("SecurityException should not occur.");
100         } catch (NoSuchFieldException e) {
101             fail("NoSuchFieldException should not occur.");
102         }
103         assertEquals(field.getName(), "attr");
104     }
105 
106     public void testSetPropertyByName() {
107         FixtureVO2 fixtureVO2 = new FixtureVO2();
108         try {
109             ReflectionUtils.setProperty(fixtureVO2, "attr", "blabla");
110         } catch (SecurityException e) {
111             fail("SecurityException should not occur.");
112         } catch (IllegalArgumentException e) {
113             fail("IllegalArgumentException should not occur.");
114         } catch (NoSuchFieldException e) {
115             fail("NoSuchFieldException should not occur.");
116         } catch (IllegalAccessException e) {
117             fail("IllegalAccessException should not occur.");
118         }
119         assertEquals(fixtureVO2.getAttr(), "blabla");
120     }
121 
122     public void testSetArrayPropertyByName() {
123         FixtureVO2 fixtureVO2 = new FixtureVO2();
124         try {
125             ReflectionUtils.setProperty(fixtureVO2, "attr3", new String[] { "blabla", "blabla" });
126         } catch (SecurityException e) {
127             fail("SecurityException should not occur.");
128         } catch (IllegalArgumentException e) {
129             fail("IllegalArgumentException should not occur.");
130         } catch (NoSuchFieldException e) {
131             fail("NoSuchFieldException should not occur.");
132         } catch (IllegalAccessException e) {
133             fail("IllegalAccessException should not occur.");
134         }
135         assertTrue(Arrays.deepEquals(fixtureVO2.getAttr3(), new String[] { "blabla", "blabla" }));
136     }
137 
138     public void testSetPropertyBySetter() {
139         FixtureVO2 fixtureVO2 = new FixtureVO2();
140         try {
141             ReflectionUtils.setProperty(fixtureVO2, FixtureVO2.class.getMethod("setAttr", new Class[] { String.class }), "blabla");
142         } catch (IllegalArgumentException e) {
143             fail("IllegalArgumentException should not occur.");
144         } catch (SecurityException e) {
145             fail("SecurityException should not occur.");
146         } catch (IllegalAccessException e) {
147             fail("IllegalAccessException should not occur.");
148         } catch (InvocationTargetException e) {
149             fail("InvocationTargetException should not occur.");
150         } catch (NoSuchMethodException e) {
151             fail("NoSuchMethodException should not occur.");
152         }
153         assertEquals(fixtureVO2.getAttr(), "blabla");
154     }
155 
156     public void testSetArrayPropertyBySetter() {
157         FixtureVO2 fixtureVO2 = new FixtureVO2();
158         try {
159             ReflectionUtils.setProperty(fixtureVO2, FixtureVO2.class.getMethod("setAttr3", new Class[] { String[].class }), new String[] { "blabla", "blabla" });
160         } catch (IllegalArgumentException e) {
161             fail("IllegalArgumentException should not occur.");
162         } catch (SecurityException e) {
163             fail("SecurityException should not occur.");
164         } catch (IllegalAccessException e) {
165             fail("IllegalAccessException should not occur.");
166         } catch (InvocationTargetException e) {
167             fail("InvocationTargetException should not occur.");
168         } catch (NoSuchMethodException e) {
169             fail("NoSuchMethodException should not occur.");
170         }
171         assertTrue(Arrays.deepEquals(fixtureVO2.getAttr3(), new String[] { "blabla", "blabla" }));
172     }
173 }