You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2006/07/28 00:58:13 UTC

svn commit: r426310 - /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SystemPropertyTestSetup.java

Author: djd
Date: Thu Jul 27 15:58:12 2006
New Revision: 426310

URL: http://svn.apache.org/viewvc?rev=426310&view=rev
Log: (empty)

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SystemPropertyTestSetup.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SystemPropertyTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SystemPropertyTestSetup.java?rev=426310&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SystemPropertyTestSetup.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SystemPropertyTestSetup.java Thu Jul 27 15:58:12 2006
@@ -0,0 +1,108 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.functionTests.util.SystemPropertyTestSetup
+ *
+ * Copyright 2006 The Apache Software Foundation or its 
+ * licensors, as applicable.
+ *
+ * Licensed 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.functionTests.util;
+
+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 {
+	
+	private final Properties newValues;
+	private final Properties oldValues;
+	
+	/**
+	 * 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)
+	{
+		super(test);
+		this.newValues = newValues;
+		this.oldValues = new Properties();
+	}
+
+	/**
+	 * 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
+    {
+    	setProperties(newValues);
+    }
+
+    /**
+     * Revert the properties to their values prior to the setUp call.
+     */
+    protected void tearDown()
+    throws java.lang.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.
+    			if (change = !old.equals(value))
+    			   oldValues.setProperty(key, old);
+    		}
+    		else {
+    			// notset, needs to be set
+    			change = true;
+    		}
+    		
+    		if (change) {
+    			BaseTestCase.setSystemProperty(key, value);
+    		}
+    	}
+    }
+}