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 {@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)
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         //return (acceptsURL(url) ? new TesterConnection() : null);
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 }