1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.util.properties;
17
18 import java.util.Iterator;
19
20 import org.junit.Test;
21 import org.kuali.rice.kns.exception.DuplicateKeyException;
22 import org.kuali.rice.kns.exception.PropertiesException;
23 import org.kuali.rice.kns.util.properties.FilePropertySource;
24 import org.kuali.rice.kns.util.properties.PropertyHolder;
25 import org.kuali.test.KNSTestCase;
26
27
28
29
30 public class PropertyHolderTest extends KNSTestCase {
31 private static final String KNOWN_KEY1 = "key1";
32 private static final String KNOWN_VALUE1 = "value1";
33
34 private static final String KNOWN_KEY2 = "key 2";
35 private static final String KNOWN_VALUE2 = "value 2";
36
37 private static final String KNOWN_KEY3 = "";
38 private static final String KNOWN_VALUE3 = "";
39
40 @Test public void testIsEmpty_emptyHolder() {
41 PropertyHolder propertyHolder = new PropertyHolder();
42
43 assertTrue(propertyHolder.isEmpty());
44 }
45
46 @Test public void testIsEmpty_notEmptyHolder() {
47 PropertyHolder propertyHolder = buildNonEmpty();
48
49 assertTrue(!propertyHolder.isEmpty());
50 }
51
52 @Test public void testContainsKey_invalidKey() {
53 PropertyHolder propertyHolder = new PropertyHolder();
54
55 boolean failedAsExpected = false;
56 try {
57 propertyHolder.containsKey(null);
58 }
59 catch (IllegalArgumentException e) {
60 failedAsExpected = true;
61 }
62
63 assertTrue(failedAsExpected);
64 }
65
66 @Test public void testContainsKey_emptyHolder() {
67 PropertyHolder propertyHolder = new PropertyHolder();
68
69 assertFalse(propertyHolder.containsKey(KNOWN_KEY1));
70 assertFalse(propertyHolder.containsKey(KNOWN_KEY2));
71 assertFalse(propertyHolder.containsKey(KNOWN_KEY3));
72 }
73
74 @Test public void testContainsKey_notContains() {
75 PropertyHolder propertyHolder = buildNonEmpty();
76
77 assertFalse(propertyHolder.containsKey(KNOWN_KEY1 + "foo"));
78 assertFalse(propertyHolder.containsKey(KNOWN_KEY2 + "foo"));
79 assertFalse(propertyHolder.containsKey(KNOWN_KEY3 + "foo"));
80 }
81
82 @Test public void testContainsKey_contains() {
83 PropertyHolder propertyHolder = buildNonEmpty();
84
85 assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
86 assertTrue(propertyHolder.containsKey(KNOWN_KEY2));
87 assertTrue(propertyHolder.containsKey(KNOWN_KEY3));
88 }
89
90 @Test public void testGetProperty_invalidKey() {
91 PropertyHolder propertyHolder = new PropertyHolder();
92
93 boolean failedAsExpected = false;
94 try {
95 propertyHolder.getProperty(null);
96 }
97 catch (IllegalArgumentException e) {
98 failedAsExpected = true;
99 }
100
101 assertTrue(failedAsExpected);
102 }
103
104 @Test public void testGetProperty_emptyHolder() {
105 PropertyHolder propertyHolder = new PropertyHolder();
106
107 assertNull(propertyHolder.getProperty(KNOWN_KEY1));
108 assertNull(propertyHolder.getProperty(KNOWN_KEY2));
109 assertNull(propertyHolder.getProperty(KNOWN_KEY3));
110 }
111
112 @Test public void testGetProperty_notContains() {
113 PropertyHolder propertyHolder = buildNonEmpty();
114
115 assertNull(propertyHolder.getProperty(KNOWN_KEY1 + "foo"));
116 assertNull(propertyHolder.getProperty(KNOWN_KEY2 + "foo"));
117 assertNull(propertyHolder.getProperty(KNOWN_KEY3 + "foo"));
118 }
119
120 @Test public void testGetProperty_contains() {
121 PropertyHolder propertyHolder = buildNonEmpty();
122
123 String value = propertyHolder.getProperty(KNOWN_KEY1);
124 assertEquals(KNOWN_VALUE1, value);
125 value = propertyHolder.getProperty(KNOWN_KEY2);
126 assertEquals(KNOWN_VALUE2, value);
127 value = propertyHolder.getProperty(KNOWN_KEY3);
128 assertEquals(KNOWN_VALUE3, value);
129 }
130
131 @Test public void testSetProperty_invalidKey() {
132 PropertyHolder propertyHolder = new PropertyHolder();
133
134 boolean failedAsExpected = false;
135 try {
136 propertyHolder.setProperty(null, KNOWN_VALUE1);
137 }
138 catch (IllegalArgumentException e) {
139 failedAsExpected = true;
140 }
141
142 assertTrue(failedAsExpected);
143 }
144
145 @Test public void testSetProperty_invalidValue() {
146 PropertyHolder propertyHolder = new PropertyHolder();
147
148 boolean failedAsExpected = false;
149 try {
150 propertyHolder.setProperty(KNOWN_KEY1, null);
151 }
152 catch (IllegalArgumentException e) {
153 failedAsExpected = true;
154 }
155
156 assertTrue(failedAsExpected);
157 }
158
159 @Test public void testSetProperty_uniqueKey() {
160 PropertyHolder propertyHolder = new PropertyHolder();
161
162 propertyHolder.setProperty(KNOWN_KEY1, KNOWN_VALUE1);
163 assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
164 assertEquals(KNOWN_VALUE1, propertyHolder.getProperty(KNOWN_KEY1));
165 }
166
167 @Test public void testSetProperty_duplicateKey() {
168 PropertyHolder propertyHolder = buildNonEmpty();
169
170 boolean failedAsExpected = false;
171 assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
172 try {
173 propertyHolder.setProperty(KNOWN_KEY1, KNOWN_VALUE1);
174 }
175 catch (DuplicateKeyException e) {
176 failedAsExpected = true;
177 }
178
179 assertTrue(failedAsExpected);
180 }
181
182 @Test public void testClearProperty_invalidKey() {
183 PropertyHolder propertyHolder = buildNonEmpty();
184
185 boolean failedAsExpected = false;
186 try {
187 propertyHolder.clearProperty(null);
188 }
189 catch (IllegalArgumentException e) {
190 failedAsExpected = true;
191 }
192
193 assertTrue(failedAsExpected);
194 }
195
196 @Test public void testClearProperty_unknownKey() {
197 PropertyHolder propertyHolder = buildNonEmpty();
198
199 assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
200 propertyHolder.clearProperty(KNOWN_KEY1 + "foo");
201 assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
202 }
203
204 @Test public void testClearProperty_knownKey() {
205 PropertyHolder propertyHolder = buildNonEmpty();
206
207 assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
208 assertTrue(propertyHolder.containsKey(KNOWN_KEY2));
209 propertyHolder.clearProperty(KNOWN_KEY1);
210 assertFalse(propertyHolder.containsKey(KNOWN_KEY1));
211 assertTrue(propertyHolder.containsKey(KNOWN_KEY2));
212 }
213
214 @Test public void testClearProperties_empty() {
215 PropertyHolder propertyHolder = new PropertyHolder();
216
217 assertTrue(propertyHolder.isEmpty());
218 propertyHolder.clearProperties();
219 assertTrue(propertyHolder.isEmpty());
220 }
221
222 @Test public void testClearProperties_nonEmpty() {
223 PropertyHolder propertyHolder = buildNonEmpty();
224
225 assertFalse(propertyHolder.isEmpty());
226 propertyHolder.clearProperties();
227 assertTrue(propertyHolder.isEmpty());
228 }
229
230 @Test public void testGetKeys_empty() {
231 PropertyHolder propertyHolder = new PropertyHolder();
232
233 assertTrue(propertyHolder.isEmpty());
234 Iterator i = propertyHolder.getKeys();
235 assertFalse(i.hasNext());
236 }
237
238 @Test public void testGetKeys_nonEmpty() {
239 PropertyHolder propertyHolder = buildNonEmpty();
240
241 assertFalse(propertyHolder.isEmpty());
242 Iterator i = propertyHolder.getKeys();
243 assertTrue(i.hasNext());
244
245 for (; i.hasNext();) {
246 String key = (String) i.next();
247 assertTrue(propertyHolder.containsKey(key));
248 }
249 }
250
251 @Test public void testLoadProperties_nullPropertySource() {
252 PropertyHolder propertyHolder = new PropertyHolder();
253
254 boolean failedAsExpected = false;
255 try {
256 propertyHolder.loadProperties(null);
257 }
258 catch (IllegalArgumentException e) {
259 failedAsExpected = true;
260 }
261
262 assertTrue(failedAsExpected);
263 }
264
265 @Test public void testLoadProperties_invalidPropertySource() {
266 PropertyHolder propertyHolder = new PropertyHolder();
267 FilePropertySource fps = new FilePropertySource();
268
269 boolean failedAsExpected = false;
270 try {
271 propertyHolder.loadProperties(fps);
272 }
273 catch (IllegalStateException e) {
274 failedAsExpected = true;
275 }
276
277 assertTrue(failedAsExpected);
278 }
279
280 @Test public void testLoadProperties_unknownPropertySource() {
281 PropertyHolder propertyHolder = new PropertyHolder();
282 FilePropertySource fps = new FilePropertySource();
283 fps.setFileName("foo");
284
285 boolean failedAsExpected = false;
286 try {
287 propertyHolder.loadProperties(fps);
288 }
289 catch (PropertiesException e) {
290 failedAsExpected = true;
291 }
292
293 assertTrue(failedAsExpected);
294 }
295
296 private final PropertyHolder buildNonEmpty() {
297 PropertyHolder propertyHolder = new PropertyHolder();
298 propertyHolder.setProperty(KNOWN_KEY1, KNOWN_VALUE1);
299 propertyHolder.setProperty(KNOWN_KEY2, KNOWN_VALUE2);
300 propertyHolder.setProperty(KNOWN_KEY3, KNOWN_VALUE3);
301
302 return propertyHolder;
303 }
304 }