001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp;
019:
020: import java.sql.Connection;
021: import java.sql.Driver;
022: import java.sql.DriverManager;
023: import java.sql.DriverPropertyInfo;
024: import java.sql.SQLException;
025: import java.util.Properties;
026:
027: /**
028: * Mock object implementing the <code>java.sql.Driver</code> interface.
029: * Returns <code>TestConnection</code>'s from getConnection methods.
030: * Valid username, password combinations are:
031: *
032: * <table>
033: * <tr><th>user</th><th>password</th></tr>
034: * <tr><td>foo</td><td>bar</td></tr>
035: * <tr><td>u1</td><td>p1</td></tr>
036: * <tr><td>u2</td><td>p2</td></tr>
037: * <tr><td>username</td><td>password</td></tr>
038: * </table>
039: *
040: * @author Rodney Waldhoff
041: * @author Dirk Verbeeck
042: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
043: */
044: public class TesterDriver implements Driver {
045: private static Properties validUserPasswords = new Properties();
046: static {
047: try {
048: DriverManager.registerDriver(new TesterDriver());
049: } catch (Exception e) {
050: }
051: validUserPasswords.put("foo", "bar");
052: validUserPasswords.put("u1", "p1");
053: validUserPasswords.put("u2", "p2");
054: validUserPasswords.put("username", "password");
055: }
056:
057: /**
058: * TesterDriver specific method to add users to the list of valid users
059: */
060: public static void addUser(String username, String password) {
061: validUserPasswords.put(username, password);
062: }
063:
064: public boolean acceptsURL(String url) throws SQLException {
065: return CONNECT_STRING.startsWith(url);
066: }
067:
068: private void assertValidUserPassword(String user, String password)
069: throws SQLException {
070: String realPassword = validUserPasswords.getProperty(user);
071: if (realPassword == null) {
072: throw new SQLException(user + " is not a valid username.");
073: }
074: if (!realPassword.equals(password)) {
075: throw new SQLException(password
076: + " is not the correct password for " + user
077: + ". The correct password is " + realPassword);
078: }
079: }
080:
081: public Connection connect(String url, Properties info)
082: throws SQLException {
083: //return (acceptsURL(url) ? new TesterConnection() : null);
084: Connection conn = null;
085: if (acceptsURL(url)) {
086: String username = "test";
087: String password = "test";
088: if (info != null) {
089: username = info.getProperty("user");
090: password = info.getProperty("password");
091: assertValidUserPassword(username, password);
092: }
093: conn = new TesterConnection(username, password);
094: }
095:
096: return conn;
097: }
098:
099: public int getMajorVersion() {
100: return MAJOR_VERSION;
101: }
102:
103: public int getMinorVersion() {
104: return MINOR_VERSION;
105: }
106:
107: public boolean jdbcCompliant() {
108: return true;
109: }
110:
111: public DriverPropertyInfo[] getPropertyInfo(String url,
112: Properties info) {
113: return new DriverPropertyInfo[0];
114: }
115:
116: protected static String CONNECT_STRING = "jdbc:apache:commons:testdriver";
117:
118: // version numbers
119: protected static int MAJOR_VERSION = 1;
120: protected static int MINOR_VERSION = 0;
121:
122: }
|