1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.dbunit.database;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27
28 import java.sql.DatabaseMetaData;
29 import java.sql.PreparedStatement;
30 import java.util.Locale;
31
32 import org.dbunit.DatabaseEnvironment;
33 import org.dbunit.DatabaseUnitException;
34 import org.dbunit.dataset.ITable;
35 import org.junit.jupiter.api.Test;
36
37
38
39
40
41
42 class DatabaseConnectionIT extends AbstractDatabaseConnectionIT
43 {
44
45 @Override
46 protected String convertString(final String str) throws Exception
47 {
48 return getEnvironment().convertString(str);
49 }
50
51 @Test
52 void testCreateNullConnection_withNullJdbcConnection_throwsNullPointerException() throws Exception
53 {
54 assertThrows(NullPointerException.class,
55 () -> new DatabaseConnection(null),
56 "Should not be able to create a database connection without a JDBC connection");
57 }
58
59 @Test
60 void testCreateConnectionWithNonExistingSchemaAndStrictValidation_withInvalidSchemaAndValidateTrue_throwsDatabaseUnitException()
61 throws Exception
62 {
63 final DatabaseEnvironment environment = getEnvironment();
64 final String schema =
65 environment.convertString("XYZ_INVALID_SCHEMA_1642344539");
66 final IDatabaseConnection validConnection = super.getConnection();
67 final String expectedMsg = "The given schema '" + convertString(schema)
68 + "' does not exist.";
69
70
71 final boolean validate = true;
72 final DatabaseUnitException expected = assertThrows(
73 DatabaseUnitException.class,
74 () -> new DatabaseConnection(validConnection.getConnection(),
75 schema, validate),
76 "Should not be able to create a database connection object with an unknown schema.");
77 assertThat(expected).hasMessageContaining(expectedMsg);
78 }
79
80 @Test
81 void testCreateConnectionWithNonExistingSchemaAndLenientValidation_withInvalidSchemaAndValidateFalse_createsConnectionSuccessfully()
82 throws Exception
83 {
84 final DatabaseEnvironment environment = getEnvironment();
85 final String schema =
86 environment.convertString("XYZ_INVALID_SCHEMA_1642344539");
87 final IDatabaseConnection validConnection = super.getConnection();
88
89 final boolean validate = false;
90 final DatabaseConnection dbConnection = new DatabaseConnection(
91 validConnection.getConnection(), schema, validate);
92 assertThat(dbConnection).isNotNull();
93 }
94
95 @Test
96 void testCreateConnectionWithSchemaDbStoresUpperCaseIdentifiers_whenDbStoresUpperCase_normalizesSchemaToUpperCase()
97 throws Exception
98 {
99 final IDatabaseConnection validConnection = super.getConnection();
100 final String schema = validConnection.getSchema();
101 assertThat(schema)
102 .as("Precondition: schema of connection must not be null")
103 .isNotNull();
104
105 final DatabaseMetaData metaData =
106 validConnection.getConnection().getMetaData();
107 if (metaData.storesUpperCaseIdentifiers())
108 {
109 final boolean validate = true;
110 final DatabaseConnection dbConnection =
111 new DatabaseConnection(validConnection.getConnection(),
112 schema.toLowerCase(Locale.ENGLISH), validate);
113 assertThat(dbConnection).isNotNull();
114 assertThat(dbConnection.getSchema())
115 .isEqualTo(schema.toUpperCase(Locale.ENGLISH));
116 } else
117 {
118
119 assertThat(true).isTrue();
120 }
121 }
122
123 @Test
124 void testCreateConnectionWithSchemaDbStoresLowerCaseIdentifiers_whenDbStoresLowerCase_normalizesSchemaToLowerCase()
125 throws Exception
126 {
127 final IDatabaseConnection validConnection = super.getConnection();
128 final String schema = validConnection.getSchema();
129 assertThat(schema)
130 .as("Precondition: schema of connection must not be null")
131 .isNotNull();
132
133 final DatabaseMetaData metaData =
134 validConnection.getConnection().getMetaData();
135 if (metaData.storesLowerCaseIdentifiers())
136 {
137 final boolean validate = true;
138 final DatabaseConnection dbConnection =
139 new DatabaseConnection(validConnection.getConnection(),
140 schema.toUpperCase(Locale.ENGLISH), validate);
141 assertThat(dbConnection).isNotNull();
142 assertThat(dbConnection.getSchema())
143 .isEqualTo(schema.toLowerCase(Locale.ENGLISH));
144 } else
145 {
146
147 assertThat(true).isTrue();
148 }
149 }
150
151 @Test
152 void testCreateQueryWithPreparedStatement_withReusedPreparedStatement_returnsCorrectTableForEachExecution() throws Exception
153 {
154 final IDatabaseConnection connection = super.getConnection();
155 final PreparedStatement pstmt = connection.getConnection()
156 .prepareStatement("select * from TEST_TABLE where COLUMN0=?");
157
158 try
159 {
160 pstmt.setString(1, "row 1 col 0");
161 final ITable table = connection.createTable("MY_TABLE", pstmt);
162 assertThat(table.getRowCount()).isEqualTo(1);
163 assertThat(table.getTableMetaData().getColumns()).hasSize(4);
164 assertThat(table.getValue(0, "COLUMN1")).isEqualTo("row 1 col 1");
165
166
167 pstmt.setString(1, "row 2 col 0");
168 final ITable table2 = connection.createTable("MY_TABLE", pstmt);
169 assertThat(table2.getRowCount()).isEqualTo(1);
170 assertThat(table2.getTableMetaData().getColumns()).hasSize(4);
171 assertThat(table2.getValue(0, "COLUMN1")).isEqualTo("row 2 col 1");
172 } finally
173 {
174 pstmt.close();
175 }
176 }
177
178 }