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
44 static TesterDriver INSTANCE = new TesterDriver();
45
46 private static final Properties validUserPasswords = new Properties();
47
48 static {
49 try {
50 DriverManager.registerDriver(new TesterDriver());
51 } catch (final Exception e) {
52
53 }
54 validUserPasswords.put("foo", "bar");
55 validUserPasswords.put("u1", "p1");
56 validUserPasswords.put("u2", "p2");
57 validUserPasswords.put("userName", "password");
58 }
59
60 private static final String CONNECT_STRING = "jdbc:apache:commons:testdriver";
61
62
63 private static final int MAJOR_VERSION = 1;
64
65 private static final int MINOR_VERSION = 0;
66
67
68
69
70 public static void addUser(final String userName, final String password) {
71 synchronized (validUserPasswords) {
72 validUserPasswords.put(userName, password);
73 }
74 }
75
76 @Override
77 public boolean acceptsURL(final String url) throws SQLException {
78 return url != null && url.startsWith(CONNECT_STRING);
79 }
80
81 private void assertValidUserPassword(final String userName, final String password)
82 throws SQLException {
83 if (userName == null){
84 throw new SQLException("user name cannot be null.");
85 }
86 synchronized (validUserPasswords) {
87 final String realPassword = validUserPasswords.getProperty(userName);
88 if (realPassword == null) {
89 throw new SQLException(userName + " is not a valid user name.");
90 }
91 if (!realPassword.equals(password)) {
92 throw new SQLException(password + " is not the correct password for " + userName
93 + ". The correct password is " + realPassword);
94 }
95 }
96 }
97
98 @Override
99 public Connection connect(final String url, final Properties info) throws SQLException {
100
101 Connection conn = null;
102 if (acceptsURL(url))
103 {
104 String userName = "test";
105 String password = "test";
106 if (info != null)
107 {
108 userName = info.getProperty(Constants.KEY_USER);
109 password = info.getProperty(Constants.KEY_PASSWORD);
110 if (userName == null) {
111 final String[] parts = url.split(";");
112 userName = parts[1];
113 userName = userName.split("=")[1];
114 password = parts[2];
115 password = password.split("=")[1];
116 }
117 assertValidUserPassword(userName, password);
118 }
119 conn = new TesterConnection(userName, password);
120 }
121
122 return conn;
123 }
124
125 @Override
126 public int getMajorVersion() {
127 return MAJOR_VERSION;
128 }
129
130 @Override
131 public int getMinorVersion() {
132 return MINOR_VERSION;
133 }
134
135 @Override
136 public Logger getParentLogger() throws SQLFeatureNotSupportedException {
137 throw new SQLFeatureNotSupportedException();
138 }
139
140 @Override
141 public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) {
142 return new DriverPropertyInfo[0];
143 }
144 @Override
145 public boolean jdbcCompliant() {
146 return true;
147 }
148
149 }