01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.compass.gps.device.jdbc;
18:
19: import java.sql.Connection;
20: import java.sql.PreparedStatement;
21: import java.sql.SQLException;
22: import java.sql.Statement;
23:
24: import junit.framework.TestCase;
25: import org.compass.gps.device.jdbc.datasource.SingleConnectionDataSource;
26:
27: public abstract class AbstractJdbcGpsDeviceTests extends TestCase {
28:
29: private static final String DB_SETUP = ""
30: + "CREATE TABLE parent (id INTEGER NOT NULL IDENTITY PRIMARY KEY, first_name VARCHAR(30), last_name VARCHAR(30), version BIGINT NOT NULL );"
31: + "CREATE TABLE child (id INTEGER NOT NULL IDENTITY PRIMARY KEY, parent_id INTEGER NOT NULL, first_name VARCHAR(30), last_name VARCHAR(30), version BIGINT NOT NULL );"
32: + "alter table child add constraint fk_child_parent foreign key (parent_id) references parent(id);";
33:
34: private static final String[] DB_DATA = {
35: "INSERT INTO parent VALUES (1, 'parent first 1', 'last 1', 1);",
36: "INSERT INTO parent VALUES (2, 'parent first 2', 'last 2', 1);",
37: "INSERT INTO parent VALUES (3, 'parent first 3', 'last 3', 1);",
38: "INSERT INTO parent VALUES (4, 'parent first 4', 'last 4', 1);",
39: "INSERT INTO child VALUES (1, 1, 'child first 1 1', 'last 1 1', 1);",
40: "INSERT INTO child VALUES (2, 1, 'child first 1 2', 'last 1 2', 1);",
41: "INSERT INTO child VALUES (3, 1, 'child first 1 3', 'last 1 3', 1);",
42: "INSERT INTO child VALUES (4, 2, 'child first 2 1', 'last 2 1', 1);",
43: "INSERT INTO child VALUES (5, 3, 'child first 3 1', 'last 3 1', 1);",
44: "INSERT INTO child VALUES (6, 4, 'child first 3 2', 'last 3 2', 1);" };
45:
46: private static final String DB_TEARDOWN = "DROP TABLE child; DROP TABLE parent;";
47:
48: protected SingleConnectionDataSource dataSource;
49:
50: protected void setUp() throws Exception {
51: dataSource = new SingleConnectionDataSource(
52: "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:test", "sa",
53: "", true);
54: setUpDB();
55: setUpDBData();
56: }
57:
58: protected void tearDown() throws Exception {
59: tearDownDB();
60: dataSource.destroy();
61: }
62:
63: protected void setUpDB() throws SQLException {
64: try {
65: tearDownDB();
66: } catch (Exception e) {
67: // do nothing
68: }
69: Connection con = dataSource.getConnection();
70: PreparedStatement ps = con.prepareStatement(DB_SETUP);
71: ps.execute();
72: ps.close();
73: con.close();
74: }
75:
76: protected void tearDownDB() throws SQLException {
77: Connection con = dataSource.getConnection();
78: PreparedStatement ps = con.prepareStatement(DB_TEARDOWN);
79: try {
80: ps.execute();
81: ps.close();
82: } finally {
83: con.close();
84: }
85: }
86:
87: protected void setUpDBData() throws SQLException {
88: Connection con = dataSource.getConnection();
89: Statement stmt = con.createStatement();
90: for (int i = 0; i < DB_DATA.length; i++) {
91: stmt.addBatch(DB_DATA[i]);
92: }
93: stmt.executeBatch();
94: stmt.close();
95: con.close();
96: }
97:
98: }
|