View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Mock object implementing the <code>java.sql.Driver</code> interface.
31   * Returns <code>TestConnection</code>'s from getConnection methods.
32   * Valid user name, password combinations are:
33   *
34   * <table summary="valid credentials">
35   * <tr><th>user</th><th>password</th></tr>
36   * <tr><td>foo</td><td>bar</td></tr>
37   * <tr><td>u1</td><td>p1</td></tr>
38   * <tr><td>u2</td><td>p2</td></tr>
39   * <tr><td>username</td><td>password</td></tr>
40   * </table>
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              // ignore
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      // version numbers
59      private static final int MAJOR_VERSION = 1;
60  
61      private static final int MINOR_VERSION = 0;
62  
63      /**
64       * TesterDriver specific method to add users to the list of valid users
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          //return (acceptsURL(url) ? new TesterConnection() : null);
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 }