/
AnnotationDescriptionForLoadedAnnotationTest.java
76 lines (58 loc) · 2.59 KB
/
AnnotationDescriptionForLoadedAnnotationTest.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
package net.bytebuddy.description.annotation;
import net.bytebuddy.description.method.MethodDescription;
import org.junit.Ignore;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class AnnotationDescriptionForLoadedAnnotationTest extends AbstractAnnotationDescriptionTest {
private static final String FOO = "foo";
@Override
protected AnnotationDescription describe(Annotation annotation, Class<?> declaringType) {
return AnnotationDescription.ForLoadedAnnotation.of(annotation);
}
@Test
public void testAnnotationNonVisible() throws Exception {
assertThat(describe(Carrier.class.getAnnotation(PrivateAnnotation.class), Carrier.class)
.getValue(new MethodDescription.ForLoadedMethod(PrivateAnnotation.class.getDeclaredMethod("value")),
String.class), is(FOO));
}
@Test
public void testAnnotationNonVisibleAccessible() throws Exception {
Method method = PrivateAnnotation.class.getDeclaredMethod("value");
method.setAccessible(true);
assertThat(describe(Carrier.class.getAnnotation(PrivateAnnotation.class), Carrier.class)
.getValue(new MethodDescription.ForLoadedMethod(method), String.class), is(FOO));
}
@Test(expected = IllegalStateException.class)
public void testInoperational() throws Exception {
describe(PrivateAnnotation.Defect.INSTANCE, Carrier.class)
.getValue(new MethodDescription.ForLoadedMethod(PrivateAnnotation.class.getDeclaredMethod("value")));
}
@Test(expected = IllegalArgumentException.class)
@Ignore("Rewrite test to consider different class loader.")
public void testLoadAnnotationWrongClassLoader() throws Exception {
describe(Carrier.class.getAnnotation(PrivateAnnotation.class), Carrier.class).prepare(PrivateAnnotation.class).load();
}
@Retention(RetentionPolicy.RUNTIME)
private @interface PrivateAnnotation {
String value();
enum Defect implements PrivateAnnotation {
INSTANCE;
@Override
public String value() {
throw new RuntimeException();
}
@Override
public Class<? extends Annotation> annotationType() {
return PrivateAnnotation.class;
}
}
}
@PrivateAnnotation(FOO)
private static class Carrier {
}
}