File: SystemPropertyTestSetup.java

package info (click to toggle)
derby 10.14.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,740 kB
  • sloc: java: 691,931; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 46
file content (192 lines) | stat: -rw-r--r-- 6,593 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 *
 * Derby - Class org.apache.derbyTesting.functionTests.util.SystemPropertyTestSetup
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an 
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific 
 * language governing permissions and limitations under the License.
 */
package org.apache.derbyTesting.junit;

import java.security.PrivilegedActionException;
import java.util.Enumeration;
import java.util.Properties;


import junit.extensions.TestSetup;
import junit.framework.Test;

/**
 * Test decorator to set a set of system properties on setUp
 * and restore them to the previous values on tearDown.
 *
 */
public class SystemPropertyTestSetup extends TestSetup {
	
	protected Properties newValues;
	private Properties oldValues;
	private boolean staticProperties;
	
	/**
	 * Create a test decorator that sets and restores the passed
	 * in properties. Assumption is that the contents of
	 * properties and values will not change during execution.
	 * @param test test to be decorated
	 * @param newValues properties to be set
	 */
	public SystemPropertyTestSetup(Test test,
			Properties newValues,
			boolean staticProperties)
	{
		super(test);
		this.newValues = newValues;
		this.staticProperties = staticProperties;
	}

	/**
	 * Create a test decorator that sets and restores 
	 * System properties.  Do not shutdown engine after
	 * setting properties
	 * @param test
	 * @param newValues
	 */
	public SystemPropertyTestSetup(Test test,
			Properties newValues)
	{
		super(test);
		this.newValues = newValues;
		this.staticProperties = false;
	}

    /**
     * Decorate a test so that it sets a single system property in
     * {@code setUp()} and resets it in {@code tearDown()}. The engine is
     * not shut down after the property is set.
     */
    public static Test singleProperty(Test test, String property, String value)
    {
        return singleProperty(test, property, value, false);
    }

    /**
     * Decorate a test so that it sets a single system property in
     * {@code setUp()} and resets it in {@code tearDown()}. The engine is
     * shut down after the property is set if {@code staticProperty} is
     * {@code true}.
     */
    public static Test singleProperty(Test test, String property, String value,
            boolean staticProperty)
    {
        Properties properties = new Properties();
        properties.setProperty(property, value);
        return new SystemPropertyTestSetup(test, properties, staticProperty);
    }

	/**
	 * For each property store the current value and
	 * replace it with the new value, unless there is no change.
	 */
    protected void setUp()
    throws java.lang.Exception
    {
    	//DERBY-5663 Getting NPE when trying to set 
    	// derby.language.logStatementText property to true inside a junit 
    	// suite.
    	//The same instance of SystemPropertyTestSetup can be used again
    	// and hence we want to make sure that oldValues is not null as set
    	// in the tearDown() method. If we leave it null, we will run into NPE
    	// during the tearDown of SystemPropertyTestSetup during the 
    	// decorator's reuse.
		this.oldValues = new Properties();

        // Shutdown engine so static properties take effect.
        // Shutdown the engine before setting the properties. This
        // is because the properties may change authentication settings
        // to NATIVE authentication and we may be missing a credentials DB.
        if (staticProperties) {
            // Use deregister == false here lest in client server mode
            // we try to connect to a network server without an embedded
            // driver registered. Issue seen with ConstraintCharacteristicsTest.
            TestConfiguration.getCurrent().shutdownEngine(false);
        }
        
    	setProperties(newValues);
    }

    /**
     * Revert the properties to their values prior to the setUp call.
     */
    protected void tearDown()
    throws java.lang.Exception
    {
        // Shut down the engine to restore any static properties. Do that
        // before the properties are reset to their old values, since the
        // engine shutdown may rely on some of the system properties. For
        // example, the system properties could contain the user database
        // (in derby.user.* style properties), and clearing those first
        // would lead to "invalid authentication" errors when attempting
        // to shut down the engine.
        try {
            if (staticProperties) {
                TestConfiguration.getCurrent().shutdownEngine();
            }
        } finally {
            restoreOldPropertyValues();
            oldValues = null;
        }
    }

    private void restoreOldPropertyValues() throws Exception {
    	// Clear all the system properties set by the new set
    	// that will not be reset by the old set.
       	for (Enumeration e = newValues.propertyNames(); e.hasMoreElements();)
       	{
       		String key = (String) e.nextElement();
       		if (oldValues.getProperty(key) == null)
       		    BaseTestCase.removeSystemProperty(key);
       	}
    	// and then reset nay old values
    	setProperties(oldValues);
    }

    private void setProperties(Properties values)
        throws PrivilegedActionException
    {
    	for (Enumeration e = values.propertyNames(); e.hasMoreElements();)
    	{
    		String key = (String) e.nextElement();
    		String value = values.getProperty(key);
    		String old = BaseTestCase.getSystemProperty(key);
    		
    		boolean change;
    		if (old != null)
    		{
                // set, might need to be changed.
                change = !old.equals(value);
                
                //Reference equality is ok here.
    			if (values != oldValues)
    			   oldValues.setProperty(key, old);
    		}
    		else {
    			// notset, needs to be set
    			change = true;
    		}
    		
    		if (change) {
    			BaseTestCase.setSystemProperty(key, value);
    		}
    	}
    }
}