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