001: /*
002: *
003: * Derby - Class SURDataModelSetup
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.functionTests.tests.jdbcapi;
021:
022: import org.apache.derbyTesting.functionTests.util.TestUtil;
023: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
024: import org.apache.derbyTesting.junit.BaseJDBCTestSetup;
025: import org.apache.derbyTesting.junit.BaseTestCase;
026: import org.apache.derbyTesting.junit.TestConfiguration;
027:
028: import java.sql.Connection;
029: import java.sql.PreparedStatement;
030: import java.sql.SQLException;
031: import java.sql.Statement;
032: import junit.framework.Test;
033: import java.util.Set;
034: import java.util.HashSet;
035: import java.util.Arrays;
036: import java.util.Collections;
037:
038: /**
039: * This class is a decorator for the Scrollable Updatable Resultset
040: * tests. It sets up a datamodel and populates it with data.
041: * @author Andreas Korneliussen
042: */
043: public class SURDataModelSetup extends BaseJDBCTestSetup {
044: /**
045: * Constructor.
046: * @param test test to decorate with this setup
047: * @param model enumerator for which model to use.
048: * (Alternatively we could use a subclass for each model)
049: */
050: public SURDataModelSetup(Test test, SURDataModel model) {
051: super (test);
052: this .model = model;
053: }
054:
055: /**
056: * Creates a datamodel for testing Scrollable Updatable ResultSets
057: * and populates the database model with data.
058: * @param model enumerator for which model to use
059: * @param con connection to database
060: * @param records number of records in the data model
061: */
062: public static void createDataModel(SURDataModel model,
063: Connection con, int records) throws SQLException {
064: Statement statement = con.createStatement();
065:
066: try {
067: statement.execute("drop table t1");
068: } catch (SQLException e) {
069: assertEquals(
070: "'drop table t1' failed with unexpected SQL State",
071: TABLE_EXISTS_SQL_STATE, e.getSQLState());
072:
073: // The net framework does not give any valuable error code
074: if (!TestConfiguration.getCurrent().getJDBCClient()
075: .isEmbedded()) {
076:
077: assertEquals(
078: "'drop table t1' failed with unexpected error code",
079: NET_ERROR, e.getErrorCode());
080: } else {
081: assertEquals(
082: "'drop table t1' failed with unexpected error code",
083: TABLE_EXISTS_ERRORCODE, e.getErrorCode());
084: }
085:
086: }
087: ;
088:
089: /** Create the table */
090: statement.execute(model.getCreateTableStatement());
091: BaseTestCase.println(model.getCreateTableStatement());
092:
093: /** Create secondary index */
094: if (model.hasSecondaryKey()) {
095: statement.execute("create index a_on_t on t1(a)");
096: BaseTestCase.println("create index a_on_t on t1(a)");
097: }
098:
099: /** Populate with data */
100: PreparedStatement ps = con
101: .prepareStatement("insert into t1 values (?,?,?,?)");
102:
103: for (int i = 0; i < records; i++) {
104: ps.setInt(1, i);
105: ps.setInt(2, i);
106: ps.setInt(3, i * 2 + 17);
107: ps.setString(4, "Tuple " + i);
108: ps.addBatch();
109: }
110: ps.executeBatch();
111: ps.close();
112: statement.close();
113: con.commit();
114: }
115:
116: /**
117: * Creates a datamodel for testing Scrollable Updatable ResultSets
118: * and populates the database model with data.
119: * The model will be set up with the number of records as defined by
120: * the recordCount attribute.
121: * @param model enumerator for which model to use
122: * @param con connection to database
123: */
124: public static void createDataModel(SURDataModel model,
125: Connection con) throws SQLException {
126: createDataModel(model, con, recordCount);
127: }
128:
129: /**
130: * Creates a datamodel for testing Scrollable Updatable ResultSets
131: * and populates the database model with data.
132: */
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); // Print the entire stack
142: throw e;
143: }
144: }
145:
146: /**
147: * Delete the datamodel
148: */
149: protected void tearDown() throws Exception {
150: try {
151: Connection con = getConnection();
152: con.rollback();
153: con.createStatement().execute("drop table t1");
154: con.commit();
155: } catch (SQLException e) {
156: printStackTrace(e);
157: }
158: super .tearDown();
159: }
160:
161: public String toString() {
162: return "SURDataModel tests with model: " + model;
163: }
164:
165: private final SURDataModel model;
166: final static int recordCount = 10; // Number of records in data model.
167:
168: /**
169: * Enum for the layout of the data model
170: */
171: public final static class SURDataModel {
172:
173: /** Model with no keys */
174: public final static SURDataModel MODEL_WITH_NO_KEYS = new SURDataModel(
175: "NO_KEYS");
176:
177: /** Model with primary key */
178: public final static SURDataModel MODEL_WITH_PK = new SURDataModel(
179: "PK");
180:
181: /** Model with secondary index */
182: public final static SURDataModel MODEL_WITH_SECONDARY_KEY = new SURDataModel(
183: "SECONDARY_KEY");
184:
185: /** Model with primary key and secondary index */
186: public final static SURDataModel MODEL_WITH_PK_AND_SECONDARY_KEY = new SURDataModel(
187: "PK_AND_SECONDARY_KEY");
188:
189: /** Array with all values */
190: private final static Set values = Collections
191: .unmodifiableSet(new HashSet((Arrays
192: .asList(new SURDataModel[] {
193: MODEL_WITH_NO_KEYS, MODEL_WITH_PK,
194: MODEL_WITH_SECONDARY_KEY,
195: MODEL_WITH_PK_AND_SECONDARY_KEY }))));
196:
197: /**
198: * Returns an unmodifyable set of all valid data models
199: */
200: public final static Set values() {
201: return values;
202: }
203:
204: /** Returns true if this model has primary key */
205: public boolean hasPrimaryKey() {
206: return (this == MODEL_WITH_PK || this == MODEL_WITH_PK_AND_SECONDARY_KEY);
207: }
208:
209: /** Returns true if this model has a secondary key */
210: public boolean hasSecondaryKey() {
211: return (this == MODEL_WITH_SECONDARY_KEY || this == MODEL_WITH_PK_AND_SECONDARY_KEY);
212: }
213:
214: /**
215: * Returns the string for creating the table
216: */
217: public String getCreateTableStatement() {
218: return hasPrimaryKey() ? "create table t1 (id int primary key, a int, b int, c varchar(5000))"
219: : "create table t1 (id int, a int, b int, c varchar(5000))";
220: }
221:
222: /**
223: * Returns a string representation of the model
224: * @return string representation of this object
225: */
226: public String toString() {
227: return name;
228: }
229:
230: /**
231: * Constructor
232: */
233: private SURDataModel(String name) {
234: this .name = name;
235: }
236:
237: private final String name;
238: }
239:
240: /**
241: * Prints the stack trace. If run in the harness, the
242: * harness will mark the test as failed if this method
243: * has been called.
244: */
245: static void printStackTrace(Throwable t) {
246: BaseJDBCTestCase.printStackTrace(t);
247: }
248:
249: /**
250: * Error codes and SQL state
251: */
252: private final static String TABLE_EXISTS_SQL_STATE = "42Y55";
253: private final static int TABLE_EXISTS_ERRORCODE = 20000;
254: private final static int NET_ERROR = -1;
255: }
|