01: /*
02: *
03: * Derby - Class org.apache.derbyTesting.functionTests.util.BaseJDBCTestSetup
04: *
05: * Licensed to the Apache Software Foundation (ASF) under one or more
06: * contributor license agreements. See the NOTICE file distributed with
07: * this work for additional information regarding copyright ownership.
08: * The ASF licenses this file to You under the Apache License, Version 2.0
09: * (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:
24: import junit.extensions.TestSetup;
25: import junit.framework.Test;
26:
27: /**
28: * Base class for JDBC JUnit test decorators.
29: */
30: public abstract class BaseJDBCTestSetup extends TestSetup {
31:
32: public BaseJDBCTestSetup(Test test) {
33: super (test);
34: }
35:
36: /**
37: * Maintain a single connection to the default
38: * database, opened at the first call to getConnection.
39: * Typical setup will just require a single connection.
40: * @see BaseJDBCTestSetup#getConnection()
41: */
42: private Connection conn;
43:
44: /**
45: * Return the current configuration for the test.
46: */
47: public final TestConfiguration getTestConfiguration() {
48: return TestConfiguration.getCurrent();
49: }
50:
51: /**
52: * Obtain the connection to the default database.
53: * This class maintains a single connection returned
54: * by this class, it is opened on the first call to
55: * this method. Subsequent calls will return the same
56: * connection object unless it has been closed. In that
57: * case a new connection object will be returned.
58: * <P>
59: * The tearDown method will close the connection if
60: * it is open.
61: * @see TestConfiguration#openDefaultConnection()
62: */
63: public final Connection getConnection() throws SQLException {
64: if (conn != null) {
65: if (!conn.isClosed())
66: return conn;
67: conn = null;
68: }
69: return conn = getTestConfiguration().openDefaultConnection();
70: }
71:
72: /**
73: * Print debug string.
74: * @param text String to print
75: */
76: public void println(final String text) {
77: if (getTestConfiguration().isVerbose()) {
78: System.out.println("DEBUG: " + text);
79: }
80: }
81:
82: /**
83: * Tear down this fixture, sub-classes should call
84: * super.tearDown(). This cleanups & closes the connection
85: * if it is open.
86: */
87: protected void tearDown() throws java.lang.Exception {
88: JDBC.cleanup(conn);
89: conn = null;
90: }
91: }
|