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