|
Commons DBCP example source code file (TesterDriver.java)
The Commons DBCP TesterDriver.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.dbcp; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.DriverPropertyInfo; import java.sql.SQLException; import java.util.Properties; /** * Mock object implementing the <code>java.sql.Driver interface. * Returns <code>TestConnection's from getConnection methods. * Valid username, password combinations are: * * <table> * <tr> | foo | bar | * <tr>u1 | p1 | * <tr>u2 | p2 | * <tr>username | password | * </table> * * @author Rodney Waldhoff * @author Dirk Verbeeck * @version $Revision: 907288 $ $Date: 2010-02-06 14:42:58 -0500 (Sat, 06 Feb 2010) $ */ public class TesterDriver implements Driver { private static final Properties validUserPasswords = new Properties(); static { try { DriverManager.registerDriver(new TesterDriver()); } catch(Exception e) { } validUserPasswords.put("foo", "bar"); validUserPasswords.put("u1", "p1"); validUserPasswords.put("u2", "p2"); validUserPasswords.put("username", "password"); } /** * TesterDriver specific method to add users to the list of valid users */ public static void addUser(String username, String password) { synchronized (validUserPasswords) { validUserPasswords.put(username, password); } } public boolean acceptsURL(String url) throws SQLException { return CONNECT_STRING.startsWith(url); } private void assertValidUserPassword(String user, String password) throws SQLException { synchronized (validUserPasswords) { String realPassword = validUserPasswords.getProperty(user); if (realPassword == null) { throw new SQLException(user + " is not a valid username."); } if (!realPassword.equals(password)) { throw new SQLException(password + " is not the correct password for " + user + ". The correct password is " + realPassword); } } } public Connection connect(String url, Properties info) throws SQLException { //return (acceptsURL(url) ? new TesterConnection() : null); Connection conn = null; if (acceptsURL(url)) { String username = "test"; String password = "test"; if (info != null) { username = info.getProperty("user"); password = info.getProperty("password"); assertValidUserPassword(username, password); } conn = new TesterConnection(username, password); } return conn; } public int getMajorVersion() { return MAJOR_VERSION; } public int getMinorVersion() { return MINOR_VERSION; } public boolean jdbcCompliant() { return true; } public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { return new DriverPropertyInfo[0]; } protected static final String CONNECT_STRING = "jdbc:apache:commons:testdriver"; // version numbers protected static final int MAJOR_VERSION = 1; protected static final int MINOR_VERSION = 0; }
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.