KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > SysPropertiesUtil


1 /*
2  * @(#)SysPropertiesUtil.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * 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 JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.Enumeration JavaDoc;
32
33 //import java.lang.reflect.Method;
34
//import java.lang.reflect.InvocationTargetException;
35

36
37 /**
38  * Utility that allows for easy setting and reseting of System properties.
39  * Some classes that need testing may depend upon a System property setting,
40  * and this class will help testing that. This is JDK 1.1 and above
41  * compatible.
42  * <P>
43  * Each instance of this utility should be created in the <tt>setUp()</tt>
44  * method of the test case, then the utility should have its <tt>reset()</tt>
45  * method called in the <tt>tearDown()</tt> method. This ensures that the
46  * standard system properties have been set correctly.
47  *
48  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
49  * @version $Date: 2003/07/21 13:57:54 $
50  * @since December 26, 2002
51  */

52 public class SysPropertiesUtil
53 {
54     private Properties JavaDoc originals;
55     private Properties JavaDoc newProps;
56     
57     
58     /**
59      * Each instance should be an instance variable for a test class, and
60      * it should be created in the <tt>setUp()</tt> method.
61      */

62     public SysPropertiesUtil()
63     {
64         this.originals = System.getProperties();
65         restoreProps();
66     }
67     
68     
69     /**
70      * Sets the system property. If the new value is <tt>null</tt>, then
71      * the property, if it exists, will be removed. If the key is
72      * <tt>null</tt>, then an <tt>IllegalArgumentException</tt> will be thrown.
73      */

74     public void setValue( String JavaDoc key, String JavaDoc newVal )
75     {
76         if (key == null)
77         {
78             throw new IllegalArgumentException JavaDoc( "No null keys." );
79         }
80         synchronized( this )
81         {
82             if (newVal == null)
83             {
84                 this.newProps.remove( key );
85             }
86             else
87             {
88                 this.newProps.setProperty( key, newVal );
89             }
90             setSystemProperties( this.newProps );
91         }
92     }
93     
94     
95     /**
96      * Resets the system properties to the way they were when this class was
97      * created.
98      */

99     public synchronized void reset()
100     {
101         setSystemProperties( this.originals );
102         restoreProps();
103     }
104     
105     
106     private void setSystemProperties( Properties JavaDoc props )
107     {
108         System.setProperties( props );
109     }
110     
111     
112     private void restoreProps()
113     {
114         this.newProps = (Properties JavaDoc)this.originals.clone();
115     }
116 }
117
118
Popular Tags