Source code: com/mockobjects/sql/MockResultSet.java
1 package com.mockobjects.sql;
2
3 import com.mockobjects.ExpectationCounter;
4 import com.mockobjects.MockObject;
5 import com.mockobjects.ReturnValue;
6 import com.mockobjects.util.AssertMo;
7
8 import java.io.InputStream;
9 import java.io.Reader;
10 import java.math.BigDecimal;
11 import java.sql.*;
12 import java.util.Calendar;
13 import java.util.Map;
14
15 /**
16 * This is the base implementation of a mock result set.
17 * It manages converting objects from the current row into a other types.
18 * Entries can be found by either column index or column name.
19 * For basic java types (e.g. int, boolean), insert an instance of
20 * the appropriate object (e.g. Integer, Boolean)
21 * It also counts close() and next() calls
22 * To force throwing a SQLException on a getter,
23 * set the corresponding value to be of type SQLException.
24 */
25 abstract class MockResultSet extends MockObject implements ResultSet {
26
27 private final ExpectationCounter myCloseCalls;
28 protected final ExpectationCounter myNextCalls;
29 protected final String name;
30
31 private final ReturnValue myMetaData = new ReturnValue("meta data");
32 private final ReturnValue myStatement = new ReturnValue("statement");
33
34 public MockResultSet() {
35 this(MockResultSet.class.getName());
36 }
37
38 /**
39 * @param name Label used to identify mock when it errors
40 */
41 public MockResultSet(String name) {
42 this.name = name;
43 myCloseCalls = new ExpectationCounter(name + ".close");
44 myNextCalls = new ExpectationCounter(name + ".next");
45 }
46
47 public void setExpectedCloseCalls(int calls) {
48 myCloseCalls.setExpected(calls);
49 }
50
51 public void setExpectedNextCalls(int calls) {
52 myNextCalls.setExpected(calls);
53 }
54
55 // --------------------------------------------------------------------- setup
56
57 public void setupMetaData(ResultSetMetaData metaData) {
58 myMetaData.setValue(metaData);
59 }
60
61 public void setupStatement(Statement statement) {
62 myStatement.setValue(statement);
63 }
64
65 // ------------------------------------------------------------------ abstract
66
67 /**
68 * Used as the base implementation for getting all types of object,
69 * based on 1-based column index
70 * @see java.sql.ResultSet#getObject(int)
71 */
72 abstract public Object getObject(int columnIndex) throws SQLException;
73
74 /**
75 * Used as the base implementation for getting all types of object,
76 * based on columnName
77 * @see java.sql.ResultSet#getObject(int)
78 */
79 abstract public Object getObject(String columnName) throws SQLException;
80
81 abstract public boolean next() throws SQLException;
82
83 abstract public int getRow() throws SQLException;
84
85 // --------------------------------------------------------------- implemented
86
87 public void close() throws SQLException {
88 myCloseCalls.inc();
89 }
90
91 public Array getArray(int i) throws SQLException {
92 return (Array) getObject(i);
93 }
94
95 public Array getArray(String colName) throws SQLException {
96 return (Array) getObject(colName);
97 }
98
99 public InputStream getAsciiStream(int columnIndex) throws SQLException {
100 return (InputStream) getObject(columnIndex);
101 }
102
103 public InputStream getAsciiStream(String columnName) throws SQLException {
104 return (InputStream) getObject(columnName);
105 }
106
107 public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException {
108 return getBigDecimal(columnName);
109 }
110
111 public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
112 return (BigDecimal) getObject(columnIndex);
113 }
114
115 public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
116 return (BigDecimal) getObject(columnIndex);
117 }
118
119 public BigDecimal getBigDecimal(String columnName) throws SQLException {
120 return (BigDecimal) getObject(columnName);
121 }
122
123 public InputStream getBinaryStream(int columnIndex) throws SQLException {
124 return (InputStream) getObject(columnIndex);
125 }
126
127 public InputStream getBinaryStream(String columnName) throws SQLException {
128 return (InputStream) getObject(columnName);
129 }
130
131 public Blob getBlob(int i) throws SQLException {
132 return (Blob) getObject(i);
133 }
134
135 public Blob getBlob(String colName) throws SQLException {
136 return (Blob) getObject(colName);
137 }
138
139 public boolean getBoolean(int columnIndex) throws SQLException {
140 return ((Boolean) getObject(columnIndex)).booleanValue();
141 }
142
143 public boolean getBoolean(String columnName) throws SQLException {
144 return ((Boolean) getObject(columnName)).booleanValue();
145 }
146
147 public byte getByte(int columnIndex) throws SQLException {
148 return ((Byte) getObject(columnIndex)).byteValue();
149 }
150
151 public byte getByte(String columnName) throws SQLException {
152 return ((Byte) getObject(columnName)).byteValue();
153 }
154
155 public byte[] getBytes(int columnIndex) throws SQLException {
156 return (byte[]) getObject(columnIndex);
157 }
158
159 public byte[] getBytes(String columnName) throws SQLException {
160 return (byte[]) getObject(columnName);
161 }
162
163 public Reader getCharacterStream(int columnIndex) throws SQLException {
164 return (Reader) getObject(columnIndex);
165 }
166
167 public Reader getCharacterStream(String columnName) throws SQLException {
168 return (Reader) getObject(columnName);
169 }
170
171 public Clob getClob(int i) throws SQLException {
172 return (Clob) getObject(i);
173 }
174
175 public Clob getClob(String colName) throws SQLException {
176 return (Clob) getObject(colName);
177 }
178
179 public Date getDate(int columnIndex) throws SQLException {
180 return (Date) getObject(columnIndex);
181 }
182
183 public Date getDate(String columnName) throws SQLException {
184 return (Date) getObject(columnName);
185 }
186
187 public Date getDate(int columnIndex, Calendar cal) throws SQLException {
188 return getDate(columnIndex);
189 }
190
191 public Date getDate(String columnName, Calendar cal) throws SQLException {
192 return getDate(columnName);
193 }
194
195 public double getDouble(int columnIndex) throws SQLException {
196 return ((Double) getObject(columnIndex)).doubleValue();
197 }
198
199 public double getDouble(String columnName) throws SQLException {
200 return ((Double) getObject(columnName)).doubleValue();
201 }
202
203 public float getFloat(int columnIndex) throws SQLException {
204 return ((Float) getObject(columnIndex)).floatValue();
205 }
206
207 public float getFloat(String columnName) throws SQLException {
208 return ((Float) getObject(columnName)).floatValue();
209 }
210
211 public int getInt(int columnIndex) throws SQLException {
212 return ((Integer) getObject(columnIndex)).intValue();
213 }
214
215 public int getInt(String columnName) throws SQLException {
216 return ((Integer) getObject(columnName)).intValue();
217 }
218
219 public long getLong(int columnIndex) throws SQLException {
220 return ((Long) getObject(columnIndex)).longValue();
221 }
222
223 public long getLong(String columnName) throws SQLException {
224 return ((Long) getObject(columnName)).longValue();
225 }
226
227 public ResultSetMetaData getMetaData() throws SQLException {
228 return (ResultSetMetaData) myMetaData.getValue();
229 }
230
231 public Ref getRef(int i) throws SQLException {
232 return (Ref) getObject(i);
233 }
234
235 public Ref getRef(String colName) throws SQLException {
236 return (Ref) getObject(colName);
237 }
238
239 public short getShort(String columnName) throws SQLException {
240 return ((Short) getObject(columnName)).shortValue();
241 }
242
243 public short getShort(int columnIndex) throws SQLException {
244 return ((Short) getObject(columnIndex)).shortValue();
245 }
246
247 public Statement getStatement() throws SQLException {
248 return (Statement) myStatement.getValue();
249 }
250
251 public String getString(int columnIndex) throws SQLException {
252 final Object object = getObject(columnIndex);
253 if (object != null) {
254 AssertMo.assertTrue("Column " + columnIndex + " in " + name + " is a " +
255 object.getClass().getName() + " not a String", object instanceof String);
256
257 return (String) object;
258 } else {
259 return null;
260 }
261 }
262
263 public String getString(String columnName) throws SQLException {
264 return (String) getObject(columnName);
265 }
266
267 public Time getTime(int columnIndex) throws SQLException {
268 return (Time) getObject(columnIndex);
269 }
270
271 public Time getTime(String columnName) throws SQLException {
272 return (Time) getObject(columnName);
273 }
274
275 public Time getTime(int columnIndex, Calendar cal) throws SQLException {
276 return getTime(columnIndex);
277 }
278
279 public Time getTime(String columnName, Calendar cal) throws SQLException {
280 return getTime(columnName);
281 }
282
283 public Timestamp getTimestamp(int columnIndex) throws SQLException {
284 return (Timestamp) getObject(columnIndex);
285 }
286
287 public Timestamp getTimestamp(String columnName) throws SQLException {
288 return (Timestamp) getObject(columnName);
289 }
290
291 public Timestamp getTimestamp(int columnIndex, Calendar cal)
292 throws SQLException {
293 return getTimestamp(columnIndex);
294 }
295
296 public Timestamp getTimestamp(String columnName, Calendar cal)
297 throws SQLException {
298 return getTimestamp(columnName);
299 }
300
301 public InputStream getUnicodeStream(int columnIndex)
302 throws SQLException {
303 return (InputStream) getObject(columnIndex);
304 }
305
306 public InputStream getUnicodeStream(String columnName)
307 throws SQLException {
308 return (InputStream) getObject(columnName);
309 }
310
311 // ------------------------------------------------------------ notImplemented
312
313 public String getCursorName() throws SQLException {
314 notImplemented();
315 return null;
316 }
317
318 public int getConcurrency() throws SQLException {
319 notImplemented();
320 return 0;
321 }
322
323 /**
324 * Calls notImplemented. Returns 0.
325 */
326 public int getFetchDirection() throws SQLException {
327 notImplemented();
328 return 0;
329 }
330
331 /**
332 * Calls notImplemented. Returns 0.
333 */
334 public int getFetchSize() throws SQLException {
335 notImplemented();
336 return 0;
337 }
338
339 /**
340 * Calls notImplemented. Returns null.
341 */
342 public Object getObject(int i, Map map) throws SQLException {
343 notImplemented();
344 return null;
345 }
346
347 /**
348 * Calls notImplemented. Returns null.
349 */
350 public Object getObject(String colName, Map map) throws SQLException {
351 notImplemented();
352 return null;
353 }
354
355 /**
356 * Calls notImplemented. Returns 0.
357 */
358 public int getType() throws SQLException {
359 notImplemented();
360 return 0;
361 }
362
363 /**
364 * Calls notImplemented. Returns null.
365 */
366 public SQLWarning getWarnings() throws SQLException {
367 notImplemented();
368 return null;
369 }
370
371 /**
372 * Calls notImplemented.
373 */
374 public void clearWarnings() throws SQLException {
375 notImplemented();
376 }
377
378 /**
379 * Calls notImplemented. Returns 0.
380 */
381 public int findColumn(String columnName) throws SQLException {
382 notImplemented();
383 return 0;
384 }
385
386 /**
387 * Calls notImplemented. Returns false.
388 */
389 public boolean isBeforeFirst() throws SQLException {
390 notImplemented();
391 return false;
392 }
393
394 /**
395 * Calls notImplemented. Returns false.
396 */
397 public boolean isAfterLast() throws SQLException {
398 notImplemented();
399 return false;
400 }
401
402 /**
403 * Calls notImplemented. Returns false.
404 */
405 public boolean isFirst() throws SQLException {
406 notImplemented();
407 return false;
408 }
409
410 /**
411 * Calls notImplemented. Returns false.
412 */
413 public boolean isLast() throws SQLException {
414 notImplemented();
415 return false;
416 }
417
418 /**
419 * Calls notImplemented.
420 */
421 public void beforeFirst() throws SQLException {
422 notImplemented();
423 }
424
425 /**
426 * Calls notImplemented.
427 */
428 public void afterLast() throws SQLException {
429 notImplemented();
430 }
431
432 /**
433 * Calls notImplemented. Returns false.
434 */
435 public boolean first() throws SQLException {
436 notImplemented();
437 return false;
438 }
439
440 /**
441 * Calls notImplemented. Returns false.
442 */
443 public boolean last() throws SQLException {
444 notImplemented();
445 return false;
446 }
447
448 /**
449 * Calls notImplemented. Returns false.
450 */
451 public boolean absolute(int row) throws SQLException {
452 notImplemented();
453 return false;
454 }
455
456 /**
457 * Calls notImplemented. Returns false.
458 */
459 public boolean relative(int rows) throws SQLException {
460 notImplemented();
461 return false;
462 }
463
464 /**
465 * Calls notImplemented. Returns false.
466 */
467 public boolean previous() throws SQLException {
468 notImplemented();
469 return false;
470 }
471
472 /**
473 * Calls notImplemented.
474 */
475 public void setFetchDirection(int direction) throws SQLException {
476 notImplemented();
477 }
478
479 /**
480 * Calls notImplemented.
481 */
482 public void setFetchSize(int rows) throws SQLException {
483 notImplemented();
484 }
485
486 /**
487 * Calls notImplemented. Returns false.
488 */
489 public boolean rowUpdated() throws SQLException {
490 notImplemented();
491 return false;
492 }
493
494 /**
495 * Calls notImplemented. Returns false.
496 */
497 public boolean rowInserted() throws SQLException {
498 notImplemented();
499 return false;
500 }
501
502 /**
503 * Calls notImplemented. Returns false.
504 */
505 public boolean rowDeleted() throws SQLException {
506 notImplemented();
507 return false;
508 }
509
510 /**
511 * Calls notImplemented.
512 */
513 public void updateNull(int columnIndex) throws SQLException {
514 notImplemented();
515 }
516
517 /**
518 * Calls notImplemented.
519 */
520 public void updateBoolean(int columnIndex, boolean x)
521 throws SQLException {
522 notImplemented();
523 }
524
525 /**
526 * Calls notImplemented.
527 */
528 public void updateByte(int columnIndex, byte x) throws SQLException {
529 notImplemented();
530 }
531
532 /**
533 * Calls notImplemented.
534 */
535 public void updateShort(int columnIndex, short x) throws SQLException {
536 notImplemented();
537 }
538
539 /**
540 * Calls notImplemented.
541 */
542 public void updateInt(int columnIndex, int x) throws SQLException {
543 notImplemented();
544 }
545
546 /**
547 * Calls notImplemented.
548 */
549 public void updateLong(int columnIndex, long x) throws SQLException {
550 notImplemented();
551 }
552
553 /**
554 * Calls notImplemented.
555 */
556 public void updateFloat(int columnIndex, float x) throws SQLException {
557 notImplemented();
558 }
559
560 /**
561 * Calls notImplemented.
562 */
563 public void updateDouble(int columnIndex, double x) throws SQLException {
564 notImplemented();
565 }
566
567 /**
568 * Calls notImplemented.
569 */
570 public void updateBigDecimal(int columnIndex, BigDecimal x)
571 throws SQLException {
572 notImplemented();
573 }
574
575 /**
576 * Calls notImplemented.
577 */
578 public void updateString(int columnIndex, String x) throws SQLException {
579 notImplemented();
580 }
581
582 /**
583 * Calls notImplemented.
584 */
585 public void updateBytes(int columnIndex, byte x[]) throws SQLException {
586 notImplemented();
587 }
588
589 /**
590 * Calls notImplemented.
591 */
592 public void updateDate(int columnIndex, Date x) throws SQLException {
593 notImplemented();
594 }
595
596 /**
597 * Calls notImplemented.
598 */
599 public void updateTime(int columnIndex, Time x) throws SQLException {
600 notImplemented();
601 }
602
603 /**
604 * Calls notImplemented.
605 */
606 public void updateTimestamp(int columnIndex, Timestamp x)
607 throws SQLException {
608 notImplemented();
609 }
610
611 /**
612 * Calls notImplemented.
613 */
614 public void updateAsciiStream(int columnIndex,
615 InputStream x,
616 int length) throws SQLException {
617 notImplemented();
618 }
619
620 /**
621 * Calls notImplemented.
622 */
623 public void updateBinaryStream(int columnIndex,
624 InputStream x,
625 int length) throws SQLException {
626 notImplemented();
627 }
628
629 /**
630 * Calls notImplemented.
631 */
632 public void updateCharacterStream(int columnIndex,
633 Reader x,
634 int length) throws SQLException {
635 notImplemented();
636 }
637
638 /**
639 * Calls notImplemented.
640 */
641 public void updateObject(int columnIndex, Object x, int scale)
642 throws SQLException {
643 notImplemented();
644 }
645
646 /**
647 * Calls notImplemented.
648 */
649 public void updateObject(int columnIndex, Object x) throws SQLException {
650 notImplemented();
651 }
652
653 /**
654 * Calls notImplemented.
655 */
656 public void updateNull(String columnName) throws SQLException {
657 notImplemented();
658 }
659
660 /**
661 * Calls notImplemented.
662 */
663 public void updateBoolean(String columnName, boolean x)
664 throws SQLException {
665 notImplemented();
666 }
667
668 /**
669 * Calls notImplemented.
670 */
671 public void updateByte(String columnName, byte x) throws SQLException {
672 notImplemented();
673 }
674
675 /**
676 * Calls notImplemented.
677 */
678 public void updateShort(String columnName, short x) throws SQLException {
679 notImplemented();
680 }
681
682 /**
683 * Calls notImplemented.
684 */
685 public void updateInt(String columnName, int x) throws SQLException {
686 notImplemented();
687 }
688
689 /**
690 * Calls notImplemented.
691 */
692 public void updateLong(String columnName, long x) throws SQLException {
693 notImplemented();
694 }
695
696 /**
697 * Calls notImplemented.
698 */
699 public void updateFloat(String columnName, float x) throws SQLException {
700 notImplemented();
701 }
702
703 /**
704 * Calls notImplemented.
705 */
706 public void updateDouble(String columnName, double x) throws SQLException {
707 notImplemented();
708 }
709
710 /**
711 * Calls notImplemented.
712 */
713 public void updateBigDecimal(String columnName, BigDecimal x)
714 throws SQLException {
715 notImplemented();
716 }
717
718 /**
719 * Calls notImplemented.
720 */
721 public void updateString(String columnName, String x) throws SQLException {
722 notImplemented();
723 }
724
725 /**
726 * Calls notImplemented.
727 */
728 public void updateBytes(String columnName, byte x[]) throws SQLException {
729 notImplemented();
730 }
731
732 /**
733 * Calls notImplemented.
734 */
735 public void updateDate(String columnName, Date x) throws SQLException {
736 notImplemented();
737 }
738
739 /**
740 * Calls notImplemented.
741 */
742 public void updateTime(String columnName, Time x) throws SQLException {
743 notImplemented();
744 }
745
746 /**
747 * Calls notImplemented.
748 */
749 public void updateTimestamp(String columnName, Timestamp x)
750 throws SQLException {
751 notImplemented();
752 }
753
754 /**
755 * Calls notImplemented.
756 */
757 public void updateAsciiStream(String columnName,
758 InputStream x,
759 int length) throws SQLException {
760 notImplemented();
761 }
762
763 /**
764 * Calls notImplemented.
765 */
766 public void updateBinaryStream(String columnName,
767 InputStream x,
768 int length) throws SQLException {
769 notImplemented();
770 }
771
772 /**
773 * Calls notImplemented.
774 */
775 public void updateCharacterStream(String columnName,
776 Reader reader,
777 int length) throws SQLException {
778 notImplemented();
779 }
780
781 /**
782 * Calls notImplemented.
783 */
784 public void updateObject(String columnName, Object x, int scale)
785 throws SQLException {
786 notImplemented();
787 }
788
789 /**
790 * Calls notImplemented.
791 */
792 public void updateObject(String columnName, Object x)
793 throws SQLException {
794 notImplemented();
795 }
796
797 /**
798 * Calls notImplemented.
799 */
800 public void insertRow() throws SQLException {
801 notImplemented();
802 }
803
804 /**
805 * Calls notImplemented.
806 */
807 public void updateRow() throws SQLException {
808 notImplemented();
809 }
810
811 /**
812 * Calls notImplemented.
813 */
814 public void deleteRow() throws SQLException {
815 notImplemented();
816 }
817
818 /**
819 * Calls notImplemented.
820 */
821 public void refreshRow() throws SQLException {
822 notImplemented();
823 }
824
825 /**
826 * Calls notImplemented.
827 */
828 public void cancelRowUpdates() throws SQLException {
829 notImplemented();
830 }
831
832 /**
833 * Calls notImplemented.
834 */
835 public void moveToInsertRow() throws SQLException {
836 notImplemented();
837 }
838
839 /**
840 * Calls notImplemented.
841 */
842 public void moveToCurrentRow() throws SQLException {
843 notImplemented();
844 }
845
846 /**
847 * Calls notImplemented.
848 */
849 public boolean wasNull() throws SQLException {
850 notImplemented();
851 return false;
852 }
853
854 } // end MockResultSet