1   package com.lexicalscope.fluentreflection;
2   
3   import static com.lexicalscope.fluentreflection.FluentReflection.method;
4   import static com.lexicalscope.fluentreflection.ReflectionMatchers.reflectingOn;
5   import static org.hamcrest.MatcherAssert.assertThat;
6   import static org.hamcrest.Matchers.*;
7   
8   import org.junit.Rule;
9   import org.junit.Test;
10  import org.junit.rules.ExpectedException;
11  
12  /*
13   * Copyright 2011 Tim Wood
14   *
15   * Licensed under the Apache License, Version 2.0 (the "License");
16   * you may not use this file except in compliance with the License.
17   * You may obtain a copy of the License at
18   *
19   * http://www.apache.org/licenses/LICENSE-2.0
20   *
21   * Unless required by applicable law or agreed to in writing, software
22   * distributed under the License is distributed on an "AS IS" BASIS,
23   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24   * See the License for the specific language governing permissions and
25   * limitations under the License.
26   */
27  
28  public class TestBoundReflectedMethodImpl {
29      @Rule
30      public ExpectedException exception = ExpectedException.none();
31  
32      static class ClassWithStaticMethod {
33          static void method() {
34          }
35      }
36  
37      static class ClassWithMethod {
38          Integer method() {
39              return 42;
40          }
41  
42          @Override
43          public String toString() {
44              return "my toString";
45          }
46      }
47  
48      private final BoundFluentMethodImpl methodWithReturnType;
49  
50      public TestBoundReflectedMethodImpl() throws SecurityException, NoSuchMethodException {
51          methodWithReturnType = new BoundFluentMethodImpl(
52                  null,
53                  method(ClassWithMethod.class.getDeclaredMethod("method")),
54                  new ClassWithMethod());
55      }
56  
57      @Test
58      public void bindingStaticMethodIsNotAllowed() throws Exception {
59          exception.expect(IllegalArgumentException.class);
60  
61          new BoundFluentMethodImpl(
62                  null,
63                  method(ClassWithStaticMethod.class.getDeclaredMethods()[0]),
64                  new ClassWithStaticMethod());
65      }
66  
67      @Test
68      public void boundMethodToStringIsAllowed() throws Exception {
69          assertThat(
70                  methodWithReturnType,
71                  hasToString(containsString("method() in my toString")));
72      }
73  
74      @Test
75      public void boundMethodReturnTypeIsAvailable() throws Exception {
76          assertThat(
77                  methodWithReturnType.type(),
78                  reflectingOn(Integer.class));
79      }
80  }