001: /*
002: * @(#)SysPropertiesUtilUTest.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.junit.v1;
028:
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032:
033: import java.util.Properties;
034: import java.util.Enumeration;
035:
036: /**
037: * Tests the TestRunnable class.
038: *
039: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
040: * @since March 1, 2002
041: * @version $Date: 2003/02/10 22:52:23 $
042: */
043: public class SysPropertiesUtilUTest extends TestCase {
044: //-------------------------------------------------------------------------
045: // Standard JUnit Class-specific declarations
046:
047: private static final Class THIS_CLASS = SysPropertiesUtilUTest.class;
048:
049: public SysPropertiesUtilUTest(String name) {
050: super (name);
051: }
052:
053: //-------------------------------------------------------------------------
054: // Tests
055:
056: public void testConstructor1() {
057: new SysPropertiesUtil();
058: }
059:
060: public void testSetValue1() {
061: String uk = getUniqueKey();
062: spu.setValue(uk, "b");
063: assertEquals("Did not set unique value correctly.", "b",
064: getValue(uk));
065: }
066:
067: public void testSetValue2() {
068: try {
069: spu.setValue(null, "a");
070: fail("Did not throw IllegalArgumentException for null value.");
071: } catch (IllegalArgumentException ex) {
072: // test exception
073: }
074: }
075:
076: public void testSetValue3() {
077: String uk = getUniqueKey();
078: spu.setValue(uk, null);
079: assertNull("Did not set/do nothing with unique value.",
080: getValue(uk));
081: }
082:
083: public void testSetValue4() {
084: String ek = getExistingKey();
085: String val = getValue(ek);
086: assertNotNull(
087: "The existing value in the set should NOT be null!",
088: val);
089: String newVal = val + "-not real";
090: spu.setValue(ek, newVal);
091: assertEquals("Did not set existing value correctly.", newVal,
092: getValue(ek));
093: }
094:
095: public void testSetValue5() {
096: String ek = getExistingKey();
097: spu.setValue(ek, null);
098: assertNull("Did not remove key with existing value.",
099: getValue(ek));
100: }
101:
102: public void testReset1() {
103: String ek = getExistingKey();
104: String val = getValue(ek);
105: assertNotNull(
106: "The existing value in the set should NOT be null!",
107: val);
108: String newVal = val + "-not real";
109: spu.setValue(ek, newVal);
110: spu.reset();
111: assertEquals("The existing value did not get reset.", val,
112: getValue(ek));
113: }
114:
115: public void testReset2() {
116: String ek = getExistingKey();
117: String val = getValue(ek);
118: assertNotNull(
119: "The existing value in the set should NOT be null!",
120: val);
121: spu.setValue(ek, null);
122: spu.reset();
123: assertEquals("The existing value did not get reset.", val,
124: getValue(ek));
125: }
126:
127: public void testReset3() {
128: String uk = getUniqueKey();
129: String origVal = getValue(uk);
130: assertNull("The unique key was not null, but '" + origVal
131: + "'.", origVal);
132: spu.setValue(uk, "a");
133: spu.reset();
134: String val = getValue(uk);
135: assertNull(
136: "The unique key did not get reset to null, but was set to '"
137: + val + "'.", val);
138: }
139:
140: public void testReset4() {
141: String ek = getExistingKey();
142: String val = getValue(ek);
143: String newVal = val + "-not real";
144: spu.setValue(ek, newVal);
145: spu.reset();
146: spu.setValue(ek, null);
147: spu.reset();
148: assertEquals("The existing value did not get reset.", val,
149: getValue(ek));
150: }
151:
152: public void testReset5() {
153: String ek = getExistingKey();
154: String val = getValue(ek);
155: String newVal = val + "-not real";
156: spu.setValue(ek, newVal);
157: spu.reset();
158: spu.setValue(ek, newVal + "1");
159: spu.reset();
160: assertEquals("The existing key did not get reset.", val,
161: getValue(ek));
162: }
163:
164: public void testReset6() {
165: String uk = getUniqueKey();
166: spu.setValue(uk, "a");
167: spu.reset();
168: spu.setValue(uk, "b");
169: spu.reset();
170: assertNull("The unique key did not get reset.", getValue(uk));
171: }
172:
173: public void testReset7() {
174: spu.reset();
175: }
176:
177: //-------------------------------------------------------------------------
178: // Helpers
179:
180: private String getValue(String key) {
181: return System.getProperty(key);
182: }
183:
184: private String getUniqueKey()
185: {
186: Enumeration enum = System.getProperties().keys();
187: String key = "";
188: if (enum.hasMoreElements())
189: {
190: key = (String)enum.nextElement();
191: }
192: int count = 0;
193: String uk = key;
194: while (getValue( uk ) != null)
195: {
196: ++count;
197: uk = key + count;
198: }
199: return uk;
200: }
201:
202: private String getExistingKey()
203: {
204: Enumeration enum = System.getProperties().keys();
205: if (enum.hasMoreElements())
206: {
207: return (String)enum.nextElement();
208: }
209: fail( "There are no existing system keys. Your JVM is wack." );
210: // never reachable
211: throw new IllegalStateException( "Not reachable." );
212: }
213:
214: private SysPropertiesUtil spu;
215:
216: //-------------------------------------------------------------------------
217: // Standard JUnit declarations
218:
219: public static Test suite() {
220: TestSuite suite = new TestSuite(THIS_CLASS);
221:
222: return suite;
223: }
224:
225: public static void main(String[] args) {
226: String[] name = { THIS_CLASS.getName() };
227:
228: // junit.textui.TestRunner.main( name );
229: // junit.swingui.TestRunner.main( name );
230:
231: junit.textui.TestRunner.main(name);
232: }
233:
234: /**
235: *
236: * @exception Exception thrown under any exceptional condition.
237: */
238: protected void setUp() throws Exception {
239: super .setUp();
240:
241: // set ourself up
242: this .spu = new SysPropertiesUtil();
243: }
244:
245: /**
246: *
247: * @exception Exception thrown under any exceptional condition.
248: */
249: protected void tearDown() throws Exception {
250: // tear ourself down
251: this.spu.reset();
252:
253: super.tearDown();
254: }
255: }
|