1   package com.lexicalscope.fluentreflection.bean.endtoend;
2   
3   import static com.lexicalscope.fluentreflection.bean.MapBean.bean;
4   import static org.hamcrest.MatcherAssert.assertThat;
5   import static org.hamcrest.Matchers.*;
6   
7   import java.util.ArrayList;
8   import java.util.HashMap;
9   import java.util.List;
10  import java.util.Map;
11  
12  import org.junit.Test;
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 TestMapBeanWithNoPrefixes {
31      interface Bean {
32          String property();
33          Boolean hasProperty();
34          Boolean isProperty();
35          void property(String value);
36  
37          List<Integer> optionalList();
38          void optionalList(List<Integer> integer);
39          boolean isOptionalList();
40      }
41  
42      private final Map<String, Object> map = new HashMap<String, Object>();
43      private final Bean bean = bean(Bean.class, map);
44  
45      @Test public void mapCanBeQueriedViaInterface() throws Exception {
46          assertThat(bean.isProperty(), equalTo(false));
47  
48          bean.property("my value");
49  
50          assertThat(bean.isProperty(), equalTo(true));
51      }
52  
53      @Test public void mapCanBeReadViaInterface() throws Exception {
54          map.put("property", "my value");
55          assertThat(bean.property(), equalTo("my value"));
56      }
57  
58      @Test public void mapCanBeSetViaInterface() throws Exception {
59          bean.property("my value");
60  
61          assertThat(map.get("property"), equalTo((Object) "my value"));
62      }
63  
64      @Test public void mapBeanEqualToItself() throws Exception {
65          assertThat(bean, equalTo(bean));
66          assertThat(bean.hashCode(), equalTo(bean.hashCode()));
67      }
68  
69      @Test public void mapBeanNotEqualToSecondBeanOnTheSameMap() throws Exception {
70          assertThat(bean, not(equalTo(bean(Bean.class, map))));
71      }
72  
73      @Test public void mapBeanOptionalCollectionQueryMethodIsFalseIfTheListIsNotPresent() throws Exception {
74          assertThat(bean.isOptionalList(), equalTo(false));
75  
76          bean.optionalList(new ArrayList<Integer>());
77  
78          assertThat(bean.isOptionalList(), equalTo(true));
79      }
80  }