/
ReflectionCacheTest.java
250 lines (181 loc) · 6.51 KB
/
ReflectionCacheTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package rocks.inspectit.agent.java.util;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.Matchers.theInstance;
import org.mockito.InjectMocks;
import org.testng.annotations.Test;
import rocks.inspectit.agent.java.AbstractLogSupport;
/**
* Tests for the {@link ReflectionCache} class.
*
* @author Patrice Bouillet
* @author Marius Oehler
*
*/
@SuppressWarnings("PMD")
public class ReflectionCacheTest extends AbstractLogSupport {
/**
* Class under test.
*/
@InjectMocks
ReflectionCache cache;
/**
* Tests for the
* {@link ReflectionCache#invokeMethod(Class, String, Class[], Object, Object[], Object)}
* method.
*
* @author Patrice Bouillet
*
*/
public static class InvokeMethod extends ReflectionCacheTest {
@Test
public void normalUsage() {
String testString = "I am a test";
String result = (String) cache.invokeMethod(String.class, "toString", null, testString, null, null);
assertThat(result, is(testString));
}
@Test
public void multipleInvocationOnSameObject() {
String testString = "I am a test";
cache.invokeMethod(String.class, "toString", null, testString, null, null);
cache.invokeMethod(String.class, "toString", null, testString, null, null);
String result = (String) cache.invokeMethod(String.class, "toString", null, testString, null, null);
assertThat(result, is(testString));
}
@Test
public void methodWithParameter() {
Object errorvalue = "errorvalue";
String input = "input";
String concat = "concat";
String result = (String) cache.invokeMethod(String.class, "concat", new Class[] { String.class }, input, new Object[] { concat }, errorvalue);
assertThat(result, is(input.concat(concat)));
}
@Test
public void methodDoesNotExist() {
Object errorvalue = "errorvalue";
Object result = cache.invokeMethod(String.class, "methodDoesNotExist", null, "test", null, errorvalue);
assertThat(result, is(errorvalue));
}
@Test
public void methodWithParametersDoesNotExist() {
Object errorvalue = "errorvalue";
Object result = cache.invokeMethod(String.class, "toString", new Class[] { String.class }, "test", new Object[] { "test" }, errorvalue);
assertThat(result, is(errorvalue));
}
@Test
public void nullMethod() {
Object errorvalue = "errorvalue";
Object result = cache.invokeMethod(String.class, null, null, "test", null, errorvalue);
assertThat(result, is(errorvalue));
}
@Test
public void nullClass() {
Object errorvalue = "errorvalue";
Object result = cache.invokeMethod(null, "abc", null, "test", null, errorvalue);
assertThat(result, is(errorvalue));
}
@Test
public void nullInstanceGiven() {
Object errorvalue = "errorvalue";
String result = (String) cache.invokeMethod(String.class, "toString", null, null, null, errorvalue);
assertThat(result, is(errorvalue));
}
@Test
public void throughInterface() {
Object object = "string";
Integer result = (Integer) cache.invokeMethod(String.class, "compareTo", new Class<?>[] { Object.class }, object, new Object[] { object }, -1, Comparable.class.getName());
assertThat(result, is(0));
}
@Test
public void throughInterfaceNotExisting() {
Object object = "string";
Integer result = (Integer) cache.invokeMethod(String.class, "compareTo", new Class<?>[] { Object.class }, object, new Object[] { object }, -1, "my.ComparableClass");
assertThat(result, is(0));
}
@Test
public void throughInterfaceMethodNotExisting() {
String object = "string";
String result = (String) cache.invokeMethod(String.class, "toString", null, object, null, "errorValue", Comparable.class.getName());
assertThat(result, is(object));
}
@Test
public void throughInterfaceMethodNotExistingAtAll() {
String object = "string";
String errorValue = "errorValue";
String result = (String) cache.invokeMethod(String.class, "someMethod", null, object, null, errorValue, Comparable.class.getName());
assertThat(result, is(errorValue));
}
}
/**
* Tests for the {@link ReflectionCache#getField(Class, String, Object)} method.
*
* @author Marius Oehler
*
*/
public static class GetField extends ReflectionCacheTest {
@Test
public void normalUsage() {
TestClass testInstance = new TestClass();
String field = (String) cache.getField(TestClass.class, "field", testInstance, null);
assertThat(field, is(equalTo("X")));
}
@Test
public void normalUsageJavaClass() {
Exception testCause = new Exception();
Exception testInstance = new Exception(testCause);
Exception cause = (Exception) cache.getField(testInstance.getClass(), "cause", testInstance, null);
assertThat(cause, is(sameInstance(testCause)));
}
@Test
public void getFieldInSuperClass() {
TestClass testInstance = new TestClass();
String field = (String) cache.getField(TestClass.class, "superField", testInstance, null);
assertThat(field, is(equalTo("Y")));
}
@Test
public void getFieldInSuperSuperClass() {
TestClass testInstance = new TestClass();
String field = (String) cache.getField(TestClass.class, "superSuperField", testInstance, null);
assertThat(field, is(equalTo("Z")));
}
@Test
public void getStaticField() {
String field = (String) cache.getField(TestClass.class, "staticField", null, null);
assertThat(field, is(equalTo("S")));
}
@Test
public void nullClass() {
Object errorvalue = "errorvalue";
Object result = cache.getField(null, "field", "instance", errorvalue);
assertThat(result, is(theInstance(errorvalue)));
}
@Test
public void nullField() {
Object errorvalue = "errorvalue";
Object result = cache.getField(TestClass.class, null, "instance", errorvalue);
assertThat(result, is(theInstance(errorvalue)));
}
@Test
public void noSuchField() {
TestClass testInstance = new TestClass();
Object errorValue = "errorValue";
Object field = cache.getField(TestClass.class, "notExisting", testInstance, errorValue);
assertThat(field, is(theInstance(errorValue)));
}
}
@SuppressWarnings("unused")
static abstract class SuperSuperTestClass {
private String superSuperField = "Z";
}
@SuppressWarnings("unused")
static abstract class SuperTestClass extends SuperSuperTestClass {
private String superField = "Y";
}
@SuppressWarnings("unused")
static class TestClass extends SuperTestClass {
private static String staticField = "S";
private String field = "X";
}
}