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.Collection;
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  import java.util.Set;
13  
14  import org.junit.Test;
15  
16  /*
17   * Copyright 2011 Tim Wood
18   *
19   * Licensed under the Apache License, Version 2.0 (the "License");
20   * you may not use this file except in compliance with the License.
21   * You may obtain a copy of the License at
22   *
23   * http://www.apache.org/licenses/LICENSE-2.0
24   *
25   * Unless required by applicable law or agreed to in writing, software
26   * distributed under the License is distributed on an "AS IS" BASIS,
27   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   * See the License for the specific language governing permissions and
29   * limitations under the License.
30   */
31  
32  public class TestMapBean {
33      interface Bean {
34          String getProperty();
35          Boolean hasProperty();
36          Boolean isProperty();
37          void setProperty(String value);
38  
39          int getInt();
40          void setInt(int integer);
41  
42          char getChar();
43          void setChar(char character);
44  
45          Integer getInteger();
46          void setInteger(Integer integer);
47  
48          Iterable<Integer> getIterable();
49          void setIterable(Iterable<Integer> integer);
50  
51          Collection<Integer> getCollection();
52          void setCollection(Collection<Integer> integer);
53  
54          Set<Integer> getSet();
55          void setSet(Set<Integer> integer);
56  
57          List<Integer> getList();
58          void setList(List<Integer> integer);
59  
60          List<Integer> getOptionalList();
61          void setOptionalList(List<Integer> integer);
62          boolean isOptionalList();
63      }
64  
65      private final Map<String, Object> map = new HashMap<String, Object>();
66      private final Bean bean = bean(Bean.class, map);
67  
68      @Test public void mapCanBeQueriedViaInterface() throws Exception {
69          assertThat(bean.isProperty(), equalTo(false));
70  
71          bean.setProperty("my value");
72  
73          assertThat(bean.isProperty(), equalTo(true));
74      }
75  
76      @Test public void mapCanBeReadViaInterface() throws Exception {
77          map.put("property", "my value");
78          assertThat(bean.getProperty(), equalTo("my value"));
79      }
80  
81      @Test public void mapCanBeSetViaInterface() throws Exception {
82          bean.setProperty("my value");
83  
84          assertThat(map.get("property"), equalTo((Object) "my value"));
85      }
86  
87      @Test public void mapBeanEqualToItself() throws Exception {
88          assertThat(bean, equalTo(bean));
89          assertThat(bean.hashCode(), equalTo(bean.hashCode()));
90      }
91  
92      @Test public void mapBeanNotEqualToSecondBeanOnTheSameMap() throws Exception {
93          assertThat(bean, not(equalTo(bean(Bean.class, map))));
94      }
95  
96      @Test public void mapBeanOptionalCollectionQueryMethodIsFalseIfTheListIsNotPresent() throws Exception {
97          assertThat(bean.isOptionalList(), equalTo(false));
98  
99          bean.setOptionalList(new ArrayList<Integer>());
100 
101         assertThat(bean.isOptionalList(), equalTo(true));
102     }
103 }