-
Notifications
You must be signed in to change notification settings - Fork 135
/
DBUnitConfigTest.java
355 lines (300 loc) · 15.5 KB
/
DBUnitConfigTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.github.database.rider.core.configuration;
import com.github.database.rider.core.api.configuration.DBUnit;
import com.github.database.rider.core.api.configuration.DataSetMergingStrategy;
import com.github.database.rider.core.api.configuration.Orthography;
import com.github.database.rider.core.connection.RiderDataSource;
import com.github.database.rider.core.replacers.CustomReplacer;
import org.dbunit.database.IMetadataHandler;
import org.dbunit.dataset.datatype.DataType;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Created by pestano on 03/09/16.
*/
@RunWith(JUnit4.class)
public class DBUnitConfigTest {
@Rule
public final EnvironmentVariables env = new EnvironmentVariables();
private File customConfigFile = new File("target/test-classes/dbunit.yml");
@Before
public void cleanProperties() {
List<String> propertiesToClean = Arrays.asList("dbunit.driver", "dbunit.url", "dbunit.user", "dbunit.password", "dbunit.escapePattern", "spring.datasource.username", "dbunit.caseStrategy");
for (String property : propertiesToClean) {
cleanProperty(property);
}
}
private void cleanProperty(String propertyName) {
System.clearProperty(propertyName);
env.clear(propertyName);
}
@After
public void deleteConfigFile() {
customConfigFile.delete();
}
@Test
public void shouldInitDBUnitConfigWithDefaultValues() {
DBUnitConfig config = GlobalConfig.newInstance().getDbUnitConfig();
assertThat(config).isNotNull()
.hasFieldOrPropertyWithValue("cacheConnection", true)
.hasFieldOrPropertyWithValue("cacheTableNames", true)
.hasFieldOrPropertyWithValue("leakHunter", false)
.hasFieldOrPropertyWithValue("raiseExceptionOnCleanUp", false)
.hasFieldOrPropertyWithValue("expectedDbType", RiderDataSource.DBType.UNKNOWN)
.hasFieldOrPropertyWithValue("caseInsensitiveStrategy", Orthography.UPPERCASE)
.hasFieldOrPropertyWithValue("mergingStrategy", DataSetMergingStrategy.METHOD)
.hasFieldOrPropertyWithValue("disablePKCheckFor", null);
assertThat(config.getProperties()).
containsEntry("batchedStatements", false).
containsEntry("qualifiedTableNames", false).
containsEntry("schema", null).
containsEntry("caseSensitiveTableNames", false).
containsEntry("batchSize", 100).
containsEntry("fetchSize", 100).
containsEntry("allowEmptyFields", false).
containsKey("replacers").
doesNotContainKey("escapePattern").
doesNotContainKey("datatypeFactory");
assertThat(config.getConnectionConfig()).isNotNull()
.hasFieldOrPropertyWithValue("driver", "")
.hasFieldOrPropertyWithValue("url", "")
.hasFieldOrPropertyWithValue("user", "")
.hasFieldOrPropertyWithValue("password", "");
}
@Test
public void shouldLoadDBUnitConfigViaCustomGlobalFile() throws IOException {
copyResourceToFile("/config/sample-dbunit.yml", customConfigFile);
DBUnitConfig config = GlobalConfig.newInstance().getDbUnitConfig();
assertThat(config).isNotNull()
.hasFieldOrPropertyWithValue("cacheConnection", false)
.hasFieldOrPropertyWithValue("cacheTableNames", false)
.hasFieldOrPropertyWithValue("leakHunter", true)
.hasFieldOrPropertyWithValue("raiseExceptionOnCleanUp", true)
.hasFieldOrPropertyWithValue("disableSequenceFiltering", true)
.hasFieldOrPropertyWithValue("alwaysCleanBefore", true)
.hasFieldOrPropertyWithValue("alwaysCleanAfter", true)
.hasFieldOrPropertyWithValue("expectedDbType", RiderDataSource.DBType.HSQLDB)
.hasFieldOrPropertyWithValue("caseInsensitiveStrategy", Orthography.LOWERCASE)
.hasFieldOrPropertyWithValue("mergingStrategy", DataSetMergingStrategy.CLASS)
.hasFieldOrPropertyWithValue("disablePKCheckFor", new String[]{"NoPKTable"});
assertThat(config.getProperties()).
containsEntry("allowEmptyFields", true).
containsEntry("batchedStatements", true).
containsEntry("qualifiedTableNames", true).
containsEntry("schema", "public").
containsEntry("batchSize", 200).
containsEntry("fetchSize", 200).
containsEntry("escapePattern", "[?]").
containsEntry("datatypeFactory", new MockDataTypeFactory()).
containsEntry("replacers", new ArrayList<>(Arrays.asList(new CustomReplacer()))).
containsEntry("tableType", Arrays.asList("TABLE", "VIEW"));
}
@Test
public void shouldMergeDBUnitConfigViaCustomGlobalFile() throws IOException {
copyResourceToFile("/config/merge-dbunit.yml", customConfigFile);
DBUnitConfig config = GlobalConfig.newInstance().getDbUnitConfig();
assertThat(config).isNotNull()
.hasFieldOrPropertyWithValue("cacheConnection", true)
.hasFieldOrPropertyWithValue("cacheTableNames", true)
.hasFieldOrPropertyWithValue("leakHunter", true)
.hasFieldOrPropertyWithValue("raiseExceptionOnCleanUp", false)
.hasFieldOrPropertyWithValue("expectedDbType", RiderDataSource.DBType.UNKNOWN)
.hasFieldOrPropertyWithValue("caseInsensitiveStrategy", Orthography.UPPERCASE);
assertThat(config.getProperties()).
containsEntry("batchedStatements", false).
containsEntry("qualifiedTableNames", true).
containsEntry("caseSensitiveTableNames", false).
containsEntry("batchSize", 100).
containsEntry("fetchSize", 200).
containsEntry("allowEmptyFields", false).
containsEntry("escapePattern", "[?]");
}
@Test
@DBUnit(cacheTableNames = false, allowEmptyFields = true, batchSize = 50, schema = "public",
expectedDbType = RiderDataSource.DBType.HSQLDB, tableType = {"TABLE", "VIEW"})
public void shouldLoadDBUnitConfigViaAnnotation() throws NoSuchMethodException {
Method method = getClass().getMethod("shouldLoadDBUnitConfigViaAnnotation");
DBUnit dbUnit = method.getAnnotation(DBUnit.class);
DBUnitConfig dbUnitConfig = DBUnitConfig.from(dbUnit);
assertThat(dbUnitConfig).isNotNull()
.hasFieldOrPropertyWithValue("cacheConnection", true)
.hasFieldOrPropertyWithValue("cacheTableNames", false)
.hasFieldOrPropertyWithValue("expectedDbType", RiderDataSource.DBType.HSQLDB);
assertThat(dbUnitConfig.getProperties()).
containsEntry("allowEmptyFields", true).
containsEntry("batchedStatements", false).
containsEntry("qualifiedTableNames", false).
containsEntry("schema", "public").
containsEntry("batchSize", 50).
containsEntry("fetchSize", 100).
containsEntry("tableType", new String[]{"TABLE", "VIEW"}).
doesNotContainKey("escapePattern").
doesNotContainKey("datatypeFactory");
}
@Test
@DBUnit(cacheTableNames = false, allowEmptyFields = true, batchSize = 50, schema = "${spring.datasource.username}",
escapePattern = "${dbunit.escapePattern}",
url = "${dbunit.url}",
user = "${dbunit.user}",
password = "${dbunit.password}",
driver = "${dbunit.driver}")
public void shouldLoadDBUnitConfigWithSystemPropertiesViaAnnotation() throws NoSuchMethodException {
setSysPropsAndEnvVars();
Method method = getClass().getMethod("shouldLoadDBUnitConfigWithSystemPropertiesViaAnnotation");
DBUnit dbUnit = method.getAnnotation(DBUnit.class);
DBUnitConfig dbUnitConfig = DBUnitConfig.from(dbUnit);
assertThat(dbUnitConfig).isNotNull()
.hasFieldOrPropertyWithValue("schema", "test");
assertThat(dbUnitConfig.getProperties()).
containsEntry("escapePattern", "[?]").
containsEntry("schema", "test").
containsEntry("batchSize", 50).
containsEntry("fetchSize", 100).
doesNotContainKey("datatypeFactory");
assertThat(dbUnitConfig.getConnectionConfig()).isNotNull()
.hasFieldOrPropertyWithValue("driver", "org.hsqldb.jdbcDriver")
.hasFieldOrPropertyWithValue("user", "user")
.hasFieldOrPropertyWithValue("password", "password")
.hasFieldOrPropertyWithValue("url", "jdbc:hsqldb:mem:test-config;DB_CLOSE_DELAY=-1");
}
@Test
public void shouldLoadDBUnitConfigViaCustomGlobalFileWithSystemProperties() throws IOException {
copyResourceToFile("/config/dbunit-with-system-properties.yml", customConfigFile);
setSysPropsAndEnvVars();
DBUnitConfig config = GlobalConfig.newInstance().getDbUnitConfig();
assertThat(config).isNotNull()
.hasFieldOrPropertyWithValue("schema", "test");
assertThat(config.getProperties()).
containsEntry("escapePattern", "[?]").
containsEntry("schema", "test").
containsEntry("batchSize", 200).
containsEntry("fetchSize", 200).
doesNotContainKey("datatypeFactory");
assertThat(config.getConnectionConfig()).isNotNull()
.hasFieldOrPropertyWithValue("driver", "org.hsqldb.jdbcDriver")
.hasFieldOrPropertyWithValue("user", "user")
.hasFieldOrPropertyWithValue("password", "password")
.hasFieldOrPropertyWithValue("url", "jdbc:hsqldb:mem:test-config;DB_CLOSE_DELAY=-1");
}
@Test
@DBUnit()
public void shouldTreatAnnotationWithNonExistingSchemaAsNull() throws NoSuchMethodException {
Method method = getClass().getMethod("shouldTreatAnnotationWithNonExistingSchemaAsNull");
DBUnit dbUnit = method.getAnnotation(DBUnit.class);
DBUnitConfig dbUnitConfig = DBUnitConfig.from(dbUnit);
assertThat(dbUnitConfig.getProperties()).
containsEntry("schema", null);
}
@Test
@DBUnit(dataTypeFactoryClass = MockDataTypeFactory.class)
public void shouldInstantiateDataTypeFactoryFromAnnotationIfSpecified() throws NoSuchMethodException {
Method method = getClass().getMethod("shouldInstantiateDataTypeFactoryFromAnnotationIfSpecified");
DBUnit dbUnit = method.getAnnotation(DBUnit.class);
DBUnitConfig dbUnitConfig = DBUnitConfig.from(dbUnit);
assertThat(dbUnitConfig.getProperties()).
containsEntry("datatypeFactory", new MockDataTypeFactory());
}
@Test
@DBUnit(metaDataHandler = MockMetadataHandler.class)
public void shouldInstantiateMetadataHandlerFromAnnotationIfSpecified() throws NoSuchMethodException, SecurityException {
Method method = getClass().getMethod("shouldInstantiateMetadataHandlerFromAnnotationIfSpecified");
DBUnit dbUnit = method.getAnnotation(DBUnit.class);
DBUnitConfig dbUnitConfig = DBUnitConfig.from(dbUnit);
assertThat(dbUnitConfig.getProperties()).
containsEntry("metadataHandler", new MockMetadataHandler());
}
private void copyResourceToFile(String resourceName, File to) throws IOException {
try (InputStream from = getClass().getResourceAsStream(resourceName)) {
Files.copy(from, to.toPath());
}
}
private void setSysPropsAndEnvVars() {
env.set("dbunit.user", "user").set("dbunit.password", "pass").set("dbunit.driver", "org.hsqldb.jdbcDriver");
System.setProperty("dbunit.url", "jdbc:hsqldb:mem:test-config;DB_CLOSE_DELAY=-1");
System.setProperty("dbunit.password", "password");//should override env var
System.setProperty("spring.datasource.username", "test");
System.setProperty("dbunit.escapePattern", "[?]");
}
static class MockDataTypeFactory implements org.dbunit.dataset.datatype.IDataTypeFactory {
@Override
public DataType createDataType(int i, String s) {
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public DataType createDataType(int i, String s, String s1, String s2) {
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
return o != null && getClass() == o.getClass();
}
@Override
public int hashCode() {
return Objects.hash(getClass());
}
}
static class MockMetadataHandler implements IMetadataHandler {
@Override
public ResultSet getColumns(DatabaseMetaData databaseMetaData, String schemaName, String tableName)
throws SQLException {
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public boolean matches(ResultSet resultSet, String schema, String table, boolean caseSensitive)
throws SQLException {
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public boolean matches(ResultSet resultSet, String catalog, String schema, String table, String column,
boolean caseSensitive) throws SQLException {
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public String getSchema(ResultSet resultSet) throws SQLException {
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public boolean tableExists(DatabaseMetaData databaseMetaData, String schemaName, String tableName)
throws SQLException {
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public ResultSet getTables(DatabaseMetaData databaseMetaData, String schemaName, String[] tableTypes)
throws SQLException {
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public ResultSet getPrimaryKeys(DatabaseMetaData databaseMetaData, String schemaName, String tableName)
throws SQLException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("only for configuration tests");
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
return o != null && getClass() == o.getClass();
}
@Override
public int hashCode() {
return Objects.hash(getClass());
}
}
}