001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
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, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import java.sql.Connection;
027: import java.sql.DriverManager;
028:
029: import org.dbunit.database.DatabaseConnection;
030: import org.dbunit.database.IDatabaseConnection;
031:
032: /**
033: * DatabaseTester that uses JDBC's Driver Manager to create connections.<br>
034: *
035: * @author Andres Almiray <aalmiray@users.sourceforge.net>
036: * @author Felipe Leme <dbunit@felipeal.net>
037: */
038: public class JdbcDatabaseTester extends AbstractDatabaseTester {
039:
040: /**
041: * Logger for this class
042: */
043: private static final Logger logger = LoggerFactory
044: .getLogger(JdbcDatabaseTester.class);
045:
046: private String connectionUrl;
047: private String driverClass;
048: private boolean initialized = false;
049: private String password;
050: private String username;
051:
052: /**
053: * Creates a new JdbcDatabaseTester with the specified properties.<br>
054: * Username and Password are set to null.
055: *
056: * @param driverClass the classname of the JDBC driver to use
057: * @param connectionUrl the connection url
058: */
059: public JdbcDatabaseTester(String driverClass, String connectionUrl) {
060: this (driverClass, connectionUrl, null, null);
061: }
062:
063: /**
064: * Creates a new JdbcDatabaseTester with the specified properties.
065: *
066: * @param driverClass the classname of the JDBC driver to use
067: * @param connectionUrl the connection url
068: * @param username a username that can has access to the database
069: * @param password the user's password
070: */
071: public JdbcDatabaseTester(String driverClass, String connectionUrl,
072: String username, String password) {
073: super ();
074: this .driverClass = driverClass;
075: this .connectionUrl = connectionUrl;
076: this .username = username;
077: this .password = password;
078: }
079:
080: public IDatabaseConnection getConnection() throws Exception {
081: logger.debug("getConnection() - start");
082:
083: if (!initialized) {
084: initialize();
085: }
086: assertNotNullNorEmpty("connectionUrl", connectionUrl);
087: Connection conn = null;
088: if (username == null && password == null) {
089: conn = DriverManager.getConnection(connectionUrl);
090: } else {
091: conn = DriverManager.getConnection(connectionUrl, username,
092: password);
093: }
094: if (getSchema() != null) {
095: return new DatabaseConnection(conn, getSchema());
096: }
097: return new DatabaseConnection(conn);
098: }
099:
100: /**
101: * Sets the value of the user's password.
102: */
103: public void setPassword(String password) {
104: logger.debug("setPassword(password=" + password + ") - start");
105:
106: this .password = password;
107: }
108:
109: /**
110: * Sets the value of the username from the connection.
111: */
112: public void setUsername(String username) {
113: logger.debug("setUsername(username=" + username + ") - start");
114:
115: this .username = username;
116: }
117:
118: /**
119: * Verifies the configured properties and initializes the driver.<br>
120: * This method is called by {@link getConnection} if the tester has not been
121: * initialized yet.
122: */
123: protected void initialize() throws Exception {
124: logger.debug("initialize() - start");
125:
126: assertNotNullNorEmpty("driverClass", driverClass);
127: Class.forName(driverClass);
128: initialized = true;
129: }
130:
131: /**
132: * Sets the value of the connection url.
133: */
134: protected void setConnectionUrl(String connectionUrl) {
135: logger.debug("setConnectionUrl(connectionUrl=" + connectionUrl
136: + ") - start");
137:
138: this .connectionUrl = connectionUrl;
139: }
140:
141: /**
142: * Sets the value of the JDBC driver classname.
143: */
144: protected void setDriverClass(String driverClass) {
145: logger.debug("setDriverClass(driverClass=" + driverClass
146: + ") - start");
147:
148: this.driverClass = driverClass;
149: }
150: }
|