1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.Connection ; 20 import java.sql.Driver ; 21 import java.sql.DriverManager ; 22 import java.sql.DriverPropertyInfo ; 23 import java.sql.SQLException ; 24 import java.util.Properties ; 25 26 43 public class TesterDriver implements Driver { 44 private static Properties validUserPasswords = new Properties (); 45 static { 46 try { 47 DriverManager.registerDriver(new TesterDriver()); 48 } catch(Exception e) { 49 } 50 validUserPasswords.put("foo", "bar"); 51 validUserPasswords.put("u1", "p1"); 52 validUserPasswords.put("u2", "p2"); 53 validUserPasswords.put("username", "password"); 54 } 55 56 59 public static void addUser(String username, String password) { 60 validUserPasswords.put(username, password); 61 } 62 63 public boolean acceptsURL(String url) throws SQLException { 64 return CONNECT_STRING.startsWith(url); 65 } 66 67 private void assertValidUserPassword(String user, String password) 68 throws SQLException { 69 String realPassword = validUserPasswords.getProperty(user); 70 if (realPassword == null) 71 { 72 throw new SQLException (user + " is not a valid username."); 73 } 74 if (!realPassword.equals(password)) 75 { 76 throw new SQLException (password + 77 " is not the correct password for " + 78 user + ". The correct password is " + 79 realPassword); 80 } 81 } 82 83 public Connection connect(String url, Properties info) throws SQLException { 84 Connection conn = null; 86 if (acceptsURL(url)) 87 { 88 String username = "test"; 89 String password = "test"; 90 if (info != null) 91 { 92 username = info.getProperty("user"); 93 password = info.getProperty("password"); 94 assertValidUserPassword(username, password); 95 } 96 conn = new TesterConnection(username, password); 97 } 98 99 return conn; 100 } 101 102 public int getMajorVersion() { 103 return MAJOR_VERSION; 104 } 105 106 public int getMinorVersion() { 107 return MINOR_VERSION; 108 } 109 110 public boolean jdbcCompliant() { 111 return true; 112 } 113 114 public DriverPropertyInfo [] getPropertyInfo(String url, Properties info) { 115 return new DriverPropertyInfo [0]; 116 } 117 118 protected static String CONNECT_STRING = "jdbc:apache:commons:testdriver"; 119 120 protected static int MAJOR_VERSION = 1; 122 protected static int MINOR_VERSION = 0; 123 124 } 125 | Popular Tags |