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) throws SQLException {
82 if (userName == null) {
83 throw new SQLException("user name cannot be null.");
84 }
85 synchronized (validUserPasswords) {
86 final String realPassword = validUserPasswords.getProperty(userName);
87 if (realPassword == null) {
88 throw new SQLException(userName + " is not a valid user name.");
89 }
90 if (!realPassword.equals(password)) {
91 throw new SQLException(password + " is not the correct password for " + userName + ". The correct password is " + realPassword);
92 }
93 }
94 }
95
96 @Override
97 public Connection connect(final String url, final Properties info) throws SQLException {
98
99 Connection conn = null;
100 if (acceptsURL(url))
101 {
102 String userName = "test";
103 String password = "test";
104 if (info != null)
105 {
106 userName = info.getProperty(Constants.KEY_USER);
107 password = info.getProperty(Constants.KEY_PASSWORD);
108 if (userName == null) {
109 final String[] parts = url.split(";");
110 userName = parts[1];
111 userName = userName.split("=")[1];
112 password = parts[2];
113 password = password.split("=")[1];
114 }
115 assertValidUserPassword(userName, password);
116 }
117 conn = new TesterConnection(userName, password);
118 }
119
120 return conn;
121 }
122
123 @Override
124 public int getMajorVersion() {
125 return MAJOR_VERSION;
126 }
127
128 @Override
129 public int getMinorVersion() {
130 return MINOR_VERSION;
131 }
132
133 @Override
134 public Logger getParentLogger() throws SQLFeatureNotSupportedException {
135 throw new SQLFeatureNotSupportedException();
136 }
137
138 @Override
139 public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) {
140 return new DriverPropertyInfo[0];
141 }
142 @Override
143 public boolean jdbcCompliant() {
144 return true;
145 }
146
147 }