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 javax.sql.DataSource;
20  
21  import org.dbunit.database.DatabaseDataSourceConnection;
22  import org.dbunit.database.IDatabaseConnection;
23  import org.springframework.beans.factory.FactoryBean;
24  import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
25  import org.springframework.transaction.PlatformTransactionManager;
26  import org.springframework.util.Assert;
27  
28  /**
29   * A {@link FactoryBean} that can be used to create a {@link #setTransactionAware transaction} aware
30   * {@link DatabaseDataSourceConnection} using the specified {@link #setDataSource dataSource}. Additional configuration
31   * is also supported using {@link #setDatabaseConfig(DatabaseConfigBean)}.
32   *
33   * @author Phillip Webb
34   */
35  public class DatabaseDataSourceConnectionFactoryBean implements FactoryBean<DatabaseDataSourceConnection> {
36  
37  	private DataSource dataSource;
38  
39  	private boolean transactionAware = true;
40  
41  	private String username;
42  
43  	private String password;
44  
45  	private String schema;
46  
47  	private DatabaseConfigBean databaseConfig;
48  
49  	public DatabaseDataSourceConnectionFactoryBean() {
50  		super();
51  	}
52  
53  	public DatabaseDataSourceConnectionFactoryBean(DataSource dataSource) {
54  		super();
55  		this.dataSource = dataSource;
56  	}
57  
58  	public DatabaseDataSourceConnection getObject() throws Exception {
59  		Assert.notNull(this.dataSource, "The dataSource is required");
60  		DatabaseDataSourceConnection dataSourceConnection = new DatabaseDataSourceConnection(
61  				makeTransactionAware(this.dataSource), this.schema, this.username, this.password);
62  		if (this.databaseConfig != null) {
63  			this.databaseConfig.apply(dataSourceConnection.getConfig());
64  		}
65  		return dataSourceConnection;
66  	}
67  
68  	private DataSource makeTransactionAware(DataSource dataSource) {
69  		if ((dataSource instanceof TransactionAwareDataSourceProxy) || !this.transactionAware) {
70  			return dataSource;
71  		}
72  		return new TransactionAwareDataSourceProxy(dataSource);
73  	}
74  
75  	public Class<?> getObjectType() {
76  		return DatabaseDataSourceConnection.class;
77  	}
78  
79  	public boolean isSingleton() {
80  		return true;
81  	}
82  
83  	/**
84  	 * Set the data source that will be used for the {@link DatabaseDataSourceConnection}. This value must be set before
85  	 * the connection can be created.
86  	 * @param dataSource the data source
87  	 */
88  	public void setDataSource(DataSource dataSource) {
89  		Assert.notNull(dataSource, "The dataSource is required.");
90  		this.dataSource = dataSource;
91  	}
92  
93  	/**
94  	 * Set the user name to use when accessing the data source.
95  	 * @param username the user name or <tt>null</tt>
96  	 */
97  	public void setUsername(String username) {
98  		this.username = username;
99  	}
100 
101 	/**
102 	 * Set the password to use when accessing the data source.
103 	 * @param password the password or <tt>null</tt>
104 	 */
105 	public void setPassword(String password) {
106 		this.password = password;
107 	}
108 
109 	/**
110 	 * Set the schema to use when accessing the data source.
111 	 * @param schema the schema or <tt>null</tt>
112 	 */
113 	public void setSchema(String schema) {
114 		this.schema = schema;
115 	}
116 
117 	/**
118 	 * Set an optional {@link DatabaseConfigBean configuration} that will be applied to the newly created
119 	 * {@link DatabaseDataSourceConnection}
120 	 *
121 	 * @param databaseConfig the database configuration or <tt>null</tt> if no additional configuration is required.
122 	 */
123 	public void setDatabaseConfig(DatabaseConfigBean databaseConfig) {
124 		this.databaseConfig = databaseConfig;
125 	}
126 
127 	/**
128 	 * Determines if the {@link IDatabaseConnection} created by this bean should be aware of Spring
129 	 * {@link PlatformTransactionManager}s. Defaults to <tt>true</tt>
130 	 * @param transactionAware If the connection should be transaction aware
131 	 */
132 	public void setTransactionAware(boolean transactionAware) {
133 		this.transactionAware = transactionAware;
134 	}
135 
136 	/**
137 	 * Convenience method that can be used to construct a transaction aware {@link IDatabaseConnection} from a
138 	 * {@link DataSource}.
139 	 * @param dataSource The data source
140 	 * @return A {@link IDatabaseConnection}
141 	 */
142 	public static IDatabaseConnection newConnection(DataSource dataSource) {
143 		try {
144 			return (new DatabaseDataSourceConnectionFactoryBean(dataSource)).getObject();
145 		} catch (Exception ex) {
146 			throw new IllegalStateException(ex);
147 		}
148 	}
149 
150 }