1 package com.lexicalscope.fluentreflection.endtoend;
2
3 import static com.lexicalscope.fluentreflection.FluentReflection.*;
4 import static com.lexicalscope.fluentreflection.ReflectionMatchers.*;
5 import static java.lang.annotation.RetentionPolicy.RUNTIME;
6 import static org.hamcrest.MatcherAssert.assertThat;
7 import static org.hamcrest.Matchers.equalTo;
8
9 import java.lang.annotation.Retention;
10
11 import org.hamcrest.Matchers;
12 import org.junit.Test;
13
14 import com.lexicalscope.fluentreflection.FluentMethod;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class TestReflectionOnAnnotatedMethods {
33 @Retention(RUNTIME) @interface MyAnnotation {
34 String value();
35 }
36
37 class ClassWithAnnotatedMethod {
38 @MyAnnotation(value = "my value") void myAnnotatedMethod() {};
39 void myNonAnnotatedMethod() {};
40 }
41
42 private final FluentMethod annotatedMethod = type(ClassWithAnnotatedMethod.class).method(
43 hasName("myAnnotatedMethod"));
44
45 private final FluentMethod boundAnnotatedMethod = object(new ClassWithAnnotatedMethod()).method(
46 hasName("myAnnotatedMethod"));
47
48 private final FluentMethod nonAnnotatedMethod = type(ClassWithAnnotatedMethod.class).method(
49 hasName("myNonAnnotatedMethod"));
50
51 private final FluentMethod boundNonAnnotatedMethod = type(ClassWithAnnotatedMethod.class).method(
52 hasName("myNonAnnotatedMethod"));
53
54 @Test public void canReadAnnotationOnMethod() throws Exception {
55 assertThat(annotatedMethod.annotation(MyAnnotation.class).value(), equalTo("my value"));
56 assertThat(boundAnnotatedMethod.annotation(MyAnnotation.class).value(), equalTo("my value"));
57 }
58
59 @Test public void whenMethodHasAnnotationAnnotatedWithReturnsTrue() {
60 assertThat(annotatedMethod.annotatedWith(MyAnnotation.class), equalTo(true));
61 assertThat(boundAnnotatedMethod.annotatedWith(MyAnnotation.class), equalTo(true));
62 }
63
64 @Test public void whenMethodDoesNotHaveAnnotationAnnotatedWithReturnsFalse() {
65 assertThat(nonAnnotatedMethod.annotatedWith(MyAnnotation.class), equalTo(false));
66 assertThat(boundNonAnnotatedMethod.annotatedWith(MyAnnotation.class), equalTo(false));
67 }
68
69 @Test public void matchingAnnotationIsReflectedOn() {
70 assertThat(annotatedMethod.annotation(Matchers.anything()), hasSimpleName("MyAnnotation"));
71 assertThat(boundAnnotatedMethod.annotation(Matchers.anything()), hasSimpleName("MyAnnotation"));
72 }
73 }