1   package com.lexicalscope.fluentreflection.usecases;
2   
3   import static com.lexicalscope.fluentreflection.FluentReflection.object;
4   import static com.lexicalscope.fluentreflection.ReflectionMatchers.*;
5   import static org.hamcrest.Matchers.containsInAnyOrder;
6   import static org.junit.Assert.assertThat;
7   
8   import org.hamcrest.Matcher;
9   import org.junit.Test;
10  
11  import com.lexicalscope.fluentreflection.ListBuilder;
12  import com.lexicalscope.fluentreflection.FluentMember;
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 TestFindAllBeanMethods {
31      static class Bean {
32          private int readWriteProperty;
33          private String readOnlyProperty;
34  
35          String getReadOnlyProperty() {
36              return readOnlyProperty;
37          }
38  
39          int getReadWriteProperty() {
40              return readWriteProperty;
41          }
42  
43          void setReadWriteProperty(final int readWriteProperty) {
44              this.readWriteProperty = readWriteProperty;
45          }
46  
47          void setWriteOnlyProperty(final Object writeOnlyProperty) {
48          // do something with it
49          }
50  
51          int getMethodWithArgument(final int outOfPlace) {
52              return outOfPlace;
53          }
54  
55          void getMethodReturningVoid() {}
56      }
57  
58      // TODO[tim]: test "is" prefix
59  
60      @Test public void canSelectAllGetters() throws Exception {
61          assertThat(
62                  object(new Bean()).methods(isGetter()),
63                  containsInAnyOrder(
64                          ListBuilder.<Matcher<? super FluentMember>>
65                                  list(hasName("getReadOnlyProperty")).add(
66                                          hasName("getReadWriteProperty")).$()));
67      }
68  
69      @Test public void canSelectAllSetters() throws Exception {
70          assertThat(
71                  object(new Bean()).methods(
72                          isSetter()),
73                  containsInAnyOrder(
74                          ListBuilder.<Matcher<? super FluentMember>>
75                                  list(hasName("setWriteOnlyProperty")).add(
76                                          hasName("setReadWriteProperty")).$()));
77      }
78  }