1   package com.lexicalscope.fluentreflection.endtoend;
2   
3   import static com.lexicalscope.fluentreflection.FluentReflection.object;
4   import static com.lexicalscope.fluentreflection.ReflectionMatchers.*;
5   import static org.hamcrest.MatcherAssert.assertThat;
6   import static org.hamcrest.Matchers.*;
7   
8   import org.hamcrest.Matchers;
9   import org.junit.Test;
10  
11  import com.lexicalscope.fluentreflection.FluentMethod;
12  import com.lexicalscope.fluentreflection.FluentObject;
13  
14  /*
15   * Copyright 2011 Tim Wood
16   *
17   * Licensed under the Apache License, Version 2.0 (the "License");
18   * you may not use this file except in compliance with the License.
19   * You may obtain a copy of the License at
20   *
21   * http://www.apache.org/licenses/LICENSE-2.0
22   *
23   * Unless required by applicable law or agreed to in writing, software
24   * distributed under the License is distributed on an "AS IS" BASIS,
25   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26   * See the License for the specific language governing permissions and
27   * limitations under the License.
28   */
29  
30  public class TestReflectedObject {
31      static class ExampleObject {
32          int method() {
33              return 42;
34          }
35  
36          int doubleIt(final int arg) {
37              return arg * 2;
38          }
39  
40          static void staticMethod() {
41  
42          }
43      }
44  
45      private final FluentObject<ExampleObject> reflectedInstance = object(new ExampleObject());
46  
47      @Test public void instanceMethodsCanBeCalled() throws Exception {
48          assertThat((Integer) reflectedInstance.method("method").callRaw(), equalTo(42));
49      }
50  
51      @Test public void instanceMethodsCanHaveReturnTypeBoundAndBeCalled() throws Exception {
52          assertThat(reflectedInstance.method("method").as(Integer.class).callRaw(), equalTo(42));
53      }
54  
55      @Test public void instanceMethodsWithArgumentsCanBeCalled() throws Exception {
56          assertThat((Integer) reflectedInstance.method("doubleIt").callRaw(42), equalTo(84));
57      }
58  
59      @Test public void instanceMethodArgumentsCountIsCorrect() throws Exception {
60          assertThat(reflectedInstance.method("doubleIt").argCount(), equalTo(1));
61      }
62  
63      @Test public void instanceMethodArgumentTypeIsCorrect() throws Exception {
64          assertThat(
65                  reflectedInstance.method("doubleIt").args(),
66                  contains(reflectingOn(int.class)));
67      }
68  
69      @Test public void instanceMethodDeclaringTypeIsCorrect() throws Exception {
70          assertThat(
71                  reflectedInstance.method("doubleIt").declarer(),
72                  reflectingOn(ExampleObject.class));
73      }
74  
75      @Test public void staticMethodsAreNotFound() throws Exception {
76          assertThat(reflectedInstance.methods(), not(Matchers.<FluentMethod>hasItem(isStatic())));
77      }
78  
79      @Test public void methodsAreFound() throws Exception {
80          assertThat(reflectedInstance.methods(), Matchers.<FluentMethod>hasItem(hasName("method")));
81          assertThat(reflectedInstance.methods(), Matchers.<FluentMethod>hasItem(hasName("doubleIt")));
82      }
83  
84      @Test public void classUnderReflectionIsCorrect() throws Exception {
85          assertThat(reflectedInstance.classUnderReflection(), equalTo(ExampleObject.class));
86          assertThat(reflectedInstance.reflectedClass().classUnderReflection(), equalTo(ExampleObject.class));
87      }
88  }