KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > DBTestCase


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella;
17
18 import junit.framework.AssertionFailedError;
19 import scriptella.jdbc.JdbcException;
20 import scriptella.jdbc.JdbcUtils;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.DriverManager JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28
29
30 /**
31  * TODO: Add documentation
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 public abstract class DBTestCase extends AbstractTestCase {
37     static {
38         try {
39             Class.forName("org.hsqldb.jdbcDriver");
40         } catch (ClassNotFoundException JavaDoc e) {
41             throw new AssertionFailedError(e.getMessage());
42         }
43     }
44
45     private Collection JavaDoc<String JavaDoc> dbNames = new HashSet JavaDoc<String JavaDoc>();
46     private Collection JavaDoc<Connection JavaDoc> connections = new ArrayList JavaDoc<Connection JavaDoc>();
47
48     protected Connection JavaDoc getConnection(final String JavaDoc db) {
49         dbNames.add(db);
50
51         try {
52             final Connection JavaDoc c = DriverManager.getConnection("jdbc:hsqldb:mem:" +
53                     db, "sa", "");
54             connections.add(c);
55
56             return c;
57         } catch (SQLException JavaDoc e) {
58             throw new JdbcException(e.getMessage(), e);
59         }
60     }
61
62     protected void tearDown() throws Exception JavaDoc {
63         super.tearDown();
64
65         for (String JavaDoc s : dbNames) {
66             try {
67                 getConnection(s).createStatement().execute("SHUTDOWN");
68             } catch (Exception JavaDoc e) {
69                 e.printStackTrace();
70             }
71         }
72
73         for (Connection JavaDoc connection : connections) {
74             JdbcUtils.closeSilent(connection);
75         }
76
77         dbNames.clear();
78         connections.clear();
79     }
80 }
81
Popular Tags