1 package com.lexicalscope.fluentreflection.bean.endtoend;
2
3 import static com.lexicalscope.fluentreflection.ReflectionMatchers.*;
4 import static com.lexicalscope.fluentreflection.bean.BeanMap.*;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.*;
7
8 import java.util.Map;
9
10 import org.junit.Test;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class TestBeanMapBuilder {
29 static class Bean {
30 private int readWriteProperty;
31 private String readOnlyProperty;
32 private Object writeOnlyProperty;
33
34 String getReadOnlyProperty() {
35 return readOnlyProperty;
36 }
37
38 int getReadWriteProperty() {
39 return readWriteProperty;
40 }
41
42 void setReadWriteProperty(final int readWriteProperty) {
43 this.readWriteProperty = readWriteProperty;
44 }
45
46 void setWriteOnlyProperty(final Object writeOnlyProperty) {
47 this.writeOnlyProperty = writeOnlyProperty;
48 }
49 }
50
51 static class NoPrefixBean {
52 private int readWriteProperty;
53 private String readOnlyProperty;
54 private Object writeOnlyProperty;
55
56 String readOnlyProperty() {
57 return readOnlyProperty;
58 }
59
60 int readWriteProperty() {
61 return readWriteProperty;
62 }
63
64 void readWriteProperty(final int readWriteProperty) {
65 this.readWriteProperty = readWriteProperty;
66 }
67
68 void writeOnlyProperty(final Object writeOnlyProperty) {
69 this.writeOnlyProperty = writeOnlyProperty;
70 }
71 }
72
73 private final Bean bean = new Bean();
74
75 @Test public void canSelectOnlyReadWriteProperties() {
76 final Map<String, Object> map = beanMap().keys(onlyReadWriteProperties()).build(bean);
77
78 assertThat(map.keySet(), containsInAnyOrder("readWriteProperty"));
79 }
80
81 @Test public void canSelectAllReadableProperties() {
82 final Map<String, Object> map = beanMap().keys(allReadableProperties()).build(bean);
83
84 assertThat(map.keySet(), containsInAnyOrder("readWriteProperty", "readOnlyProperty"));
85 }
86
87 @Test public void canSelectAllWritableProperties() {
88 final Map<String, Object> map = beanMap().keys(allWriteableProperties()).build(bean);
89
90 assertThat(map.keySet(), containsInAnyOrder("readWriteProperty", "writeOnlyProperty"));
91 }
92
93 @Test public void canSelectWritableOnlyProperties() {
94 final Map<String, Object> map = beanMap().keys(writeablePropertiesOnly()).build(bean);
95
96 assertThat(map.keySet(), containsInAnyOrder("writeOnlyProperty"));
97 }
98
99 @Test public void canSelectReadableOnlyProperties() {
100 final Map<String, Object> map = beanMap().keys(readablePropertiesOnly()).build(bean);
101
102 assertThat(map.keySet(), containsInAnyOrder("readOnlyProperty"));
103 }
104
105 @Test public void canLowercaseProperties() {
106 final Map<String, Object> map = beanMap().propertyNames(lowercasePropertyName()).build(bean);
107
108 assertThat(map.keySet(), containsInAnyOrder("readonlyproperty", "readwriteproperty", "writeonlyproperty"));
109
110 map.put("writeonlyproperty", "my value");
111 assertThat(bean.writeOnlyProperty, equalTo((Object) "my value"));
112
113 map.put("readwriteproperty", 14);
114 assertThat(map.get("readwriteproperty"), equalTo((Object) 14));
115 }
116
117 @Test public void beanDoesNotNeedGetAndSetPrefixes() {
118 final NoPrefixBean noPrefixBean = new NoPrefixBean();
119 final Map<String, Object> map = beanMap().getters(isQuery()).setters(isMutator()).build(noPrefixBean);
120
121 assertThat(map.keySet(), containsInAnyOrder("readOnlyProperty", "readWriteProperty", "writeOnlyProperty"));
122
123 map.put("writeOnlyProperty", "my value");
124 assertThat(noPrefixBean.writeOnlyProperty, equalTo((Object) "my value"));
125
126 map.put("readWriteProperty", 14);
127 assertThat(map.get("readWriteProperty"), equalTo((Object) 14));
128 }
129 }