001: /*
002: *
003: * Derby - Class org.apache.derbyTesting.functionTests.util.CleanDatabase
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.junit;
021:
022: import java.sql.*;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.derbyTesting.functionTests.tests.lang.TimeHandlingTest;
028:
029: import junit.framework.Test;
030:
031: /**
032: * Test decorator that cleans a database on setUp and
033: * tearDown to provide a test with a consistent empty
034: * database as a starting point.
035: * <P>
036: * Tests can extend to provide a decorator that defines
037: * some schema items and then have CleanDatabaseTestSetup
038: * automatically clean them up by implementing the decorateSQL method..
039: * As an example:
040: * <code>
041: return new CleanDatabaseTestSetup(suite) {
042: protected void decorateSQL(Statement s) throws SQLException {
043:
044: s.execute("CREATE TABLE T (I INT)");
045: s.execute("CREATE INDEX TI ON T(I)")
046:
047: }
048: };
049: * </code>
050: *
051: */
052: public class CleanDatabaseTestSetup extends BaseJDBCTestSetup {
053:
054: /**
055: * Decorator this test with the cleaner
056: */
057: public CleanDatabaseTestSetup(Test test) {
058: super (test);
059: }
060:
061: /**
062: * Clean the default database using the default connection
063: * and calls the decorateSQL to allow sub-classes to
064: * initialize their schema requirments.
065: */
066: protected void setUp() throws Exception {
067: Connection conn = getConnection();
068: conn.setAutoCommit(false);
069: CleanDatabaseTestSetup.cleanDatabase(conn);
070:
071: Statement s = conn.createStatement();
072: decorateSQL(s);
073:
074: s.close();
075: conn.commit();
076: conn.close();
077: }
078:
079: /**
080: * Sub-classes can override this method to execute
081: * SQL statements executed at setUp time once the
082: * database has been cleaned.
083: * Once this method returns the statement will be closed,
084: * commit called and the connection closed. The connection
085: * returned by s.getConnection() is the default connection
086: * and is in auto-commit false mode.
087: * <BR>
088: * This implementation does nothing. Sub-classes need not call it.
089: * @throws SQLException
090: */
091: protected void decorateSQL(Statement s) throws SQLException {
092: // nothing in the default case.
093: }
094:
095: /**
096: * Clean the default database using the default connection.
097: */
098: protected void tearDown() throws Exception {
099: Connection conn = getConnection();
100: conn.setAutoCommit(false);
101: CleanDatabaseTestSetup.cleanDatabase(conn);
102: super .tearDown();
103: }
104:
105: /**
106: * Clean a complete database
107: * @param conn Connection to be used, must not be in auto-commit mode.
108: * @throws SQLException database error
109: */
110: public static void cleanDatabase(Connection conn)
111: throws SQLException {
112: DatabaseMetaData dmd = conn.getMetaData();
113:
114: // Fetch all the user schemas into a list
115: List schemas = new ArrayList();
116: ResultSet rs = dmd.getSchemas();
117: while (rs.next()) {
118:
119: String schema = rs.getString("TABLE_SCHEM");
120: if (schema.startsWith("SYS"))
121: continue;
122: if (schema.equals("SQLJ"))
123: continue;
124: if (schema.equals("NULLID"))
125: continue;
126:
127: schemas.add(schema);
128: }
129: rs.close();
130:
131: // DROP all the user schemas.
132: for (Iterator i = schemas.iterator(); i.hasNext();) {
133: String schema = (String) i.next();
134: JDBC.dropSchema(dmd, schema);
135: }
136: }
137:
138: }
|