forked from google/guice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCglibRemovalTest.java
236 lines (204 loc) · 6.35 KB
/
CglibRemovalTest.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
package com.google.inject;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import com.google.common.testing.NullPointerTester;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.inject.matcher.AbstractMatcher;
import com.google.inject.matcher.Matchers;
import com.google.inject.testing.NullpointerObject;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.function.Function;
import junit.framework.TestCase;
import org.junit.Test;
public class CglibRemovalTest extends TestCase {
interface Interface {
int method();
}
static class Impl implements Interface {
private final String str;
@Inject
Impl(String str) {
this.str = str;
}
@Override
public int method() {
return str.hashCode();
}
}
private static final String STRING = "foobar";
static class TestModule extends AbstractModule {
@Override
protected void configure() {
bind(String.class).toInstance(STRING);
bind(Interface.class).to(Impl.class);
}
}
static class InterceptorModule extends AbstractModule {
@Override
protected void configure() {
// Test passes if no interceptor is bound.
bindInterceptor(Matchers.any(), Matchers.returns(Matchers.subclassesOf(int.class)),
invocation -> invocation.proceed());
}
}
@Test
public void testUseInjectedClass() throws Throwable {
Interface instance = Guice.createInjector(new TestModule(), new InterceptorModule()).getInstance(Interface.class);
Class<? extends Interface> klass = instance.getClass();
MethodHandles.Lookup lookup = MethodHandles.lookup();
CallSite callsite = LambdaMetafactory.metafactory(
lookup,
"apply",
MethodType.methodType(Function.class),
MethodType.methodType(Object.class, Object.class),
lookup.findVirtual(klass, "method", MethodType.methodType(int.class)),
MethodType.methodType(int.class, klass)
);
Object res = ((Function)callsite.getTarget().invokeExact()).apply(instance);
assertEquals(STRING.hashCode(), res);
}
@Test
public void testMockitoSpy() throws Exception {
Interface instance = spy(Guice.createInjector(
new TestModule(),
new InterceptorModule()
).getInstance(Interface.class));
instance.method();
verify(instance).method();
}
@Test
public void testMethodMatcherError() throws Exception {
Module interceptorModule = new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(Matchers.subclassesOf(Interface.class), new AbstractMatcher<Method>() {
@Override
public boolean matches(Method method) {
if (method.getReturnType().isPrimitive()) {
throw new IllegalArgumentException("can't intercept method " + method.getName());
}
return true;
}
},
invocation -> invocation.proceed());
}
};
try {
Guice.createInjector(new TestModule(), interceptorModule);
fail("should fail");
} catch (UncheckedExecutionException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
}
}
private static class PrivateConstructor {
int method() {
return 1;
}
}
@Test
public void testPrivateConstructor() {
Injector injector = Guice.createInjector(new InterceptorModule());
try {
injector.getInstance(PrivateConstructor.class);
fail("should fail");
} catch (ConfigurationException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
}
}
static class ConstructorThrowsException {
@Inject static ConstructorThrowsException instance;
@Inject
ConstructorThrowsException() throws Exception {
throw new ExecutionError("failed", new IllegalAccessError("failed"));
}
}
@Test
public void testStaticInjection() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
requestStaticInjection(ConstructorThrowsException.class);
}
});
fail("expected to fail");
} catch (CreationException e) {
// expected
}
}
static class Foo {
@Inject
Foo() {
throw new AssertionError("fail!");
}
}
@Test
public void testAssertionErrorInConstructor() {
Injector injector = Guice.createInjector();
try {
injector.getInstance(Foo.class);
fail("expected to throw exception");
} catch (RuntimeException e) {
// expected.
}
}
@Test
public void testEnhancedMethodVisibility() {
NullpointerObject object = Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(NullpointerObject.Intercept.class),
invocation -> invocation.proceed());
}
}
).getInstance(NullpointerObject.class);
new NullPointerTester().testAllPublicInstanceMethods(object);
}
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface Measure {}
static abstract class Base<T> {
protected final T t;
protected Base(T t) {
this.t = t;
}
protected T getT() {return t;}
}
static abstract class Subclass<F> extends Base<String> {
protected Subclass(String t) {
super(t);
}
protected abstract void otherMethod(F f);
@Override
protected final String getT() {return t;}
}
@Measure
static class SubSubClass extends Subclass<String> {
@Inject
SubSubClass() {
super("");
}
@Override
protected final void otherMethod(String s) {}
}
@Test
public void testOverrideFinalMethod() {
Class<?> c = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(Matchers.annotatedWith(Measure.class), Matchers.any(), invocation -> invocation.proceed());
}
}).getInstance(SubSubClass.class)
.getClass();
assertNotNull(c.getCanonicalName());
}
}