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/08/25 06:52:10 UTC

svn commit: r436653 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/jdbcapi/ConcurrencyTest.java junit/DatabasePropertyTestSetup.java junit/SystemPropertyTestSetup.java

Author: djd
Date: Thu Aug 24 21:52:09 2006
New Revision: 436653

URL: http://svn.apache.org/viewvc?rev=436653&view=rev
Log:
Add DatabasePropertyTestSetup decorator that sets and clears database properties.
Fix a bug in SystemPropertyTestSetup noticed while testing DatabasePropertyTestSetup.
Change ConcurrencyTest to use DatabasePropertyTestSetup to work around bug DERBY-1762.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java   (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ConcurrencyTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ConcurrencyTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ConcurrencyTest.java?rev=436653&r1=436652&r2=436653&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ConcurrencyTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ConcurrencyTest.java Thu Aug 24 21:52:09 2006
@@ -18,11 +18,18 @@
  * language governing permissions and limitations under the License.
  */
 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
-import junit.framework.*;
-import java.sql.*;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
 import org.apache.derbyTesting.junit.JDBC;
-import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
 
 /**
  * Testing concurrency behaviour in derby when creating the resultsets with
@@ -875,7 +882,7 @@
         final Properties properties = new Properties();
         properties.setProperty("derby.locks.waitTimeout", "4");
         
-        return new SystemPropertyTestSetup(suite, properties);
+        return new DatabasePropertyTestSetup(suite, properties);
     }
     
 }

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java?rev=436653&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java Thu Aug 24 21:52:09 2006
@@ -0,0 +1,144 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.functionTests.util.DatabasePropertyTestSetup
+ *
+ * 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.sql.CallableStatement;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Enumeration;
+import java.util.Properties;
+
+import junit.framework.Test;
+
+/**
+ * Test decorator to set a set of database properties on setUp
+ * and restore them to the previous values on tearDown.
+ *
+ */
+public class DatabasePropertyTestSetup extends BaseJDBCTestSetup {
+	
+	private Properties newValues;
+	private 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 DatabasePropertyTestSetup(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
+    {
+        Connection conn = getConnection();
+        conn.setAutoCommit(false);
+        CallableStatement setDBP =  conn.prepareCall(
+            "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, NULL)");
+    	// 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)
+            {
+                 setDBP.setString(1, key);
+                 setDBP.executeUpdate();
+             }
+        }
+    	// and then reset nay old values which will cause the commit.
+    	setProperties(oldValues);
+        super.tearDown();
+        newValues = null;
+        oldValues = null;
+    }
+    
+    private void setProperties(Properties values) throws SQLException
+    {
+        Connection conn = getConnection();
+        conn.setAutoCommit(false);
+        
+        PreparedStatement getDBP =  conn.prepareStatement(
+            "VALUES SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY(?)");
+        CallableStatement setDBP =  conn.prepareCall(
+            "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
+        
+        
+    	for (Enumeration e = values.propertyNames(); e.hasMoreElements();)
+    	{
+    		final String key = (String) e.nextElement();
+    		final String value = values.getProperty(key);
+            
+            getDBP.setString(1, key);
+            ResultSet rs = getDBP.executeQuery();
+            rs.next();
+            String old = rs.getString(1);
+            rs.close();
+                		
+    		boolean change;
+    		if (old != null)
+    		{
+                // set, might need to be changed.
+                change = !old.equals(value);
+                
+                // If we are not processing the oldValues
+                // then store in the oldValues. Reference equality is ok here.
+    			if (change && (values != oldValues))
+    			   oldValues.setProperty(key, old);
+    		}
+    		else {
+    			// notset, needs to be set
+    			change = true;
+    		}
+    		
+    		if (change) {
+                setDBP.setString(1, key);
+                setDBP.setString(2, value);
+                setDBP.executeUpdate();
+   		    }
+    	}
+        conn.commit();
+        getDBP.close();
+        setDBP.close();
+        conn.close();
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java?rev=436653&r1=436652&r2=436653&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SystemPropertyTestSetup.java Thu Aug 24 21:52:09 2006
@@ -34,8 +34,8 @@
  */
 public class SystemPropertyTestSetup extends TestSetup {
 	
-	private final Properties newValues;
-	private final Properties oldValues;
+	private Properties newValues;
+	private Properties oldValues;
 	
 	/**
 	 * Create a test decorator that sets and restores the passed
@@ -78,6 +78,8 @@
        	}
     	// and then reset nay old values
     	setProperties(oldValues);
+        newValues = null;
+        oldValues = null;
     }
     
     private void setProperties(Properties values)
@@ -92,8 +94,12 @@
     		boolean change;
     		if (old != null)
     		{
-    			// set, might need to be changed.
-    			if (change = !old.equals(value))
+                // set, might need to be changed.
+                change = !old.equals(value);
+                
+                // If we are not processing the oldValues
+                // then store in the oldValues. Reference equality is ok here.
+    			if (change && (values != oldValues))
     			   oldValues.setProperty(key, old);
     		}
     		else {