View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
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   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.testfoundation;
20  
21  import java.sql.Connection;
22  import java.sql.SQLException;
23  
24  import org.dbunit.DatabaseUnitException;
25  import org.dbunit.database.DatabaseConnection;
26  import org.dbunit.database.DatabaseSequenceFilter;
27  import org.dbunit.database.QueryDataSet;
28  import org.dbunit.dataset.FilteredDataSet;
29  import org.dbunit.dataset.IDataSet;
30  import org.dbunit.dataset.filter.ITableFilter;
31  
32  /**
33   * A factory for creating DataSetConnection objects.
34   */
35  public final class DataSetConnectionFactory {
36  
37  	/**
38  	 * Instantiates a new data set connection factory.
39  	 */
40  	public DataSetConnectionFactory() {
41  		super();
42  	}
43  
44  	/**
45  	 * Gets the data set.
46  	 *
47  	 * @param connection
48  	 *            the connection
49  	 * @return the data set
50  	 * @throws DatasetFactoryException
51  	 *             the dataset factory exception
52  	 */
53  	public IDataSet getDataSet(final Connection connection) throws DatasetFactoryException {
54  		DatabaseConnection databaseConnection;
55  		try {
56  			databaseConnection = new DatabaseConnection(connection);
57  			final ITableFilter filter = new DatabaseSequenceFilter(databaseConnection);
58  			return new FilteredDataSet(filter, databaseConnection.createDataSet());
59  		} catch (DatabaseUnitException | SQLException e) {
60  			throw new DatasetFactoryException(e);
61  		}
62  	}
63  
64  	
65  	/**
66  	 * Gets the query dataset.
67  	 *
68  	 * @param connection
69  	 *            the connection
70  	 * @return the query dataset
71  	 * @throws DatasetFactoryException
72  	 *             the dataset factory exception
73  	 */
74  	public QueryDataSet getQueryDataset(final Connection connection) throws DatasetFactoryException {
75  		try {
76  			return new QueryDataSet(new DatabaseConnection(connection));
77  		} catch (DatabaseUnitException e) {
78  			throw new DatasetFactoryException(e);
79  		}
80  	}
81  
82  }