1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.github.springtestdbunit.bean;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.dbunit.database.DatabaseConfig;
23 import org.dbunit.database.DatabaseConfig.ConfigProperty;
24 import org.dbunit.database.IMetadataHandler;
25 import org.dbunit.database.IResultSetTableFactory;
26 import org.dbunit.database.statement.IStatementFactory;
27 import org.dbunit.dataset.datatype.IDataTypeFactory;
28 import org.dbunit.dataset.filter.IColumnFilter;
29 import org.springframework.util.Assert;
30
31
32
33
34
35
36
37
38 public class DatabaseConfigBean {
39
40 private static final Map<String, ConfigProperty> CONFIG_PROPERTIES;
41
42 static {
43 CONFIG_PROPERTIES = new HashMap<String, ConfigProperty>();
44 for (ConfigProperty configProperty : DatabaseConfig.ALL_PROPERTIES) {
45 CONFIG_PROPERTIES.put(configProperty.getProperty(), configProperty);
46 }
47 }
48
49 private DatabaseConfig databaseConfig = new DatabaseConfig();
50
51
52
53
54
55
56 public IStatementFactory getStatementFactory() {
57 return (IStatementFactory) getProperty("statementFactory", DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
58 }
59
60
61
62
63
64
65 public void setStatementFactory(IStatementFactory statementFactory) {
66 setProperty("statementFactory", DatabaseConfig.PROPERTY_STATEMENT_FACTORY, statementFactory);
67 }
68
69
70
71
72
73
74 public IResultSetTableFactory getResultsetTableFactory() {
75 return (IResultSetTableFactory) getProperty("resultSetTableFactory",
76 DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);
77 }
78
79
80
81
82
83
84 public void setResultsetTableFactory(IResultSetTableFactory resultSetTableFactory) {
85 setProperty("resultSetTableFactory", DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, resultSetTableFactory);
86 }
87
88
89
90
91
92
93 public IDataTypeFactory getDatatypeFactory() {
94 return (IDataTypeFactory) getProperty("dataTypeFactory", DatabaseConfig.PROPERTY_DATATYPE_FACTORY);
95 }
96
97
98
99
100
101
102 public void setDatatypeFactory(IDataTypeFactory dataTypeFactory) {
103 setProperty("dataTypeFactory", DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory);
104 }
105
106
107
108
109
110
111 public String getEscapePattern() {
112 return (String) getProperty("escapePattern", DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
113 }
114
115
116
117
118
119
120 public void setEscapePattern(String escapePattern) {
121 setProperty("escapePattern", DatabaseConfig.PROPERTY_ESCAPE_PATTERN, escapePattern);
122 }
123
124
125
126
127
128
129 public String[] getTableType() {
130 return (String[]) getProperty("tableTable", DatabaseConfig.PROPERTY_TABLE_TYPE);
131 }
132
133
134
135
136
137
138 public void setTableType(String[] tableTable) {
139 setProperty("tableTable", DatabaseConfig.PROPERTY_TABLE_TYPE, tableTable);
140 }
141
142
143
144
145
146
147 public IColumnFilter getPrimaryKeyFilter() {
148 return (IColumnFilter) getProperty("primaryKeyFilter", DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER);
149 }
150
151
152
153
154
155
156 public void setPrimaryKeyFilter(IColumnFilter primaryKeyFilter) {
157 setProperty("primaryKeyFilter", DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER, primaryKeyFilter);
158 }
159
160
161
162
163
164
165 public Integer getBatchSize() {
166 return (Integer) getProperty("batchSize", DatabaseConfig.PROPERTY_BATCH_SIZE);
167 }
168
169
170
171
172
173
174 public void setBatchSize(Integer batchSize) {
175 setProperty("batchSize", DatabaseConfig.PROPERTY_BATCH_SIZE, batchSize);
176 }
177
178
179
180
181
182
183 public Integer getFetchSize() {
184 return (Integer) getProperty("fetchSize", DatabaseConfig.PROPERTY_FETCH_SIZE);
185 }
186
187
188
189
190
191
192 public void setFetchSize(Integer fetchSize) {
193 setProperty("fetchSize", DatabaseConfig.PROPERTY_FETCH_SIZE, fetchSize);
194 }
195
196
197
198
199
200
201 public IMetadataHandler getMetadataHandler() {
202 return (IMetadataHandler) getProperty("metadataHandler", DatabaseConfig.PROPERTY_METADATA_HANDLER);
203 }
204
205
206
207
208
209
210 public void setMetadataHandler(IMetadataHandler metadataHandler) {
211 setProperty("metadataHandler", DatabaseConfig.PROPERTY_METADATA_HANDLER, metadataHandler);
212 }
213
214
215
216
217
218
219 public Boolean getCaseSensitiveTableNames() {
220 return (Boolean) getProperty("caseSensitiveTableNames", DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES);
221 }
222
223
224
225
226
227
228 public void setCaseSensitiveTableNames(Boolean caseSensitiveTableNames) {
229 setProperty("caseSensitiveTableNames", DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES,
230 caseSensitiveTableNames);
231 }
232
233
234
235
236
237
238 public Boolean getQualifiedTableNames() {
239 return (Boolean) getProperty("qualifiedTableNames", DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES);
240 }
241
242
243
244
245
246
247 public void setQualifiedTableNames(Boolean qualifiedTableNames) {
248 setProperty("qualifiedTableNames", DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, qualifiedTableNames);
249 }
250
251
252
253
254
255
256 public Boolean getBatchedStatements() {
257 return (Boolean) getProperty("batchedStatements", DatabaseConfig.FEATURE_BATCHED_STATEMENTS);
258 }
259
260
261
262
263
264
265 public void setBatchedStatements(Boolean batchedStatements) {
266 setProperty("batchedStatements", DatabaseConfig.FEATURE_BATCHED_STATEMENTS, batchedStatements);
267 }
268
269
270
271
272
273
274 public Boolean getDatatypeWarning() {
275 return (Boolean) getProperty("datatypeWarning", DatabaseConfig.FEATURE_DATATYPE_WARNING);
276 }
277
278
279
280
281
282
283 public void setDatatypeWarning(Boolean datatypeWarning) {
284 setProperty("datatypeWarning", DatabaseConfig.FEATURE_DATATYPE_WARNING, datatypeWarning);
285 }
286
287
288
289
290
291
292 public Boolean getSkipOracleRecyclebinTables() {
293 return (Boolean) getProperty("skipOracleRecyclebinTables",
294 DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES);
295 }
296
297
298
299
300
301
302 public void setSkipOracleRecyclebinTables(Boolean skipOracleRecyclebinTables) {
303 setProperty("skipOracleRecyclebinTables", DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES,
304 skipOracleRecyclebinTables);
305 }
306
307
308
309
310
311
312 public Boolean getAllowEmptyFields() {
313 return (Boolean) getProperty("allowEmptyFields", DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS);
314 }
315
316
317
318
319
320
321 public void setAllowEmptyFields(Boolean allowEmptyFields) {
322 setProperty("allowEmptyFields", DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, allowEmptyFields);
323 }
324
325
326
327
328
329
330
331 private Object getProperty(String propertyName, String dataConfigPropertyName) {
332 try {
333 return this.databaseConfig.getProperty(dataConfigPropertyName);
334 } catch (RuntimeException ex) {
335 throw new IllegalArgumentException("Unable to get " + propertyName, ex);
336 }
337 }
338
339
340
341
342
343
344
345 private void setProperty(String propertyName, String dataConfigPropertyName, Object value) {
346 ConfigProperty configProperty = CONFIG_PROPERTIES.get(dataConfigPropertyName);
347 Assert.state(configProperty != null,
348 "Unsupported config property " + dataConfigPropertyName + " for " + propertyName);
349 if (!configProperty.isNullable()) {
350 Assert.notNull(value, propertyName + " cannot be null");
351 }
352 if (value != null) {
353 Assert.isInstanceOf(configProperty.getPropertyType(), value, "Unable to set " + propertyName + " ");
354 }
355 this.databaseConfig.setProperty(dataConfigPropertyName, value);
356 }
357
358
359
360
361
362 public void apply(DatabaseConfig databaseConfig) {
363 for (ConfigProperty configProperty : DatabaseConfig.ALL_PROPERTIES) {
364 String name = configProperty.getProperty();
365 Object value = this.databaseConfig.getProperty(name);
366 if ((configProperty.isNullable()) || ((!configProperty.isNullable()) && (value != null))) {
367 databaseConfig.setProperty(name, value);
368 }
369 }
370 }
371
372 }