Source code: jdbc/db/TestDriverImpl.java
1 /**
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio Technologies. Exolab is a registered
23 * trademark of Intalio Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2000 (C) Intalio Technologies Inc. All Rights Reserved.
42 *
43 */
44
45 package jdbc.db;
46
47 import java.sql.*;
48 import java.util.*;
49
50 /**
51 * The test jdbc driver.
52 * <P>
53 * The number of connections created is across all TestDriverImpl instances
54 */
55 public final class TestDriverImpl
56 implements Driver
57 {
58 /**
59 * The start of a valid test url (header + subprotocol)
60 */
61 public static final String TEST_URL_START = "jdbc:tyrex_test";
62
63
64 /**
65 * Empty driver property array
66 */
67 private static final DriverPropertyInfo[] emptyDriverPropertyInfo = new DriverPropertyInfo[0];
68
69
70 /**
71 * The Test driver major version
72 */
73 private static final int TEST_MAJOR_VERSION = 1;
74
75
76 /**
77 * The number of connections created
78 */
79 private static int _numberOfCreatedConnections = 0;
80
81
82 /**
83 * The Test driver minor version
84 */
85 private static final int TEST_MINOR_VERSION = 0;
86
87 /**
88 * The last connection returned
89 */
90 private TestConnectionImpl _lastConnection;
91
92
93 static {
94 try {
95 DriverManager.registerDriver(new TestDriverImpl());
96 }
97 catch(Exception e) {
98 System.out.println("Failed to register TestDriverImpl.");
99 e.printStackTrace();
100 }
101 }
102
103
104 /**
105 * The default constructor
106 */
107 public TestDriverImpl()
108 {
109 }
110
111 /**
112 * Return a <code>Connection</code> object that represents a
113 * connection to the URL.
114 *
115 * @param url the URL of the database to which to connect
116 * @param info a list of arbitrary string tag/value pairs as
117 * connection arguments.
118 * @return a <code>Connection</code> object that represents a
119 * connection to the URL
120 * @exception SQLException if a database access error occurs
121 */
122 public Connection connect(String url, Properties info)
123 throws SQLException
124 {
125 if (!acceptsURL(url)) {
126 return null;
127 }
128
129 TestConnectionImpl connection = new TestConnectionImpl(url, info);
130 ++_numberOfCreatedConnections;
131 _lastConnection = connection;
132 return connection;
133 }
134
135
136 /**
137 * Return the last connection that was created.
138 * Can be null if no connections were created.
139 *
140 * @return the last connection that was created.
141 */
142 public TestConnectionImpl getLastConnection()
143 {
144 return _lastConnection;
145 }
146
147
148 /**
149 * Return the number of connections created by
150 * this driver instance.
151 *
152 * @return the number of connections created by
153 * this driver instance.
154 */
155 public int getNumberOfCreatedConnections()
156 {
157 return _numberOfCreatedConnections;
158 }
159
160
161 /**
162 * Clear the number of connections created by
163 * this driver instance.
164 */
165 public void clearNumberOfCreatedConnections()
166 {
167 _numberOfCreatedConnections = 0;
168 }
169
170 /**
171 * Returns true if the driver thinks that it can open a connection
172 * to the given URL.
173 *
174 * @param url the URL of the database
175 * @return true if this driver can connect to the given URL
176 * @exception SQLException if a database access error occurs
177 */
178 public boolean acceptsURL(String url)
179 throws SQLException
180 {
181 return null == url ? false : url.startsWith(TEST_URL_START);
182 }
183
184
185 /**
186 * Return an empty DriverPropertyInfo array.
187 *
188 * @param url the URL of the database to which to connect
189 * @param info a proposed list of tag/value pairs that will be sent on
190 * connect open
191 * @return an empty DriverPropertyInfo array.
192 * @exception SQLException if a database access error occurs
193 */
194 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
195 throws SQLException
196 {
197 return emptyDriverPropertyInfo;
198 }
199
200
201 /**
202 * Gets the driver's major version number.
203 *
204 * @return this driver's major version number
205 */
206 public int getMajorVersion()
207 {
208 return TEST_MAJOR_VERSION;
209 }
210
211 /**
212 * Gets the driver's minor version number.
213 *
214 * @return this driver's minor version number
215 */
216 public int getMinorVersion()
217 {
218 return TEST_MINOR_VERSION;
219 }
220
221
222 /**
223 * Test driver is not jdbc compliant (not yet at least :-)).
224 *
225 * @return false;
226 */
227 public boolean jdbcCompliant()
228 {
229 return false;
230 }
231 }
232