001: package com.mockrunner.mock.jdbc;
002:
003: import java.sql.Array;
004: import java.sql.Blob;
005: import java.sql.CallableStatement;
006: import java.sql.Clob;
007: import java.sql.Connection;
008: import java.sql.DatabaseMetaData; //import java.sql.NClob;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.SQLWarning; //import java.sql.SQLXML;
013: import java.sql.Savepoint;
014: import java.sql.Statement;
015: import java.sql.Struct;
016: import java.util.Collections;
017: import java.util.HashMap;
018: import java.util.Map;
019: import java.util.Properties;
020:
021: import com.mockrunner.base.NestedApplicationException;
022: import com.mockrunner.jdbc.CallableStatementResultSetHandler;
023: import com.mockrunner.jdbc.PreparedStatementResultSetHandler;
024: import com.mockrunner.jdbc.StatementResultSetHandler;
025:
026: /**
027: * Mock implementation of <code>Connection</code>.
028: */
029: public class MockConnection implements Connection {
030: private StatementResultSetHandler statementHandler;
031: private PreparedStatementResultSetHandler preparedStatementHandler;
032: private CallableStatementResultSetHandler callableStatementHandler;
033: private DatabaseMetaData metaData;
034: private Map savepoints;
035: private int savepointCount;
036: private boolean closed;
037: private boolean autoCommit;
038: private boolean readOnly;
039: private int holdability;
040: private int level;
041: private Map typeMap;
042: private String catalog;
043: private int numberCommits;
044: private int numberRollbacks;
045: private Properties clientInfo;
046:
047: public MockConnection() {
048: statementHandler = new StatementResultSetHandler();
049: preparedStatementHandler = new PreparedStatementResultSetHandler();
050: callableStatementHandler = new CallableStatementResultSetHandler();
051: metaData = new MockDatabaseMetaData();
052: ((MockDatabaseMetaData) metaData).setConnection(this );
053: closed = false;
054: autoCommit = false;
055: readOnly = false;
056: holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
057: try {
058: level = metaData.getDefaultTransactionIsolation();
059: } catch (SQLException exc) {
060: throw new NestedApplicationException(exc);
061: }
062: typeMap = new HashMap();
063: savepoints = new HashMap();
064: savepointCount = 0;
065: catalog = null;
066: numberCommits = 0;
067: numberRollbacks = 0;
068: clientInfo = new Properties();
069: }
070:
071: public void setMetaData(DatabaseMetaData metaData)
072: throws SQLException {
073: if (metaData != null
074: && metaData instanceof MockDatabaseMetaData) {
075: ((MockDatabaseMetaData) metaData).setConnection(this );
076: }
077: this .metaData = metaData;
078: }
079:
080: public int getNumberCommits() {
081: return numberCommits;
082: }
083:
084: public int getNumberRollbacks() {
085: return numberRollbacks;
086: }
087:
088: public Map getSavepointMap() {
089: return Collections.unmodifiableMap(savepoints);
090: }
091:
092: public void resetNumberCommits() {
093: numberCommits = 0;
094: }
095:
096: public void resetNumberRollbacks() {
097: numberRollbacks = 0;
098: }
099:
100: public void resetSavepointMap() {
101: savepoints.clear();
102: }
103:
104: public StatementResultSetHandler getStatementResultSetHandler() {
105: return statementHandler;
106: }
107:
108: public PreparedStatementResultSetHandler getPreparedStatementResultSetHandler() {
109: return preparedStatementHandler;
110: }
111:
112: public CallableStatementResultSetHandler getCallableStatementResultSetHandler() {
113: return callableStatementHandler;
114: }
115:
116: public Statement createStatement() throws SQLException {
117: MockStatement statement = new MockStatement(this );
118: getStatementResultSetHandler().addStatement(statement);
119: return statement;
120: }
121:
122: public Statement createStatement(int resultSetType,
123: int resultSetConcurrency) throws SQLException {
124: MockStatement statement = new MockStatement(this ,
125: resultSetType, resultSetConcurrency);
126: getStatementResultSetHandler().addStatement(statement);
127: return statement;
128: }
129:
130: public Statement createStatement(int resultSetType,
131: int resultSetConcurrency, int resultSetHoldability)
132: throws SQLException {
133: MockStatement statement = new MockStatement(this ,
134: resultSetType, resultSetConcurrency,
135: resultSetHoldability);
136: getStatementResultSetHandler().addStatement(statement);
137: return statement;
138: }
139:
140: public CallableStatement prepareCall(String sql)
141: throws SQLException {
142: MockCallableStatement statement = new MockCallableStatement(
143: this , sql);
144: getCallableStatementResultSetHandler().addCallableStatement(
145: statement);
146: return statement;
147: }
148:
149: public CallableStatement prepareCall(String sql, int resultSetType,
150: int resultSetConcurrency) throws SQLException {
151: MockCallableStatement statement = new MockCallableStatement(
152: this , sql, resultSetType, resultSetConcurrency);
153: getCallableStatementResultSetHandler().addCallableStatement(
154: statement);
155: return statement;
156: }
157:
158: public CallableStatement prepareCall(String sql, int resultSetType,
159: int resultSetConcurrency, int resultSetHoldability)
160: throws SQLException {
161: MockCallableStatement statement = new MockCallableStatement(
162: this , sql, resultSetType, resultSetConcurrency,
163: resultSetHoldability);
164: getCallableStatementResultSetHandler().addCallableStatement(
165: statement);
166: return statement;
167: }
168:
169: public PreparedStatement prepareStatement(String sql)
170: throws SQLException {
171: MockPreparedStatement statement = new MockPreparedStatement(
172: this , sql);
173: getPreparedStatementResultSetHandler().addPreparedStatement(
174: statement);
175: return statement;
176: }
177:
178: public PreparedStatement prepareStatement(String sql,
179: int resultSetType, int resultSetConcurrency)
180: throws SQLException {
181: MockPreparedStatement statement = new MockPreparedStatement(
182: this , sql, resultSetType, resultSetConcurrency);
183: getPreparedStatementResultSetHandler().addPreparedStatement(
184: statement);
185: return statement;
186: }
187:
188: public PreparedStatement prepareStatement(String sql,
189: int resultSetType, int resultSetConcurrency,
190: int resultSetHoldability) throws SQLException {
191: MockPreparedStatement statement = new MockPreparedStatement(
192: this , sql, resultSetType, resultSetConcurrency,
193: resultSetHoldability);
194: getPreparedStatementResultSetHandler().addPreparedStatement(
195: statement);
196: return statement;
197: }
198:
199: public PreparedStatement prepareStatement(String sql,
200: int autoGeneratedKeys) throws SQLException {
201: verifyAutoGeneratedKeysParameter(autoGeneratedKeys);
202: MockPreparedStatement statement = new MockPreparedStatement(
203: this , sql,
204: autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS);
205: ;
206: getPreparedStatementResultSetHandler().addPreparedStatement(
207: statement);
208: return statement;
209: }
210:
211: public PreparedStatement prepareStatement(String sql,
212: int[] columnIndexes) throws SQLException {
213: return prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
214: }
215:
216: public PreparedStatement prepareStatement(String sql,
217: String[] columnNames) throws SQLException {
218: return prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
219: }
220:
221: public Blob createBlob() throws SQLException {
222: return new MockBlob(new byte[0]);
223: }
224:
225: public Clob createClob() throws SQLException {
226: return new MockClob("");
227: }
228:
229: /*public NClob createNClob() throws SQLException
230: {
231: return new MockNClob("");
232: }*/
233:
234: /*public SQLXML createSQLXML() throws SQLException
235: {
236: return new MockSQLXML();
237: }*/
238:
239: public Array createArrayOf(String typeName, Object[] elements)
240: throws SQLException {
241: MockArray array = new MockArray(elements);
242: array.setBaseTypeName(typeName);
243: return array;
244: }
245:
246: public Struct createStruct(String typeName, Object[] attributes)
247: throws SQLException {
248: return new MockStruct(typeName, attributes);
249: }
250:
251: private void verifyAutoGeneratedKeysParameter(int autoGeneratedKeys)
252: throws SQLException {
253: if (Statement.RETURN_GENERATED_KEYS != autoGeneratedKeys
254: && Statement.NO_GENERATED_KEYS != autoGeneratedKeys) {
255: throw new SQLException(
256: "autoGeneratedKeys must be either Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS");
257: }
258: }
259:
260: public void close() throws SQLException {
261: closed = true;
262: }
263:
264: public boolean getAutoCommit() throws SQLException {
265: return autoCommit;
266: }
267:
268: public String getCatalog() throws SQLException {
269: return catalog;
270: }
271:
272: public int getHoldability() throws SQLException {
273: return holdability;
274: }
275:
276: public DatabaseMetaData getMetaData() throws SQLException {
277: return metaData;
278: }
279:
280: public int getTransactionIsolation() throws SQLException {
281: return level;
282: }
283:
284: public Map getTypeMap() throws SQLException {
285: return typeMap;
286: }
287:
288: public SQLWarning getWarnings() throws SQLException {
289: return null;
290: }
291:
292: public Properties getClientInfo() throws SQLException {
293: Properties properties = new Properties();
294: properties.putAll(clientInfo);
295: return properties;
296: }
297:
298: public String getClientInfo(String name) throws SQLException {
299: return clientInfo.getProperty(name);
300: }
301:
302: public boolean isClosed() throws SQLException {
303: return closed;
304: }
305:
306: public boolean isReadOnly() throws SQLException {
307: return readOnly;
308: }
309:
310: public boolean isValid(int timeout) throws SQLException {
311: return isClosed();
312: }
313:
314: public String nativeSQL(String sql) throws SQLException {
315: return sql;
316: }
317:
318: public void setAutoCommit(boolean autoCommit) throws SQLException {
319: this .autoCommit = autoCommit;
320: }
321:
322: public void setCatalog(String catalog) throws SQLException {
323: this .catalog = catalog;
324: }
325:
326: public void setHoldability(int holdability) throws SQLException {
327: this .holdability = holdability;
328: }
329:
330: public void setReadOnly(boolean readOnly) throws SQLException {
331: this .readOnly = readOnly;
332: }
333:
334: public Savepoint setSavepoint() throws SQLException {
335: return setSavepoint("");
336: }
337:
338: public Savepoint setSavepoint(String name) throws SQLException {
339: MockSavepoint savePoint = new MockSavepoint(name,
340: savepointCount);
341: savepoints.put(new Integer(savePoint.getSavepointId()),
342: savePoint);
343: savepointCount++;
344: return savePoint;
345: }
346:
347: public void setTransactionIsolation(int level) throws SQLException {
348: this .level = level;
349: }
350:
351: public void setTypeMap(Map typeMap) throws SQLException {
352: this .typeMap = typeMap;
353: }
354:
355: public void setClientInfo(Properties properties) {
356: clientInfo.clear();
357: clientInfo.putAll(properties);
358: }
359:
360: public void setClientInfo(String name, String value) {
361: clientInfo.setProperty(name, value);
362: }
363:
364: public void releaseSavepoint(Savepoint savepoint)
365: throws SQLException {
366: MockSavepoint currentSavepoint = (MockSavepoint) savepoints
367: .get(new Integer(savepoint.getSavepointId()));
368: if (currentSavepoint.isReleased()) {
369: throw new SQLException("Savepoint with id "
370: + currentSavepoint.getSavepointId() + " and name "
371: + currentSavepoint.getSavepointName()
372: + " is released");
373: }
374: currentSavepoint.setReleased(true);
375: }
376:
377: public void commit() throws SQLException {
378: numberCommits++;
379: }
380:
381: public void rollback() throws SQLException {
382: numberRollbacks++;
383: }
384:
385: public void rollback(Savepoint savepoint) throws SQLException {
386: MockSavepoint currentSavepoint = (MockSavepoint) savepoints
387: .get(new Integer(savepoint.getSavepointId()));
388: if (currentSavepoint.isReleased()) {
389: throw new SQLException("Savepoint with id "
390: + currentSavepoint.getSavepointId() + " and name "
391: + currentSavepoint.getSavepointName()
392: + " is released");
393: }
394: currentSavepoint.setRolledBack(true);
395: numberRollbacks++;
396: }
397:
398: public void clearWarnings() throws SQLException {
399:
400: }
401:
402: public boolean isWrapperFor(Class iface) throws SQLException {
403: return false;
404: }
405:
406: public Object unwrap(Class iface) throws SQLException {
407: throw new SQLException("No object found for " + iface);
408: }
409: }
|