01: /*
02: * @(#)SysPropertiesUtil.java
03: *
04: * Copyright (C) 2002-2003 Matt Albrecht
05: * groboclown@users.sourceforge.net
06: * http://groboutils.sourceforge.net
07: *
08: * Permission is hereby granted, free of charge, to any person obtaining a
09: * copy of this software and associated documentation files (the "Software"),
10: * to deal in the Software without restriction, including without limitation
11: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12: * and/or sell copies of the Software, and to permit persons to whom the
13: * Software is furnished to do so, subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in
16: * all copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24: * DEALINGS IN THE SOFTWARE.
25: */
26:
27: package net.sourceforge.groboutils.junit.v1;
28:
29: import java.util.Properties;
30: import java.util.Vector;
31: import java.util.Enumeration;
32:
33: //import java.lang.reflect.Method;
34: //import java.lang.reflect.InvocationTargetException;
35:
36: /**
37: * Utility that allows for easy setting and reseting of System properties.
38: * Some classes that need testing may depend upon a System property setting,
39: * and this class will help testing that. This is JDK 1.1 and above
40: * compatible.
41: * <P>
42: * Each instance of this utility should be created in the <tt>setUp()</tt>
43: * method of the test case, then the utility should have its <tt>reset()</tt>
44: * method called in the <tt>tearDown()</tt> method. This ensures that the
45: * standard system properties have been set correctly.
46: *
47: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
48: * @version $Date: 2003/07/21 13:57:54 $
49: * @since December 26, 2002
50: */
51: public class SysPropertiesUtil {
52: private Properties originals;
53: private Properties newProps;
54:
55: /**
56: * Each instance should be an instance variable for a test class, and
57: * it should be created in the <tt>setUp()</tt> method.
58: */
59: public SysPropertiesUtil() {
60: this .originals = System.getProperties();
61: restoreProps();
62: }
63:
64: /**
65: * Sets the system property. If the new value is <tt>null</tt>, then
66: * the property, if it exists, will be removed. If the key is
67: * <tt>null</tt>, then an <tt>IllegalArgumentException</tt> will be thrown.
68: */
69: public void setValue(String key, String newVal) {
70: if (key == null) {
71: throw new IllegalArgumentException("No null keys.");
72: }
73: synchronized (this ) {
74: if (newVal == null) {
75: this .newProps.remove(key);
76: } else {
77: this .newProps.setProperty(key, newVal);
78: }
79: setSystemProperties(this .newProps);
80: }
81: }
82:
83: /**
84: * Resets the system properties to the way they were when this class was
85: * created.
86: */
87: public synchronized void reset() {
88: setSystemProperties(this .originals);
89: restoreProps();
90: }
91:
92: private void setSystemProperties(Properties props) {
93: System.setProperties(props);
94: }
95:
96: private void restoreProps() {
97: this .newProps = (Properties) this.originals.clone();
98: }
99: }
|