View Javadoc

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