1
2
3
4
5
6
7 package org.asyrinx.joey.test.dao;
8
9 import org.dbunit.database.DatabaseConnection;
10 import org.dbunit.database.IDatabaseConnection;
11 import org.dbunit.dataset.IDataSet;
12 import org.dbunit.operation.DatabaseOperation;
13 import org.seasar.extension.unit.S2TestCase;
14
15 /***
16 * @author akima
17 */
18 public abstract class S2DBTestCase extends S2TestCase {
19
20 /***
21 * @param name
22 */
23 public S2DBTestCase(String name) {
24 super(name);
25 }
26
27
28
29
30
31
32
33 /***
34 * Returns the test dataset.
35 */
36 protected abstract IDataSet getDataSet() throws Exception;
37
38 /***
39 * Returns the test database connection.
40 */
41 protected IDatabaseConnection getDBUnitConnection() throws Exception {
42 return new DatabaseConnection(getConnection());
43 }
44
45 /***
46 * Close the specified connection. Ovverride this method of you want to
47 * keep your connection alive between tests.
48 */
49 protected void closeConnection(IDatabaseConnection connection)
50 throws Exception {
51 connection.close();
52 }
53
54 /***
55 * Returns the database operation executed in test setup.
56 */
57 protected DatabaseOperation getSetUpOperation() throws Exception {
58 return DatabaseOperation.CLEAN_INSERT;
59 }
60
61 /***
62 * Returns the database operation executed in test cleanup.
63 */
64 protected DatabaseOperation getTearDownOperation() throws Exception {
65 return DatabaseOperation.NONE;
66 }
67
68 private void executeOperation(DatabaseOperation operation)
69 throws Exception {
70 if (operation != DatabaseOperation.NONE) {
71 IDatabaseConnection connection = getDBUnitConnection();
72 try {
73 operation.execute(connection, getDataSet());
74 } finally {
75 closeConnection(connection);
76 }
77 }
78 }
79
80
81
82
83 protected void setUp() throws Exception {
84 super.setUp();
85 }
86
87 protected void tearDown() throws Exception {
88 super.tearDown();
89 }
90
91
92
93
94 protected void runTest() throws Throwable {
95 executeOperation(getSetUpOperation());
96 try {
97 super.runTest();
98 } finally {
99 executeOperation(getTearDownOperation());
100 }
101 }
102
103 }