001: /*
002: *
003: * Derby - Class org.apache.derbyTesting.functionTests.util.DatabasePropertyTestSetup
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.sql.CallableStatement;
023: import java.sql.Connection;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.util.Enumeration;
028: import java.util.Properties;
029:
030: import junit.framework.Test;
031:
032: import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
033:
034: /**
035: * Test decorator to set a set of database properties on setUp
036: * and restore them to the previous values on tearDown.
037: *
038: */
039: public class DatabasePropertyTestSetup extends BaseJDBCTestSetup {
040:
041: private Properties newValues;
042: private Properties oldValues;
043:
044: /**
045: * Create a test decorator that sets and restores the passed
046: * in properties. Assumption is that the contents of
047: * properties and values will not change during execution.
048: * @param test test to be decorated
049: * @param newValues properties to be set
050: */
051: public DatabasePropertyTestSetup(Test test, Properties newValues) {
052: super (test);
053: this .newValues = newValues;
054: this .oldValues = new Properties();
055: }
056:
057: /**
058: * For each property store the current value and
059: * replace it with the new value, unless there is no change.
060: */
061: protected void setUp() throws java.lang.Exception {
062: setProperties(newValues);
063: }
064:
065: /**
066: * Revert the properties to their values prior to the setUp call.
067: */
068: protected void tearDown() throws java.lang.Exception {
069: Connection conn = getConnection();
070: conn.setAutoCommit(false);
071: CallableStatement setDBP = conn
072: .prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, NULL)");
073: // Clear all the system properties set by the new set
074: // that will not be reset by the old set. Ignore any
075: // invalid property values.
076: try {
077: for (Enumeration e = newValues.propertyNames(); e
078: .hasMoreElements();) {
079: String key = (String) e.nextElement();
080: if (oldValues.getProperty(key) == null) {
081: setDBP.setString(1, key);
082: setDBP.executeUpdate();
083: }
084: }
085: } catch (SQLException sqle) {
086: if (!sqle.getSQLState().equals(
087: SQLStateConstants.PROPERTY_UNSUPPORTED_CHANGE))
088: throw sqle;
089: }
090: // and then reset nay old values which will cause the commit.
091: setProperties(oldValues);
092: super .tearDown();
093: newValues = null;
094: oldValues = null;
095: }
096:
097: private void setProperties(Properties values) throws SQLException {
098: Connection conn = getConnection();
099: conn.setAutoCommit(false);
100:
101: PreparedStatement getDBP = conn
102: .prepareStatement("VALUES SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY(?)");
103: CallableStatement setDBP = conn
104: .prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
105:
106: for (Enumeration e = values.propertyNames(); e
107: .hasMoreElements();) {
108: final String key = (String) e.nextElement();
109: final String value = values.getProperty(key);
110:
111: getDBP.setString(1, key);
112: ResultSet rs = getDBP.executeQuery();
113: rs.next();
114: String old = rs.getString(1);
115: rs.close();
116:
117: boolean change;
118: if (old != null) {
119: // set, might need to be changed.
120: change = !old.equals(value);
121:
122: // If we are not processing the oldValues
123: // then store in the oldValues. Reference equality is ok here.
124: if (change && (values != oldValues))
125: oldValues.setProperty(key, old);
126: } else {
127: // notset, needs to be set
128: change = true;
129: }
130:
131: if (change) {
132: setDBP.setString(1, key);
133: setDBP.setString(2, value);
134: setDBP.executeUpdate();
135: }
136: }
137: conn.commit();
138: getDBP.close();
139: setDBP.close();
140: conn.close();
141: }
142: }
|