1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.dbcp2;
19
20 import java.sql.Connection;
21 import java.sql.Driver;
22 import java.sql.DriverManager;
23 import java.sql.DriverPropertyInfo;
24 import java.sql.SQLException;
25 import java.sql.SQLFeatureNotSupportedException;
26 import java.util.Properties;
27 import java.util.logging.Logger;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class TesterDriver implements Driver {
43 private static final Properties validUserPasswords = new Properties();
44 static {
45 try {
46 DriverManager.registerDriver(new TesterDriver());
47 } catch (final Exception e) {
48
49 }
50 validUserPasswords.put("foo", "bar");
51 validUserPasswords.put("u1", "p1");
52 validUserPasswords.put("u2", "p2");
53 validUserPasswords.put("userName", "password");
54 }
55
56 private static final String CONNECT_STRING = "jdbc:apache:commons:testdriver";
57
58
59 private static final int MAJOR_VERSION = 1;
60
61 private static final int MINOR_VERSION = 0;
62
63
64
65
66 public static void addUser(final String userName, final String password) {
67 synchronized (validUserPasswords) {
68 validUserPasswords.put(userName, password);
69 }
70 }
71
72 @Override
73 public boolean acceptsURL(final String url) throws SQLException {
74 return url != null && url.startsWith(CONNECT_STRING);
75 }
76
77 private void assertValidUserPassword(final String userName, final String password)
78 throws SQLException {
79 if (userName == null){
80 throw new SQLException("user name cannot be null.");
81 }
82 synchronized (validUserPasswords) {
83 final String realPassword = validUserPasswords.getProperty(userName);
84 if (realPassword == null) {
85 throw new SQLException(userName + " is not a valid user name.");
86 }
87 if (!realPassword.equals(password)) {
88 throw new SQLException(password + " is not the correct password for " + userName
89 + ". The correct password is " + realPassword);
90 }
91 }
92 }
93
94 @Override
95 public Connection connect(final String url, final Properties info) throws SQLException {
96
97 Connection conn = null;
98 if (acceptsURL(url))
99 {
100 String userName = "test";
101 String password = "test";
102 if (info != null)
103 {
104 userName = info.getProperty(Constants.KEY_USER);
105 password = info.getProperty(Constants.KEY_PASSWORD);
106 if (userName == null) {
107 final String[] parts = url.split(";");
108 userName = parts[1];
109 userName = userName.split("=")[1];
110 password = parts[2];
111 password = password.split("=")[1];
112 }
113 assertValidUserPassword(userName, password);
114 }
115 conn = new TesterConnection(userName, password);
116 }
117
118 return conn;
119 }
120
121 @Override
122 public int getMajorVersion() {
123 return MAJOR_VERSION;
124 }
125
126 @Override
127 public int getMinorVersion() {
128 return MINOR_VERSION;
129 }
130
131 @Override
132 public Logger getParentLogger() throws SQLFeatureNotSupportedException {
133 throw new SQLFeatureNotSupportedException();
134 }
135
136 @Override
137 public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) {
138 return new DriverPropertyInfo[0];
139 }
140 @Override
141 public boolean jdbcCompliant() {
142 return true;
143 }
144
145 }