001: /* StandardConnectionFactory.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: * www.ddsteps.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License version 2.1 as published by the Free Software Foundation.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, visit
018: * http://www.opensource.org/licenses/lgpl-license.php
019: */
020:
021: package org.ddsteps.dbunit;
022:
023: import javax.sql.DataSource;
024:
025: import org.apache.commons.lang.Validate;
026: import org.dbunit.database.DatabaseConnection;
027: import org.dbunit.database.IDatabaseConnection;
028: import org.springframework.jdbc.datasource.DataSourceUtils;
029:
030: /**
031: * Factory for creating generic Database Connections.
032: * <p>
033: * Has configuration for the schema you want to use.
034: *
035: * @author adam
036: * @version $Id: StandardConnectionFactory.java,v 1.1 2005/09/05 11:42:00
037: * adamskogman Exp $
038: */
039: public class StandardConnectionFactory implements
040: DatabaseConnectionFactory {
041:
042: /**
043: * Property: The schema to use. Default is null, i.e. don't set any specific
044: * schema on the Database Connection.
045: */
046: protected String schema;
047:
048: /**
049: * Dependency: The data source to get connections from. This datasource will
050: * be responsible for producing connections that handle close etc.
051: */
052: protected final DataSource dataSource;
053:
054: /**
055: * Dependency Injection constructor.
056: *
057: * @param dataSource
058: * Dependency. Not null.
059: */
060: public StandardConnectionFactory(DataSource dataSource) {
061: super ();
062:
063: Validate.notNull(dataSource,
064: "Argument dataSource must not be null");
065: this .dataSource = dataSource;
066: }
067:
068: /**
069: * @see org.ddsteps.dbunit.DatabaseConnectionFactory#getConnection()
070: */
071: public IDatabaseConnection getConnection() {
072: return new DatabaseConnection(DataSourceUtils
073: .getConnection(dataSource), schema);
074: }
075:
076: /**
077: * @return Returns the schema.
078: */
079: public String getSchema() {
080: return schema;
081: }
082:
083: /**
084: * The schema to use. Default is null, i.e. don't set any specific schema on
085: * the Database Connection.
086: *
087: * @param schema
088: * The schema to set.
089: */
090: public void setSchema(String schema) {
091: this .schema = schema;
092: }
093:
094: /**
095: * @return Returns the dataSource.
096: */
097: public DataSource getDataSource() {
098: return dataSource;
099: }
100:
101: }
|