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   * Copyright 2011 Tim Wood
18   *
19   * Licensed under the Apache License, Version 2.0 (the "License");
20   * you may not use this file except in compliance with the License.
21   * You may obtain a copy of the License at
22   *
23   * http://www.apache.org/licenses/LICENSE-2.0
24   *
25   * Unless required by applicable law or agreed to in writing, software
26   * distributed under the License is distributed on an "AS IS" BASIS,
27   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   * See the License for the specific language governing permissions and
29   * limitations under the License. 
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  }