1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.database;
23
24 import java.sql.Connection;
25 import java.sql.DriverManager;
26 import java.sql.SQLException;
27
28 import org.dbunit.AbstractDatabaseIT;
29 import org.dbunit.DatabaseProfile;
30 import org.dbunit.IDatabaseTester;
31
32
33
34
35
36
37 public abstract class AbstractDatabaseConnectionIT extends AbstractDatabaseIT
38 {
39 private String schema;
40 private DatabaseProfile profile;
41
42 public AbstractDatabaseConnectionIT(String s)
43 {
44 super(s);
45 }
46
47
48 protected void setUp() throws Exception {
49 super.setUp();
50 this.profile = super.getEnvironment().getProfile();
51 this.schema = this.profile.getSchema();
52 }
53
54
55 public final void testGetRowCount() throws Exception
56 {
57 assertEquals("EMPTY_TABLE", 0, _connection.getRowCount("EMPTY_TABLE", null));
58 assertEquals("EMPTY_TABLE", 0, _connection.getRowCount("EMPTY_TABLE"));
59
60 assertEquals("TEST_TABLE", 6, _connection.getRowCount("TEST_TABLE", null));
61 assertEquals("TEST_TABLE", 6, _connection.getRowCount("TEST_TABLE"));
62
63 assertEquals("PK_TABLE", 1, _connection.getRowCount("PK_TABLE", "where PK0 = 0"));
64 }
65
66 public final void testGetRowCount_NonexistingSchema() throws Exception
67 {
68 DatabaseProfile profile = super.getEnvironment().getProfile();
69 String nonexistingSchema = profile.getSchema() + "_444_XYZ_TEST";
70 this.schema = nonexistingSchema;
71
72 IDatabaseTester dbTester = this.newDatabaseTester(nonexistingSchema);
73 try {
74 IDatabaseConnection dbConnection = dbTester.getConnection();
75
76 assertEquals(convertString(nonexistingSchema), dbConnection.getSchema());
77 try {
78 dbConnection.getRowCount("TEST_TABLE");
79 fail("Should not be able to retrieve row count for non-existing schema " + nonexistingSchema);
80 }
81 catch(SQLException expected)
82 {
83
84 }
85 }
86 finally {
87
88 dbTester.setSchema(profile.getSchema());
89 }
90 }
91
92 public final void testGetRowCount_NoSchemaSpecified() throws Exception
93 {
94 DatabaseProfile profile = super.getEnvironment().getProfile();
95 this.schema = null;
96 IDatabaseTester dbTester = this.newDatabaseTester(this.schema);
97 try {
98 IDatabaseConnection dbConnection = dbTester.getConnection();
99
100 assertEquals(null, dbConnection.getSchema());
101 assertEquals("TEST_TABLE", 6, _connection.getRowCount("TEST_TABLE", null));
102 }
103 finally {
104
105 dbTester.setSchema(profile.getSchema());
106 }
107 }
108
109
110 private IDatabaseTester newDatabaseTester(String schema) throws Exception {
111 IDatabaseTester tester = super.newDatabaseTester();
112 tester.setSchema(schema);
113 return tester;
114 }
115
116
117 protected IDatabaseConnection getConnection() throws Exception {
118 String name = profile.getDriverClass();
119 Class.forName(name);
120 Connection connection = DriverManager.getConnection(
121 profile.getConnectionUrl(), profile.getUser(),
122 profile.getPassword());
123 _connection = new DatabaseConnection(connection,
124 profile.getSchema());
125
126 IDatabaseConnection dbunitConnection = new DatabaseConnection(connection,
127 this.schema);
128 return dbunitConnection;
129 }
130
131
132
133 }
134
135