001: package net.sourceforge.squirrel_sql.mo.sql;
002:
003: import java.io.InputStream;
004: import java.io.Reader;
005: import java.math.BigDecimal;
006: import java.net.URL;
007: import java.sql.Array;
008: import java.sql.Blob;
009: import java.sql.Clob;
010: import java.sql.Date;
011: import java.sql.Ref;
012: import java.sql.ResultSet;
013: import java.sql.ResultSetMetaData;
014: import java.sql.SQLException;
015: import java.sql.SQLWarning;
016: import java.sql.Statement;
017: import java.sql.Time;
018: import java.sql.Timestamp;
019: import java.util.ArrayList;
020: import java.util.Calendar;
021: import java.util.Map;
022:
023: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
024:
025: /**
026: * A Mock ResultSet object useful for unit tests that require ResultSets.
027: *
028: * @author manningr
029: *
030: */
031: public class MockResultSet implements ResultSet {
032:
033: /**
034: * ResultSetMetaData describing this result.
035: */
036: ResultSetMetaData rsmd = null;
037:
038: /**
039: * The TableColumnInfos that describe the columns of the result.
040: */
041: TableColumnInfo[] _infos = null;
042:
043: /**
044: * The simulated results from the "database"
045: */
046: ArrayList<Object[]> rowData = new ArrayList<Object[]>();
047:
048: /**
049: * The index into rowData of the next row to be used to return column
050: * values
051: */
052: int cursorIndex = -1;
053:
054: public MockResultSet() {
055: }
056:
057: public void setMetaData(ResultSetMetaData md) {
058: rsmd = md;
059: }
060:
061: /**
062: *
063: * @param infos an array of TableColumnInfo items that describe the columns
064: * of the result set.
065: */
066: public MockResultSet(TableColumnInfo[] infos) {
067: _infos = infos;
068: rsmd = new MockResultSetMetaData(infos);
069: }
070:
071: public void setTableColumnInfos(TableColumnInfo[] infos) {
072: _infos = infos;
073: rsmd = new MockResultSetMetaData(infos);
074: }
075:
076: /**
077: * Adds a row of values to the result. Successive calls create additional
078: * rows.
079: *
080: * @param values
081: */
082: public void addRow(Object[] values) {
083: rowData.add(values);
084: }
085:
086: public boolean absolute(int arg0) throws SQLException {
087: // TODO Auto-generated method stub
088: return false;
089: }
090:
091: public void afterLast() throws SQLException {
092: // TODO Auto-generated method stub
093:
094: }
095:
096: public void beforeFirst() throws SQLException {
097: // TODO Auto-generated method stub
098:
099: }
100:
101: public void cancelRowUpdates() throws SQLException {
102: // TODO Auto-generated method stub
103:
104: }
105:
106: public void clearWarnings() throws SQLException {
107: // TODO Auto-generated method stub
108:
109: }
110:
111: public void close() throws SQLException {
112: // TODO Auto-generated method stub
113:
114: }
115:
116: public void deleteRow() throws SQLException {
117: // TODO Auto-generated method stub
118:
119: }
120:
121: public int findColumn(String arg0) throws SQLException {
122: // TODO Auto-generated method stub
123: return 0;
124: }
125:
126: public boolean first() throws SQLException {
127: // TODO Auto-generated method stub
128: return false;
129: }
130:
131: public Array getArray(int arg0) throws SQLException {
132: // TODO Auto-generated method stub
133: return null;
134: }
135:
136: public Array getArray(String arg0) throws SQLException {
137: // TODO Auto-generated method stub
138: return null;
139: }
140:
141: public InputStream getAsciiStream(int arg0) throws SQLException {
142: // TODO Auto-generated method stub
143: return null;
144: }
145:
146: public InputStream getAsciiStream(String arg0) throws SQLException {
147: // TODO Auto-generated method stub
148: return null;
149: }
150:
151: public BigDecimal getBigDecimal(int arg0) throws SQLException {
152: // TODO Auto-generated method stub
153: return null;
154: }
155:
156: public BigDecimal getBigDecimal(String arg0) throws SQLException {
157: // TODO Auto-generated method stub
158: return null;
159: }
160:
161: @SuppressWarnings("deprecation")
162: public BigDecimal getBigDecimal(int arg0, int arg1)
163: throws SQLException {
164: // TODO Auto-generated method stub
165: return null;
166: }
167:
168: @SuppressWarnings("deprecation")
169: public BigDecimal getBigDecimal(String arg0, int arg1)
170: throws SQLException {
171: // TODO Auto-generated method stub
172: return null;
173: }
174:
175: public InputStream getBinaryStream(int arg0) throws SQLException {
176: // TODO Auto-generated method stub
177: return null;
178: }
179:
180: public InputStream getBinaryStream(String arg0) throws SQLException {
181: // TODO Auto-generated method stub
182: return null;
183: }
184:
185: public Blob getBlob(int arg0) throws SQLException {
186: // TODO Auto-generated method stub
187: return null;
188: }
189:
190: public Blob getBlob(String arg0) throws SQLException {
191: // TODO Auto-generated method stub
192: return null;
193: }
194:
195: public boolean getBoolean(int arg0) throws SQLException {
196: // TODO Auto-generated method stub
197: return false;
198: }
199:
200: public boolean getBoolean(String arg0) throws SQLException {
201: // TODO Auto-generated method stub
202: return false;
203: }
204:
205: public byte getByte(int arg0) throws SQLException {
206: // TODO Auto-generated method stub
207: return 0;
208: }
209:
210: public byte getByte(String arg0) throws SQLException {
211: // TODO Auto-generated method stub
212: return 0;
213: }
214:
215: public byte[] getBytes(int arg0) throws SQLException {
216: // TODO Auto-generated method stub
217: return null;
218: }
219:
220: public byte[] getBytes(String arg0) throws SQLException {
221: // TODO Auto-generated method stub
222: return null;
223: }
224:
225: public Reader getCharacterStream(int arg0) throws SQLException {
226: // TODO Auto-generated method stub
227: return null;
228: }
229:
230: public Reader getCharacterStream(String arg0) throws SQLException {
231: // TODO Auto-generated method stub
232: return null;
233: }
234:
235: public Clob getClob(int arg0) throws SQLException {
236: // TODO Auto-generated method stub
237: return null;
238: }
239:
240: public Clob getClob(String arg0) throws SQLException {
241: // TODO Auto-generated method stub
242: return null;
243: }
244:
245: public int getConcurrency() throws SQLException {
246: // TODO Auto-generated method stub
247: return 0;
248: }
249:
250: public String getCursorName() throws SQLException {
251: // TODO Auto-generated method stub
252: return null;
253: }
254:
255: public Date getDate(int colIdx) throws SQLException {
256: Object[] currentRow = currentRow();
257: if (colIdx - 1 < currentRow.length) {
258:
259: if (currentRow[colIdx - 1] instanceof Date) {
260: return (Date) currentRow[colIdx - 1];
261: } else {
262: throw new SQLException("not a Date: "
263: + currentRow[colIdx - 1].getClass().getName());
264: }
265: } else {
266: throw new SQLException(
267: "Error: requested value for non-existant column with index="
268: + (colIdx - 1));
269: }
270: }
271:
272: public Date getDate(String arg0) throws SQLException {
273: // TODO Auto-generated method stub
274: return null;
275: }
276:
277: public Date getDate(int arg0, Calendar arg1) throws SQLException {
278: // TODO Auto-generated method stub
279: return null;
280: }
281:
282: public Date getDate(String arg0, Calendar arg1) throws SQLException {
283: // TODO Auto-generated method stub
284: return null;
285: }
286:
287: public double getDouble(int arg0) throws SQLException {
288: // TODO Auto-generated method stub
289: return 0;
290: }
291:
292: public double getDouble(String arg0) throws SQLException {
293: // TODO Auto-generated method stub
294: return 0;
295: }
296:
297: public int getFetchDirection() throws SQLException {
298: // TODO Auto-generated method stub
299: return 0;
300: }
301:
302: public int getFetchSize() throws SQLException {
303: // TODO Auto-generated method stub
304: return 0;
305: }
306:
307: public float getFloat(int arg0) throws SQLException {
308: // TODO Auto-generated method stub
309: return 0;
310: }
311:
312: public float getFloat(String arg0) throws SQLException {
313: // TODO Auto-generated method stub
314: return 0;
315: }
316:
317: public int getInt(int arg0) throws SQLException {
318: return getIntValue(arg0);
319: }
320:
321: public int getInt(String arg0) throws SQLException {
322: // TODO Auto-generated method stub
323: return 0;
324: }
325:
326: public long getLong(int arg0) throws SQLException {
327: return getLongValue(arg0);
328: }
329:
330: public long getLong(String arg0) throws SQLException {
331: // TODO Auto-generated method stub
332: return 0;
333: }
334:
335: public ResultSetMetaData getMetaData() throws SQLException {
336: return rsmd;
337: }
338:
339: public Object getObject(int column) throws SQLException {
340: return currentRow()[column - 1];
341: }
342:
343: public Object getObject(String arg0) throws SQLException {
344: // TODO Auto-generated method stub
345: return null;
346: }
347:
348: @SuppressWarnings("unchecked")
349: public Object getObject(int arg0, Map arg1) throws SQLException {
350: // TODO Auto-generated method stub
351: return null;
352: }
353:
354: @SuppressWarnings("unchecked")
355: public Object getObject(String arg0, Map arg1) throws SQLException {
356: // TODO Auto-generated method stub
357: return null;
358: }
359:
360: public Ref getRef(int arg0) throws SQLException {
361: // TODO Auto-generated method stub
362: return null;
363: }
364:
365: public Ref getRef(String arg0) throws SQLException {
366: // TODO Auto-generated method stub
367: return null;
368: }
369:
370: public int getRow() throws SQLException {
371: // TODO Auto-generated method stub
372: return 0;
373: }
374:
375: public short getShort(int arg0) throws SQLException {
376: // TODO Auto-generated method stub
377: return 0;
378: }
379:
380: public short getShort(String arg0) throws SQLException {
381: // TODO Auto-generated method stub
382: return 0;
383: }
384:
385: public Statement getStatement() throws SQLException {
386: // TODO Auto-generated method stub
387: return null;
388: }
389:
390: public String getString(int arg0) throws SQLException {
391: return getStringValue(arg0);
392: }
393:
394: public String getString(String arg0) throws SQLException {
395: // TODO Auto-generated method stub
396: return null;
397: }
398:
399: public Time getTime(int arg0) throws SQLException {
400: // TODO Auto-generated method stub
401: return null;
402: }
403:
404: public Time getTime(String arg0) throws SQLException {
405: // TODO Auto-generated method stub
406: return null;
407: }
408:
409: public Time getTime(int arg0, Calendar arg1) throws SQLException {
410: // TODO Auto-generated method stub
411: return null;
412: }
413:
414: public Time getTime(String arg0, Calendar arg1) throws SQLException {
415: // TODO Auto-generated method stub
416: return null;
417: }
418:
419: public Timestamp getTimestamp(int column) throws SQLException {
420: Object date = getObject(column);
421: if (date instanceof java.util.Date) {
422: long time = ((java.util.Date) date).getTime();
423: return new Timestamp(time);
424: } else {
425: return (Timestamp) date;
426: }
427: }
428:
429: public Timestamp getTimestamp(String arg0) throws SQLException {
430: // TODO Auto-generated method stub
431: return null;
432: }
433:
434: public Timestamp getTimestamp(int arg0, Calendar arg1)
435: throws SQLException {
436: // TODO Auto-generated method stub
437: return null;
438: }
439:
440: public Timestamp getTimestamp(String arg0, Calendar arg1)
441: throws SQLException {
442: // TODO Auto-generated method stub
443: return null;
444: }
445:
446: public int getType() throws SQLException {
447: // TODO Auto-generated method stub
448: return 0;
449: }
450:
451: public URL getURL(int arg0) throws SQLException {
452: // TODO Auto-generated method stub
453: return null;
454: }
455:
456: public URL getURL(String arg0) throws SQLException {
457: // TODO Auto-generated method stub
458: return null;
459: }
460:
461: @SuppressWarnings("deprecation")
462: public InputStream getUnicodeStream(int arg0) throws SQLException {
463: // TODO Auto-generated method stub
464: return null;
465: }
466:
467: @SuppressWarnings("deprecation")
468: public InputStream getUnicodeStream(String arg0)
469: throws SQLException {
470: // TODO Auto-generated method stub
471: return null;
472: }
473:
474: public SQLWarning getWarnings() throws SQLException {
475: // TODO Auto-generated method stub
476: return null;
477: }
478:
479: public void insertRow() throws SQLException {
480: // TODO Auto-generated method stub
481:
482: }
483:
484: public boolean isAfterLast() throws SQLException {
485: // TODO Auto-generated method stub
486: return false;
487: }
488:
489: public boolean isBeforeFirst() throws SQLException {
490: // TODO Auto-generated method stub
491: return false;
492: }
493:
494: public boolean isFirst() throws SQLException {
495: // TODO Auto-generated method stub
496: return false;
497: }
498:
499: public boolean isLast() throws SQLException {
500: // TODO Auto-generated method stub
501: return false;
502: }
503:
504: public boolean last() throws SQLException {
505: // TODO Auto-generated method stub
506: return false;
507: }
508:
509: public void moveToCurrentRow() throws SQLException {
510: // TODO Auto-generated method stub
511:
512: }
513:
514: public void moveToInsertRow() throws SQLException {
515: // TODO Auto-generated method stub
516:
517: }
518:
519: public boolean next() throws SQLException {
520: // TODO Auto-generated method stub
521: if (cursorIndex + 1 < rowData.size()) {
522: cursorIndex++;
523: return true;
524: }
525: return false;
526: }
527:
528: public boolean previous() throws SQLException {
529: // TODO Auto-generated method stub
530: return false;
531: }
532:
533: public void refreshRow() throws SQLException {
534: // TODO Auto-generated method stub
535:
536: }
537:
538: public boolean relative(int arg0) throws SQLException {
539: // TODO Auto-generated method stub
540: return false;
541: }
542:
543: public boolean rowDeleted() throws SQLException {
544: // TODO Auto-generated method stub
545: return false;
546: }
547:
548: public boolean rowInserted() throws SQLException {
549: // TODO Auto-generated method stub
550: return false;
551: }
552:
553: public boolean rowUpdated() throws SQLException {
554: // TODO Auto-generated method stub
555: return false;
556: }
557:
558: public void setFetchDirection(int arg0) throws SQLException {
559: // TODO Auto-generated method stub
560:
561: }
562:
563: public void setFetchSize(int arg0) throws SQLException {
564: // TODO Auto-generated method stub
565:
566: }
567:
568: public void updateArray(int arg0, Array arg1) throws SQLException {
569: // TODO Auto-generated method stub
570:
571: }
572:
573: public void updateArray(String arg0, Array arg1)
574: throws SQLException {
575: // TODO Auto-generated method stub
576:
577: }
578:
579: public void updateAsciiStream(int arg0, InputStream arg1, int arg2)
580: throws SQLException {
581: // TODO Auto-generated method stub
582:
583: }
584:
585: public void updateAsciiStream(String arg0, InputStream arg1,
586: int arg2) throws SQLException {
587: // TODO Auto-generated method stub
588:
589: }
590:
591: public void updateBigDecimal(int arg0, BigDecimal arg1)
592: throws SQLException {
593: // TODO Auto-generated method stub
594:
595: }
596:
597: public void updateBigDecimal(String arg0, BigDecimal arg1)
598: throws SQLException {
599: // TODO Auto-generated method stub
600:
601: }
602:
603: public void updateBinaryStream(int arg0, InputStream arg1, int arg2)
604: throws SQLException {
605: // TODO Auto-generated method stub
606:
607: }
608:
609: public void updateBinaryStream(String arg0, InputStream arg1,
610: int arg2) throws SQLException {
611: // TODO Auto-generated method stub
612:
613: }
614:
615: public void updateBlob(int arg0, Blob arg1) throws SQLException {
616: // TODO Auto-generated method stub
617:
618: }
619:
620: public void updateBlob(String arg0, Blob arg1) throws SQLException {
621: // TODO Auto-generated method stub
622:
623: }
624:
625: public void updateBoolean(int arg0, boolean arg1)
626: throws SQLException {
627: // TODO Auto-generated method stub
628:
629: }
630:
631: public void updateBoolean(String arg0, boolean arg1)
632: throws SQLException {
633: // TODO Auto-generated method stub
634:
635: }
636:
637: public void updateByte(int arg0, byte arg1) throws SQLException {
638: // TODO Auto-generated method stub
639:
640: }
641:
642: public void updateByte(String arg0, byte arg1) throws SQLException {
643: // TODO Auto-generated method stub
644:
645: }
646:
647: public void updateBytes(int arg0, byte[] arg1) throws SQLException {
648: // TODO Auto-generated method stub
649:
650: }
651:
652: public void updateBytes(String arg0, byte[] arg1)
653: throws SQLException {
654: // TODO Auto-generated method stub
655:
656: }
657:
658: public void updateCharacterStream(int arg0, Reader arg1, int arg2)
659: throws SQLException {
660: // TODO Auto-generated method stub
661:
662: }
663:
664: public void updateCharacterStream(String arg0, Reader arg1, int arg2)
665: throws SQLException {
666: // TODO Auto-generated method stub
667:
668: }
669:
670: public void updateClob(int arg0, Clob arg1) throws SQLException {
671: // TODO Auto-generated method stub
672:
673: }
674:
675: public void updateClob(String arg0, Clob arg1) throws SQLException {
676: // TODO Auto-generated method stub
677:
678: }
679:
680: public void updateDate(int arg0, Date arg1) throws SQLException {
681: // TODO Auto-generated method stub
682:
683: }
684:
685: public void updateDate(String arg0, Date arg1) throws SQLException {
686: // TODO Auto-generated method stub
687:
688: }
689:
690: public void updateDouble(int arg0, double arg1) throws SQLException {
691: // TODO Auto-generated method stub
692:
693: }
694:
695: public void updateDouble(String arg0, double arg1)
696: throws SQLException {
697: // TODO Auto-generated method stub
698:
699: }
700:
701: public void updateFloat(int arg0, float arg1) throws SQLException {
702: // TODO Auto-generated method stub
703:
704: }
705:
706: public void updateFloat(String arg0, float arg1)
707: throws SQLException {
708: // TODO Auto-generated method stub
709:
710: }
711:
712: public void updateInt(int arg0, int arg1) throws SQLException {
713: // TODO Auto-generated method stub
714:
715: }
716:
717: public void updateInt(String arg0, int arg1) throws SQLException {
718: // TODO Auto-generated method stub
719:
720: }
721:
722: public void updateLong(int arg0, long arg1) throws SQLException {
723: // TODO Auto-generated method stub
724:
725: }
726:
727: public void updateLong(String arg0, long arg1) throws SQLException {
728: // TODO Auto-generated method stub
729:
730: }
731:
732: public void updateNull(int arg0) throws SQLException {
733: // TODO Auto-generated method stub
734:
735: }
736:
737: public void updateNull(String arg0) throws SQLException {
738: // TODO Auto-generated method stub
739:
740: }
741:
742: public void updateObject(int arg0, Object arg1) throws SQLException {
743: // TODO Auto-generated method stub
744:
745: }
746:
747: public void updateObject(String arg0, Object arg1)
748: throws SQLException {
749: // TODO Auto-generated method stub
750:
751: }
752:
753: public void updateObject(int arg0, Object arg1, int arg2)
754: throws SQLException {
755: // TODO Auto-generated method stub
756:
757: }
758:
759: public void updateObject(String arg0, Object arg1, int arg2)
760: throws SQLException {
761: // TODO Auto-generated method stub
762:
763: }
764:
765: public void updateRef(int arg0, Ref arg1) throws SQLException {
766: // TODO Auto-generated method stub
767:
768: }
769:
770: public void updateRef(String arg0, Ref arg1) throws SQLException {
771: // TODO Auto-generated method stub
772:
773: }
774:
775: public void updateRow() throws SQLException {
776: // TODO Auto-generated method stub
777:
778: }
779:
780: public void updateShort(int arg0, short arg1) throws SQLException {
781: // TODO Auto-generated method stub
782:
783: }
784:
785: public void updateShort(String arg0, short arg1)
786: throws SQLException {
787: // TODO Auto-generated method stub
788:
789: }
790:
791: public void updateString(int arg0, String arg1) throws SQLException {
792: // TODO Auto-generated method stub
793:
794: }
795:
796: public void updateString(String arg0, String arg1)
797: throws SQLException {
798: // TODO Auto-generated method stub
799:
800: }
801:
802: public void updateTime(int arg0, Time arg1) throws SQLException {
803: // TODO Auto-generated method stub
804:
805: }
806:
807: public void updateTime(String arg0, Time arg1) throws SQLException {
808: // TODO Auto-generated method stub
809:
810: }
811:
812: public void updateTimestamp(int arg0, Timestamp arg1)
813: throws SQLException {
814: // TODO Auto-generated method stub
815:
816: }
817:
818: public void updateTimestamp(String arg0, Timestamp arg1)
819: throws SQLException {
820: // TODO Auto-generated method stub
821:
822: }
823:
824: public boolean wasNull() throws SQLException {
825: // TODO Auto-generated method stub
826: return false;
827: }
828:
829: /**
830: * JDBC numbers it's columns starting at 1. Since we use an array
831: * to represent columns, and the array has first element at index 0,
832: * be sure to subtract 1 from the specific JDBC colIdx to look in our
833: * array.
834: *
835: * @param colIdx
836: * @return
837: * @throws SQLException
838: */
839: private String getStringValue(int colIdx) throws SQLException {
840: Object[] currentRow = currentRow();
841: if (colIdx - 1 < currentRow.length) {
842: return String.valueOf(currentRow[colIdx - 1]);
843: } else {
844: throw new SQLException(
845: "Error: requested value for non-existant column with index="
846: + (colIdx - 1));
847: }
848: }
849:
850: private int getIntValue(int colIdx) throws SQLException {
851: return Integer.parseInt(getStringValue(colIdx));
852: }
853:
854: private long getLongValue(int colIdx) throws SQLException {
855: return Long.parseLong(getStringValue(colIdx));
856: }
857:
858: private Object[] currentRow() {
859: return rowData.get(cursorIndex);
860: }
861:
862: }
|