KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > JitTest


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.JitTest
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, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DatabaseMetaData JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import org.apache.derby.tools.ij;
31 import org.apache.derby.tools.JDBCDisplayUtil;
32
33 /**
34  * DERBY-1327
35  * Identity column can be created with wrong and very large start with
36  * value with "J2RE 1.5.0 IBM Windows 32 build pwi32dev-20060412 (SR2)"
37  * with JIT on
38  */

39 public class JitTest {
40
41   public static void main (String JavaDoc args[])
42   {
43     try {
44         /* Load the JDBC Driver class */
45         // use the ij utility to read the property file and
46
// make the initial connection.
47
ij.getPropertyArg(args);
48         Connection JavaDoc conn = ij.startJBMS();
49
50         System.out.println("Start JitTest");
51         //add tests specific to a jit issue
52
testDerby1327BadStartWithForAutoIncColumn(conn);
53         conn.close();
54     } catch (Exception JavaDoc e) {
55         System.out.println("FAIL -- unexpected exception "+e);
56         JDBCDisplayUtil.ShowException(System.out, e);
57         e.printStackTrace();
58     }
59   }
60   
61   /**
62    * After some number of table creations with JIT turned on, the START WITH
63    * value for the table being created and all the ones already created gets
64    * mysteriously changed with pwi32dev-20060412 (SR2)
65    *
66    * @throws Exception
67    */

68   public static void testDerby1327BadStartWithForAutoIncColumn(Connection JavaDoc conn)
69     throws Exception JavaDoc
70   {
71     conn.setAutoCommit(false);
72         Statement JavaDoc stmt = null;
73
74         dropAllAppTables(conn);
75         System.out.println("Create tables until we get a wrong Start with value");
76         stmt = conn.createStatement();
77
78         // numBadStartWith will be changed if any columns get a bad start with value.
79
int numBadStartWith = 0;
80         String JavaDoc createTableSQL = null;
81         try {
82             // create 200 tables. Break out if we get a table that has a bad
83
// start with value.
84
for (int i = 0; (i < 200) && (numBadStartWith == 0); i++)
85             {
86                 String JavaDoc tableName = "APP.MYTABLE" + i;
87                 createTableSQL = "CREATE TABLE " + tableName +
88                 " (ROLEID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ("+
89                 "START WITH 2, INCREMENT BY 1), INSTANCEID INTEGER, STATUS"+
90                 " INTEGER, LOGICAL_STATE INTEGER, LSTATE_TSTAMP TIMESTAMP,"+
91                 " UPDT_TSTAMP TIMESTAMP, TSTAMP TIMESTAMP,"+
92                 " CLALEVEL1_CLALEVEL2_CLALEVEL2ID VARCHAR(255), "+
93                 "CLALEVEL1_CLALEVEL2_CLALEVEL3_CLALEVEL3ID VARCHAR(255))";
94                 
95                 stmt.executeUpdate(createTableSQL);
96                 conn.commit();
97                 numBadStartWith = checkBadStartWithCols(conn,2);
98                 if (numBadStartWith > 0)
99                     break;
100             }
101         } catch (SQLException JavaDoc se)
102         {
103             System.out.println("Failed on " + createTableSQL);
104             JDBCDisplayUtil.ShowSQLException(System.out,se);
105
106         }
107         
108         if (numBadStartWith == 0)
109         {
110             System.out.println("PASS: All 200 tables created without problems");
111             dropAllAppTables(conn);
112         }
113         stmt.close();
114         conn.rollback();
115   }
116
117
118   /**
119    * Check that all tables in App do not have a an autoincrementstart value
120    * greater tan maxautoincrementstart
121    * @param conn
122    * @param maxautoincrementstart Maximum expected autoincrementstart value
123    * @return number of columns with bad autoincrementstart value
124    */

125     private static int checkBadStartWithCols(Connection JavaDoc conn, int
126             maxautoincrementstart) throws Exception JavaDoc
127     {
128         Statement JavaDoc stmt = conn.createStatement();
129         ResultSet JavaDoc rs =stmt.executeQuery("select count(autoincrementstart) from"+
130                 " sys.syscolumns c, sys.systables t, sys.sysschemas s WHERE"+
131                 " t.schemaid = s.schemaid and s.schemaname = 'APP' and"+
132                 " autoincrementstart > " + maxautoincrementstart);
133
134         rs.next();
135         int numBadStartWith = rs.getInt(1);
136         if (numBadStartWith > 0)
137             System.out.println(numBadStartWith + " columns have bad START WITH VALUE");
138         rs.close();
139         
140         if (numBadStartWith > 0)
141         {
142             rs =stmt.executeQuery("select tablename, columnname,"+
143                     " autoincrementstart from sys.syscolumns c, sys.systables t,"+
144                     " sys.sysschemas s WHERE t.schemaid = s.schemaid and"+
145                     " s.schemaname = 'APP' and autoincrementstart > 2 ORDER"+
146                     " BY tablename");
147             while (rs.next())
148             {
149                 System.out.println("Unexpected start value: " +
150                                    rs.getLong(3) +
151                                    " on column " + rs.getString(1) +
152                                    "(" + rs.getString(2) + ")");
153                 
154                 
155             }
156         }
157          return numBadStartWith;
158     }
159
160     /**
161        * Drop all tables in schema APP
162      * @param conn
163      * @throws SQLException
164      */

165     private static void dropAllAppTables(Connection JavaDoc conn) throws SQLException JavaDoc
166     {
167         Statement JavaDoc stmt1 = conn.createStatement();
168         Statement JavaDoc stmt2 = conn.createStatement();
169         System.out.println("Drop all tables in APP schema");
170         ResultSet JavaDoc rs = stmt1.executeQuery("SELECT tablename from sys.systables"+
171                 " t, sys.sysschemas s where t.schemaid = s.schemaid and"+
172                 " s.schemaname = 'APP'");
173
174         while (rs.next())
175         {
176             String JavaDoc tableName = rs.getString(1);
177             
178             try {
179                 stmt2.executeUpdate("DROP TABLE " + tableName);
180             }
181             catch (SQLException JavaDoc se)
182             {
183                 System.out.println("Error dropping table:" + tableName);
184                 se.printStackTrace();
185                 continue;
186             }
187         }
188     }
189 }
190
Popular Tags