1   package com.lexicalscope.fluentreflection.endtoend;
2   
3   import static com.lexicalscope.fluentreflection.FluentReflection.type;
4   import static org.hamcrest.MatcherAssert.assertThat;
5   import static org.hamcrest.Matchers.hasToString;
6   
7   import java.util.List;
8   
9   import org.junit.Test;
10  
11  import com.lexicalscope.fluentreflection.TypeToken;
12  
13  /*
14   * Copyright 2011 Tim Wood
15   *
16   * Licensed under the Apache License, Version 2.0 (the "License");
17   * you may not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   *
20   * http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an "AS IS" BASIS,
24   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License. 
27   */
28  
29  public class TestReflectedMethodToString {
30      static class ClassWithMethods<T> {
31          void myMethod() {};
32          public void myPublicMethod() {};
33          @SuppressWarnings("unused") private void myPrivateMethod() {};
34          protected void myProtectedMethod() {};
35          final void myFinalMethod() {};
36          static void myStaticMethod() {};
37          List<String> myMethodWithGenericReturnType() {
38              return null;
39          };
40          List<T> myMethodWithParameterizedGenericReturnType() {
41              return null;
42          };
43          <S> List<S> myGenericMethodWithParameterizedGenericReturnType() {
44              return null;
45          };
46          void myMethodWithOneArgument(final String argument) {};
47          void myMethodWithTwoArguments(final String argument0, final String argument1) {};
48          void myMethodWithGenericArgument(final List<String> argument) {};
49          void myMethodWithParameterizedGenericArgument(final List<T> argument) {};
50          <S> void myGenericMethodWithParameterizedGenericArgument(final List<S> argument) {};
51      }
52  
53      @Test public void voidMethodWithNoArguments() {
54          assertThat(type(ClassWithMethods.class).method("myMethod"), hasToString("void myMethod()"));
55      }
56  
57      @Test public void publicMethod() {
58          assertThat(type(ClassWithMethods.class).method("myPublicMethod"), hasToString("public void myPublicMethod()"));
59      }
60  
61      @Test public void privateMethod() {
62          assertThat(
63                  type(ClassWithMethods.class).method("myPrivateMethod"),
64                  hasToString("private void myPrivateMethod()"));
65      }
66  
67      @Test public void protectedMethod() {
68          assertThat(
69                  type(ClassWithMethods.class).method("myProtectedMethod"),
70                  hasToString("protected void myProtectedMethod()"));
71      }
72  
73      @Test public void finalMethod() {
74          assertThat(
75                  type(ClassWithMethods.class).method("myFinalMethod"),
76                  hasToString("final void myFinalMethod()"));
77      }
78  
79      @Test public void staticMethod() {
80          assertThat(
81                  type(ClassWithMethods.class).method("myStaticMethod"),
82                  hasToString("static void myStaticMethod()"));
83      }
84  
85      @Test public void methodWithGenericReturnType() {
86          assertThat(
87                  type(ClassWithMethods.class).method("myMethodWithGenericReturnType"),
88                  hasToString("java.util.List<java.lang.String> myMethodWithGenericReturnType()"));
89      }
90  
91      @Test public void methodWithBoundParameterizedGenericReturnType() {
92          assertThat(
93                  type(new TypeToken<ClassWithMethods<Integer>>() {})
94                          .method("myMethodWithParameterizedGenericReturnType"),
95                  hasToString("java.util.List<java.lang.Integer> myMethodWithParameterizedGenericReturnType()"));
96      }
97  
98      @Test public void methodWithParameterizedGenericReturnType() {
99          assertThat(
100                 type(ClassWithMethods.class)
101                         .method("myMethodWithParameterizedGenericReturnType"),
102                 hasToString("java.util.List<T> myMethodWithParameterizedGenericReturnType()"));
103     }
104 
105     @Test public void parameterizedGenericMethodWithGenericReturnType() {
106         assertThat(
107                 type(ClassWithMethods.class)
108                         .method("myGenericMethodWithParameterizedGenericReturnType"),
109                 hasToString("<S> java.util.List<S> myGenericMethodWithParameterizedGenericReturnType()"));
110     }
111 
112     @Test public void methodWithOneArgument() {
113         assertThat(
114                 type(ClassWithMethods.class)
115                         .method("myMethodWithOneArgument"),
116                 hasToString("void myMethodWithOneArgument(java.lang.String)"));
117     }
118 
119     @Test public void methodWithTwoArguments() {
120         assertThat(
121                 type(ClassWithMethods.class)
122                         .method("myMethodWithTwoArguments"),
123                 hasToString("void myMethodWithTwoArguments(java.lang.String, java.lang.String)"));
124     }
125 
126     @Test public void methodWithGenericArgument() {
127         assertThat(
128                 type(ClassWithMethods.class)
129                         .method("myMethodWithGenericArgument"),
130                 hasToString("void myMethodWithGenericArgument(java.util.List<java.lang.String>)"));
131     }
132 
133     @Test public void methodWithBoundParameterizedGenericArgument() {
134         assertThat(
135                 type(new TypeToken<ClassWithMethods<Integer>>() {})
136                         .method("myMethodWithParameterizedGenericArgument"),
137                 hasToString("void myMethodWithParameterizedGenericArgument(java.util.List<java.lang.Integer>)"));
138     }
139 
140     @Test public void methodWithParameterizedGenericReturnArgument() {
141         assertThat(
142                 type(ClassWithMethods.class)
143                         .method("myMethodWithParameterizedGenericArgument"),
144                 hasToString("void myMethodWithParameterizedGenericArgument(java.util.List<T>)"));
145     }
146 
147     @Test public void genericMethodWithParameterizedGenericReturnArgument() {
148         assertThat(
149                 type(new TypeToken<ClassWithMethods<Integer>>() {})
150                         .method("myGenericMethodWithParameterizedGenericArgument"),
151                 hasToString("<S> void myGenericMethodWithParameterizedGenericArgument(java.util.List<S>)"));
152     }
153 }