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
18
19
20
21
22
23
24
25
26
27
28
29
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 }