1   package com.lexicalscope.fluentreflection.usecases;
2   
3   import static com.lexicalscope.fluentreflection.FluentReflection.object;
4   import static com.lexicalscope.fluentreflection.ReflectionMatchers.hasName;
5   import static org.hamcrest.MatcherAssert.assertThat;
6   import static org.hamcrest.Matchers.equalTo;
7   
8   import org.junit.Test;
9   
10  /*
11   * Copyright 2012 Tim Wood
12   *
13   * Licensed under the Apache License, Version 2.0 (the "License");
14   * you may not use this file except in compliance with the License.
15   * You may obtain a copy of the License at
16   *
17   * http://www.apache.org/licenses/LICENSE-2.0
18   *
19   * Unless required by applicable law or agreed to in writing, software
20   * distributed under the License is distributed on an "AS IS" BASIS,
21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   * See the License for the specific language governing permissions and
23   * limitations under the License.
24   */
25  
26  public class TestCanReadAndSetFields {
27      public static class AnnotatedFields {
28          public String fieldPublic;
29          String fieldPackage;
30          protected String fieldProtected;
31          private String fieldPrivate;
32      }
33  
34      private final AnnotatedFields subject = new AnnotatedFields();
35  
36      @Test public void canSetPublicField() throws Exception {
37          object(subject).field(hasName("fieldPublic")).callRaw("value");
38  
39          assertThat(subject.fieldPublic, equalTo("value"));
40      }
41  
42      @Test public void canReadPublicField() throws Exception {
43          subject.fieldPublic = "value";
44  
45          final String value = object(subject).field(hasName("fieldPublic")).as(String.class).callRaw();
46  
47          assertThat(value, equalTo("value"));
48      }
49  
50      @Test public void canSetPackageField() throws Exception {
51          object(subject).field(hasName("fieldPackage")).callRaw("value");
52  
53          assertThat(subject.fieldPackage, equalTo("value"));
54      }
55  
56      @Test public void canReadPackageField() throws Exception {
57          subject.fieldPackage = "value";
58  
59          final String value = object(subject).field(hasName("fieldPackage")).as(String.class).callRaw();
60  
61          assertThat(value, equalTo("value"));
62      }
63  
64      @Test public void canSetProtectedField() throws Exception {
65          object(subject).field(hasName("fieldProtected")).callRaw("value");
66  
67          assertThat(subject.fieldProtected, equalTo("value"));
68      }
69  
70      @Test public void canReadProtectedField() throws Exception {
71          subject.fieldProtected = "value";
72  
73          final String value = object(subject).field(hasName("fieldProtected")).as(String.class).callRaw();
74  
75          assertThat(value, equalTo("value"));
76      }
77  
78      @Test public void canSetPrivateField() throws Exception {
79          object(subject).field(hasName("fieldPrivate")).callRaw("value");
80  
81          assertThat(subject.fieldPrivate, equalTo("value"));
82      }
83  
84      @Test public void canReadPrivateField() throws Exception {
85          subject.fieldPrivate = "value";
86  
87          final String value = object(subject).field(hasName("fieldPrivate")).as(String.class).callRaw();
88  
89          assertThat(value, equalTo("value"));
90      }
91  }