1 20 package org.apache.derbyTesting.junit; 21 22 import java.sql.CallableStatement ; 23 import java.sql.Connection ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.util.Enumeration ; 28 import java.util.Properties ; 29 30 import junit.framework.Test; 31 32 import org.apache.derbyTesting.functionTests.util.SQLStateConstants; 33 34 39 public class DatabasePropertyTestSetup extends BaseJDBCTestSetup { 40 41 private Properties newValues; 42 private Properties oldValues; 43 44 51 public DatabasePropertyTestSetup(Test test, 52 Properties newValues) 53 { 54 super(test); 55 this.newValues = newValues; 56 this.oldValues = new Properties (); 57 } 58 59 63 protected void setUp() 64 throws java.lang.Exception  65 { 66 setProperties(newValues); 67 } 68 69 72 protected void tearDown() 73 throws java.lang.Exception  74 { 75 Connection conn = getConnection(); 76 conn.setAutoCommit(false); 77 CallableStatement setDBP = conn.prepareCall( 78 "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, NULL)"); 79 try { 83 for (Enumeration e = newValues.propertyNames(); e.hasMoreElements();) 84 { 85 String key = (String ) e.nextElement(); 86 if (oldValues.getProperty(key) == null) 87 { 88 setDBP.setString(1, key); 89 setDBP.executeUpdate(); 90 } 91 } 92 } catch (SQLException sqle) { 93 if(!sqle.getSQLState().equals(SQLStateConstants.PROPERTY_UNSUPPORTED_CHANGE)) 94 throw sqle; 95 } 96 setProperties(oldValues); 98 super.tearDown(); 99 newValues = null; 100 oldValues = null; 101 } 102 103 private void setProperties(Properties values) throws SQLException  104 { 105 Connection conn = getConnection(); 106 conn.setAutoCommit(false); 107 108 PreparedStatement getDBP = conn.prepareStatement( 109 "VALUES SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY(?)"); 110 CallableStatement setDBP = conn.prepareCall( 111 "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)"); 112 113 114 for (Enumeration e = values.propertyNames(); e.hasMoreElements();) 115 { 116 final String key = (String ) e.nextElement(); 117 final String value = values.getProperty(key); 118 119 getDBP.setString(1, key); 120 ResultSet rs = getDBP.executeQuery(); 121 rs.next(); 122 String old = rs.getString(1); 123 rs.close(); 124 125 boolean change; 126 if (old != null) 127 { 128 change = !old.equals(value); 130 131 if (change && (values != oldValues)) 134 oldValues.setProperty(key, old); 135 } 136 else { 137 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
|