KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > junit > SystemPropertyTestSetup


1 /*
2  *
3  * Derby - Class org.apache.derbyTesting.functionTests.util.SystemPropertyTestSetup
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.junit;
21
22 import java.security.PrivilegedActionException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Properties JavaDoc;
25
26
27 import junit.extensions.TestSetup;
28 import junit.framework.Test;
29
30 /**
31  * Test decorator to set a set of system properties on setUp
32  * and restore them to the previous values on tearDown.
33  *
34  */

35 public class SystemPropertyTestSetup extends TestSetup {
36     
37     private Properties JavaDoc newValues;
38     private Properties JavaDoc oldValues;
39     
40     /**
41      * Create a test decorator that sets and restores the passed
42      * in properties. Assumption is that the contents of
43      * properties and values will not change during execution.
44      * @param test test to be decorated
45      * @param newValues properties to be set
46      */

47     public SystemPropertyTestSetup(Test test,
48             Properties JavaDoc newValues)
49     {
50         super(test);
51         this.newValues = newValues;
52         this.oldValues = new Properties JavaDoc();
53     }
54
55     /**
56      * For each property store the current value and
57      * replace it with the new value, unless there is no change.
58      */

59     protected void setUp()
60     throws java.lang.Exception JavaDoc
61     {
62         setProperties(newValues);
63     }
64
65     /**
66      * Revert the properties to their values prior to the setUp call.
67      */

68     protected void tearDown()
69     throws java.lang.Exception JavaDoc
70     {
71         // Clear all the system properties set by the new set
72
// that will not be reset by the old set.
73
for (Enumeration JavaDoc e = newValues.propertyNames(); e.hasMoreElements();)
74         {
75             String JavaDoc key = (String JavaDoc) e.nextElement();
76             if (oldValues.getProperty(key) == null)
77                 BaseTestCase.removeSystemProperty(key);
78         }
79         // and then reset nay old values
80
setProperties(oldValues);
81         newValues = null;
82         oldValues = null;
83     }
84     
85     private void setProperties(Properties JavaDoc values)
86         throws PrivilegedActionException JavaDoc
87     {
88         for (Enumeration JavaDoc e = values.propertyNames(); e.hasMoreElements();)
89         {
90             String JavaDoc key = (String JavaDoc) e.nextElement();
91             String JavaDoc value = values.getProperty(key);
92             String JavaDoc old = BaseTestCase.getSystemProperty(key);
93             
94             boolean change;
95             if (old != null)
96             {
97                 // set, might need to be changed.
98
change = !old.equals(value);
99                 
100                 // If we are not processing the oldValues
101
// then store in the oldValues. Reference equality is ok here.
102
if (change && (values != oldValues))
103                    oldValues.setProperty(key, old);
104             }
105             else {
106                 // notset, needs to be set
107
change = true;
108             }
109             
110             if (change) {
111                 BaseTestCase.setSystemProperty(key, value);
112             }
113         }
114     }
115 }
116
Popular Tags