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
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
67
68
69 public void clearWarnings() throws SQLException {
70 assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
71 throw new NotYetImplementedException();
72 }
73
74
75
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
97
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
116
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
131
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
150
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
173
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
186
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
200
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
214
215
216 public DatabaseMetaData getMetaData() throws SQLException {
217 assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
218 throw new NotYetImplementedException();
219 }
220
221
222
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
235
236
237 public Map<String, Class<?>> getTypeMap() throws SQLException {
238 assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
239 throw new NotYetImplementedException();
240 }
241
242
243
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
255
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
267
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
280
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
292
293
294 public CallableStatement prepareCall(String sql) throws SQLException {
295 assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
296 throw new NotYetImplementedException();
297 }
298
299
300
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
309
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
320
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
335
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
353
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
370
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
387
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
406
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
428
429
430 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
431 assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
432 throw new NotYetImplementedException();
433 }
434
435
436
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
450
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
463
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
475
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
487
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
499
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
511
512
513 public Savepoint setSavepoint() throws SQLException {
514 assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
515 throw new NotYetImplementedException();
516 }
517
518
519
520
521 public Savepoint setSavepoint(String name) throws SQLException {
522 assertThat(mockConnectionState, IsNot.not(ConnectionState.CLOSE));
523 throw new NotYetImplementedException();
524 }
525
526
527
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
539
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
548
549 public String getUrl() {
550 return url;
551 }
552
553
554
555
556 public Properties getInfo() {
557 return info;
558 }
559
560
561
562
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
576
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
589
590 public void assertExplicitCommit() {
591 assertThat("", mockTransactionState, Is.is(TransactionState.COMMIT));
592 }
593
594
595
596
597 public void assertAutoCommit() {
598 assertThat("", mockTransactionState, Is.is(TransactionState.AUTOCOMMIT));
599 }
600
601
602
603
604 public void assertExplicitRollback() {
605 assertThat(mockTransactionState, Is.is(TransactionState.ROLLBACK));
606 }
607
608
609
610
611 public void assertAutoRollback() {
612 assertThat(mockTransactionState, Is.is(TransactionState.AUTOROLLBACK));
613 }
614
615
616
617
618 protected List<MockStatement> getMockStatements() {
619 return mockStatements;
620 }
621
622
623
624
625 protected ConnectionState getMockConnectionState() {
626 return mockConnectionState;
627 }
628
629
630
631
632 protected TransactionState getMockTransactionState() {
633 return mockTransactionState;
634 }
635
636 protected void completeOtherStatements(MockStatement st) {
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 }