1
2
3
4 package com.github.smokestack.jdbc;
5
6 import static org.hamcrest.MatcherAssert.assertThat;
7
8 import java.sql.Connection;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.sql.SQLWarning;
12 import java.sql.Statement;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
17 import org.apache.commons.lang.builder.ToStringStyle;
18 import org.hamcrest.core.AnyOf;
19 import org.hamcrest.core.Is;
20 import org.hamcrest.core.IsNot;
21
22 import com.github.smokestack.exception.NeedsMockDefinitionException;
23 import com.github.smokestack.exception.NotYetImplementedException;
24
25
26
27
28
29 public class MockStatement implements Statement {
30
31 private static long autoId = 0;
32 private long id;
33 private int direction;
34 private Connection connection;
35 private MockConnection parent;
36
37 private List<MockResultSet> mockResultSets=new ArrayList<MockResultSet>();
38
39 public enum StatementState {NEW, CLOSE, COMPLETE, AUTOCLOSE};
40
41 protected StatementState mockState=StatementState.NEW;
42 private int fetchSize;
43 private List<String> batchSQLs = new ArrayList<String>();
44 private int maxFieldSize;
45 private int maxRows;
46 private int queryTimeout;
47 private int resultSetConcurrency;
48 private int resultSetHoldability;
49 private int resultSetType;
50 private int updateCount;
51 private String cursorName;
52 private boolean escapeProcessing;
53
54 public MockStatement(Connection connection) {
55 this.connection=connection;
56 this.parent = (MockConnection) connection;
57 this.id = ++autoId;
58 }
59
60
61
62
63 public void addBatch(String sql) throws SQLException {
64 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
65 assertThat("Auto Commit State", parent.autoCommitState, Is.is(MockConnection.AutoCommitState.DISABLED));
66 _addBatch(sql);
67 batchSQLs.add(new String());
68 }
69
70 public void _addBatch(String sql) {
71 }
72
73
74
75
76 public void cancel() throws SQLException {
77 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
78 _cancel();
79 this.mockState = StatementState.NEW;
80 }
81
82 public void _cancel() throws SQLException {
83 }
84
85
86
87
88 public void clearBatch() throws SQLException {
89 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
90 assertThat("Auto Commit State", parent.autoCommitState, Is.is(MockConnection.AutoCommitState.DISABLED));
91 _clearBatch();
92 batchSQLs.clear();
93 }
94
95 public void _clearBatch() {
96 }
97
98
99
100
101 public void clearWarnings() throws SQLException {
102 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
103 _clearWarnings();
104 this.mockState = StatementState.NEW;
105 }
106
107 public void _clearWarnings() {
108 }
109
110
111
112
113 public void close() throws SQLException {
114 assertThat("Statement State", mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
115 assertThat("Statement State", mockState, Is.is(StatementState.COMPLETE));
116 _close();
117 this.mockState=StatementState.CLOSE;
118 }
119
120 public void _close() throws SQLException {
121 }
122
123
124
125
126 public boolean execute(String sql) throws SQLException {
127 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
128 mockState = StatementState.COMPLETE;
129 parent.completeOtherStatements(this);
130 return _execute(sql);
131 }
132
133
134
135
136 public boolean _execute(String sql) throws SQLException {
137 throw new NeedsMockDefinitionException();
138 }
139
140
141
142
143 public boolean execute(String sql, int autoGeneratedKeys)
144 throws SQLException {
145 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
146 mockState = StatementState.COMPLETE;
147 parent.completeOtherStatements(this);
148 return _execute(sql, autoGeneratedKeys);
149 }
150
151 public boolean _execute(String sql, int autoGeneratedKeys) {
152 throw new NeedsMockDefinitionException();
153 }
154
155
156
157
158 public boolean execute(String sql, int[] columnIndexes) throws SQLException {
159 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
160 mockState = StatementState.COMPLETE;
161 parent.completeOtherStatements(this);
162 return _execute(sql, columnIndexes);
163 }
164
165 public boolean _execute(String sql, int[] columnIndexes) {
166 throw new NeedsMockDefinitionException();
167 }
168
169
170
171
172 public boolean execute(String sql, String[] columnNames)
173 throws SQLException {
174 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
175 mockState = StatementState.COMPLETE;
176 parent.completeOtherStatements(this);
177 return _execute(sql, columnNames);
178 }
179
180 public boolean _execute(String sql, String[] columnNames) {
181 throw new NeedsMockDefinitionException();
182 }
183
184
185
186
187 public int[] executeBatch() throws SQLException {
188 assertThat("mock State", mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
189 assertThat("Auto Commit State", parent.autoCommitState, Is.is(MockConnection.AutoCommitState.DISABLED));
190 int[] b = null;
191 try{
192 b = _executeBatch();
193 mockState = StatementState.COMPLETE;
194 parent.completeOtherStatements(this);
195 }catch (SQLException se){
196 parent.mockTransactionState= MockConnection.TransactionState.ROLLBACK;
197 }
198 return b;
199 }
200
201 public int[] _executeBatch() throws SQLException {
202 throw new NeedsMockDefinitionException();
203 }
204
205
206
207
208 public ResultSet executeQuery(String sql) throws SQLException {
209 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
210 MockResultSet rs=new MockResultSet(sql);
211 rs.setParent(this);
212 mockResultSets.add(rs);
213 mockState = StatementState.COMPLETE;
214 parent.completeOtherStatements(this);
215 _executeQuery(sql);
216 return rs;
217 }
218
219 public void _executeQuery(String sql) {
220 }
221
222
223
224
225 public int executeUpdate(String sql) throws SQLException {
226 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
227 int b = 0;
228 parent.mockTransactionState= MockConnection.TransactionState.AUTOCOMMIT;
229 try{
230 mockState = StatementState.COMPLETE;
231 parent.completeOtherStatements(this);
232 b = _executeUpdate(sql);
233 }catch (SQLException se){
234 parent.mockTransactionState= MockConnection.TransactionState.ROLLBACK;
235 }
236 return b;
237 }
238
239
240
241
242 public int _executeUpdate(String sql) throws SQLException {
243 throw new NeedsMockDefinitionException();
244 }
245
246
247
248 public int executeUpdate(String sql, int autoGeneratedKeys)
249 throws SQLException {
250 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
251 int b = 0;
252 parent.mockTransactionState= MockConnection.TransactionState.AUTOCOMMIT;
253 try{
254 mockState = StatementState.COMPLETE;
255 parent.completeOtherStatements(this);
256 b = _executeUpdate(sql, autoGeneratedKeys);
257 }catch (SQLException se){
258 parent.mockTransactionState= MockConnection.TransactionState.ROLLBACK;
259 }
260 return b;
261 }
262
263 public int _executeUpdate(String sql, int autoGeneratedKeys) throws SQLException{
264 throw new NeedsMockDefinitionException();
265 }
266
267
268
269
270 public int executeUpdate(String sql, int[] columnIndexes)
271 throws SQLException {
272 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
273 int b = 0;
274 parent.mockTransactionState= MockConnection.TransactionState.AUTOCOMMIT;
275 try{
276 mockState = StatementState.COMPLETE;
277 parent.completeOtherStatements(this);
278 b = _executeUpdate(sql, columnIndexes);
279 }catch (SQLException se){
280 parent.mockTransactionState= MockConnection.TransactionState.ROLLBACK;
281 }
282 return b;
283 }
284
285 public int _executeUpdate(String sql, int[] columnIndexes) throws SQLException{
286 throw new NeedsMockDefinitionException();
287 }
288
289
290
291
292 public int executeUpdate(String sql, String[] columnNames)
293 throws SQLException {
294 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
295 int b = 0;
296 parent.mockTransactionState= MockConnection.TransactionState.AUTOCOMMIT;
297 try{
298 mockState = StatementState.COMPLETE;
299 parent.completeOtherStatements(this);
300 b = _executeUpdate(sql, columnNames);
301 }catch (SQLException se){
302 parent.mockTransactionState= MockConnection.TransactionState.ROLLBACK;
303 }
304 return b;
305 }
306
307 public int _executeUpdate(String sql, String[] columnNames) throws SQLException{
308 throw new NeedsMockDefinitionException();
309 }
310
311
312
313
314 public Connection getConnection() throws SQLException {
315 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
316 _getConnection();
317 return connection;
318 }
319
320 public Connection _getConnection() {
321 return null;
322 }
323
324
325
326
327 public int getFetchDirection() throws SQLException {
328 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
329 _getFetchDirection();
330 return direction;
331 }
332
333 public int _getFetchDirection() {
334 return -1;
335 }
336
337
338
339
340 public int getFetchSize() throws SQLException {
341 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
342 _getFetchSize();
343 return fetchSize;
344 }
345
346 public int _getFetchSize() {
347 return -1;
348 }
349
350
351
352
353 public ResultSet getGeneratedKeys() throws SQLException {
354 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
355 throw new NotYetImplementedException();
356 }
357
358
359
360
361 public int getMaxFieldSize() throws SQLException {
362 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
363 _getMaxFieldSize();
364 return maxFieldSize;
365 }
366
367 public int _getMaxFieldSize() {
368 return -1;
369 }
370
371
372
373
374 public int getMaxRows() throws SQLException {
375 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
376 _getMaxRows();
377 return maxRows;
378 }
379
380 public int _getMaxRows() {
381 return -1;
382 }
383
384
385
386
387 public boolean getMoreResults() throws SQLException {
388 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
389 return _getMoreResults();
390 }
391
392 public boolean _getMoreResults() {
393 throw new NeedsMockDefinitionException();
394 }
395
396
397
398
399 public boolean getMoreResults(int current) throws SQLException {
400 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
401 return _getMoreResults(current);
402 }
403
404 public boolean _getMoreResults(int current) {
405 throw new NeedsMockDefinitionException();
406 }
407
408
409
410
411 public int getQueryTimeout() throws SQLException {
412 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
413 _getQueryTimeout();
414 return queryTimeout;
415 }
416
417 public int _getQueryTimeout() {
418 return -1;
419 }
420
421
422
423
424 public ResultSet getResultSet() throws SQLException {
425 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
426 _getResultSet();
427 return this.mockResultSets.get(mockResultSets.size()-1);
428 }
429
430 public ResultSet _getResultSet() {
431 return null;
432 }
433
434
435
436
437 public int getResultSetConcurrency() throws SQLException {
438 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
439 _getResultSetConcurrency();
440 return resultSetConcurrency;
441 }
442
443 public int _getResultSetConcurrency() {
444 return -1;
445 }
446
447
448
449
450 public int getResultSetHoldability() throws SQLException {
451 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
452 _getResultSetHoldability();
453 return resultSetHoldability;
454 }
455
456 public int _getResultSetHoldability() {
457 return -1;
458 }
459
460
461
462
463 public int getResultSetType() throws SQLException {
464 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
465 _getResultSetType();
466 return resultSetType;
467 }
468
469 public int _getResultSetType() {
470 return -1;
471 }
472
473
474
475
476 public int getUpdateCount() throws SQLException {
477 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
478 return _getUpdateCount();
479 }
480
481 public int _getUpdateCount() {
482 throw new NeedsMockDefinitionException();
483 }
484
485
486
487
488 public SQLWarning getWarnings() throws SQLException {
489 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
490 return _getWarnings();
491 }
492
493 public SQLWarning _getWarnings() {
494 throw new NeedsMockDefinitionException();
495 }
496
497
498
499
500 public void setCursorName(String name) throws SQLException {
501 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
502 _setCursorName( name);
503 this.cursorName = name;
504 }
505
506 public void _setCursorName(String name) {
507 }
508
509
510
511
512 public void setEscapeProcessing(boolean enable) throws SQLException {
513 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
514 _setEscapeProcessing(enable);
515 this.escapeProcessing = enable;
516 }
517
518 public void _setEscapeProcessing(boolean enable) {
519 }
520
521
522
523
524 public void setFetchDirection(int direction) throws SQLException {
525 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
526 _setFetchDirection( direction);
527 this.direction=direction;
528 }
529
530 public void _setFetchDirection(int direction) {
531 }
532
533
534
535
536 public void setFetchSize(int rows) throws SQLException {
537 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
538 _setFetchSize(rows);
539 this.fetchSize = rows;
540 }
541
542 public void _setFetchSize(int rows) {
543 }
544
545
546
547
548 public void setMaxFieldSize(int max) throws SQLException {
549 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
550 _setMaxFieldSize(max);
551 this.maxFieldSize = max;
552 }
553
554 public void _setMaxFieldSize(int max) {
555 }
556
557
558
559
560 public void setMaxRows(int max) throws SQLException {
561 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
562 _setMaxRows(max);
563 this.maxRows = max;
564 }
565
566 public void _setMaxRows(int max) {
567 }
568
569
570
571
572 public void setQueryTimeout(int seconds) throws SQLException {
573 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
574 _setQueryTimeout(seconds);
575 this.queryTimeout = seconds;
576 }
577
578 public void _setQueryTimeout(int seconds) {
579 }
580
581
582
583
584 public List<MockResultSet> getMockResultSets() {
585 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
586 return mockResultSets;
587 }
588
589 protected void autoClose() {
590 assertThat(mockState, AnyOf.anyOf(IsNot.not(StatementState.CLOSE), IsNot.not(StatementState.AUTOCLOSE)));
591 this.mockState=StatementState.AUTOCLOSE;
592 }
593
594 public void complete() {
595 mockState = StatementState.COMPLETE;
596 }
597
598 public long getId() {
599
600 return id;
601 }
602
603 public void setParent(MockConnection mockConnection) {
604 this.parent = mockConnection;
605 }
606
607 public void setId(int id) {
608 this.id = id;
609 }
610
611 public void setResultType(int resultSetType) {
612 this.resultSetType = resultSetType;
613 }
614
615 public void setResultSetConcurrency(int resultSetConcurrency) {
616 this.resultSetConcurrency = resultSetConcurrency;
617 }
618
619 public void setHoldability(int resultSetHoldability) {
620 this.resultSetHoldability = resultSetHoldability;
621 }
622
623 public void assertExplicitClose() {
624 assertThat(mockState, Is.is(StatementState.CLOSE));
625 for(MockResultSet rs: mockResultSets){
626 rs.assertExplicitClose();
627 }
628 }
629
630
631 public void assertClosed() {
632 assertThat(mockState, AnyOf.anyOf(Is.is(StatementState.CLOSE), Is.is(StatementState.AUTOCLOSE)));
633 for(MockResultSet rs: mockResultSets){
634 rs.assertClosed();
635 }
636 }
637
638 @Override
639 public String toString(){
640 return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
641 }
642 }