KickJava   Java API By Example, From Geeks To Geeks.

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


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.CallableStatement JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DatabaseMetaData JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.SQLWarning JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * A dummy {@link Connection}, for testing purposes.
30  *
31  * @author Rodney Waldhoff
32  * @author Dirk Verbeeck
33  * @version $Revision: 1.14 $ $Date: 2004/05/20 13:07:43 $
34  */

35 public class TesterConnection implements Connection JavaDoc {
36     protected boolean _open = true;
37     protected boolean _autoCommit = true;
38     protected int _transactionIsolation = 1;
39     protected DatabaseMetaData JavaDoc _metaData = null;
40     protected String JavaDoc _catalog = null;
41     protected Map JavaDoc _typeMap = null;
42     protected boolean _readOnly = false;
43     protected SQLWarning JavaDoc warnings = null;
44     protected String JavaDoc username = null;
45     protected String JavaDoc password = null;
46     protected Exception JavaDoc failure;
47
48     public TesterConnection(String JavaDoc username, String JavaDoc password) {
49         this.username = username;
50         this.password = password;
51     }
52     
53     public String JavaDoc getUsername() {
54         return this.username;
55     }
56
57     public void setWarnings(SQLWarning JavaDoc warning) {
58         this.warnings = warning;
59     }
60
61     public void clearWarnings() throws SQLException JavaDoc {
62         checkOpen();
63         warnings = null;
64     }
65
66     public void close() throws SQLException JavaDoc {
67         checkOpen();
68         _open = false;
69     }
70
71     public void commit() throws SQLException JavaDoc {
72         checkOpen();
73         if (isReadOnly()) {
74             throw new SQLException JavaDoc("Cannot commit a readonly connection");
75         }
76     }
77
78     public Statement JavaDoc createStatement() throws SQLException JavaDoc {
79         checkOpen();
80         return new TesterStatement(this);
81     }
82
83     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
84         checkOpen();
85         return new TesterStatement(this);
86     }
87
88     public boolean getAutoCommit() throws SQLException JavaDoc {
89         checkOpen();
90         return _autoCommit;
91     }
92
93     public String JavaDoc getCatalog() throws SQLException JavaDoc {
94         checkOpen();
95         return _catalog;
96     }
97
98     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
99         checkOpen();
100         return _metaData;
101     }
102
103     public int getTransactionIsolation() throws SQLException JavaDoc {
104         checkOpen();
105         return _transactionIsolation;
106     }
107
108     public Map JavaDoc getTypeMap() throws SQLException JavaDoc {
109         checkOpen();
110         return _typeMap;
111     }
112
113     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
114         checkOpen();
115         return warnings;
116     }
117
118     public boolean isClosed() throws SQLException JavaDoc {
119         checkFailure();
120         return !_open;
121     }
122
123     public boolean isReadOnly() throws SQLException JavaDoc {
124         checkOpen();
125         return _readOnly;
126     }
127
128     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
129         checkOpen();
130         return sql;
131     }
132
133     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
134         checkOpen();
135         if ("warning".equals(sql)) {
136             setWarnings(new SQLWarning JavaDoc("warning in prepareCall"));
137         }
138         return null;
139     }
140
141     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
142         checkOpen();
143         return null;
144     }
145
146     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
147         checkOpen();
148         if("null".equals(sql)) {
149             return null;
150         } if("invalid".equals(sql)) {
151             throw new SQLException JavaDoc("invalid query");
152         } if ("broken".equals(sql)) {
153             throw new SQLException JavaDoc("broken connection");
154         }
155         return new TesterPreparedStatement(this, sql);
156     }
157
158     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
159         checkOpen();
160         return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
161     }
162
163     public void rollback() throws SQLException JavaDoc {
164         checkOpen();
165         if (isReadOnly()) {
166             throw new SQLException JavaDoc("Cannot rollback a readonly connection");
167         }
168     }
169
170     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
171         checkOpen();
172         _autoCommit = autoCommit;
173     }
174
175     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
176         checkOpen();
177         _catalog = catalog;
178     }
179
180     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
181         checkOpen();
182         _readOnly = readOnly;
183     }
184
185     public void setTransactionIsolation(int level) throws SQLException JavaDoc {
186         checkOpen();
187         _transactionIsolation = level;
188     }
189
190     public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc {
191         checkOpen();
192         _typeMap = map;
193     }
194
195     protected void checkOpen() throws SQLException JavaDoc {
196         if(!_open) {
197             throw new SQLException JavaDoc("Connection is closed.");
198         }
199         checkFailure();
200     }
201     
202     protected void checkFailure() throws SQLException JavaDoc {
203         if (failure != null) {
204             throw new SQLNestedException("TesterConnection failure", failure);
205         }
206     }
207     
208     public void setFailure(Exception JavaDoc failure) {
209         this.failure = failure;
210     }
211     
212     // ------------------- JDBC 3.0 -----------------------------------------
213
// Will be commented by the build process on a JDBC 2.0 system
214

215 /* JDBC_3_ANT_KEY_BEGIN */
216
217     public int getHoldability() throws SQLException JavaDoc {
218         throw new SQLException JavaDoc("Not implemented.");
219     }
220
221     public void setHoldability(int holdability) throws SQLException JavaDoc {
222         throw new SQLException JavaDoc("Not implemented.");
223     }
224
225     public java.sql.Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
226         throw new SQLException JavaDoc("Not implemented.");
227     }
228
229     public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
230         throw new SQLException JavaDoc("Not implemented.");
231     }
232
233     public void rollback(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
234         throw new SQLException JavaDoc("Not implemented.");
235     }
236
237     public void releaseSavepoint(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
238         throw new SQLException JavaDoc("Not implemented.");
239     }
240
241     public Statement JavaDoc createStatement(int resultSetType,
242                                      int resultSetConcurrency,
243                                      int resultSetHoldability)
244         throws SQLException JavaDoc {
245         throw new SQLException JavaDoc("Not implemented.");
246     }
247
248     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,
249                                               int resultSetConcurrency,
250                                               int resultSetHoldability)
251         throws SQLException JavaDoc {
252         throw new SQLException JavaDoc("Not implemented.");
253     }
254
255     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
256                                          int resultSetConcurrency,
257                                          int resultSetHoldability)
258         throws SQLException JavaDoc {
259         throw new SQLException JavaDoc("Not implemented.");
260     }
261
262     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
263         throws SQLException JavaDoc {
264         throw new SQLException JavaDoc("Not implemented.");
265     }
266
267     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int columnIndexes[])
268         throws SQLException JavaDoc {
269         throw new SQLException JavaDoc("Not implemented.");
270     }
271
272     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc columnNames[])
273         throws SQLException JavaDoc {
274         throw new SQLException JavaDoc("Not implemented.");
275     }
276
277 /* JDBC_3_ANT_KEY_END */
278 }
279
Popular Tags