001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.util.properties;
017:
018: import java.util.Iterator;
019:
020: import org.kuali.core.exceptions.DuplicateKeyException;
021: import org.kuali.core.exceptions.PropertiesException;
022: import org.kuali.kfs.context.KualiTestBase;
023:
024: /**
025: * This class tests the PropertyHolder methods.
026: */
027: public class PropertyHolderTest extends KualiTestBase {
028: private static final String KNOWN_KEY1 = "key1";
029: private static final String KNOWN_VALUE1 = "value1";
030:
031: private static final String KNOWN_KEY2 = "key 2";
032: private static final String KNOWN_VALUE2 = "value 2";
033:
034: private static final String KNOWN_KEY3 = "";
035: private static final String KNOWN_VALUE3 = "";
036:
037: public final void testIsEmpty_emptyHolder() {
038: PropertyHolder propertyHolder = new PropertyHolder();
039:
040: assertTrue(propertyHolder.isEmpty());
041: }
042:
043: public final void testIsEmpty_notEmptyHolder() {
044: PropertyHolder propertyHolder = buildNonEmpty();
045:
046: assertTrue(!propertyHolder.isEmpty());
047: }
048:
049: public final void testContainsKey_invalidKey() {
050: PropertyHolder propertyHolder = new PropertyHolder();
051:
052: boolean failedAsExpected = false;
053: try {
054: propertyHolder.containsKey(null);
055: } catch (IllegalArgumentException e) {
056: failedAsExpected = true;
057: }
058:
059: assertTrue(failedAsExpected);
060: }
061:
062: public final void testContainsKey_emptyHolder() {
063: PropertyHolder propertyHolder = new PropertyHolder();
064:
065: assertFalse(propertyHolder.containsKey(KNOWN_KEY1));
066: assertFalse(propertyHolder.containsKey(KNOWN_KEY2));
067: assertFalse(propertyHolder.containsKey(KNOWN_KEY3));
068: }
069:
070: public final void testContainsKey_notContains() {
071: PropertyHolder propertyHolder = buildNonEmpty();
072:
073: assertFalse(propertyHolder.containsKey(KNOWN_KEY1 + "foo"));
074: assertFalse(propertyHolder.containsKey(KNOWN_KEY2 + "foo"));
075: assertFalse(propertyHolder.containsKey(KNOWN_KEY3 + "foo"));
076: }
077:
078: public final void testContainsKey_contains() {
079: PropertyHolder propertyHolder = buildNonEmpty();
080:
081: assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
082: assertTrue(propertyHolder.containsKey(KNOWN_KEY2));
083: assertTrue(propertyHolder.containsKey(KNOWN_KEY3));
084: }
085:
086: public final void testGetProperty_invalidKey() {
087: PropertyHolder propertyHolder = new PropertyHolder();
088:
089: boolean failedAsExpected = false;
090: try {
091: propertyHolder.getProperty(null);
092: } catch (IllegalArgumentException e) {
093: failedAsExpected = true;
094: }
095:
096: assertTrue(failedAsExpected);
097: }
098:
099: public final void testGetProperty_emptyHolder() {
100: PropertyHolder propertyHolder = new PropertyHolder();
101:
102: assertNull(propertyHolder.getProperty(KNOWN_KEY1));
103: assertNull(propertyHolder.getProperty(KNOWN_KEY2));
104: assertNull(propertyHolder.getProperty(KNOWN_KEY3));
105: }
106:
107: public final void testGetProperty_notContains() {
108: PropertyHolder propertyHolder = buildNonEmpty();
109:
110: assertNull(propertyHolder.getProperty(KNOWN_KEY1 + "foo"));
111: assertNull(propertyHolder.getProperty(KNOWN_KEY2 + "foo"));
112: assertNull(propertyHolder.getProperty(KNOWN_KEY3 + "foo"));
113: }
114:
115: public final void testGetProperty_contains() {
116: PropertyHolder propertyHolder = buildNonEmpty();
117:
118: String value = propertyHolder.getProperty(KNOWN_KEY1);
119: assertEquals(KNOWN_VALUE1, value);
120: value = propertyHolder.getProperty(KNOWN_KEY2);
121: assertEquals(KNOWN_VALUE2, value);
122: value = propertyHolder.getProperty(KNOWN_KEY3);
123: assertEquals(KNOWN_VALUE3, value);
124: }
125:
126: public final void testSetProperty_invalidKey() {
127: PropertyHolder propertyHolder = new PropertyHolder();
128:
129: boolean failedAsExpected = false;
130: try {
131: propertyHolder.setProperty(null, KNOWN_VALUE1);
132: } catch (IllegalArgumentException e) {
133: failedAsExpected = true;
134: }
135:
136: assertTrue(failedAsExpected);
137: }
138:
139: public final void testSetProperty_invalidValue() {
140: PropertyHolder propertyHolder = new PropertyHolder();
141:
142: boolean failedAsExpected = false;
143: try {
144: propertyHolder.setProperty(KNOWN_KEY1, null);
145: } catch (IllegalArgumentException e) {
146: failedAsExpected = true;
147: }
148:
149: assertTrue(failedAsExpected);
150: }
151:
152: public final void testSetProperty_uniqueKey() {
153: PropertyHolder propertyHolder = new PropertyHolder();
154:
155: propertyHolder.setProperty(KNOWN_KEY1, KNOWN_VALUE1);
156: assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
157: assertEquals(KNOWN_VALUE1, propertyHolder
158: .getProperty(KNOWN_KEY1));
159: }
160:
161: public final void testSetProperty_duplicateKey() {
162: PropertyHolder propertyHolder = buildNonEmpty();
163:
164: boolean failedAsExpected = false;
165: assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
166: try {
167: propertyHolder.setProperty(KNOWN_KEY1, KNOWN_VALUE1);
168: } catch (DuplicateKeyException e) {
169: failedAsExpected = true;
170: }
171:
172: assertTrue(failedAsExpected);
173: }
174:
175: public final void testClearProperty_invalidKey() {
176: PropertyHolder propertyHolder = buildNonEmpty();
177:
178: boolean failedAsExpected = false;
179: try {
180: propertyHolder.clearProperty(null);
181: } catch (IllegalArgumentException e) {
182: failedAsExpected = true;
183: }
184:
185: assertTrue(failedAsExpected);
186: }
187:
188: public final void testClearProperty_unknownKey() {
189: PropertyHolder propertyHolder = buildNonEmpty();
190:
191: assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
192: propertyHolder.clearProperty(KNOWN_KEY1 + "foo");
193: assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
194: }
195:
196: public final void testClearProperty_knownKey() {
197: PropertyHolder propertyHolder = buildNonEmpty();
198:
199: assertTrue(propertyHolder.containsKey(KNOWN_KEY1));
200: assertTrue(propertyHolder.containsKey(KNOWN_KEY2));
201: propertyHolder.clearProperty(KNOWN_KEY1);
202: assertFalse(propertyHolder.containsKey(KNOWN_KEY1));
203: assertTrue(propertyHolder.containsKey(KNOWN_KEY2));
204: }
205:
206: public final void testClearProperties_empty() {
207: PropertyHolder propertyHolder = new PropertyHolder();
208:
209: assertTrue(propertyHolder.isEmpty());
210: propertyHolder.clearProperties();
211: assertTrue(propertyHolder.isEmpty());
212: }
213:
214: public final void testClearProperties_nonEmpty() {
215: PropertyHolder propertyHolder = buildNonEmpty();
216:
217: assertFalse(propertyHolder.isEmpty());
218: propertyHolder.clearProperties();
219: assertTrue(propertyHolder.isEmpty());
220: }
221:
222: public final void testGetKeys_empty() {
223: PropertyHolder propertyHolder = new PropertyHolder();
224:
225: assertTrue(propertyHolder.isEmpty());
226: Iterator i = propertyHolder.getKeys();
227: assertFalse(i.hasNext());
228: }
229:
230: public final void testGetKeys_nonEmpty() {
231: PropertyHolder propertyHolder = buildNonEmpty();
232:
233: assertFalse(propertyHolder.isEmpty());
234: Iterator i = propertyHolder.getKeys();
235: assertTrue(i.hasNext());
236:
237: for (; i.hasNext();) {
238: String key = (String) i.next();
239: assertTrue(propertyHolder.containsKey(key));
240: }
241: }
242:
243: public final void testLoadProperties_nullPropertySource() {
244: PropertyHolder propertyHolder = new PropertyHolder();
245:
246: boolean failedAsExpected = false;
247: try {
248: propertyHolder.loadProperties(null);
249: } catch (IllegalArgumentException e) {
250: failedAsExpected = true;
251: }
252:
253: assertTrue(failedAsExpected);
254: }
255:
256: public final void testLoadProperties_invalidPropertySource() {
257: PropertyHolder propertyHolder = new PropertyHolder();
258: FilePropertySource fps = new FilePropertySource();
259:
260: boolean failedAsExpected = false;
261: try {
262: propertyHolder.loadProperties(fps);
263: } catch (IllegalStateException e) {
264: failedAsExpected = true;
265: }
266:
267: assertTrue(failedAsExpected);
268: }
269:
270: public final void testLoadProperties_unknownPropertySource() {
271: PropertyHolder propertyHolder = new PropertyHolder();
272: FilePropertySource fps = new FilePropertySource();
273: fps.setFileName("foo");
274:
275: boolean failedAsExpected = false;
276: try {
277: propertyHolder.loadProperties(fps);
278: } catch (PropertiesException e) {
279: failedAsExpected = true;
280: }
281:
282: assertTrue(failedAsExpected);
283: }
284:
285: private final PropertyHolder buildNonEmpty() {
286: PropertyHolder propertyHolder = new PropertyHolder();
287: propertyHolder.setProperty(KNOWN_KEY1, KNOWN_VALUE1);
288: propertyHolder.setProperty(KNOWN_KEY2, KNOWN_VALUE2);
289: propertyHolder.setProperty(KNOWN_KEY3, KNOWN_VALUE3);
290:
291: return propertyHolder;
292: }
293: }
|