View Javadoc
1   /*
2    * Copyright 2002-2016 the original author or authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A bean representation of the DB unit {@link DatabaseConfig} class. This bean allows the database configuration from
33   * spring using standard property arguments. The configuration from this bean can be {@link #apply applied} to an
34   * existing {@link DatabaseConfig}.
35   *
36   * @author Phillip Webb
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  	 * Gets the statement factory database config property.
53  	 * @return the statement factory
54  	 * @see DatabaseConfig#PROPERTY_STATEMENT_FACTORY
55  	 */
56  	public IStatementFactory getStatementFactory() {
57  		return (IStatementFactory) getProperty("statementFactory", DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
58  	}
59  
60  	/**
61  	 * Sets the statement factory database config property.
62  	 * @param statementFactory the statement factory
63  	 * @see DatabaseConfig#PROPERTY_STATEMENT_FACTORY
64  	 */
65  	public void setStatementFactory(IStatementFactory statementFactory) {
66  		setProperty("statementFactory", DatabaseConfig.PROPERTY_STATEMENT_FACTORY, statementFactory);
67  	}
68  
69  	/**
70  	 * Gets the result set table factory database config property.
71  	 * @return the result set table factory
72  	 * @see DatabaseConfig#PROPERTY_RESULTSET_TABLE_FACTORY
73  	 */
74  	public IResultSetTableFactory getResultsetTableFactory() {
75  		return (IResultSetTableFactory) getProperty("resultSetTableFactory",
76  				DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);
77  	}
78  
79  	/**
80  	 * Sets the result set table factory database config property.
81  	 * @param resultSetTableFactory the result set table factory
82  	 * @see DatabaseConfig#PROPERTY_RESULTSET_TABLE_FACTORY
83  	 */
84  	public void setResultsetTableFactory(IResultSetTableFactory resultSetTableFactory) {
85  		setProperty("resultSetTableFactory", DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, resultSetTableFactory);
86  	}
87  
88  	/**
89  	 * Gets the data type factory database config property.
90  	 * @return the data type factory
91  	 * @see DatabaseConfig#PROPERTY_DATATYPE_FACTORY
92  	 */
93  	public IDataTypeFactory getDatatypeFactory() {
94  		return (IDataTypeFactory) getProperty("dataTypeFactory", DatabaseConfig.PROPERTY_DATATYPE_FACTORY);
95  	}
96  
97  	/**
98  	 * Sets the data type factory database config property.
99  	 * @param dataTypeFactory the data type factory
100 	 * @see DatabaseConfig#PROPERTY_DATATYPE_FACTORY
101 	 */
102 	public void setDatatypeFactory(IDataTypeFactory dataTypeFactory) {
103 		setProperty("dataTypeFactory", DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory);
104 	}
105 
106 	/**
107 	 * Gets the escape pattern database config property.
108 	 * @return the escape pattern
109 	 * @see DatabaseConfig#PROPERTY_ESCAPE_PATTERN
110 	 */
111 	public String getEscapePattern() {
112 		return (String) getProperty("escapePattern", DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
113 	}
114 
115 	/**
116 	 * Sets the escape pattern database config property.
117 	 * @param escapePattern the escape pattern
118 	 * @see DatabaseConfig#PROPERTY_ESCAPE_PATTERN
119 	 */
120 	public void setEscapePattern(String escapePattern) {
121 		setProperty("escapePattern", DatabaseConfig.PROPERTY_ESCAPE_PATTERN, escapePattern);
122 	}
123 
124 	/**
125 	 * Gets the table type database config property.
126 	 * @return the table type
127 	 * @see DatabaseConfig#PROPERTY_TABLE_TYPE
128 	 */
129 	public String[] getTableType() {
130 		return (String[]) getProperty("tableTable", DatabaseConfig.PROPERTY_TABLE_TYPE);
131 	}
132 
133 	/**
134 	 * Sets the table type database config property.
135 	 * @param tableTable the table type
136 	 * @see DatabaseConfig#PROPERTY_TABLE_TYPE
137 	 */
138 	public void setTableType(String[] tableTable) {
139 		setProperty("tableTable", DatabaseConfig.PROPERTY_TABLE_TYPE, tableTable);
140 	}
141 
142 	/**
143 	 * Gets the primary key filter database config property.
144 	 * @return the primary key filter
145 	 * @see DatabaseConfig#PROPERTY_PRIMARY_KEY_FILTER
146 	 */
147 	public IColumnFilter getPrimaryKeyFilter() {
148 		return (IColumnFilter) getProperty("primaryKeyFilter", DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER);
149 	}
150 
151 	/**
152 	 * Sets the primary key filter database config property.
153 	 * @param primaryKeyFilter the primary key filter
154 	 * @see DatabaseConfig#PROPERTY_PRIMARY_KEY_FILTER
155 	 */
156 	public void setPrimaryKeyFilter(IColumnFilter primaryKeyFilter) {
157 		setProperty("primaryKeyFilter", DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER, primaryKeyFilter);
158 	}
159 
160 	/**
161 	 * Gets the batch size database config property.
162 	 * @return the batch size
163 	 * @see DatabaseConfig#PROPERTY_BATCH_SIZE
164 	 */
165 	public Integer getBatchSize() {
166 		return (Integer) getProperty("batchSize", DatabaseConfig.PROPERTY_BATCH_SIZE);
167 	}
168 
169 	/**
170 	 * Sets the batch size database config property.
171 	 * @param batchSize the batch size
172 	 * @see DatabaseConfig#PROPERTY_BATCH_SIZE
173 	 */
174 	public void setBatchSize(Integer batchSize) {
175 		setProperty("batchSize", DatabaseConfig.PROPERTY_BATCH_SIZE, batchSize);
176 	}
177 
178 	/**
179 	 * Gets the fetch size database config property.
180 	 * @return the fetch size
181 	 * @see DatabaseConfig#PROPERTY_FETCH_SIZE
182 	 */
183 	public Integer getFetchSize() {
184 		return (Integer) getProperty("fetchSize", DatabaseConfig.PROPERTY_FETCH_SIZE);
185 	}
186 
187 	/**
188 	 * Sets the fetch size database config property.
189 	 * @param fetchSize the fetch size
190 	 * @see DatabaseConfig#PROPERTY_FETCH_SIZE
191 	 */
192 	public void setFetchSize(Integer fetchSize) {
193 		setProperty("fetchSize", DatabaseConfig.PROPERTY_FETCH_SIZE, fetchSize);
194 	}
195 
196 	/**
197 	 * Gets the meta-data handler database config property.
198 	 * @return the meta-data handler
199 	 * @see DatabaseConfig#PROPERTY_METADATA_HANDLER
200 	 */
201 	public IMetadataHandler getMetadataHandler() {
202 		return (IMetadataHandler) getProperty("metadataHandler", DatabaseConfig.PROPERTY_METADATA_HANDLER);
203 	}
204 
205 	/**
206 	 * Sets the meta-data handler database config property.
207 	 * @param metadataHandler meta-data handler
208 	 * @see DatabaseConfig#PROPERTY_METADATA_HANDLER
209 	 */
210 	public void setMetadataHandler(IMetadataHandler metadataHandler) {
211 		setProperty("metadataHandler", DatabaseConfig.PROPERTY_METADATA_HANDLER, metadataHandler);
212 	}
213 
214 	/**
215 	 * Gets the case sensitive table names database config feature.
216 	 * @return case sensitive table names
217 	 * @see DatabaseConfig#FEATURE_CASE_SENSITIVE_TABLE_NAMES
218 	 */
219 	public Boolean getCaseSensitiveTableNames() {
220 		return (Boolean) getProperty("caseSensitiveTableNames", DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES);
221 	}
222 
223 	/**
224 	 * Sets the case sensitive table names database config feature.
225 	 * @param caseSensitiveTableNames case sensitive table names
226 	 * @see DatabaseConfig#FEATURE_CASE_SENSITIVE_TABLE_NAMES
227 	 */
228 	public void setCaseSensitiveTableNames(Boolean caseSensitiveTableNames) {
229 		setProperty("caseSensitiveTableNames", DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES,
230 				caseSensitiveTableNames);
231 	}
232 
233 	/**
234 	 * Gets the qualified table names database config feature.
235 	 * @return the qualified table names
236 	 * @see DatabaseConfig#FEATURE_QUALIFIED_TABLE_NAMES
237 	 */
238 	public Boolean getQualifiedTableNames() {
239 		return (Boolean) getProperty("qualifiedTableNames", DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES);
240 	}
241 
242 	/**
243 	 * Sets the qualified table names database config feature.
244 	 * @param qualifiedTableNames the qualified table names
245 	 * @see DatabaseConfig#FEATURE_QUALIFIED_TABLE_NAMES
246 	 */
247 	public void setQualifiedTableNames(Boolean qualifiedTableNames) {
248 		setProperty("qualifiedTableNames", DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, qualifiedTableNames);
249 	}
250 
251 	/**
252 	 * Gets the batched statements database config feature.
253 	 * @return the batched statements
254 	 * @see DatabaseConfig#FEATURE_BATCHED_STATEMENTS
255 	 */
256 	public Boolean getBatchedStatements() {
257 		return (Boolean) getProperty("batchedStatements", DatabaseConfig.FEATURE_BATCHED_STATEMENTS);
258 	}
259 
260 	/**
261 	 * Sets the batched statements database config feature.
262 	 * @param batchedStatements the batched statements
263 	 * @see DatabaseConfig#FEATURE_BATCHED_STATEMENTS
264 	 */
265 	public void setBatchedStatements(Boolean batchedStatements) {
266 		setProperty("batchedStatements", DatabaseConfig.FEATURE_BATCHED_STATEMENTS, batchedStatements);
267 	}
268 
269 	/**
270 	 * Gets the datatype warning database config feature.
271 	 * @return the datatype warning
272 	 * @see DatabaseConfig#FEATURE_DATATYPE_WARNING
273 	 */
274 	public Boolean getDatatypeWarning() {
275 		return (Boolean) getProperty("datatypeWarning", DatabaseConfig.FEATURE_DATATYPE_WARNING);
276 	}
277 
278 	/**
279 	 * Sets the datatype warning database config feature.
280 	 * @param datatypeWarning the datatype warning
281 	 * @see DatabaseConfig#FEATURE_DATATYPE_WARNING
282 	 */
283 	public void setDatatypeWarning(Boolean datatypeWarning) {
284 		setProperty("datatypeWarning", DatabaseConfig.FEATURE_DATATYPE_WARNING, datatypeWarning);
285 	}
286 
287 	/**
288 	 * Gets the skip oracle recyclebin tables database config feature.
289 	 * @return the skip oracle recyclebin tables
290 	 * @see DatabaseConfig#FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES
291 	 */
292 	public Boolean getSkipOracleRecyclebinTables() {
293 		return (Boolean) getProperty("skipOracleRecyclebinTables",
294 				DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES);
295 	}
296 
297 	/**
298 	 * Sets the skip oracle recyclebin tables database config feature.
299 	 * @param skipOracleRecyclebinTables skip oracle recyclebin tables
300 	 * @see DatabaseConfig#FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES
301 	 */
302 	public void setSkipOracleRecyclebinTables(Boolean skipOracleRecyclebinTables) {
303 		setProperty("skipOracleRecyclebinTables", DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES,
304 				skipOracleRecyclebinTables);
305 	}
306 
307 	/**
308 	 * Gets the allow empty fields database config feature.
309 	 * @return the allow empty fields
310 	 * @see DatabaseConfig#FEATURE_ALLOW_EMPTY_FIELDS
311 	 */
312 	public Boolean getAllowEmptyFields() {
313 		return (Boolean) getProperty("allowEmptyFields", DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS);
314 	}
315 
316 	/**
317 	 * Sets the allow empty fields database config feature.
318 	 * @param allowEmptyFields allow empty fields
319 	 * @see DatabaseConfig#FEATURE_ALLOW_EMPTY_FIELDS
320 	 */
321 	public void setAllowEmptyFields(Boolean allowEmptyFields) {
322 		setProperty("allowEmptyFields", DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, allowEmptyFields);
323 	}
324 
325 	/**
326 	 * Get a property from the underlying database config.
327 	 * @param propertyName The name of the attribute
328 	 * @param dataConfigPropertyName The data config property name
329 	 * @return the value of the property
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 	 * Set a property to the underlying data config.
341 	 * @param propertyName the name of the property
342 	 * @param dataConfigPropertyName the data config property name
343 	 * @param value the value to set
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 	 * Apply the configuration represented by this bean to the specified databaseConfig.
360 	 * @param databaseConfig the database config to be updated.
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 }