KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  *
3  * Derby - Class org.apache.derbyTesting.functionTests.util.BaseJDBCTestSetup
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
24
25 import junit.extensions.TestSetup;
26 import junit.framework.Test;
27
28 /**
29  * Base class for JDBC JUnit test decorators.
30  */

31 public abstract class BaseJDBCTestSetup
32     extends TestSetup {
33     
34     public BaseJDBCTestSetup(Test test) {
35         super(test);
36     }
37     
38     /**
39      * Maintain a single connection to the default
40      * database, opened at the first call to getConnection.
41      * Typical setup will just require a single connection.
42      * @see BaseJDBCTestSetup#getConnection()
43      */

44     private Connection conn;
45     
46     /**
47      * Return the current configuration for the test.
48      */

49     public final TestConfiguration getTestConfiguration()
50     {
51         return TestConfiguration.getCurrent();
52     }
53     
54     /**
55      * Obtain the connection to the default database.
56      * This class maintains a single connection returned
57      * by this class, it is opened on the first call to
58      * this method. Subsequent calls will return the same
59      * connection object unless it has been closed. In that
60      * case a new connection object will be returned.
61      * <P>
62      * The tearDown method will close the connection if
63      * it is open.
64      * @see TestConfiguration#openDefaultConnection()
65      */

66     public final Connection getConnection() throws SQLException
67     {
68         if (conn != null)
69         {
70             if (!conn.isClosed())
71                 return conn;
72             conn = null;
73         }
74         return conn = getTestConfiguration().openDefaultConnection();
75     }
76     
77     /**
78      * Print debug string.
79      * @param text String to print
80      */

81     public void println(final String JavaDoc text) {
82         if (getTestConfiguration().isVerbose()) {
83             System.out.println("DEBUG: " + text);
84         }
85     }
86     
87     /**
88      * Tear down this fixture, sub-classes should call
89      * super.tearDown(). This cleanups & closes the connection
90      * if it is open.
91      */

92     protected void tearDown()
93     throws java.lang.Exception JavaDoc
94     {
95         JDBC.cleanup(conn);
96         conn = null;
97     }
98 }
Popular Tags