View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jdbc;
5   
6   import static org.hamcrest.MatcherAssert.assertThat;
7   
8   import java.sql.CallableStatement;
9   import java.sql.Connection;
10  import java.sql.DatabaseMetaData;
11  import java.sql.PreparedStatement;
12  import java.sql.SQLException;
13  import java.sql.SQLWarning;
14  import java.sql.Savepoint;
15  import java.sql.Statement;
16  import java.util.ArrayList;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Properties;
20  
21  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
22  import org.apache.commons.lang.builder.ToStringStyle;
23  import org.hamcrest.core.Is;
24  import org.hamcrest.core.IsNot;
25  
26  import com.github.smokestack.exception.NeedsMockDefinitionException;
27  import com.github.smokestack.exception.NotYetImplementedException;
28  
29  /**
30   * @author gliptak
31   *
32   */
33  public class MockConnection implements Connection {
34  
35  	private String url;
36  	private Properties info;
37  	private List<MockStatement> mockStatements=new ArrayList<MockStatement>();
38  	private List<MockPreparedStatement> mockPreparedStatements=new ArrayList<MockPreparedStatement>();
39  	
40  	public enum ConnectionState {NEW, CLOSE};
41  	protected ConnectionState mockConnectionState=ConnectionState.NEW;
42  
43  	public enum AutoCommitState {ENABLED, DISABLED};
44  	protected AutoCommitState autoCommitState=AutoCommitState.ENABLED;
45  
46  	public enum TransactionState {NEW, ROLLBACK, COMMIT, AUTOROLLBACK, AUTOCOMMIT};
47  	public TransactionState mockTransactionState=TransactionState.AUTOCOMMIT;
48  	private int holdability;
49  	private boolean readOnly;
50  	private int TransactionIsolationLevel;
51  	private String catalog;
52  	private int transactionIsolation;
53  	private String nativeSQL;
54  	private int resultSetType;
55  	private int resultSetConcurrency;
56  	private int autoGeneratedKeys;
57  		
58  	public MockConnection(String url, Properties info) {
59  		this.url=url;
60  		this.info=info;
61  	}
62  
63  	public MockConnection() {
64  	}
65  
66  	/* (non-Javadoc)
67  	 * @see java.sql.Connection#clearWarnings()
68  	 */
69  	public void clearWarnings() throws SQLException {
70  		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
71  		throw new NotYetImplementedException();
72  	}
73  
74  	/* (non-Javadoc)
75  	 * @see java.sql.Connection#close()
76  	 */
77  	public void close() throws SQLException {
78  		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
79  		_close();
80  		for(MockStatement m: mockStatements){
81  			if(m.mockState != MockStatement.StatementState.CLOSE){
82  				m.autoClose();
83  			}
84  		}
85  		for( MockPreparedStatement mp: mockPreparedStatements){
86  			if(mp.mockState != MockStatement.StatementState.CLOSE){
87  				mp.autoClose();
88  			}
89  		}
90  		mockConnectionState=ConnectionState.CLOSE;
91  	}
92  
93  	public void _close() {
94  	}
95  
96  	/* (non-Javadoc)
97  	 * @see java.sql.Connection#commit()
98  	 */
99  	public void commit() throws SQLException {
100 		try{
101 			assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
102 			assertThat(autoCommitState, Is.is(AutoCommitState.DISABLED));
103 			mockTransactionState = TransactionState.COMMIT;
104 			_commit();
105 		} catch (SQLException e){
106 			mockTransactionState = TransactionState.AUTOROLLBACK;
107 			throw e;
108 		}
109 	}
110 
111 	public void _commit() throws SQLException {
112 	}
113 
114 	
115 	/* (non-Javadoc)
116 	 * @see java.sql.Connection#createStatement()
117 	 */
118 	public Statement createStatement() throws SQLException {
119 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
120 		_createStatement();
121 		MockStatement st= new MockStatement(this);
122 		mockStatements.add(st);
123 		return st;
124 	}
125 
126 	public MockStatement _createStatement()throws SQLException  {
127 		return null;
128 	}
129 
130 	/* (non-Javadoc)
131 	 * @see java.sql.Connection#createStatement(int, int)
132 	 */
133 	public Statement createStatement(int resultSetType, int resultSetConcurrency)
134 			throws SQLException {
135 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
136 		_createStatement(resultSetType, resultSetConcurrency);
137 		MockStatement st= new MockStatement(this); 
138 		st.setResultType(resultSetType);
139 		st.setResultSetConcurrency(resultSetConcurrency);
140 		mockStatements.add(st);
141 		return st;
142 	}
143 
144 	public MockStatement _createStatement(int resultSetType, int resultSetConcurrency)
145 		throws SQLException {
146 		return null;
147 	}
148 
149 	/* (non-Javadoc)
150 	 * @see java.sql.Connection#createStatement(int, int, int)
151 	 */
152 	public Statement createStatement(int resultSetType,	int resultSetConcurrency,
153 			int resultSetHoldability)	throws SQLException {
154 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
155 		_createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
156 		this.resultSetType = resultSetType;
157 		this.resultSetConcurrency = resultSetConcurrency;
158 		this.holdability = resultSetHoldability;
159 		MockStatement st= new MockStatement(this); 
160 		st.setResultType(resultSetType);
161 		st.setResultSetConcurrency(resultSetConcurrency);
162 		st.setHoldability(resultSetHoldability);
163 		mockStatements.add(st);
164 		return st;
165 	}
166 
167 	public MockStatement _createStatement(int resultSetType, int resultSetConcurrency, 
168 			int resultSetHoldability) throws SQLException {
169 		return null;
170 	}
171 
172 	/* (non-Javadoc)
173 	 * @see java.sql.Connection#getAutoCommit()
174 	 */
175 	public boolean getAutoCommit() throws SQLException {
176 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
177 		_getAutoCommit();
178 		return autoCommitState == AutoCommitState.ENABLED?true:false;
179 	}
180 
181 	public boolean _getAutoCommit() throws SQLException{
182 		return false; 
183 	}
184 
185 	/* (non-Javadoc)
186 	 * @see java.sql.Connection#getCatalog()
187 	 */
188 	public String getCatalog() throws SQLException {
189 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
190 		_getCatalog();
191 		return this.catalog;
192 		
193 	}
194 
195 	public String _getCatalog() throws SQLException{
196 		return null;
197 	}
198 
199 	/* (non-Javadoc)
200 	 * @see java.sql.Connection#getHoldability()
201 	 */
202 	public int getHoldability() throws SQLException {
203 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
204 		_getHoldability();
205 		return holdability;
206 		
207 	}
208 
209 	public int _getHoldability() throws SQLException {
210 		return -1;
211 	}
212 
213 	/* (non-Javadoc)
214 	 * @see java.sql.Connection#getMetaData()
215 	 */
216 	public DatabaseMetaData getMetaData() throws SQLException {
217 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
218 		throw new NotYetImplementedException();
219 	}
220 
221 	/* (non-Javadoc)
222 	 * @see java.sql.Connection#getTransactionIsolation()
223 	 */
224 	public int getTransactionIsolation() throws SQLException {
225 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
226 		_getTransactionIsolation();
227 		return transactionIsolation;
228 	}
229 
230 	private int _getTransactionIsolation() throws SQLException {
231 		return -1; 
232 	}
233 
234 	/* (non-Javadoc)
235 	 * @see java.sql.Connection#getTypeMap()
236 	 */
237 	public Map<String, Class<?>> getTypeMap() throws SQLException {
238 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
239 		throw new NotYetImplementedException();
240 	}
241 
242 	/* (non-Javadoc)
243 	 * @see java.sql.Connection#getWarnings()
244 	 */
245 	public SQLWarning getWarnings() throws SQLException {
246 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
247 		return _getWarnings(); 
248 	}
249 
250 	public SQLWarning _getWarnings() throws SQLException {
251 		throw new NeedsMockDefinitionException();
252 	}
253 
254 	/* (non-Javadoc)
255 	 * @see java.sql.Connection#isClosed()
256 	 */
257 	public boolean isClosed() throws SQLException {
258 		_isClosed();
259 		return mockConnectionState==ConnectionState.CLOSE;
260 	}
261 
262 	public boolean _isClosed() throws SQLException{
263 		return false;
264 	}
265 
266 	/* (non-Javadoc)
267 	 * @see java.sql.Connection#isReadOnly()
268 	 */
269 	public boolean isReadOnly() throws SQLException {
270 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
271 		_isReadOnly();
272 		return readOnly;
273 	}
274 
275 	public boolean _isReadOnly() throws SQLException{
276 		return false;
277 	}
278 
279 	/* (non-Javadoc)
280 	 * @see java.sql.Connection#nativeSQL(java.lang.String)
281 	 */
282 	public String nativeSQL(String sql) throws SQLException {
283 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
284 		return _nativeSQL(sql);
285 	}
286 
287 	public String _nativeSQL(String sql) throws SQLException{
288 		throw new NeedsMockDefinitionException();
289 	}
290 
291 	/* (non-Javadoc)
292 	 * @see java.sql.Connection#prepareCall(java.lang.String)
293 	 */
294 	public CallableStatement prepareCall(String sql) throws SQLException {
295 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
296 		throw new NotYetImplementedException();
297 	}
298 
299 	/* (non-Javadoc)
300 	 * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
301 	 */
302 	public CallableStatement prepareCall(String sql, int resultSetType,
303 			int resultSetConcurrency) throws SQLException {
304 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
305 		throw new NotYetImplementedException();
306 	}
307 
308 	/* (non-Javadoc)
309 	 * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
310 	 */
311 	public CallableStatement prepareCall(String sql, int resultSetType,
312 			int resultSetConcurrency, int resultSetHoldability)
313 			throws SQLException {
314 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
315 		this.holdability = resultSetHoldability;
316 		throw new NotYetImplementedException();
317 	}
318 
319 	/* (non-Javadoc)
320 	 * @see java.sql.Connection#prepareStatement(java.lang.String)
321 	 */
322 	public PreparedStatement prepareStatement(String sql) throws SQLException {
323 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
324 		_prepareStatement(sql);
325 		MockPreparedStatement pst = new MockPreparedStatement(this, sql);
326 		mockPreparedStatements.add(pst);
327 		return pst;
328 	}
329 
330 	public MockPreparedStatement _prepareStatement(String sql) throws SQLException{
331 		return null;
332 	}
333 
334 	/* (non-Javadoc)
335 	 * @see java.sql.Connection#prepareStatement(java.lang.String, int)
336 	 */
337 	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
338 			throws SQLException {
339 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
340 		this.autoGeneratedKeys = autoGeneratedKeys;
341 		_prepareStatement(sql, autoGeneratedKeys);
342 		MockPreparedStatement pst = new MockPreparedStatement(this, sql);
343 		mockPreparedStatements.add(pst);
344 		return pst;
345 	}
346 
347 	public MockPreparedStatement _prepareStatement(String sql, int autoGeneratedKeys) 
348 			throws SQLException{
349 		return null;
350 	}
351 
352 	/* (non-Javadoc)
353 	 * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
354 	 */
355 	public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
356 			throws SQLException {
357 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
358 		_prepareStatement(sql, columnIndexes);
359 		MockPreparedStatement pst = new MockPreparedStatement(this, sql);
360 		mockPreparedStatements.add(pst);
361 		return pst;
362 	}
363 
364 	public MockPreparedStatement _prepareStatement(String sql, int[] columnIndexes) 
365 			throws SQLException {
366 		return null;
367 	}
368 
369 	/* (non-Javadoc)
370 	 * @see java.sql.Connection#prepareStatement(java.lang.String, java.lang.String[])
371 	 */
372 	public PreparedStatement prepareStatement(String sql, String[] columnNames)
373 			throws SQLException {
374 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
375 		_prepareStatement(sql, columnNames);
376 		MockPreparedStatement pst = new MockPreparedStatement(this, sql);
377 		mockPreparedStatements.add(pst);
378 		return pst;
379 	}
380 
381 	public MockPreparedStatement _prepareStatement(String sql, String[] columnNames) 
382 			throws SQLException {
383 		return null;
384 	}
385 
386 	/* (non-Javadoc)
387 	 * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
388 	 */
389 	public PreparedStatement prepareStatement(String sql, int resultSetType,
390 			int resultSetConcurrency) throws SQLException {
391 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
392 		_prepareStatement(sql, resultSetType, resultSetConcurrency);
393 		MockPreparedStatement pst = new MockPreparedStatement(this, sql);
394 		pst.setResultType(resultSetType);
395 		pst.setResultSetConcurrency(resultSetConcurrency);
396 		mockPreparedStatements.add(pst);
397 		return pst;
398 	}
399 
400 	public MockPreparedStatement _prepareStatement(String sql, int resultSetType,
401 			int resultSetConcurrency) throws SQLException {
402 		return null;
403 	}
404 
405 	/* (non-Javadoc)
406 	 * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
407 	 */
408 	public PreparedStatement prepareStatement(String sql, int resultSetType,
409 			int resultSetConcurrency, int resultSetHoldability)
410 			throws SQLException {
411 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
412 		this.holdability = resultSetHoldability;
413 		_prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
414 		MockPreparedStatement pst = new MockPreparedStatement(this, sql);
415 		pst.setResultType(resultSetType);
416 		pst.setResultSetConcurrency(resultSetConcurrency);
417 		mockPreparedStatements.add(pst);
418 		return pst;
419 	}
420 
421 	public MockPreparedStatement _prepareStatement(String sql, int resultSetType,
422 			int resultSetConcurrency, int resultSetHoldability) 
423 			throws SQLException {
424 		return null;
425 	}
426 
427 	/* (non-Javadoc)
428 	 * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
429 	 */
430 	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
431 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
432 		throw new NotYetImplementedException();
433 	}
434 
435 	/* (non-Javadoc)
436 	 * @see java.sql.Connection#rollback()
437 	 */
438 	public void rollback() throws SQLException {
439 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
440 		assertThat(autoCommitState, Is.is(AutoCommitState.DISABLED));
441 		mockTransactionState = TransactionState.ROLLBACK;
442 		_rollback();
443 	}
444 	
445 
446 	public void _rollback() throws SQLException {
447 	}
448 
449 	/* (non-Javadoc)
450 	 * @see java.sql.Connection#rollback(java.sql.Savepoint)
451 	 */
452 	public void rollback(Savepoint savepoint) throws SQLException {
453 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
454 		assertThat(autoCommitState, Is.is(AutoCommitState.DISABLED));
455 		mockTransactionState = TransactionState.ROLLBACK; 
456 		_rollback(savepoint);
457 	}
458 	
459 	public void _rollback(Savepoint savepoint) throws SQLException {
460 	}
461 	
462 	/* (non-Javadoc)
463 	 * @see java.sql.Connection#setAutoCommit(boolean)
464 	 */
465 	public void setAutoCommit(boolean autoCommit) throws SQLException {
466 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
467 		autoCommitState = autoCommit? AutoCommitState.ENABLED:AutoCommitState.DISABLED;
468 		_setAutoCommit(autoCommit);
469 	}
470 
471 	public void _setAutoCommit(boolean autoCommit) throws SQLException {
472 	}
473 
474 	/* (non-Javadoc)
475 	 * @see java.sql.Connection#setCatalog(java.lang.String)
476 	 */
477 	public void setCatalog(String catalog) throws SQLException {
478 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
479 		_setCatalog(catalog);
480 		this.catalog = catalog;
481 	}
482 
483 	public void _setCatalog(String catalog) throws SQLException {
484 	}
485 
486 	/* (non-Javadoc)
487 	 * @see java.sql.Connection#setHoldability(int)
488 	 */
489 	public void setHoldability(int holdability) throws SQLException {
490 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
491 		_setHoldability(holdability);
492 		this.holdability = holdability;
493 	}
494 
495 	public void _setHoldability(int holdability) throws SQLException {
496 	}
497 
498 	/* (non-Javadoc)
499 	 * @see java.sql.Connection#setReadOnly(boolean)
500 	 */
501 	public void setReadOnly(boolean readOnly) throws SQLException {
502 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
503 		_setReadOnly(readOnly);
504 		this.readOnly = readOnly;
505 	}
506 
507 	public void _setReadOnly(boolean readOnly) throws SQLException {
508 	}
509 
510 	/* (non-Javadoc)
511 	 * @see java.sql.Connection#setSavepoint()
512 	 */
513 	public Savepoint setSavepoint() throws SQLException {
514 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
515 		throw new NotYetImplementedException();
516 	}
517 
518 	/* (non-Javadoc)
519 	 * @see java.sql.Connection#setSavepoint(java.lang.String)
520 	 */
521 	public Savepoint setSavepoint(String name) throws SQLException {
522 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
523 		throw new NotYetImplementedException();
524 	}
525 
526 	/* (non-Javadoc)
527 	 * @see java.sql.Connection#setTransactionIsolation(int)
528 	 */
529 	public void setTransactionIsolation(int level) throws SQLException {
530 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
531 		_setTransactionIsolation(level);
532 		this.TransactionIsolationLevel = level;
533 	}
534 
535 	public void _setTransactionIsolation(int level) {
536 	}
537 
538 	/* (non-Javadoc)
539 	 * @see java.sql.Connection#setTypeMap(java.util.Map)
540 	 */
541 	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
542 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
543 		throw new NotYetImplementedException();
544 	}
545 
546 	/**
547 	 * @return the url
548 	 */
549 	public String getUrl() {
550 		return url;
551 	}
552 
553 	/**
554 	 * @return the info
555 	 */
556 	public Properties getInfo() {
557 		return info;
558 	}
559 	
560 	/**
561 	 * Validation
562 	 * How do you know the order to be validated? Conn closed first, then stmts?
563 	 */
564 	public void assertExplicitClose() {
565 		assertThat(mockConnectionState, Is.is(ConnectionState.CLOSE));
566 		for(MockStatement st: mockStatements){
567 			st.assertExplicitClose();
568 		}
569 		for(MockPreparedStatement pst: mockPreparedStatements){
570 			pst.assertExplicitClose();
571 		}
572 	}
573 
574 	/**
575 	 * Validation
576 	 * How do you know the order to be validated? Conn closed first, then stmts?
577 	 */
578 	public void assertClosed() {
579 		assertThat(mockConnectionState, Is.is(ConnectionState.CLOSE));
580 		for(MockStatement st: mockStatements){
581 			st.assertClosed();
582 		}
583 		for(MockPreparedStatement pst: mockPreparedStatements){
584 			pst.assertClosed();
585 		}
586 	}	
587 	/**
588 	 * Validation
589 	 */
590 	public void assertExplicitCommit() {
591 		assertThat("", mockTransactionState, Is.is(TransactionState.COMMIT));
592 	}
593 
594 	/**
595 	 * Validation
596 	 */
597 	public void assertAutoCommit() {
598 		assertThat("", mockTransactionState, Is.is(TransactionState.AUTOCOMMIT));
599 	}
600 
601 	/**
602 	 * Validation
603 	 */
604 	public void assertExplicitRollback() {
605 		assertThat(mockTransactionState, Is.is(TransactionState.ROLLBACK));
606 	}
607 
608 	/**
609 	 * Validation
610 	 */
611 	public void assertAutoRollback() {
612 		assertThat(mockTransactionState, Is.is(TransactionState.AUTOROLLBACK));
613 	}
614 
615 	/**
616 	 * @return the mockStatements
617 	 */
618 	protected List<MockStatement> getMockStatements() {
619 		return mockStatements;
620 	}
621 
622 	/**
623 	 * @return the mockConnectionState
624 	 */
625 	protected ConnectionState getMockConnectionState() {
626 		return mockConnectionState;
627 	}
628 
629 	/**
630 	 * @return the mockTransactionState
631 	 */
632 	protected TransactionState getMockTransactionState() {
633 		return mockTransactionState;
634 	}
635 	
636 	protected void completeOtherStatements(MockStatement st) { //spec 3.0, pg 62 transactions
637 		assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
638 		for(MockStatement mst: mockStatements){
639 			if(mst.getId()!= st.getId()){
640 				mst.complete();
641 			}
642 		}		
643 	}
644 	
645 	@Override
646 	public String toString(){
647 		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
648 	}
649 }