1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  
19  package org.apache.commons.beanutils;
20  
21  
22  import java.io.InputStream;
23  import java.io.Reader;
24  import java.lang.reflect.InvocationHandler;
25  import java.lang.reflect.Method;
26  import java.lang.reflect.Proxy;
27  import java.math.BigDecimal;
28  import java.net.URL;
29  import java.sql.Array;
30  import java.sql.Blob;
31  import java.sql.Clob;
32  import java.sql.Date;
33  import java.sql.Ref;
34  import java.sql.ResultSet;
35  import java.sql.ResultSetMetaData;
36  import java.sql.SQLException;
37  import java.sql.SQLWarning;
38  import java.sql.Statement;
39  import java.sql.Time;
40  import java.sql.Timestamp;
41  import java.util.Calendar;
42  import java.util.Map;
43  
44  
45  /***
46   * <p>Mock object that implements enough of <code>java.sql.ResultSet</code>
47   * to exercise the {@link ResultSetDyaClass} functionality.</p>
48   *
49   * @author Craig R. McClanahan
50   * @version $Revision: 556233 $ $Date: 2007-07-14 07:37:06 +0100 (Sat, 14 Jul 2007) $
51   */
52  
53  public class TestResultSet implements InvocationHandler {
54  
55  
56      // ----------------------------------------------------- Instance Variables
57  
58  
59      /***
60       * Current row number (0 means "before the first one").
61       */
62      protected int row = 0;
63  
64  
65      /***
66       * The constant (per run) value used to initialize date/time/timestamp.
67       */
68      protected long timestamp = System.currentTimeMillis();
69  
70      /***
71       * Meta data for the result set.
72       */
73      protected ResultSetMetaData resultSetMetaData;
74  
75      /***
76       * Factory method for creating {@link ResultSet} proxies.
77       *
78       * @return A result set proxy
79       */
80      public static ResultSet createProxy() {
81          return TestResultSet.createProxy(new TestResultSet());
82      }
83  
84      /***
85       * Factory method for creating {@link ResultSet} proxies.
86       *
87       * @param invocationHandler Invocation Handler
88       * @return A result set proxy
89       */
90      public static ResultSet createProxy(InvocationHandler invocationHandler) {
91          ClassLoader classLoader = ResultSet.class.getClassLoader();
92          Class[] interfaces = new Class[] { ResultSet.class };
93          return (ResultSet)Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
94      }
95      
96      /***
97       * Create a proxy ResultSet.
98       */
99      public TestResultSet() {
100         this(TestResultSetMetaData.createProxy());
101     }
102     
103     /***
104      * Create a proxy ResultSet with the specified meta data.
105      *
106      * @param resultSetMetaData The result set meta data
107      */
108     public TestResultSet(ResultSetMetaData resultSetMetaData) {
109         this.resultSetMetaData = resultSetMetaData;
110     }
111 
112     /***
113      * Handles method invocation on the ResultSet proxy. 
114      *
115      * @param proxy The proxy ResultSet object
116      * @param method the method being invoked
117      * @param args The method arguments
118      * @return The result of invoking the method.
119      * @throws Throwable if an error occurs.
120      */
121     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
122         String methodName = method.getName();
123         if ("close".equals(methodName)) {
124             return null;
125         } if ("getMetaData".equals(methodName)) {
126             return getMetaData();
127         } if ("getObject".equals(methodName)) {
128             return getObject(columnName(args[0]));
129         } if ("getDate".equals(methodName)) {
130             return getDate(columnName(args[0]));
131         } if ("getTime".equals(methodName)) {
132             return getTime(columnName(args[0]));
133         } if ("getTimestamp".equals(methodName)) {
134             return getTimestamp(columnName(args[0]));
135         } if ("next".equals(methodName)) {
136             return (next() ? Boolean.TRUE : Boolean.FALSE);
137         } if ("updateObject".equals(methodName)) {
138             updateObject((String)args[0], args[1]);
139             return null;
140         }
141         
142         throw new UnsupportedOperationException(methodName + " not implemented");
143     }
144 
145     private String columnName(Object arg) throws SQLException {
146         if (arg instanceof Integer) {
147             return resultSetMetaData.getColumnName(((Integer)arg).intValue());
148         } else {
149             return (String)arg;
150         }
151     }
152 
153     // ---------------------------------------------------- Implemented Methods
154 
155 
156     public void close() throws SQLException {
157         // No action required
158     }
159 
160 
161     public ResultSetMetaData getMetaData() throws SQLException {
162         return resultSetMetaData;
163     }
164 
165 
166     public Object getObject(String columnName) throws SQLException {
167         if (row > 5) {
168             throw new SQLException("No current row");
169         }
170         if ("bigDecimalProperty".equals(columnName)) {
171             return (new BigDecimal(123.45));
172         } else if ("booleanProperty".equals(columnName)) {
173             if ((row % 2) == 0) {
174                 return (Boolean.TRUE);
175             } else {
176                 return (Boolean.FALSE);
177             }
178         } else if ("byteProperty".equals(columnName)) {
179             return (new Byte((byte) row));
180         } else if ("dateProperty".equals(columnName)) {
181             return (new Date(timestamp));
182         } else if ("doubleProperty".equals(columnName)) {
183             return (new Double(321.0));
184         } else if ("floatProperty".equals(columnName)) {
185             return (new Float((float) 123.0));
186         } else if ("intProperty".equals(columnName)) {
187             return (new Integer(100 + row));
188         } else if ("longProperty".equals(columnName)) {
189             return (new Long(200 + row));
190         } else if ("nullProperty".equals(columnName)) {
191             return (null);
192         } else if ("shortProperty".equals(columnName)) {
193             return (new Short((short) (300 + row)));
194         } else if ("stringProperty".equals(columnName)) {
195             return ("This is a string");
196         } else if ("timeProperty".equals(columnName)) {
197             return (new Time(timestamp));
198         } else if ("timestampProperty".equals(columnName)) {
199             return (new Timestamp(timestamp));
200         } else {
201             throw new SQLException("Unknown column name " + columnName);
202         }
203     }
204 
205     public Date getDate(String columnName) throws SQLException {
206         return (new Date(timestamp));
207     }
208 
209     public Time getTime(String columnName) throws SQLException {
210         return (new Time(timestamp));
211     }
212 
213     public Timestamp getTimestamp(String columnName) throws SQLException {
214         return (new Timestamp(timestamp));
215     }
216 
217     public boolean next() throws SQLException {
218         if (row++ < 5) {
219             return (true);
220         } else {
221             return (false);
222         }
223     }
224 
225 
226     public void updateObject(String columnName, Object x)
227         throws SQLException {
228         if (row > 5) {
229             throw new SQLException("No current row");
230         }
231         // FIXME - updateObject()
232     }
233 
234 
235     // -------------------------------------------------- Unimplemented Methods
236 
237 
238     public boolean absolute(int row) throws SQLException {
239         throw new UnsupportedOperationException();
240     }
241 
242 
243     public void afterLast() throws SQLException {
244         throw new UnsupportedOperationException();
245     }
246 
247 
248     public void beforeFirst() throws SQLException {
249         throw new UnsupportedOperationException();
250     }
251 
252 
253     public void cancelRowUpdates() throws SQLException {
254         throw new UnsupportedOperationException();
255     }
256 
257 
258     public void clearWarnings() throws SQLException {
259         throw new UnsupportedOperationException();
260     }
261 
262 
263     public void deleteRow() throws SQLException {
264         throw new UnsupportedOperationException();
265     }
266 
267 
268     public int findColumn(String columnName) throws SQLException {
269         throw new UnsupportedOperationException();
270     }
271 
272 
273     public boolean first() throws SQLException {
274         throw new UnsupportedOperationException();
275     }
276 
277 
278     public Array getArray(int columnIndex) throws SQLException {
279         throw new UnsupportedOperationException();
280     }
281 
282 
283     public Array getArray(String columnName) throws SQLException {
284         throw new UnsupportedOperationException();
285     }
286 
287 
288     public InputStream getAsciiStream(int columnIndex) throws SQLException {
289         throw new UnsupportedOperationException();
290     }
291 
292 
293     public InputStream getAsciiStream(String columnName) throws SQLException {
294         throw new UnsupportedOperationException();
295     }
296 
297 
298     public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
299         throw new UnsupportedOperationException();
300     }
301 
302     /*** @deprecated */
303     public BigDecimal getBigDecimal(int columnIndex, int scale)
304         throws SQLException {
305         throw new UnsupportedOperationException();
306     }
307 
308 
309     public BigDecimal getBigDecimal(String columnName) throws SQLException {
310         throw new UnsupportedOperationException();
311     }
312 
313 
314     /*** @deprecated */
315     public BigDecimal getBigDecimal(String columnName, int scale)
316         throws SQLException {
317         throw new UnsupportedOperationException();
318     }
319 
320 
321     public InputStream getBinaryStream(int columnIndex) throws SQLException {
322         throw new UnsupportedOperationException();
323     }
324 
325 
326     public InputStream getBinaryStream(String columnName) throws SQLException {
327         throw new UnsupportedOperationException();
328     }
329 
330 
331     public Blob getBlob(int columnIndex) throws SQLException {
332         throw new UnsupportedOperationException();
333     }
334 
335 
336     public Blob getBlob(String columnName) throws SQLException {
337         throw new UnsupportedOperationException();
338     }
339 
340 
341     public boolean getBoolean(int columnIndex) throws SQLException {
342         throw new UnsupportedOperationException();
343     }
344 
345 
346     public boolean getBoolean(String columnName) throws SQLException {
347         throw new UnsupportedOperationException();
348     }
349 
350 
351     public byte getByte(int columnIndex) throws SQLException {
352         throw new UnsupportedOperationException();
353     }
354 
355 
356     public byte getByte(String columnName) throws SQLException {
357         throw new UnsupportedOperationException();
358     }
359 
360 
361     public byte[] getBytes(int columnIndex) throws SQLException {
362         throw new UnsupportedOperationException();
363     }
364 
365 
366     public byte[] getBytes(String columnName) throws SQLException {
367         throw new UnsupportedOperationException();
368     }
369 
370 
371     public Reader getCharacterStream(int columnIndex)
372         throws SQLException {
373         throw new UnsupportedOperationException();
374     }
375 
376 
377     public Reader getCharacterStream(String columnName) throws SQLException {
378         throw new UnsupportedOperationException();
379     }
380 
381 
382     public Clob getClob(int columnIndex) throws SQLException {
383         throw new UnsupportedOperationException();
384     }
385 
386 
387     public Clob getClob(String columnName) throws SQLException {
388         throw new UnsupportedOperationException();
389     }
390 
391 
392     public int getConcurrency() throws SQLException {
393         throw new UnsupportedOperationException();
394     }
395 
396 
397     public String getCursorName() throws SQLException {
398         throw new UnsupportedOperationException();
399     }
400 
401 
402     public Date getDate(int columnIndex) throws SQLException {
403         throw new UnsupportedOperationException();
404     }
405 
406 
407     public Date getDate(int columnIndex, Calendar cal) throws SQLException {
408         throw new UnsupportedOperationException();
409     }
410 
411 
412 
413 
414     public Date getDate(String columnName, Calendar cal) throws SQLException {
415         throw new UnsupportedOperationException();
416     }
417 
418 
419     public double getDouble(int columnIndex) throws SQLException {
420         throw new UnsupportedOperationException();
421     }
422 
423 
424     public double getDouble(String columnName) throws SQLException {
425         throw new UnsupportedOperationException();
426     }
427 
428 
429     public int getFetchDirection() throws SQLException {
430         throw new UnsupportedOperationException();
431     }
432 
433 
434     public int getFetchSize() throws SQLException {
435         throw new UnsupportedOperationException();
436     }
437 
438 
439     public float getFloat(int columnIndex) throws SQLException {
440         throw new UnsupportedOperationException();
441     }
442 
443 
444     public float getFloat(String columnName) throws SQLException {
445         throw new UnsupportedOperationException();
446     }
447 
448 
449     public int getInt(int columnIndex) throws SQLException {
450         throw new UnsupportedOperationException();
451     }
452 
453 
454     public int getInt(String columnName) throws SQLException {
455         throw new UnsupportedOperationException();
456     }
457 
458 
459     public long getLong(int columnIndex) throws SQLException {
460         throw new UnsupportedOperationException();
461     }
462 
463 
464     public long getLong(String columnName) throws SQLException {
465         throw new UnsupportedOperationException();
466     }
467 
468 
469     public Object getObject(int columnIndex) throws SQLException {
470         throw new UnsupportedOperationException();
471     }
472 
473 
474     public Object getObject(int columnIndex, Map map) throws SQLException {
475         throw new UnsupportedOperationException();
476     }
477 
478 
479     public Object getObject(String columnName, Map map) throws SQLException {
480         throw new UnsupportedOperationException();
481     }
482 
483 
484     public Ref getRef(int columnIndex) throws SQLException {
485         throw new UnsupportedOperationException();
486     }
487 
488 
489     public Ref getRef(String columnName) throws SQLException {
490         throw new UnsupportedOperationException();
491     }
492 
493 
494     public int getRow() throws SQLException {
495         throw new UnsupportedOperationException();
496     }
497 
498 
499     public short getShort(int columnIndex) throws SQLException {
500         throw new UnsupportedOperationException();
501     }
502 
503 
504     public short getShort(String columnName) throws SQLException {
505         throw new UnsupportedOperationException();
506     }
507 
508 
509     public Statement getStatement() throws SQLException {
510         throw new UnsupportedOperationException();
511     }
512 
513 
514     public String getString(int columnIndex) throws SQLException {
515         throw new UnsupportedOperationException();
516     }
517 
518 
519     public String getString(String columnName) throws SQLException {
520         throw new UnsupportedOperationException();
521     }
522 
523 
524     public Time getTime(int columnIndex) throws SQLException {
525         throw new UnsupportedOperationException();
526     }
527 
528 
529     public Time getTime(int columnIndex, Calendar cal) throws SQLException {
530         throw new UnsupportedOperationException();
531     }
532 
533 
534 
535     public Time getTime(String columnName, Calendar cal) throws SQLException {
536         throw new UnsupportedOperationException();
537     }
538 
539 
540     public Timestamp getTimestamp(int columnIndex) throws SQLException {
541         throw new UnsupportedOperationException();
542     }
543 
544 
545     public Timestamp getTimestamp(int columnIndex, Calendar cal)
546         throws SQLException {
547         throw new UnsupportedOperationException();
548     }
549 
550 
551 
552     public Timestamp getTimestamp(String columnName, Calendar cal)
553         throws SQLException {
554         throw new UnsupportedOperationException();
555     }
556 
557 
558     public int getType() throws SQLException {
559         throw new UnsupportedOperationException();
560     }
561 
562 
563     /*** @deprecated */
564     public InputStream getUnicodeStream(int columnIndex) throws SQLException {
565         throw new UnsupportedOperationException();
566     }
567 
568 
569     /*** @deprecated */
570     public InputStream getUnicodeStream(String columnName) throws SQLException {
571         throw new UnsupportedOperationException();
572     }
573 
574 
575     public URL getURL(int columnIndex) throws SQLException {
576         throw new UnsupportedOperationException();
577     }
578 
579 
580     public URL getURL(String columnName) throws SQLException {
581         throw new UnsupportedOperationException();
582     }
583 
584 
585     public SQLWarning getWarnings() throws SQLException {
586         throw new UnsupportedOperationException();
587     }
588 
589 
590     public void insertRow() throws SQLException {
591         throw new UnsupportedOperationException();
592     }
593 
594 
595     public boolean isAfterLast() throws SQLException {
596         throw new UnsupportedOperationException();
597     }
598 
599 
600     public boolean isBeforeFirst() throws SQLException {
601         throw new UnsupportedOperationException();
602     }
603 
604 
605     public boolean isFirst() throws SQLException {
606         throw new UnsupportedOperationException();
607     }
608 
609 
610     public boolean isLast() throws SQLException {
611         throw new UnsupportedOperationException();
612     }
613 
614 
615     public boolean last() throws SQLException {
616         throw new UnsupportedOperationException();
617     }
618 
619 
620     public void moveToCurrentRow() throws SQLException {
621         throw new UnsupportedOperationException();
622     }
623 
624 
625     public void moveToInsertRow() throws SQLException {
626         throw new UnsupportedOperationException();
627     }
628 
629 
630     public boolean previous() throws SQLException {
631         throw new UnsupportedOperationException();
632     }
633 
634 
635     public void refreshRow() throws SQLException {
636         throw new UnsupportedOperationException();
637     }
638 
639 
640     public boolean relative(int rows) throws SQLException {
641         throw new UnsupportedOperationException();
642     }
643 
644 
645     public boolean rowDeleted() throws SQLException {
646         throw new UnsupportedOperationException();
647     }
648 
649 
650     public boolean rowInserted() throws SQLException {
651         throw new UnsupportedOperationException();
652     }
653 
654 
655     public boolean rowUpdated() throws SQLException {
656         throw new UnsupportedOperationException();
657     }
658 
659 
660     public void setFetchDirection(int direction) throws SQLException {
661         throw new UnsupportedOperationException();
662     }
663 
664 
665     public void setFetchSize(int size) throws SQLException {
666         throw new UnsupportedOperationException();
667     }
668 
669 
670     public void updateArray(int columnPosition, Array x)
671         throws SQLException {
672         throw new UnsupportedOperationException();
673     }
674 
675 
676     public void updateArray(String columnName, Array x)
677         throws SQLException {
678         throw new UnsupportedOperationException();
679     }
680 
681 
682     public void updateAsciiStream(int columnPosition, InputStream x, int len)
683         throws SQLException {
684         throw new UnsupportedOperationException();
685     }
686 
687 
688     public void updateAsciiStream(String columnName, InputStream x, int len)
689         throws SQLException {
690         throw new UnsupportedOperationException();
691     }
692 
693 
694     public void updateBigDecimal(int columnPosition, BigDecimal x)
695         throws SQLException {
696         throw new UnsupportedOperationException();
697     }
698 
699 
700     public void updateBigDecimal(String columnName, BigDecimal x)
701         throws SQLException {
702         throw new UnsupportedOperationException();
703     }
704 
705 
706     public void updateBinaryStream(int columnPosition, InputStream x, int len)
707         throws SQLException {
708         throw new UnsupportedOperationException();
709     }
710 
711 
712     public void updateBinaryStream(String columnName, InputStream x, int len)
713         throws SQLException {
714         throw new UnsupportedOperationException();
715     }
716 
717 
718     public void updateBlob(int columnPosition, Blob x)
719         throws SQLException {
720         throw new UnsupportedOperationException();
721     }
722 
723 
724     public void updateBlob(String columnName, Blob x)
725         throws SQLException {
726         throw new UnsupportedOperationException();
727     }
728 
729 
730     public void updateBoolean(int columnPosition, boolean x)
731         throws SQLException {
732         throw new UnsupportedOperationException();
733     }
734 
735 
736     public void updateBoolean(String columnName, boolean x)
737         throws SQLException {
738         throw new UnsupportedOperationException();
739     }
740 
741 
742     public void updateByte(int columnPosition, byte x)
743         throws SQLException {
744         throw new UnsupportedOperationException();
745     }
746 
747 
748     public void updateByte(String columnName, byte x)
749         throws SQLException {
750         throw new UnsupportedOperationException();
751     }
752 
753 
754     public void updateBytes(int columnPosition, byte x[])
755         throws SQLException {
756         throw new UnsupportedOperationException();
757     }
758 
759 
760     public void updateBytes(String columnName, byte x[])
761         throws SQLException {
762         throw new UnsupportedOperationException();
763     }
764 
765 
766     public void updateCharacterStream(int columnPosition, Reader x, int len)
767         throws SQLException {
768         throw new UnsupportedOperationException();
769     }
770 
771 
772     public void updateCharacterStream(String columnName, Reader x, int len)
773         throws SQLException {
774         throw new UnsupportedOperationException();
775     }
776 
777 
778     public void updateClob(int columnPosition, Clob x)
779         throws SQLException {
780         throw new UnsupportedOperationException();
781     }
782 
783 
784     public void updateClob(String columnName, Clob x)
785         throws SQLException {
786         throw new UnsupportedOperationException();
787     }
788 
789 
790     public void updateDate(int columnPosition, Date x)
791         throws SQLException {
792         throw new UnsupportedOperationException();
793     }
794 
795 
796     public void updateDate(String columnName, Date x)
797         throws SQLException {
798         throw new UnsupportedOperationException();
799     }
800 
801 
802     public void updateDouble(int columnPosition, double x)
803         throws SQLException {
804         throw new UnsupportedOperationException();
805     }
806 
807 
808     public void updateDouble(String columnName, double x)
809         throws SQLException {
810         throw new UnsupportedOperationException();
811     }
812 
813 
814     public void updateFloat(int columnPosition, float x)
815         throws SQLException {
816         throw new UnsupportedOperationException();
817     }
818 
819 
820     public void updateFloat(String columnName, float x)
821         throws SQLException {
822         throw new UnsupportedOperationException();
823     }
824 
825 
826     public void updateInt(int columnPosition, int x)
827         throws SQLException {
828         throw new UnsupportedOperationException();
829     }
830 
831 
832     public void updateInt(String columnName, int x)
833         throws SQLException {
834         throw new UnsupportedOperationException();
835     }
836 
837 
838     public void updateLong(int columnPosition, long x)
839         throws SQLException {
840         throw new UnsupportedOperationException();
841     }
842 
843 
844     public void updateLong(String columnName, long x)
845         throws SQLException {
846         throw new UnsupportedOperationException();
847     }
848 
849 
850     public void updateNull(int columnPosition)
851         throws SQLException {
852         throw new UnsupportedOperationException();
853     }
854 
855 
856     public void updateNull(String columnName)
857         throws SQLException {
858         throw new UnsupportedOperationException();
859     }
860 
861 
862     public void updateObject(int columnPosition, Object x)
863         throws SQLException {
864         throw new UnsupportedOperationException();
865     }
866 
867 
868     public void updateObject(int columnPosition, Object x, int scale)
869         throws SQLException {
870         throw new UnsupportedOperationException();
871     }
872 
873 
874     public void updateObject(String columnName, Object x, int scale)
875         throws SQLException {
876         throw new UnsupportedOperationException();
877     }
878 
879 
880     public void updateRef(int columnPosition, Ref x)
881         throws SQLException {
882         throw new UnsupportedOperationException();
883     }
884 
885 
886     public void updateRef(String columnName, Ref x)
887         throws SQLException {
888         throw new UnsupportedOperationException();
889     }
890 
891 
892     public void updateRow() throws SQLException {
893         throw new UnsupportedOperationException();
894     }
895 
896 
897     public void updateShort(int columnPosition, short x)
898         throws SQLException {
899         throw new UnsupportedOperationException();
900     }
901 
902 
903     public void updateShort(String columnName, short x)
904         throws SQLException {
905         throw new UnsupportedOperationException();
906     }
907 
908 
909     public void updateString(int columnPosition, String x)
910         throws SQLException {
911         throw new UnsupportedOperationException();
912     }
913 
914 
915     public void updateString(String columnName, String x)
916         throws SQLException {
917         throw new UnsupportedOperationException();
918     }
919 
920 
921     public void updateTime(int columnPosition, Time x)
922         throws SQLException {
923         throw new UnsupportedOperationException();
924     }
925 
926 
927     public void updateTime(String columnName, Time x)
928         throws SQLException {
929         throw new UnsupportedOperationException();
930     }
931 
932 
933     public void updateTimestamp(int columnPosition, Timestamp x)
934         throws SQLException {
935         throw new UnsupportedOperationException();
936     }
937 
938 
939     public void updateTimestamp(String columnName, Timestamp x)
940         throws SQLException {
941         throw new UnsupportedOperationException();
942     }
943 
944 
945     public boolean wasNull() throws SQLException {
946         throw new UnsupportedOperationException();
947     }
948 
949 
950 }