1 /*
2 *
3 * The DbUnit Database Testing Framework
4 * Copyright (C)2002-2004, DbUnit.org
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21 package org.dbunit;
22
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25
26 import org.dbunit.database.DatabaseConnection;
27 import org.dbunit.database.IDatabaseConnection;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32 * DatabaseTester that uses JDBC's Driver Manager to create connections.<br>
33 *
34 * @author Andres Almiray (aalmiray@users.sourceforge.net)
35 * @author Felipe Leme (dbunit@felipeal.net)
36 * @version $Revision$
37 * @since 2.2
38 */
39 public class JdbcDatabaseTester extends AbstractDatabaseTester
40 {
41
42 /**
43 * Logger for this class
44 */
45 private static final Logger logger = LoggerFactory.getLogger(JdbcDatabaseTester.class);
46
47 private String connectionUrl;
48 private String driverClass;
49 private String password;
50 private String username;
51
52 /**
53 * Creates a new JdbcDatabaseTester with the specified properties.<br>
54 * Username and Password are set to null.
55 *
56 * @param driverClass the classname of the JDBC driver to use
57 * @param connectionUrl the connection url
58 * @throws ClassNotFoundException If the given <code>driverClass</code> was not found
59 */
60 public JdbcDatabaseTester( String driverClass, String connectionUrl )
61 throws ClassNotFoundException
62 {
63 this( driverClass, connectionUrl, null, null );
64 }
65
66 /**
67 * Creates a new JdbcDatabaseTester with the specified properties.
68 *
69 * @param driverClass the classname of the JDBC driver to use
70 * @param connectionUrl the connection url
71 * @param username a username that can has access to the database
72 * @param password the user's password
73 * @throws ClassNotFoundException If the given <code>driverClass</code> was not found
74 */
75 public JdbcDatabaseTester( String driverClass, String connectionUrl, String username,
76 String password )
77 throws ClassNotFoundException
78 {
79 this(driverClass, connectionUrl, username, password, null);
80 }
81
82 /**
83 * Creates a new JdbcDatabaseTester with the specified properties.
84 *
85 * @param driverClass the classname of the JDBC driver to use
86 * @param connectionUrl the connection url
87 * @param username a username that can has access to the database - can be <code>null</code>
88 * @param password the user's password - can be <code>null</code>
89 * @param schema the database schema to be tested - can be <code>null</code>
90 * @throws ClassNotFoundException If the given <code>driverClass</code> was not found
91 * @since 2.4.3
92 */
93 public JdbcDatabaseTester( String driverClass, String connectionUrl, String username,
94 String password, String schema )
95 throws ClassNotFoundException
96 {
97 super(schema);
98 this.driverClass = driverClass;
99 this.connectionUrl = connectionUrl;
100 this.username = username;
101 this.password = password;
102
103 assertNotNullNorEmpty( "driverClass", driverClass );
104 Class.forName( driverClass );
105 }
106
107 public IDatabaseConnection getConnection() throws Exception
108 {
109 logger.debug("getConnection() - start");
110
111 assertNotNullNorEmpty( "connectionUrl", connectionUrl );
112 Connection conn = null;
113 if( username == null && password == null ){
114 conn = DriverManager.getConnection( connectionUrl );
115 }else{
116 conn = DriverManager.getConnection( connectionUrl, username, password );
117 }
118 return new DatabaseConnection( conn, getSchema() );
119 }
120
121 public String toString()
122 {
123 final StringBuilder sb = new StringBuilder();
124 sb.append(getClass().getName()).append("[");
125 sb.append("connectionUrl=").append(this.connectionUrl);
126 sb.append(", driverClass=").append(this.driverClass);
127 sb.append(", username=").append(this.username);
128 sb.append(", password=**********");
129 sb.append(", schema=").append(super.getSchema());
130 sb.append("]");
131 return sb.toString();
132 }
133
134 }