001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.properties;
007:
008: import java.util.Properties;
009:
010: import junit.framework.TestCase;
011:
012: /**
013: * Test case for PropertiesManager.
014: * Exercises property accessor methods against a test properties file.
015: * @author andrew.petro@yale.edu
016: */
017: public class PropertiesManagerTest extends TestCase {
018:
019: /**
020: * The test properties file is in the properties package of the source tree
021: * containing the PropertiesManager itself.
022: */
023: private static final String TEST_FILE = "./test.properties";
024:
025: /**
026: * Save a copy of the value of the system property so that we can reset it when we clean up.
027: */
028: private String systemPropertyValue;
029:
030: /*
031: * @see TestCase#setUp()
032: */
033: protected void setUp() throws Exception {
034: super .setUp();
035: Properties testProperties = new Properties();
036: testProperties.load(PropertiesManagerTest.class
037: .getResourceAsStream(TEST_FILE));
038: PropertiesManager.setProperties(testProperties);
039: }
040:
041: protected void tearDown() throws Exception {
042: super .tearDown();
043: if (this .systemPropertyValue != null)
044: System
045: .setProperty(
046: PropertiesManager.PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE,
047: this .systemPropertyValue);
048: }
049:
050: /**
051: * Test the getProperty method.
052: * Tests ability to retrieve a property from sample properties file.
053: * Tests for proper exception throw when property not found.
054: */
055: public void testGetPropertyString() {
056: assertEquals("splat", PropertiesManager
057: .getProperty("simpleProperty"));
058: try {
059: PropertiesManager.getProperty("missingProperty");
060: } catch (MissingPropertyException mpe) {
061: // correct
062: return;
063: }
064: fail("Should have thrown an MissingPropertyException because property was missing.");
065: }
066:
067: /**
068: * This test demonstrates that getPropertyUntrimmed does *not* retain leading whitespace
069: * on property values.
070: */
071: public void testGetPropertyUntrimmedLeadingWhitespace() {
072: assertEquals("twoSpacesBefore", PropertiesManager
073: .getPropertyUntrimmed("leadingWhitespace"));
074: }
075:
076: /**
077: * Test proper retention of trailing whitespace.
078: */
079: public void testGetPropertyUntrimmedTrailingWhitespace() {
080: assertEquals("oneSpaceAfter ", PropertiesManager
081: .getPropertyUntrimmed("trailingWhitespace"));
082: }
083:
084: /**
085: * Test exception throw when property missing.
086: */
087: public void testGetPropertyUntrimmedMissingProperty() {
088: try {
089: PropertiesManager.getPropertyUntrimmed("missingProperty");
090: } catch (MissingPropertyException mpe) {
091: // correct
092: return;
093: }
094: fail("Should have thrown an MissingPropertyException because a property was missing.");
095: }
096:
097: /**
098: * Test getPropertyAsBoolean().
099: * Demonstrates behavior of defaulting to false when property value doesn't "look like" true.
100: */
101: public void testGetPropertyAsBoolean() {
102: assertTrue(PropertiesManager
103: .getPropertyAsBoolean("testBooleanTrue"));
104: assertFalse(PropertiesManager
105: .getPropertyAsBoolean("testBooleanFalse"));
106:
107: // weird (e.g., "wombat") property values evaluate as false
108: assertFalse(PropertiesManager
109: .getPropertyAsBoolean("testBadBoolean"));
110: }
111:
112: /**
113: * Test getting a missing property as a boolean: throws proper exception.
114: */
115: public void testGetPropertyAsBooleanMissingProperty() {
116: try {
117: PropertiesManager.getPropertyAsBoolean("missingProperty");
118: } catch (MissingPropertyException mpe) {
119: // correct
120: return;
121: }
122: fail("Should have thrown MissingPropertyException because property was missing.");
123: }
124:
125: /**
126: * Test getPropertyAsByte().
127: */
128: public void testGetPropertyAsByte() {
129: byte result = PropertiesManager.getPropertyAsByte("testByte");
130: assertEquals(3, result);
131: }
132:
133: /**
134: * Test that getPropertyAsByte() throws proper runtime exception when property is missing.
135: */
136: public void testGetPropertyAsByteMissingProperty() {
137: try {
138: PropertiesManager.getPropertyAsByte("missingProperty");
139: } catch (MissingPropertyException mpe) {
140: // correct
141: return;
142: }
143: fail("Should have thrown MissingPropertyException because property was missing.");
144: }
145:
146: /**
147: * Test that getPropertyAsByte() throws proper runtime exception when the property
148: * value cannot be parsed as a byte.
149: */
150: public void testGetPropertyAsByteBadValue() {
151: try {
152: PropertiesManager.getPropertyAsByte("wombatProperty");
153: } catch (BadPropertyException pbe) {
154: // correct
155: return;
156: }
157: fail("Should have thrown BadPropertyException because the property value 'wombat' cannot be parsed as a byte.");
158: }
159:
160: /**
161: * Test getPropertyAsShort()
162: */
163: public void testGetPropertyAsShort() {
164: short returned = PropertiesManager
165: .getPropertyAsShort("testShort");
166: assertEquals(5, returned);
167: }
168:
169: /**
170: * Test proper exception throw from getPropertyAsShort() when property is missing.
171: */
172: public void testGetPropertyAsShortMissingProperty() {
173: try {
174: PropertiesManager.getPropertyAsShort("missingProperty");
175: } catch (MissingPropertyException mpe) {
176: // correct
177: return;
178: }
179: fail("Should have thrown MissingPropertyException because property was missing.");
180: }
181:
182: /**
183: * Test that getPropertyAsShort() throws proper runtime exception when the property
184: * value cannot be parsed as a short.
185: */
186: public void testGetPropertyAsShortBadValue() {
187: try {
188: PropertiesManager.getPropertyAsShort("wombatProperty");
189: } catch (BadPropertyException pbe) {
190: // correct
191: return;
192: }
193: fail("Should have thrown BadPropertyException because the property value 'wombat' cannot be parsed as a short.");
194: }
195:
196: /**
197: * Test getPropertyAsInt()
198: */
199: public void testGetPropertyAsInt() {
200: int returned = PropertiesManager.getPropertyAsInt("testInt");
201: assertEquals(10, returned);
202: }
203:
204: /**
205: * Test getPropertyAsInt() handling of missing property.
206: * Verifies that throws UndeclaredPortalException.
207: */
208: public void testGetPropertyAsIntMissingProperty() {
209: try {
210: PropertiesManager.getPropertyAsInt("missingProperty");
211: } catch (MissingPropertyException mpe) {
212: // correct
213: return;
214: }
215: fail("Should have thrown MissingPropertyException because property was missing.");
216: }
217:
218: /**
219: * Test that getPropertyAsInt() throws proper runtime exception when the property
220: * value cannot be parsed as an int.
221: */
222: public void testGetPropertyAsIntBadValue() {
223: try {
224: PropertiesManager.getPropertyAsInt("wombatProperty");
225: } catch (BadPropertyException pbe) {
226: // correct
227: return;
228: }
229: fail("Should have thrown BadPropertyException because the property value 'wombat' cannot be parsed as an int.");
230: }
231:
232: /**
233: * Test getPropertyAsLong()
234: */
235: public void testGetPropertyAsLong() {
236: long result = PropertiesManager.getPropertyAsLong("testLong");
237: assertEquals(45, result);
238: }
239:
240: /**
241: * Test proper error handing for getPropertyAsLong() for missing property.
242: * In particular, test that throws UndeclaredPortalException when property missing.
243: */
244: public void testGetPropertyAsLongMissingProperty() {
245: try {
246: PropertiesManager.getPropertyAsLong("missingProperty");
247: } catch (MissingPropertyException mpe) {
248: // correct
249: return;
250: }
251: fail("Should have thrown MissingPropertyException because property was missing.");
252: }
253:
254: /**
255: * Test that getPropertyAsLong() throws proper runtime exception when the property
256: * value cannot be parsed as a byte.
257: */
258: public void testGetPropertyAsLongBadValue() {
259: try {
260: PropertiesManager.getPropertyAsLong("wombatProperty");
261: } catch (BadPropertyException pbe) {
262: // correct
263: return;
264: }
265: fail("Should have thrown BadPropertyException because the property value 'wombat' cannot be parsed as a long.");
266: }
267:
268: /**
269: * Test getPropertyAsFloat()
270: */
271: public void testGetPropertyAsFloat() {
272: float result = PropertiesManager
273: .getPropertyAsFloat("testFloat");
274: assertEquals(2.718f, result, 0.01);
275: }
276:
277: /**
278: * Test getPropertyAsFloat() for proper handling of missing property.
279: * In particular, tests that UndeclaredPortalException thrown in this case.
280: */
281: public void testGetPropertyAsFloatMissingProperty() {
282: try {
283: PropertiesManager.getPropertyAsFloat("missingProperty");
284: } catch (MissingPropertyException mpe) {
285: // correct
286: return;
287: }
288: fail("Should have thrown MissingPropertyException because property was missing.");
289: }
290:
291: /**
292: * Test that getPropertyAsFloat() throws proper runtime exception when the property
293: * value cannot be parsed as a float.
294: */
295: public void testGetPropertyAsFloatBadValue() {
296: try {
297: PropertiesManager.getPropertyAsFloat("wombatProperty");
298: } catch (BadPropertyException pbe) {
299: // correct
300: return;
301: }
302: fail("Should have thrown BadPropertyException because the property value 'wombat' cannot be parsed as a float.");
303: }
304:
305: /**
306: * Test getPropertyAsDouble()
307: */
308: public void testGetPropertyAsDouble() {
309: double result = PropertiesManager
310: .getPropertyAsDouble("testDouble");
311: assertEquals(3.1415, result, 0.01);
312: }
313:
314: /**
315: * Test getPropertyAsDouble() for proper handling of missing property.
316: * In particular, tests that throws UndeclaredPortalException when property missing.
317: */
318: public void testGetPropertyAsDoubleMissingProperty() {
319: try {
320: PropertiesManager.getPropertyAsDouble("missingProperty");
321: } catch (MissingPropertyException upe) {
322: // correct
323: return;
324: }
325: fail("Should have thrown MissingPropertyException because property was missing.");
326: }
327:
328: /**
329: * Test that getPropertyAsDouble() throws proper runtime exception when the property
330: * value cannot be parsed as a byte.
331: */
332: public void testGetPropertyAsDoubleBadValue() {
333: try {
334: PropertiesManager.getPropertyAsDouble("wombatProperty");
335: } catch (BadPropertyException pbe) {
336: // correct
337: return;
338: }
339: fail("Should have thrown BadPropertyException because the property value 'wombat' cannot be parsed as a double.");
340: }
341:
342: /**
343: * Test getPropertyAsString with default value where property is present.
344: */
345: public void testGetPropertyWithDefault() {
346: String result = PropertiesManager.getProperty("simpleProperty",
347: "defaultValue");
348: assertEquals("splat", result);
349: }
350:
351: /**
352: * Test getPropertyAsString with default value where property is missing.
353: */
354: public void testGetPropertyWithDefaultPropertyMissing() {
355: String result = PropertiesManager.getProperty(
356: "missingProperty", "defaultValue");
357: assertEquals("defaultValue", result);
358: // test that we don't trim default values:
359: result = PropertiesManager.getProperty(
360: "anotherMissingProperty",
361: "defaultWithThreeTrailingSpaces ");
362: assertEquals("defaultWithThreeTrailingSpaces ", result);
363: }
364:
365: /**
366: * Test getPropertyUntrimmed() with default value where property is present.
367: */
368: public void testGetPropertyUntrimmedWithDefault() {
369: String result = PropertiesManager.getPropertyUntrimmed(
370: "trailingWhitespace", "defaultValue");
371: assertEquals("oneSpaceAfter ", result);
372: }
373:
374: /**
375: * Test getPropertyUntrimmed() with default value where property is missing.
376: */
377: public void testGetPropertyUntrimmedWithDefaultPropertyMissing() {
378: String result = PropertiesManager.getPropertyUntrimmed(
379: "missingProperty", "defaultValue");
380: assertEquals("defaultValue", result);
381: }
382:
383: /**
384: * Test getPropertyAsBoolean with default value where property is present.
385: */
386: public void testGetPropertyAsBooleanWithDefault() {
387: assertTrue(PropertiesManager.getPropertyAsBoolean(
388: "testBooleanTrue", false));
389: assertFalse(PropertiesManager.getPropertyAsBoolean(
390: "testBooleanFalse", true));
391:
392: // demonstrates behavior when property is present but weird value - returns false.
393: assertFalse(PropertiesManager.getPropertyAsBoolean(
394: "testBadBoolean", true));
395: }
396:
397: /**
398: * Test getPropertyAsBoolean with default value where property is absent.
399: */
400: public void testGetPropertyAsBooleanWithDefaultPropertyMissing() {
401: assertTrue(PropertiesManager.getPropertyAsBoolean(
402: "missingProperty", true));
403: assertFalse(PropertiesManager.getPropertyAsBoolean(
404: "missingProperty", false));
405: }
406:
407: /**
408: * Test getPropertyAsByte(String, byte) - default specified, property is present.
409: */
410: public void testGetPropertyAsByteWithDefault() {
411: byte returned = PropertiesManager.getPropertyAsByte("testByte",
412: (byte) 12);
413: assertEquals(3, returned);
414: }
415:
416: /**
417: * Test getPropertyAsByte with default value where property is missing.
418: */
419: public void testGetPropertyAsByteWithDefaultPropertyMissing() {
420: byte result = PropertiesManager.getPropertyAsByte(
421: "missingPropety", (byte) 12);
422: assertEquals((byte) 12, result);
423: }
424:
425: /**
426: * Test getPropertyAsByte() with default value where property cannot be
427: * parsed as a byte.
428: */
429: public void testGetPropertyAsByteWithDefaultPropertyBad() {
430: byte result = PropertiesManager.getPropertyAsByte(
431: "wombatProperty", (byte) 12);
432: assertEquals((byte) 12, result);
433: }
434:
435: /**
436: * Test getPropertyAsShort(String, short) - default specified, property present.
437: */
438: public void testGetPropertyAsShortWithDefault() {
439: short result = PropertiesManager.getPropertyAsShort(
440: "testShort", (short) 12);
441: assertEquals(5, result);
442: }
443:
444: /**
445: * Test getPropertyAsShort(String, short) - default specified, property absent.
446: */
447: public void testGetPropertyAsShortWithDefaultPropertyMissing() {
448: short result = PropertiesManager.getPropertyAsShort(
449: "missingProperty", (short) 12);
450: assertEquals(12, result);
451: }
452:
453: /**
454: * Test getPropertyAsShort(String, short) - default specified, property bad.
455: */
456: public void testGetPropertyAsShortWithDefaultPropertyBad() {
457: short result = PropertiesManager.getPropertyAsShort(
458: "wombatProperty", (short) 12);
459: assertEquals(12, result);
460: }
461:
462: /**
463: * Test getPropertyAsInt(String, int) - default specified, property present.
464: */
465: public void testGetPropertyAsIntWithDefault() {
466: int result = PropertiesManager.getPropertyAsInt("testInt", 12);
467: assertEquals(10, result);
468: }
469:
470: /**
471: * Test getPropertyAsInt(String, int) - default specified, property absent.
472: */
473: public void testGetPropertyAsIntWithDefaultPropertyMissing() {
474: int result = PropertiesManager.getPropertyAsInt(
475: "missingProperty", 12);
476: assertEquals(12, result);
477: }
478:
479: /**
480: * Test getPropertyAsInt(String, int) - default specified, property bad.
481: */
482: public void testGetPropertyAsIntWithDefaultPropertyBad() {
483: int result = PropertiesManager.getPropertyAsInt(
484: "wombatProperty", 12);
485: assertEquals(12, result);
486: }
487:
488: /**
489: * Test getPropertyAsLong(String, long) - default specified, property present.
490: */
491: public void testGetPropertyAsLongWithDefault() {
492: long result = PropertiesManager.getPropertyAsLong("testLong",
493: 42);
494: assertEquals(45, result);
495: }
496:
497: /**
498: * Test getPropertyAsLong(String, long) - default specified, property absent.
499: */
500: public void testGetPropertyAsLongWithDefaultPropertyMissing() {
501: long result = PropertiesManager.getPropertyAsLong(
502: "missingProperty", 42);
503: assertEquals(42, result);
504: }
505:
506: /**
507: * Test getPropertyAsLong(String, long) - default specified, property bad.
508: */
509: public void testGetPropertyAsLongWithDefaultPropertyBad() {
510: long result = PropertiesManager.getPropertyAsLong(
511: "wombatProperty", 42);
512: assertEquals(42, result);
513: }
514:
515: /**
516: * Test getPropertyAsFloat(String, float) - default specified, property present.
517: */
518: public void testGetPropertyAsFloatWithDefault() {
519: float result = PropertiesManager.getPropertyAsFloat(
520: "testFloat", (float) 4.2);
521: assertEquals(result, 2.718, 0.01);
522: }
523:
524: /**
525: * Test getPropertyAsFloat(String, float) - default specified, property absent.
526: */
527: public void testGetPropertyAsFloatWithDefaultPropertyMissing() {
528: float result = PropertiesManager.getPropertyAsFloat(
529: "missingProperty", (float) 4.2);
530: assertEquals(result, 4.2, 0.01);
531: }
532:
533: /**
534: * Test getPropertyAsFloat(String, float) - default specified, property absent.
535: */
536: public void testGetPropertyAsFloatWithDefaultPropertyBad() {
537: float result = PropertiesManager.getPropertyAsFloat(
538: "wombatProperty", (float) 4.2);
539: assertEquals(result, 4.2, 0.01);
540: }
541:
542: /**
543: * Test getPropertyAsDouble(String, double) - default specified, property present.
544: */
545: public void testGetPropertyAsDoubleWithDefault() {
546: double result = PropertiesManager.getPropertyAsDouble(
547: "testDouble", 2.22);
548: assertEquals(3.1415, result, 0.01);
549: }
550:
551: /**
552: * Test getPropertyAsDouble(String, double) - default specified, property absent.
553: */
554: public void testGetPropertyAsDoubleWithDefaultPropertyMissing() {
555: double result = PropertiesManager.getPropertyAsDouble(
556: "missingProperty", 2.22);
557: assertEquals(2.22, result, 0.01);
558: }
559:
560: /**
561: * Test getPropertyAsDouble(String, double) - default specified, property bad.
562: */
563: public void testGetPropertyAsDoubleWithDefaultPropertyBad() {
564: double result = PropertiesManager.getPropertyAsDouble(
565: "wombatProperty", 2.22);
566: assertEquals(2.22, result, 0.01);
567: }
568:
569: /**
570: * Test that the getMissingProperties() method reports missing properties.
571: */
572: public void testGetMissingProperties() {
573: int prevMissingCount = PropertiesManager.getMissingProperties()
574: .size();
575: assertEquals("defaultValue", PropertiesManager.getProperty(
576: "emphaticallyMissing", "defaultValue"));
577: assertTrue(PropertiesManager.getMissingProperties().contains(
578: "emphaticallyMissing"));
579: assertEquals(prevMissingCount + 1, PropertiesManager
580: .getMissingProperties().size());
581: }
582:
583: }
|