View Javadoc

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