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