001: /*
002: * <copyright>
003: * Copyright 1999-2005 Cougaar Software, Inc.
004: * under sponsorship of the Defense Advanced Research Projects
005: * Agency (DARPA).
006: *
007: * You can redistribute this software and/or modify it under the
008: * terms of the Cougaar Open Source License as published on the
009: * Cougaar Open Source Website (www.cougaar.org).
010: *
011: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
012: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
013: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
014: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
015: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
016: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
017: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
018: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
019: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
020: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
021: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022: *
023: * </copyright>
024: */
025: package org.cougaar.bootstrap;
026:
027: import java.util.Properties;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * @author srosset
033: */
034: public class SystemPropertiesTest extends TestCase {
035:
036: /*
037: * A set of properties Before Expansion.
038: */
039: private static final String BE_A = "c:\\cougaar";
040: private static final String BE_A_SUBA = "bob";
041: private static final String BE_B = "${a} ${a.${c}}/bar";
042: private static final String BE_C = "subA";
043: private static final String BE_D = "\\$\\{a\\} ${a.${c}}/bar";
044: private static final String BE_E = "\\\\$\\{a\\} ${a.${c}}/bar";
045: private static final String BE_V = "${b}";
046: private static final String BE_W = "${a.${abc}}";
047: private static final String BE_X = "${y}";
048: private static final String BE_Y = "${z}";
049: private static final String BE_Z = "${x}";
050:
051: /*
052: * A set of properties After Expansion.
053: */
054: private static final String AE_B = BE_A + " " + BE_A_SUBA + "/bar";
055: private static final String AE_D = "${a} " + BE_A_SUBA + "/bar";
056: private static final String AE_E = "\\${a} " + BE_A_SUBA + "/bar";
057:
058: public void testPropertyExpansion() {
059: System.setProperties(new Properties());
060:
061: System.setProperty("a", BE_A);
062: System.setProperty("a.subA", BE_A_SUBA);
063: System.setProperty("b", BE_B);
064: System.setProperty("c", BE_C);
065: System.setProperty("d", BE_D);
066: System.setProperty("e", BE_E);
067: // Test forward references
068: System.setProperty("V", BE_V);
069:
070: // Expand properties
071: try {
072: SystemProperties.expandProperties();
073: } catch (Exception e) {
074: fail("Unexpected property after expansion: " + e);
075: }
076:
077: checkProperty("a", BE_A);
078: checkProperty("b", AE_B);
079: checkProperty("c", BE_C);
080: checkProperty("d", AE_D);
081: checkProperty("e", AE_E);
082: checkProperty("V", AE_B);
083: }
084:
085: /**
086: * Test unresolved references
087: */
088: public void testUnresolvedReferences() {
089: System.setProperties(new Properties());
090:
091: System.setProperty("x", BE_X);
092: System.setProperty("y", BE_Y);
093:
094: // Expand properties
095: try {
096: // This should throw an exception
097: SystemProperties.expandProperties();
098: fail("Did not detect unresolved references");
099: } catch (IllegalArgumentException e) {
100: // Nothing to do. This is what we expect in the test.
101: } catch (Exception e) {
102: fail("Unexpected exception: " + e);
103: }
104:
105: System.setProperty("W", BE_W);
106: // Expand properties
107: try {
108: // This should throw an exception
109: SystemProperties.expandProperties();
110: fail("Did not detect unresolved references");
111: } catch (IllegalArgumentException e) {
112: // Nothing to do. This is what we expect in the test.
113: } catch (Exception e) {
114: fail("Unexpected exception: " + e);
115: }
116: }
117:
118: /**
119: * Test circular references
120: */
121: public void testCircularExpansion() {
122: System.setProperties(new Properties());
123: System.setProperty("x", BE_X);
124: System.setProperty("y", BE_Y);
125: System.setProperty("z", BE_Z);
126:
127: // Expand properties
128: try {
129: // This should throw an exception
130: SystemProperties.expandProperties();
131: fail("Did not detect circular reference");
132: } catch (IllegalArgumentException e) {
133: // Nothing to do. This is what we expect in the test.
134: } catch (Exception e) {
135: fail("Unexpected exception: " + e);
136: }
137: }
138:
139: /**
140: * Check the value of a System property after expansion.
141: * @param propertyName The name of a System property.
142: * @param expectedValue The expected value of the named property.
143: */
144: private void checkProperty(String propertyName, String expectedValue) {
145: String value = System.getProperty(propertyName);
146: if (expectedValue == null) {
147: assertNull(value);
148: } else {
149: assertEquals("Property " + propertyName + ": Expected "
150: + expectedValue + " but was " + value,
151: expectedValue, value);
152: }
153: }
154: }
|