KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > TesterDriver


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.dbcp;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.Driver JavaDoc;
21 import java.sql.DriverManager JavaDoc;
22 import java.sql.DriverPropertyInfo JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 /**
27  * Mock object implementing the <code>java.sql.Driver</code> interface.
28  * Returns <code>TestConnection</code>'s from getConnection methods.
29  * Valid username, password combinations are:
30  *
31  * <table>
32  * <tr><th>user</th><th>password</th></tr>
33  * <tr><td>foo</td><td>bar</td></tr>
34  * <tr><td>u1</td><td>p1</td></tr>
35  * <tr><td>u2</td><td>p2</td></tr>
36  * <tr><td>username</td><td>password</td></tr>
37  * </table>
38  *
39  * @author Rodney Waldhoff
40  * @author Dirk Verbeeck
41  * @version $Revision: 1.7 $ $Date: 2004/02/28 11:47:52 $
42  */

43 public class TesterDriver implements Driver JavaDoc {
44     private static Properties JavaDoc validUserPasswords = new Properties JavaDoc();
45     static {
46         try {
47             DriverManager.registerDriver(new TesterDriver());
48         } catch(Exception JavaDoc e) {
49         }
50         validUserPasswords.put("foo", "bar");
51         validUserPasswords.put("u1", "p1");
52         validUserPasswords.put("u2", "p2");
53         validUserPasswords.put("username", "password");
54     }
55
56     /**
57      * TesterDriver specific method to add users to the list of valid users
58      */

59     public static void addUser(String JavaDoc username, String JavaDoc password) {
60         validUserPasswords.put(username, password);
61     }
62
63     public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc {
64         return CONNECT_STRING.startsWith(url);
65     }
66
67     private void assertValidUserPassword(String JavaDoc user, String JavaDoc password)
68         throws SQLException JavaDoc {
69         String JavaDoc realPassword = validUserPasswords.getProperty(user);
70         if (realPassword == null)
71         {
72             throw new SQLException JavaDoc(user + " is not a valid username.");
73         }
74         if (!realPassword.equals(password))
75         {
76             throw new SQLException JavaDoc(password +
77                                    " is not the correct password for " +
78                                    user + ". The correct password is " +
79                                    realPassword);
80         }
81     }
82
83     public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc info) throws SQLException JavaDoc {
84         //return (acceptsURL(url) ? new TesterConnection() : null);
85
Connection JavaDoc conn = null;
86         if (acceptsURL(url))
87         {
88             String JavaDoc username = "test";
89             String JavaDoc password = "test";
90             if (info != null)
91             {
92                 username = info.getProperty("user");
93                 password = info.getProperty("password");
94                 assertValidUserPassword(username, password);
95             }
96             conn = new TesterConnection(username, password);
97         }
98         
99         return conn;
100     }
101
102     public int getMajorVersion() {
103         return MAJOR_VERSION;
104     }
105
106     public int getMinorVersion() {
107         return MINOR_VERSION;
108     }
109
110     public boolean jdbcCompliant() {
111         return true;
112     }
113
114     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info) {
115         return new DriverPropertyInfo JavaDoc[0];
116     }
117
118     protected static String JavaDoc CONNECT_STRING = "jdbc:apache:commons:testdriver";
119
120     // version numbers
121
protected static int MAJOR_VERSION = 1;
122     protected static int MINOR_VERSION = 0;
123
124 }
125
Popular Tags