001: /*
002: *
003: * Derby - Class org.apache.derbyTesting.functionTests.util.SystemPropertyTestSetup
004: *
005: * Licensed to the Apache Software Foundation (ASF) under one or more
006: * contributor license agreements. See the NOTICE file distributed with
007: * this work for additional information regarding copyright ownership.
008: * The ASF licenses this file to You under the Apache License, Version 2.0
009: * (the "License"); you may not use this file except in compliance with
010: * the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */
020: package org.apache.derbyTesting.junit;
021:
022: import java.security.PrivilegedActionException;
023: import java.util.Enumeration;
024: import java.util.Properties;
025:
026: import junit.extensions.TestSetup;
027: import junit.framework.Test;
028:
029: /**
030: * Test decorator to set a set of system properties on setUp
031: * and restore them to the previous values on tearDown.
032: *
033: */
034: public class SystemPropertyTestSetup extends TestSetup {
035:
036: private Properties newValues;
037: private Properties oldValues;
038:
039: /**
040: * Create a test decorator that sets and restores the passed
041: * in properties. Assumption is that the contents of
042: * properties and values will not change during execution.
043: * @param test test to be decorated
044: * @param newValues properties to be set
045: */
046: public SystemPropertyTestSetup(Test test, Properties newValues) {
047: super (test);
048: this .newValues = newValues;
049: this .oldValues = new Properties();
050: }
051:
052: /**
053: * For each property store the current value and
054: * replace it with the new value, unless there is no change.
055: */
056: protected void setUp() throws java.lang.Exception {
057: setProperties(newValues);
058: }
059:
060: /**
061: * Revert the properties to their values prior to the setUp call.
062: */
063: protected void tearDown() throws java.lang.Exception {
064: // Clear all the system properties set by the new set
065: // that will not be reset by the old set.
066: for (Enumeration e = newValues.propertyNames(); e
067: .hasMoreElements();) {
068: String key = (String) e.nextElement();
069: if (oldValues.getProperty(key) == null)
070: BaseTestCase.removeSystemProperty(key);
071: }
072: // and then reset nay old values
073: setProperties(oldValues);
074: newValues = null;
075: oldValues = null;
076: }
077:
078: private void setProperties(Properties values)
079: throws PrivilegedActionException {
080: for (Enumeration e = values.propertyNames(); e
081: .hasMoreElements();) {
082: String key = (String) e.nextElement();
083: String value = values.getProperty(key);
084: String old = BaseTestCase.getSystemProperty(key);
085:
086: boolean change;
087: if (old != null) {
088: // set, might need to be changed.
089: change = !old.equals(value);
090:
091: // If we are not processing the oldValues
092: // then store in the oldValues. Reference equality is ok here.
093: if (change && (values != oldValues))
094: oldValues.setProperty(key, old);
095: } else {
096: // notset, needs to be set
097: change = true;
098: }
099:
100: if (change) {
101: BaseTestCase.setSystemProperty(key, value);
102: }
103: }
104: }
105: }
|