001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp;
019:
020: import java.sql.CallableStatement;
021: import java.sql.Connection;
022: import java.sql.DatabaseMetaData;
023: import java.sql.PreparedStatement;
024: import java.sql.SQLException;
025: import java.sql.SQLWarning;
026: import java.sql.Statement;
027: import java.util.Map;
028:
029: /**
030: * A dummy {@link Connection}, for testing purposes.
031: *
032: * @author Rodney Waldhoff
033: * @author Dirk Verbeeck
034: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
035: */
036: public class TesterConnection implements Connection {
037: protected boolean _open = true;
038: protected boolean _autoCommit = true;
039: protected int _transactionIsolation = 1;
040: protected DatabaseMetaData _metaData = null;
041: protected String _catalog = null;
042: protected Map _typeMap = null;
043: protected boolean _readOnly = false;
044: protected SQLWarning warnings = null;
045: protected String username = null;
046: protected String password = null;
047: protected Exception failure;
048:
049: public TesterConnection(String username, String password) {
050: this .username = username;
051: this .password = password;
052: }
053:
054: public String getUsername() {
055: return this .username;
056: }
057:
058: public void setWarnings(SQLWarning warning) {
059: this .warnings = warning;
060: }
061:
062: public void clearWarnings() throws SQLException {
063: checkOpen();
064: warnings = null;
065: }
066:
067: public void close() throws SQLException {
068: checkOpen();
069: _open = false;
070: }
071:
072: public void commit() throws SQLException {
073: checkOpen();
074: if (isReadOnly()) {
075: throw new SQLException(
076: "Cannot commit a readonly connection");
077: }
078: }
079:
080: public Statement createStatement() throws SQLException {
081: checkOpen();
082: return new TesterStatement(this );
083: }
084:
085: public Statement createStatement(int resultSetType,
086: int resultSetConcurrency) throws SQLException {
087: checkOpen();
088: return new TesterStatement(this );
089: }
090:
091: public boolean getAutoCommit() throws SQLException {
092: checkOpen();
093: return _autoCommit;
094: }
095:
096: public String getCatalog() throws SQLException {
097: checkOpen();
098: return _catalog;
099: }
100:
101: public DatabaseMetaData getMetaData() throws SQLException {
102: checkOpen();
103: return _metaData;
104: }
105:
106: public int getTransactionIsolation() throws SQLException {
107: checkOpen();
108: return _transactionIsolation;
109: }
110:
111: public Map getTypeMap() throws SQLException {
112: checkOpen();
113: return _typeMap;
114: }
115:
116: public SQLWarning getWarnings() throws SQLException {
117: checkOpen();
118: return warnings;
119: }
120:
121: public boolean isClosed() throws SQLException {
122: checkFailure();
123: return !_open;
124: }
125:
126: public boolean isReadOnly() throws SQLException {
127: checkOpen();
128: return _readOnly;
129: }
130:
131: public String nativeSQL(String sql) throws SQLException {
132: checkOpen();
133: return sql;
134: }
135:
136: public CallableStatement prepareCall(String sql)
137: throws SQLException {
138: checkOpen();
139: if ("warning".equals(sql)) {
140: setWarnings(new SQLWarning("warning in prepareCall"));
141: }
142: return null;
143: }
144:
145: public CallableStatement prepareCall(String sql, int resultSetType,
146: int resultSetConcurrency) throws SQLException {
147: checkOpen();
148: return null;
149: }
150:
151: public PreparedStatement prepareStatement(String sql)
152: throws SQLException {
153: checkOpen();
154: if ("null".equals(sql)) {
155: return null;
156: }
157: if ("invalid".equals(sql)) {
158: throw new SQLException("invalid query");
159: }
160: if ("broken".equals(sql)) {
161: throw new SQLException("broken connection");
162: }
163: return new TesterPreparedStatement(this , sql);
164: }
165:
166: public PreparedStatement prepareStatement(String sql,
167: int resultSetType, int resultSetConcurrency)
168: throws SQLException {
169: checkOpen();
170: return new TesterPreparedStatement(this , sql, resultSetType,
171: resultSetConcurrency);
172: }
173:
174: public void rollback() throws SQLException {
175: checkOpen();
176: if (isReadOnly()) {
177: throw new SQLException(
178: "Cannot rollback a readonly connection");
179: }
180: }
181:
182: public void setAutoCommit(boolean autoCommit) throws SQLException {
183: checkOpen();
184: _autoCommit = autoCommit;
185: }
186:
187: public void setCatalog(String catalog) throws SQLException {
188: checkOpen();
189: _catalog = catalog;
190: }
191:
192: public void setReadOnly(boolean readOnly) throws SQLException {
193: checkOpen();
194: _readOnly = readOnly;
195: }
196:
197: public void setTransactionIsolation(int level) throws SQLException {
198: checkOpen();
199: _transactionIsolation = level;
200: }
201:
202: public void setTypeMap(Map map) throws SQLException {
203: checkOpen();
204: _typeMap = map;
205: }
206:
207: protected void checkOpen() throws SQLException {
208: if (!_open) {
209: throw new SQLException("Connection is closed.");
210: }
211: checkFailure();
212: }
213:
214: protected void checkFailure() throws SQLException {
215: if (failure != null) {
216: throw new SQLNestedException("TesterConnection failure",
217: failure);
218: }
219: }
220:
221: public void setFailure(Exception failure) {
222: this .failure = failure;
223: }
224:
225: // ------------------- JDBC 3.0 -----------------------------------------
226: // Will be commented by the build process on a JDBC 2.0 system
227:
228: /* JDBC_3_ANT_KEY_BEGIN */
229:
230: public int getHoldability() throws SQLException {
231: throw new SQLException("Not implemented.");
232: }
233:
234: public void setHoldability(int holdability) throws SQLException {
235: throw new SQLException("Not implemented.");
236: }
237:
238: public java.sql.Savepoint setSavepoint() throws SQLException {
239: throw new SQLException("Not implemented.");
240: }
241:
242: public java.sql.Savepoint setSavepoint(String name)
243: throws SQLException {
244: throw new SQLException("Not implemented.");
245: }
246:
247: public void rollback(java.sql.Savepoint savepoint)
248: throws SQLException {
249: throw new SQLException("Not implemented.");
250: }
251:
252: public void releaseSavepoint(java.sql.Savepoint savepoint)
253: throws SQLException {
254: throw new SQLException("Not implemented.");
255: }
256:
257: public Statement createStatement(int resultSetType,
258: int resultSetConcurrency, int resultSetHoldability)
259: throws SQLException {
260: throw new SQLException("Not implemented.");
261: }
262:
263: public PreparedStatement prepareStatement(String sql,
264: int resultSetType, int resultSetConcurrency,
265: int resultSetHoldability) throws SQLException {
266: throw new SQLException("Not implemented.");
267: }
268:
269: public CallableStatement prepareCall(String sql, int resultSetType,
270: int resultSetConcurrency, int resultSetHoldability)
271: throws SQLException {
272: throw new SQLException("Not implemented.");
273: }
274:
275: public PreparedStatement prepareStatement(String sql,
276: int autoGeneratedKeys) throws SQLException {
277: throw new SQLException("Not implemented.");
278: }
279:
280: public PreparedStatement prepareStatement(String sql,
281: int columnIndexes[]) throws SQLException {
282: throw new SQLException("Not implemented.");
283: }
284:
285: public PreparedStatement prepareStatement(String sql,
286: String columnNames[]) throws SQLException {
287: throw new SQLException("Not implemented.");
288: }
289:
290: /* JDBC_3_ANT_KEY_END */
291: }
|