1 20 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 21 import org.apache.derbyTesting.functionTests.util.TestUtil; 22 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 23 import org.apache.derbyTesting.junit.BaseJDBCTestSetup; 24 import org.apache.derbyTesting.junit.BaseTestCase; 25 import org.apache.derbyTesting.junit.TestConfiguration; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.SQLException ; 30 import java.sql.Statement ; 31 import junit.framework.Test; 32 import java.util.Set ; 33 import java.util.HashSet ; 34 import java.util.Arrays ; 35 import java.util.Collections ; 36 37 42 public class SURDataModelSetup extends BaseJDBCTestSetup 43 { 44 50 public SURDataModelSetup(Test test, SURDataModel model) { 51 super(test); 52 this.model = model; 53 } 54 55 62 public static void createDataModel(SURDataModel model, Connection con, 63 int records) 64 throws SQLException 65 { 66 Statement statement = con.createStatement(); 67 68 try { 69 statement.execute("drop table t1"); 70 } catch (SQLException e) { 71 assertEquals("'drop table t1' failed with unexpected SQL State", 72 TABLE_EXISTS_SQL_STATE, e.getSQLState()); 73 74 if (!TestConfiguration.getCurrent().getJDBCClient().isEmbedded()) { 76 77 assertEquals("'drop table t1' failed with unexpected error code", 78 NET_ERROR, e.getErrorCode()); 79 } else { 80 assertEquals("'drop table t1' failed with unexpected error code", 81 TABLE_EXISTS_ERRORCODE, e.getErrorCode()); 82 } 83 84 }; 85 86 87 88 89 statement.execute(model.getCreateTableStatement()); 90 BaseTestCase.println(model.getCreateTableStatement()); 91 92 93 if (model.hasSecondaryKey()) { 94 statement.execute("create index a_on_t on t1(a)"); 95 BaseTestCase.println("create index a_on_t on t1(a)"); 96 } 97 98 99 PreparedStatement ps = con. 100 prepareStatement("insert into t1 values (?,?,?,?)"); 101 102 for (int i=0; i<records; i++) { 103 ps.setInt(1, i); 104 ps.setInt(2, i); 105 ps.setInt(3, i*2 + 17); 106 ps.setString(4, "Tuple " +i); 107 ps.addBatch(); 108 } 109 ps.executeBatch(); 110 ps.close(); 111 statement.close(); 112 con.commit(); 113 } 114 115 123 public static void createDataModel(SURDataModel model, Connection con) 124 throws SQLException 125 { 126 createDataModel(model, con, recordCount); 127 } 128 129 133 protected void setUp() throws Exception { 134 println("Setting up datamodel: " + model); 135 136 try { 137 Connection con = getConnection(); 138 con.setAutoCommit(false); 139 createDataModel(model, con); 140 } catch (SQLException e) { 141 printStackTrace(e); throw e; 143 } 144 } 145 146 149 protected void tearDown() 150 throws Exception 151 { 152 try { 153 Connection con = getConnection(); 154 con.rollback(); 155 con.createStatement().execute("drop table t1"); 156 con.commit(); 157 } catch (SQLException e) { 158 printStackTrace(e); 159 } 160 super.tearDown(); 161 } 162 163 public String toString() { 164 return "SURDataModel tests with model: " + model; 165 } 166 167 private final SURDataModel model; 168 final static int recordCount = 10; 170 173 public final static class SURDataModel { 174 175 176 public final static SURDataModel MODEL_WITH_NO_KEYS = 177 new SURDataModel("NO_KEYS"); 178 179 180 public final static SURDataModel MODEL_WITH_PK = 181 new SURDataModel("PK"); 182 183 184 public final static SURDataModel MODEL_WITH_SECONDARY_KEY = 185 new SURDataModel("SECONDARY_KEY"); 186 187 188 public final static SURDataModel MODEL_WITH_PK_AND_SECONDARY_KEY = 189 new SURDataModel("PK_AND_SECONDARY_KEY"); 190 191 192 private final static Set values = Collections.unmodifiableSet 193 (new HashSet ((Arrays.asList(new SURDataModel[] { 194 MODEL_WITH_NO_KEYS, 195 MODEL_WITH_PK, 196 MODEL_WITH_SECONDARY_KEY, 197 MODEL_WITH_PK_AND_SECONDARY_KEY 198 })))); 199 200 203 public final static Set values() { 204 return values; 205 } 206 207 208 209 public boolean hasPrimaryKey() { 210 return (this==MODEL_WITH_PK || 211 this==MODEL_WITH_PK_AND_SECONDARY_KEY); 212 } 213 214 215 public boolean hasSecondaryKey() { 216 return (this==MODEL_WITH_SECONDARY_KEY || 217 this==MODEL_WITH_PK_AND_SECONDARY_KEY); 218 } 219 220 223 public String getCreateTableStatement() { 224 return hasPrimaryKey() 225 ? "create table t1 (id int primary key, a int, b int, c varchar(5000))" 226 : "create table t1 (id int, a int, b int, c varchar(5000))"; 227 } 228 229 233 public String toString() { 234 return name; 235 } 236 237 240 private SURDataModel(String name) { 241 this.name = name; 242 } 243 244 245 246 private final String name; 247 } 248 249 254 static void printStackTrace(Throwable t) { 255 BaseJDBCTestCase.printStackTrace(t); 256 } 257 258 261 private final static String TABLE_EXISTS_SQL_STATE = "42Y55"; 262 private final static int TABLE_EXISTS_ERRORCODE = 20000; 263 private final static int NET_ERROR = -1; 264 } 265 | Popular Tags |