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    *      https://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 {@link Driver} interface.
31   * Returns {@code TestConnection}'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  
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              // ignore
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      // version numbers
63      private static final int MAJOR_VERSION = 1;
64  
65      private static final int MINOR_VERSION = 0;
66  
67      /**
68       * TesterDriver specific method to add users to the list of valid users
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          //return (acceptsURL(url) ? new TesterConnection() : null);
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 }