1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.gmavenplus.util;
18
19 import org.junit.Test;
20
21 import static org.junit.Assert.assertEquals;
22
23
24
25
26
27
28
29 public class ReflectionUtilsTest {
30
31 @Test
32 public void testHappyPaths() throws Exception {
33 String expectedString = "some string";
34 Object test1 = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(TestClass.class));
35 ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(TestClass.class, "setStringField", String.class), test1, expectedString);
36 assertEquals(expectedString, ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(TestClass.class, "getStringField"), test1));
37 assertEquals(TestClass.HELLO_WORLD, ReflectionUtils.invokeStaticMethod(ReflectionUtils.findMethod(TestClass.class, "helloWorld")));
38 assertEquals(TestClass.ENUM.VALUE, ReflectionUtils.getEnumValue(TestClass.ENUM.class, "VALUE"));
39 assertEquals(TestClass.HELLO_WORLD, ReflectionUtils.getStaticField(ReflectionUtils.findField(TestClass.class, "HELLO_WORLD", null)));
40 Object test2 = ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(TestClass.class, String.class), expectedString);
41 assertEquals(expectedString, ReflectionUtils.getField(ReflectionUtils.findField(TestClass.class, "stringField", String.class), test2));
42 }
43
44 @Test(expected = IllegalArgumentException.class)
45 public void testFindConstructorClassNull() {
46 ReflectionUtils.findConstructor(null);
47 }
48
49 @Test(expected = IllegalArgumentException.class)
50 public void testFindConstructorNotFound() {
51 ReflectionUtils.findConstructor(TestClass.class, TestClass.class);
52 }
53
54 @Test(expected = IllegalArgumentException.class)
55 public void testFindFieldClassNull() {
56 ReflectionUtils.findField(null, null, null);
57 }
58
59 @Test(expected = IllegalArgumentException.class)
60 public void testFindFieldNameAndTypeNull() {
61 ReflectionUtils.findField(TestClass.class, null, null);
62 }
63
64 @Test(expected = IllegalArgumentException.class)
65 public void testFindFieldNotFound() {
66 ReflectionUtils.findField(TestClass.class, "nonExistentField", null);
67 }
68
69 @Test(expected = IllegalArgumentException.class)
70 public void testFindMethodClassNull() {
71 ReflectionUtils.findMethod(null, null);
72 }
73
74 @Test(expected = IllegalArgumentException.class)
75 public void testFindMethodNameNull() {
76 ReflectionUtils.findMethod(TestClass.class, null);
77 }
78
79 @Test(expected = IllegalArgumentException.class)
80 public void testFindMethodNotFound() {
81 ReflectionUtils.findMethod(TestClass.class, "nonExistentMethod");
82 }
83
84 @Test(expected = IllegalArgumentException.class)
85 public void testGetEnumConstantNonEnumClass() {
86 ReflectionUtils.getEnumValue(TestClass.class, "VALUE");
87 }
88
89 @Test(expected = IllegalArgumentException.class)
90 public void testGetEnumConstantValueNotFound() {
91 ReflectionUtils.getEnumValue(TestClass.ENUM.class, "NON_EXISTENT_VALUE");
92 }
93
94 @Test(expected = IllegalArgumentException.class)
95 public void testGetStaticFieldNotStatic() throws Exception {
96 ReflectionUtils.getStaticField(ReflectionUtils.findField(TestClass.class, "stringField", String.class));
97 }
98
99 @Test(expected = IllegalArgumentException.class)
100 public void testInvokeConstructorNull() throws Exception {
101 ReflectionUtils.invokeConstructor(null);
102 }
103
104 @Test(expected = IllegalArgumentException.class)
105 public void testInvokeMethodMethodNull() throws Exception {
106 ReflectionUtils.invokeMethod(null, null);
107 }
108
109 @Test(expected = IllegalArgumentException.class)
110 public void testInvokeMethodObjectNull() throws Exception {
111 ReflectionUtils.invokeMethod(TestClass.class.getMethod("getStringField"), null);
112 }
113
114 @Test(expected = IllegalArgumentException.class)
115 public void testInvokeStaticMethodMethodNull() throws Exception {
116 ReflectionUtils.invokeStaticMethod(null);
117 }
118
119 @Test(expected = IllegalArgumentException.class)
120 public void testInvokeStaticMethodMethodNotStatic() throws Exception {
121 ReflectionUtils.invokeStaticMethod(TestClass.class.getMethod("getStringField"));
122 }
123
124 @Test
125 public void testConstructor() throws Exception {
126 ReflectionUtils.invokeConstructor(ReflectionUtils.findConstructor(ReflectionUtils.class));
127 }
128
129 public static class TestClass {
130 public static final String HELLO_WORLD = "Hello world!";
131 public String stringField;
132
133 public TestClass() {
134 }
135
136 public TestClass(String newStringField) {
137 stringField = newStringField;
138 }
139
140 public String getStringField() {
141 return stringField;
142 }
143
144 public void setStringField(String newStringField) {
145 stringField = newStringField;
146 }
147
148 public static String helloWorld() {
149 return HELLO_WORLD;
150 }
151
152 protected enum ENUM {
153 VALUE
154 }
155 }
156
157 }