KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > SURDataModelSetup


1 /*
2  *
3  * Derby - Class SURDataModelSetup
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.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 JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31 import junit.framework.Test;
32 import java.util.Set JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Collections JavaDoc;
36
37 /**
38  * This class is a decorator for the Scrollable Updatable Resultset
39  * tests. It sets up a datamodel and populates it with data.
40  * @author Andreas Korneliussen
41  */

42 public class SURDataModelSetup extends BaseJDBCTestSetup
43 {
44     /**
45      * Constructor.
46      * @param test test to decorate with this setup
47      * @param model enumerator for which model to use.
48      * (Alternatively we could use a subclass for each model)
49      */

50     public SURDataModelSetup(Test test, SURDataModel model) {
51         super(test);
52         this.model = model;
53     }
54
55      /**
56      * Creates a datamodel for testing Scrollable Updatable ResultSets
57      * and populates the database model with data.
58      * @param model enumerator for which model to use
59      * @param con connection to database
60      * @param records number of records in the data model
61      */

62     public static void createDataModel(SURDataModel model, Connection JavaDoc con,
63                                        int records)
64         throws SQLException JavaDoc
65     {
66         Statement JavaDoc statement = con.createStatement();
67         
68         try {
69             statement.execute("drop table t1");
70         } catch (SQLException JavaDoc e) {
71             assertEquals("'drop table t1' failed with unexpected SQL State",
72                          TABLE_EXISTS_SQL_STATE, e.getSQLState());
73             
74             // The net framework does not give any valuable error code
75
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         /** Create the table */
89         statement.execute(model.getCreateTableStatement());
90         BaseTestCase.println(model.getCreateTableStatement());
91         
92         /** Create secondary index */
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         /** Populate with data */
99         PreparedStatement JavaDoc 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     /**
116      * Creates a datamodel for testing Scrollable Updatable ResultSets
117      * and populates the database model with data.
118      * The model will be set up with the number of records as defined by
119      * the recordCount attribute.
120      * @param model enumerator for which model to use
121      * @param con connection to database
122      */

123     public static void createDataModel(SURDataModel model, Connection JavaDoc con)
124         throws SQLException JavaDoc
125     {
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 JavaDoc {
134         println("Setting up datamodel: " + model);
135
136         try {
137             Connection JavaDoc con = getConnection();
138             con.setAutoCommit(false);
139             createDataModel(model, con);
140         } catch (SQLException JavaDoc e) {
141             printStackTrace(e); // Print the entire stack
142
throw e;
143         }
144     }
145     
146     /**
147      * Delete the datamodel
148      */

149     protected void tearDown()
150         throws Exception JavaDoc
151     {
152         try {
153             Connection JavaDoc con = getConnection();
154             con.rollback();
155             con.createStatement().execute("drop table t1");
156             con.commit();
157         } catch (SQLException JavaDoc e) {
158             printStackTrace(e);
159         }
160         super.tearDown();
161     }
162     
163     public String JavaDoc toString() {
164         return "SURDataModel tests with model: " + model;
165     }
166
167     private final SURDataModel model;
168     final static int recordCount = 10; // Number of records in data model.
169

170     /**
171      * Enum for the layout of the data model
172      */

173     public final static class SURDataModel {
174
175         /** Model with no keys */
176         public final static SURDataModel MODEL_WITH_NO_KEYS =
177             new SURDataModel("NO_KEYS");
178         
179         /** Model with primary key */
180         public final static SURDataModel MODEL_WITH_PK =
181             new SURDataModel("PK");
182         
183         /** Model with secondary index */
184         public final static SURDataModel MODEL_WITH_SECONDARY_KEY =
185             new SURDataModel("SECONDARY_KEY");
186         
187         /** Model with primary key and secondary index */
188         public final static SURDataModel MODEL_WITH_PK_AND_SECONDARY_KEY =
189             new SURDataModel("PK_AND_SECONDARY_KEY");
190
191         /** Array with all values */
192         private final static Set JavaDoc values = Collections.unmodifiableSet
193             (new HashSet JavaDoc((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         /**
201          * Returns an unmodifyable set of all valid data models
202          */

203         public final static Set JavaDoc values() {
204             return values;
205         }
206        
207
208         /** Returns true if this model has primary key */
209         public boolean hasPrimaryKey() {
210             return (this==MODEL_WITH_PK ||
211                     this==MODEL_WITH_PK_AND_SECONDARY_KEY);
212         }
213         
214         /** Returns true if this model has a secondary key */
215         public boolean hasSecondaryKey() {
216             return (this==MODEL_WITH_SECONDARY_KEY ||
217                     this==MODEL_WITH_PK_AND_SECONDARY_KEY);
218         }
219
220         /**
221          * Returns the string for creating the table
222          */

223         public String JavaDoc 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         /**
230          * Returns a string representation of the model
231          * @return string representation of this object
232          */

233         public String JavaDoc toString() {
234             return name;
235         }
236         
237         /**
238          * Constructor
239          */

240         private SURDataModel(String JavaDoc name) {
241             this.name = name;
242         }
243         
244         
245         
246         private final String JavaDoc name;
247     }
248
249     /**
250      * Prints the stack trace. If run in the harness, the
251      * harness will mark the test as failed if this method
252      * has been called.
253      */

254     static void printStackTrace(Throwable JavaDoc t) {
255         BaseJDBCTestCase.printStackTrace(t);
256     }
257     
258     /**
259      * Error codes and SQL state
260      */

261     private final static String JavaDoc 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