KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > junit > CleanDatabaseTestSetup


1 /*
2  *
3  * Derby - Class org.apache.derbyTesting.functionTests.util.CleanDatabase
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.junit;
21
22 import java.sql.*;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.derbyTesting.functionTests.tests.lang.TimeHandlingTest;
28
29
30 import junit.framework.Test;
31
32 /**
33  * Test decorator that cleans a database on setUp and
34  * tearDown to provide a test with a consistent empty
35  * database as a starting point.
36  * <P>
37  * Tests can extend to provide a decorator that defines
38  * some schema items and then have CleanDatabaseTestSetup
39  * automatically clean them up by implementing the decorateSQL method..
40  * As an example:
41  * <code>
42         return new CleanDatabaseTestSetup(suite) {
43             protected void decorateSQL(Statement s) throws SQLException {
44
45                 s.execute("CREATE TABLE T (I INT)");
46                 s.execute("CREATE INDEX TI ON T(I)")
47
48             }
49         };
50  * </code>
51  *
52  */

53 public class CleanDatabaseTestSetup extends BaseJDBCTestSetup {
54
55     /**
56      * Decorator this test with the cleaner
57      */

58     public CleanDatabaseTestSetup(Test test) {
59         super(test);
60     }
61
62     /**
63      * Clean the default database using the default connection
64      * and calls the decorateSQL to allow sub-classes to
65      * initialize their schema requirments.
66      */

67     protected void setUp() throws Exception JavaDoc {
68         Connection conn = getConnection();
69         conn.setAutoCommit(false);
70         CleanDatabaseTestSetup.cleanDatabase(conn);
71         
72         Statement s = conn.createStatement();
73         decorateSQL(s);
74
75         s.close();
76         conn.commit();
77         conn.close();
78     }
79     
80     /**
81      * Sub-classes can override this method to execute
82      * SQL statements executed at setUp time once the
83      * database has been cleaned.
84      * Once this method returns the statement will be closed,
85      * commit called and the connection closed. The connection
86      * returned by s.getConnection() is the default connection
87      * and is in auto-commit false mode.
88      * <BR>
89      * This implementation does nothing. Sub-classes need not call it.
90      * @throws SQLException
91      */

92     protected void decorateSQL(Statement s) throws SQLException
93     {
94         // nothing in the default case.
95
}
96
97     /**
98      * Clean the default database using the default connection.
99      */

100     protected void tearDown() throws Exception JavaDoc {
101         Connection conn = getConnection();
102         conn.setAutoCommit(false);
103         CleanDatabaseTestSetup.cleanDatabase(conn);
104         super.tearDown();
105     }
106
107     /**
108      * Clean a complete database
109      * @param conn Connection to be used, must not be in auto-commit mode.
110      * @throws SQLException database error
111      */

112      public static void cleanDatabase(Connection conn) throws SQLException {
113         DatabaseMetaData dmd = conn.getMetaData();
114
115         // Fetch all the user schemas into a list
116
List JavaDoc schemas = new ArrayList JavaDoc();
117         ResultSet rs = dmd.getSchemas();
118         while (rs.next()) {
119
120             String JavaDoc schema = rs.getString("TABLE_SCHEM");
121             if (schema.startsWith("SYS"))
122                 continue;
123             if (schema.equals("SQLJ"))
124                 continue;
125             if (schema.equals("NULLID"))
126                 continue;
127
128             schemas.add(schema);
129         }
130         rs.close();
131
132         // DROP all the user schemas.
133
for (Iterator JavaDoc i = schemas.iterator(); i.hasNext();) {
134             String JavaDoc schema = (String JavaDoc) i.next();
135             JDBC.dropSchema(dmd, schema);
136         }
137     }
138
139 }
140
Free Books   Free Magazines  
Popular Tags