KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > store > BaseTest


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.procedure
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.store;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25
26 import org.apache.derby.tools.ij;
27
28 import java.sql.Connection JavaDoc;
29 import java.sql.Statement JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33
34
35
36 /**
37 Common utility functions that can be shared across store .java tests.
38 <p>
39 If more than one store tests wants a function, put it here rather than copy
40 it. Hopefully going forward, with enough utility functions adding new store
41 tests will be easier. New store tests should extend this test to pick
42 up access to utility routines - see OnlineCompressTest.java as an example.
43
44 **/

45 public abstract class BaseTest
46 {
47     private static boolean debug_system_procedures_created = false;
48     protected static boolean verbose = false;
49
50     abstract public void testList(Connection JavaDoc conn) throws SQLException JavaDoc;
51
52     void runTests(String JavaDoc[] argv)
53         throws Throwable JavaDoc
54     {
55         ij.getPropertyArg(argv);
56         Connection JavaDoc conn = ij.startJBMS();
57         System.out.println("conn from ij.startJBMS() = " + conn);
58         conn.setAutoCommit(false);
59
60         try
61         {
62             testList(conn);
63         }
64         catch (SQLException JavaDoc sqle)
65         {
66             org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
67                 System.out, sqle);
68             sqle.printStackTrace(System.out);
69         }
70     }
71
72     public BaseTest()
73     {
74     }
75
76     protected void beginTest(
77     Connection JavaDoc conn,
78     String JavaDoc str)
79         throws SQLException JavaDoc
80     {
81         log("Beginning test: " + str);
82         conn.commit();
83     }
84
85     protected void testProgress(
86     String JavaDoc str)
87         throws SQLException JavaDoc
88     {
89         log("Executing test: " + str);
90     }
91
92     protected void endTest(
93     Connection JavaDoc conn,
94     String JavaDoc str)
95         throws SQLException JavaDoc
96     {
97         conn.commit();
98         log("Ending test: " + str);
99     }
100
101     protected void log(String JavaDoc str)
102     {
103         System.out.println(str);
104     }
105
106     protected void logError(String JavaDoc str)
107     {
108         System.out.println("ERROR: " + str);
109     }
110
111     /**
112      * Simple wrapper to execute a sql string.
113      **/

114     public void executeQuery(
115     Connection JavaDoc conn,
116     String JavaDoc stmt_str,
117     boolean commit_query)
118         throws SQLException JavaDoc
119     {
120         Statement JavaDoc stmt = conn.createStatement();
121         stmt.executeUpdate(stmt_str);
122         stmt.close();
123         if (commit_query)
124             conn.commit();
125     }
126
127     /**
128      * Call consistency checker on the table.
129      * <p>
130      **/

131     protected boolean checkConsistency(
132     Connection JavaDoc conn,
133     String JavaDoc schemaName,
134     String JavaDoc tableName)
135         throws SQLException JavaDoc
136     {
137         Statement JavaDoc s = conn.createStatement();
138
139         ResultSet JavaDoc rs =
140             s.executeQuery(
141                 "values SYSCS_UTIL.SYSCS_CHECK_TABLE('" +
142                 schemaName + "', '" +
143                 tableName + "')");
144
145         if (!rs.next())
146         {
147             if (SanityManager.DEBUG)
148             {
149                 SanityManager.THROWASSERT("no value from values clause.");
150             }
151         }
152
153         boolean consistent = rs.getBoolean(1);
154
155         rs.close();
156
157         conn.commit();
158
159         return(consistent);
160     }
161
162     /**
163      * Call consistency checker on all the tables.
164      * <p>
165      **/

166     protected boolean checkAllConsistency(
167     Connection JavaDoc conn)
168         throws SQLException JavaDoc
169     {
170         Statement JavaDoc s = conn.createStatement();
171
172         ResultSet JavaDoc rs =
173             s.executeQuery(
174                 "select schemaname, tablename, SYSCS_UTIL.SYSCS_CHECK_TABLE(schemaname, tablename) " +
175                 "from sys.systables a, sys.sysschemas b where a.schemaid = b.schemaid");
176
177         int table_count = 0;
178
179         while (rs.next())
180         {
181             table_count++;
182             if (rs.getInt(3) != 1)
183             {
184                 System.out.println(
185                     "Bad return from consistency check of " +
186                     rs.getString(1) + "." + rs.getString(2));
187             }
188         }
189
190         if (table_count < 5)
191         {
192             // there are at least 5 system catalogs.
193
System.out.println(
194                 "Something wrong with consistency check query, found only " +
195                 table_count + " tables.");
196         }
197
198         rs.close();
199         s.close();
200
201         conn.commit();
202
203         return(true);
204     }
205
206     /**
207      * Create a system procedures to access SANE debug table routines.
208      * <p>
209      **/

210     protected void createDebugSystemProcedures(
211     Connection JavaDoc conn)
212         throws SQLException JavaDoc
213     {
214         Statement JavaDoc s = conn.createStatement();
215         s.executeUpdate(
216             "CREATE FUNCTION D_CONGLOMID_PRINT(DBNAME VARCHAR(128), CONGLOMID INT) RETURNS VARCHAR(32000) RETURNS NULL ON NULL INPUT EXTERNAL NAME 'org.apache.derby.impl.store.raw.data.D_DiagnosticUtil.diag_conglomid' LANGUAGE JAVA PARAMETER STYLE JAVA");
217         s.executeUpdate(
218             "CREATE FUNCTION DIAG_CONGLOMID(DBNAME VARCHAR(128), CONGLOMID INT) RETURNS VARCHAR(32000) RETURNS NULL ON NULL INPUT EXTERNAL NAME 'org.apache.derby.impl.store.raw.data.D_DiagnosticUtil.diag_conglomid' LANGUAGE JAVA PARAMETER STYLE JAVA");
219         s.close();
220         conn.commit();
221
222         debug_system_procedures_created = true;
223     }
224
225     /**
226      * Return string with table information.
227      * <p>
228      * Dumps summary store information about the table, also dumps extra
229      * information about individual pages into the error log file.
230      **/

231     String JavaDoc dump_table(
232     Connection JavaDoc conn,
233     String JavaDoc schemaName,
234     String JavaDoc tableName,
235     boolean commit_transaction)
236         throws SQLException JavaDoc
237     {
238         if (!debug_system_procedures_created)
239             createDebugSystemProcedures(conn);
240
241         // run the following query:
242
//
243
// select
244
// sys.systables.tablename,
245
// sys.sysconglomerates.conglomeratenumber,
246
// DIAG_CONGLOMID('wombat', conglomeratenumber)
247
// from sys.systables, sys.sysconglomerates
248
// where
249
// sys.systables.tableid = sys.sysconglomerates.tableid and
250
// sys.systables.schemaid = sys.sysconglomerates.schemaid and
251
// sys.systables.tablename = tableName;
252
//
253
// TODO - really should join with schemaName too.
254

255         PreparedStatement JavaDoc ps =
256             conn.prepareStatement(
257                 "select sys.systables.tablename, sys.sysconglomerates.conglomeratenumber, DIAG_CONGLOMID('wombat', conglomeratenumber) from sys.systables, sys.sysconglomerates where sys.systables.tableid = sys.sysconglomerates.tableid and sys.systables.schemaid = sys.sysconglomerates.schemaid and sys.systables.tablename = ?");
258         ps.setString(1, tableName);
259         ResultSet JavaDoc rs = ps.executeQuery();
260
261         if (!rs.next())
262         {
263             if (SanityManager.DEBUG)
264             {
265                 SanityManager.THROWASSERT("no value from values clause.");
266             }
267         }
268
269         String JavaDoc dump_table_info = rs.getString(3);
270
271         rs.close();
272
273         if (commit_transaction)
274             conn.commit();
275
276         return(dump_table_info);
277
278     }
279
280     /**
281      * Get lock table.
282      * <p>
283      * Returns a single string with a dump of the entire lock table.
284      * <p>
285      *
286      * @return The lock table.
287      *
288      * @param conn The connection to use.
289      * @param include_system_locks If true include non-user locks like those
290      * requested by background internal threads.
291      *
292      **/

293     protected String JavaDoc get_lock_info(
294     Connection JavaDoc conn,
295     boolean include_system_locks)
296         throws SQLException JavaDoc
297     {
298         // Run the following query to get the current locks in the system,
299
// toggling the "t.type='UserTransaction'" based on
300
// include_system_locks input:
301
//
302
// select
303
// cast(l.xid as char(8)) as xid,
304
// cast(username as char(8)) as username,
305
// cast(t.type as char(8)) as trantype,
306
// cast(l.type as char(8)) as type,
307
// cast(lockcount as char(3)) as cnt,
308
// cast(mode as char(4)) as mode,
309
// cast(tablename as char(12)) as tabname,
310
// cast(lockname as char(10)) as lockname,
311
// state,
312
// status
313
// from
314
// SYSCS_DIAG.LOCK_TABLE l
315
// right outer join SYSCS_DIAG.TRANSACTION_TABLE t
316
// on l.xid = t.xid where l.tableType <> 'S' and
317
// t.type='UserTransaction'
318
// order by
319
// tabname, type desc, mode, cnt, lockname;
320
String JavaDoc lock_query =
321             "select cast(l.xid as char(8)) as xid, cast(username as char(8)) as username, cast(t.type as char(8)) as trantype, cast(l.type as char(8)) as type, cast(lockcount as char(3)) as cnt, cast(mode as char(4)) as mode, cast(tablename as char(12)) as tabname, cast(lockname as char(10)) as lockname, state, status from SYSCS_DIAG.LOCK_TABLE l right outer join SYSCS_DIAG.LOCK_TABLE t on l.xid = t.xid where l.tableType <> 'S' ";
322         if (!include_system_locks)
323             lock_query += "and t.type='UserTransaction' ";
324         
325         lock_query += "order by tabname, type desc, mode, cnt, lockname";
326
327         PreparedStatement JavaDoc ps = conn.prepareStatement(lock_query);
328
329         ResultSet JavaDoc rs = ps.executeQuery();
330
331         String JavaDoc lock_output =
332         "xid |username|trantype|type |cnt|mode|tabname |lockname |state|status\n" +
333         "---------------------------------------------------------------------------------\n";
334         while (rs.next())
335         {
336             String JavaDoc username = rs.getString(1);
337             String JavaDoc trantype = rs.getString(2);
338             String JavaDoc type = rs.getString(3);
339             String JavaDoc lockcount = rs.getString(4);
340             String JavaDoc mode = rs.getString(5);
341             String JavaDoc tabname = rs.getString(6);
342             String JavaDoc lockname = rs.getString(7);
343             String JavaDoc state = rs.getString(8);
344             String JavaDoc status = rs.getString(9);
345
346             lock_output +=
347                 username + "|" +
348                 trantype + "|" +
349                 type + "|" +
350                 lockcount+ "|" +
351                 mode + "|" +
352                 tabname + "|" +
353                 lockname + "|" +
354                 state + "|" +
355                 status + "\n";
356         }
357
358         rs.close();
359
360         return(lock_output);
361     }
362
363     /**
364      * create given table on the input connection.
365      * <p>
366      * Takes care of dropping the table if it exists already.
367      * <p>
368      *
369      * @exception StandardException Standard exception policy.
370      **/

371     public void createTable(
372     Connection JavaDoc conn,
373     String JavaDoc tbl_name,
374     String JavaDoc create_str)
375         throws SQLException JavaDoc
376     {
377         Statement JavaDoc stmt = conn.createStatement();
378
379         // drop table, ignore table does not exist error.
380

381         try
382         {
383             stmt.executeUpdate("drop table " + tbl_name);
384         }
385         catch (Exception JavaDoc e)
386         {
387             // ignore drop table errors.
388
}
389
390         stmt.executeUpdate(create_str);
391     }
392
393     /**
394      * call the space table vti.
395      * <p>
396      * Utility test function to call the space table vti to get information
397      * about allocated and free pages. Information is passed back in an
398      * int array as follows:
399      * is_index = ret_info[0];
400      * num_alloc = ret_info[1];
401      * num_free = ret_info[2];
402      * page_size = ret_info[3];
403      * estimate_space_savings = ret_info[4];
404      * <p>
405      *
406      * @return the space information about the table.
407      *
408      * @exception StandardException Standard exception policy.
409      **/

410     protected static final int SPACE_INFO_IS_INDEX = 0;
411     protected static final int SPACE_INFO_NUM_ALLOC = 1;
412     protected static final int SPACE_INFO_NUM_FREE = 2;
413     protected static final int SPACE_INFO_NUM_UNFILLED = 3;
414     protected static final int SPACE_INFO_PAGE_SIZE = 4;
415     protected static final int SPACE_INFO_ESTIMSPACESAVING = 5;
416
417     protected static final int SPACE_INFO_NUMCOLS = 6;
418
419     protected int[] getSpaceInfo(
420     Connection JavaDoc conn,
421     String JavaDoc schemaName,
422     String JavaDoc tableName,
423     boolean commit_xact)
424         throws SQLException JavaDoc
425     {
426         String JavaDoc stmt_str =
427             "select conglomeratename, isindex, numallocatedpages, numfreepages, numunfilledpages, pagesize, estimspacesaving from new org.apache.derby.diag.SpaceTable('" +
428             tableName + "') t where isindex = 0";
429         PreparedStatement JavaDoc space_stmt = conn.prepareStatement(stmt_str);
430         ResultSet JavaDoc rs = space_stmt.executeQuery();
431
432         if (!rs.next())
433         {
434             if (SanityManager.DEBUG)
435             {
436                 SanityManager.THROWASSERT(
437                     "No rows returned from space table query on table: " +
438                     schemaName + "." + tableName);
439             }
440         }
441
442         int[] ret_info = new int[SPACE_INFO_NUMCOLS];
443         String JavaDoc conglomerate_name = rs.getString(1);
444         for (int i = 0; i < SPACE_INFO_NUMCOLS; i++)
445         {
446             ret_info[i] = rs.getInt(i + 2);
447         }
448
449         if (rs.next())
450         {
451             if (SanityManager.DEBUG)
452             {
453                 SanityManager.THROWASSERT(
454                     "More than one row returned from space query on table: " +
455                     schemaName + "." + tableName);
456             }
457         }
458
459         if (verbose)
460         {
461             System.out.println(
462                 "Space information for " + schemaName + "." + tableName + ":");
463             System.out.println(
464                 "isindex = " + ret_info[SPACE_INFO_IS_INDEX]);
465             System.out.println(
466                 "num_alloc = " + ret_info[SPACE_INFO_NUM_ALLOC]);
467             System.out.println(
468                 "num_free = " + ret_info[SPACE_INFO_NUM_FREE]);
469             System.out.println(
470                 "num_unfilled = " + ret_info[SPACE_INFO_NUM_UNFILLED]);
471             System.out.println(
472                 "page_size = " + ret_info[SPACE_INFO_PAGE_SIZE]);
473             System.out.println(
474                 "estimspacesaving = " + ret_info[SPACE_INFO_ESTIMSPACESAVING]);
475         }
476
477         rs.close();
478
479         if (commit_xact)
480             conn.commit();
481
482         return(ret_info);
483     }
484
485     /**
486      * Given output from getSpaceInfo(), return total pages in file.
487      * <p>
488      * simply the sum of allocated and free pages.
489      *
490      **/

491     protected int total_pages(int[] space_info)
492     {
493         return(space_info[SPACE_INFO_NUM_FREE] +
494                space_info[SPACE_INFO_NUM_ALLOC]);
495     }
496 }
497
Popular Tags