KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > junit > DatabasePropertyTestSetup


1 /*
2  *
3  * Derby - Class org.apache.derbyTesting.functionTests.util.DatabasePropertyTestSetup
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.junit;
21
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import junit.framework.Test;
31
32 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
33
34 /**
35  * Test decorator to set a set of database properties on setUp
36  * and restore them to the previous values on tearDown.
37  *
38  */

39 public class DatabasePropertyTestSetup extends BaseJDBCTestSetup {
40     
41     private Properties JavaDoc newValues;
42     private Properties JavaDoc oldValues;
43     
44     /**
45      * Create a test decorator that sets and restores the passed
46      * in properties. Assumption is that the contents of
47      * properties and values will not change during execution.
48      * @param test test to be decorated
49      * @param newValues properties to be set
50      */

51     public DatabasePropertyTestSetup(Test test,
52             Properties JavaDoc newValues)
53     {
54         super(test);
55         this.newValues = newValues;
56         this.oldValues = new Properties JavaDoc();
57     }
58
59     /**
60      * For each property store the current value and
61      * replace it with the new value, unless there is no change.
62      */

63     protected void setUp()
64     throws java.lang.Exception JavaDoc
65     {
66         setProperties(newValues);
67     }
68
69     /**
70      * Revert the properties to their values prior to the setUp call.
71      */

72     protected void tearDown()
73     throws java.lang.Exception JavaDoc
74     {
75         Connection JavaDoc conn = getConnection();
76         conn.setAutoCommit(false);
77         CallableStatement JavaDoc setDBP = conn.prepareCall(
78             "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, NULL)");
79         // Clear all the system properties set by the new set
80
// that will not be reset by the old set. Ignore any
81
// invalid property values.
82
try {
83             for (Enumeration JavaDoc e = newValues.propertyNames(); e.hasMoreElements();)
84             {
85                 String JavaDoc key = (String JavaDoc) e.nextElement();
86                 if (oldValues.getProperty(key) == null)
87                 {
88                     setDBP.setString(1, key);
89                     setDBP.executeUpdate();
90                 }
91             }
92         } catch (SQLException JavaDoc sqle) {
93             if(!sqle.getSQLState().equals(SQLStateConstants.PROPERTY_UNSUPPORTED_CHANGE))
94                 throw sqle;
95         }
96         // and then reset nay old values which will cause the commit.
97
setProperties(oldValues);
98         super.tearDown();
99         newValues = null;
100         oldValues = null;
101     }
102     
103     private void setProperties(Properties JavaDoc values) throws SQLException JavaDoc
104     {
105         Connection JavaDoc conn = getConnection();
106         conn.setAutoCommit(false);
107         
108         PreparedStatement JavaDoc getDBP = conn.prepareStatement(
109             "VALUES SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY(?)");
110         CallableStatement JavaDoc setDBP = conn.prepareCall(
111             "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
112         
113         
114         for (Enumeration JavaDoc e = values.propertyNames(); e.hasMoreElements();)
115         {
116             final String JavaDoc key = (String JavaDoc) e.nextElement();
117             final String JavaDoc value = values.getProperty(key);
118             
119             getDBP.setString(1, key);
120             ResultSet JavaDoc rs = getDBP.executeQuery();
121             rs.next();
122             String JavaDoc old = rs.getString(1);
123             rs.close();
124                         
125             boolean change;
126             if (old != null)
127             {
128                 // set, might need to be changed.
129
change = !old.equals(value);
130                 
131                 // If we are not processing the oldValues
132
// then store in the oldValues. Reference equality is ok here.
133
if (change && (values != oldValues))
134                    oldValues.setProperty(key, old);
135             }
136             else {
137                 // notset, needs to be set
138
change = true;
139             }
140             
141             if (change) {
142                 setDBP.setString(1, key);
143                 setDBP.setString(2, value);
144                 setDBP.executeUpdate();
145             }
146         }
147         conn.commit();
148         getDBP.close();
149         setDBP.close();
150         conn.close();
151     }
152 }
153
Popular Tags