1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.cache;
17
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.is;
20 import static org.hamcrest.Matchers.nullValue;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25
26 import java.util.Properties;
27
28 import org.junit.Before;
29 import org.junit.Test;
30
31 import de.smartics.properties.api.config.domain.ConfigurationProperties;
32 import de.smartics.properties.api.config.domain.Property;
33 import de.smartics.properties.api.config.domain.PropertyLocation;
34 import de.smartics.properties.api.config.domain.ResolvedProperty;
35 import de.smartics.testdoc.annotations.Uut;
36
37
38
39
40 public class DependencyTrackingCacheTest
41 {
42
43
44
45
46 private static final PropertyLocation TEST_SOURCE = new PropertyLocation(
47 "test");
48
49
50 private static final String KEY_MISS = "miss";
51
52 private static final String KEY_HIT = "hit";
53
54 private static final String VALUE_HIT = "hitValue";
55
56 private static final String NO_DEFAULT = null;
57
58 private static final String NO_VALUE = null;
59
60 private static final String NO_DEFAULT_EXPRESSION = null;
61
62
63
64 private static final String DOMAIN_KEY = "domain";
65
66 private static final String DOMAIN_VALUE = "example.com";
67
68 private static final String NEW_DOMAIN_VALUE = "new.example.com";
69
70 private static final String SERVER_KEY = "server";
71
72 private static final String SERVER_VALUE = "www.${domain}";
73
74 private static final String RESOLVED_SERVER_VALUE = "www.example.com";
75
76 private static final String NEW_RESOLVED_SERVER_VALUE = "www.new.example.com";
77
78 private static final String MAIL_SERVER_KEY = "mail.server";
79
80 private static final String MAIL_SERVER_VALUE = null;
81
82 private static final String MAIL_SERVER_EXPRESSION = "mail-${server}";
83
84 private static final String RESOLVED_MAIL_SERVER_VALUE =
85 "mail-www.example.com";
86
87 private static final String NEW_RESOLVED_MAIL_SERVER_VALUE =
88 "mail-www.new.example.com";
89
90
91
92 @Uut
93 private DependencyTrackingCache uut;
94
95
96
97
98
99
100
101 @Before
102 public void setUp()
103 {
104 uut = new DependencyTrackingCache();
105 }
106
107
108
109 private void initCache()
110 {
111 final ConfigurationProperties propertySource =
112 mock(ConfigurationProperties.class);
113
114 when(propertySource.getResolvedProperty(DOMAIN_KEY, null)).thenReturn(
115 new ResolvedProperty(
116 new Property(TEST_SOURCE, DOMAIN_KEY, DOMAIN_VALUE),
117 NO_DEFAULT_EXPRESSION, DOMAIN_VALUE),
118 new ResolvedProperty(
119 new Property(TEST_SOURCE, DOMAIN_KEY, DOMAIN_VALUE),
120 NO_DEFAULT_EXPRESSION, NEW_DOMAIN_VALUE));
121 when(propertySource.getResolvedProperty(SERVER_KEY, null)).thenReturn(
122 new ResolvedProperty(
123 new Property(TEST_SOURCE, SERVER_KEY, SERVER_VALUE),
124 NO_DEFAULT_EXPRESSION, RESOLVED_SERVER_VALUE),
125 new ResolvedProperty(
126 new Property(TEST_SOURCE, SERVER_KEY, SERVER_VALUE),
127 NO_DEFAULT_EXPRESSION, NEW_RESOLVED_SERVER_VALUE));
128 when(propertySource.getResolvedProperty(MAIL_SERVER_KEY, null)).thenReturn(
129 new ResolvedProperty(new Property(TEST_SOURCE, MAIL_SERVER_KEY,
130 MAIL_SERVER_VALUE), MAIL_SERVER_EXPRESSION,
131 RESOLVED_MAIL_SERVER_VALUE),
132 new ResolvedProperty(new Property(TEST_SOURCE, MAIL_SERVER_KEY, null),
133 MAIL_SERVER_EXPRESSION, NEW_RESOLVED_MAIL_SERVER_VALUE));
134
135 uut.getResolvedProperty(propertySource, DOMAIN_KEY, NO_DEFAULT);
136 uut.getResolvedProperty(propertySource, SERVER_KEY, NO_DEFAULT);
137 uut.getResolvedProperty(propertySource, MAIL_SERVER_KEY, NO_DEFAULT);
138 assertThat(uut.getCacheSize(), is(3));
139 }
140
141
142
143 @Test
144 public void storesAMissInTheCache()
145 {
146 final ConfigurationProperties propertySource =
147 mock(ConfigurationProperties.class);
148 final ResolvedProperty resolvedProperty =
149 new ResolvedProperty(TEST_SOURCE, KEY_MISS, NO_VALUE,
150 NO_DEFAULT_EXPRESSION, null);
151 when(propertySource.getResolvedProperty(KEY_MISS, NO_DEFAULT)).thenReturn(
152 resolvedProperty);
153
154 final ResolvedProperty property =
155 uut.getResolvedProperty(propertySource, KEY_MISS, NO_DEFAULT);
156 assertThat(property, is(resolvedProperty));
157
158 uut.getResolvedProperty(propertySource, KEY_MISS, NO_DEFAULT);
159
160 assertThat(uut.getCacheSize(), is(1));
161 }
162
163 @Test
164 public void storesAHitInTheCache()
165 {
166 final ConfigurationProperties propertySource =
167 mock(ConfigurationProperties.class);
168 final ResolvedProperty resolvedProperty =
169 new ResolvedProperty(TEST_SOURCE, KEY_HIT, VALUE_HIT,
170 NO_DEFAULT_EXPRESSION, null);
171 when(propertySource.getResolvedProperty(KEY_HIT, NO_DEFAULT)).thenReturn(
172 resolvedProperty);
173
174 final ResolvedProperty property =
175 uut.getResolvedProperty(propertySource, KEY_HIT, NO_DEFAULT);
176 assertThat(property, is(resolvedProperty));
177
178 uut.getResolvedProperty(propertySource, KEY_HIT, NO_DEFAULT);
179
180 assertThat(uut.getCacheSize(), is(1));
181 verify(propertySource, times(1)).getResolvedProperty(KEY_HIT, NO_DEFAULT);
182 }
183
184 @Test
185 public void removesAValueFromTheCache()
186 {
187 final ConfigurationProperties propertySource =
188 mock(ConfigurationProperties.class);
189 final ResolvedProperty resolvedProperty =
190 new ResolvedProperty(TEST_SOURCE, KEY_HIT, VALUE_HIT,
191 NO_DEFAULT_EXPRESSION, null);
192 when(propertySource.getResolvedProperty(KEY_HIT, NO_DEFAULT)).thenReturn(
193 resolvedProperty);
194 uut.getResolvedProperty(propertySource, KEY_HIT, NO_DEFAULT);
195
196 final Property property = uut.removeFromCache(KEY_HIT);
197 assertThat(property, is((Property) resolvedProperty));
198
199 assertThat(uut.getCacheSize(), is(0));
200 }
201
202 @Test
203 public void allowsToRemoveAValueFromTheCacheThatIsNotThereGracefully()
204 {
205 final Property property = uut.removeFromCache(KEY_HIT);
206 assertThat(property, is(nullValue()));
207
208 assertThat(uut.getCacheSize(), is(0));
209 }
210
211 @Test
212 public void removedDependentPropertiesRoot()
213 {
214 initCache();
215
216 uut.removeFromCache(DOMAIN_KEY);
217 assertThat(uut.getCacheSize(), is(0));
218 }
219
220 @Test
221 public void removedDependentPropertiesMiddle()
222 {
223 initCache();
224
225 uut.removeFromCache(SERVER_KEY);
226 assertThat(uut.getCacheSize(), is(1));
227 }
228
229 @Test
230 public void removedDependentPropertiesLeaf()
231 {
232 initCache();
233
234 uut.removeFromCache(MAIL_SERVER_KEY);
235 assertThat(uut.getCacheSize(), is(2));
236 }
237
238 @Test
239 public void removeMulipleRoot()
240 {
241 initCache();
242
243 final Properties properties = new Properties();
244 properties.setProperty(DOMAIN_KEY, DOMAIN_VALUE);
245 properties.setProperty(MAIL_SERVER_KEY, "non-null");
246
247 uut.removeFromCache(properties);
248 assertThat(uut.getCacheSize(), is(0));
249 }
250
251 @Test
252 public void removeMulipleMiddle()
253 {
254 initCache();
255
256 final Properties properties = new Properties();
257 properties.setProperty(SERVER_KEY, SERVER_VALUE);
258
259 uut.removeFromCache(properties);
260 assertThat(uut.getCacheSize(), is(1));
261 }
262
263 @Test
264 public void removeMulipleLeaf()
265 {
266 initCache();
267
268 final Properties properties = new Properties();
269 properties.setProperty(SERVER_KEY, SERVER_VALUE);
270 properties.setProperty(MAIL_SERVER_KEY, "non-null");
271
272 uut.removeFromCache(properties);
273 assertThat(uut.getCacheSize(), is(1));
274 }
275
276 @Test
277 public void allowsEmptyPropertiesGracefully()
278 {
279 initCache();
280
281 final Properties properties = new Properties();
282
283 uut.removeFromCache(properties);
284 assertThat(uut.getCacheSize(), is(3));
285 }
286 }