View Javadoc
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    *      https://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  package org.apache.commons.beanutils2.sql;
19  
20  import java.io.InputStream;
21  import java.io.Reader;
22  import java.lang.reflect.InvocationHandler;
23  import java.lang.reflect.Method;
24  import java.lang.reflect.Proxy;
25  import java.math.BigDecimal;
26  import java.net.URL;
27  import java.sql.Array;
28  import java.sql.Blob;
29  import java.sql.Clob;
30  import java.sql.Date;
31  import java.sql.Ref;
32  import java.sql.ResultSet;
33  import java.sql.ResultSetMetaData;
34  import java.sql.SQLException;
35  import java.sql.SQLWarning;
36  import java.sql.Statement;
37  import java.sql.Time;
38  import java.sql.Timestamp;
39  import java.util.Calendar;
40  import java.util.Map;
41  
42  /**
43   * <p>
44   * Mock object that implements enough of {@link java.sql.ResultSet} to exercise the {@link ResultSetDynaClass} functionality.
45   * </p>
46   */
47  
48  public class TestResultSet implements InvocationHandler {
49  
50      /**
51       * Factory method for creating {@link ResultSet} proxies.
52       *
53       * @return A result set proxy
54       */
55      public static ResultSet createProxy() {
56          return TestResultSet.createProxy(new TestResultSet());
57      }
58  
59      /**
60       * Factory method for creating {@link ResultSet} proxies.
61       *
62       * @param invocationHandler Invocation Handler
63       * @return A result set proxy
64       */
65      public static ResultSet createProxy(final InvocationHandler invocationHandler) {
66          final ClassLoader classLoader = ResultSet.class.getClassLoader();
67          final Class<?>[] interfaces = new Class[] { ResultSet.class };
68          return (ResultSet) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
69      }
70  
71      /**
72       * Current row number (0 means "before the first one").
73       */
74      protected int row;
75  
76      /**
77       * The constant (per run) value used to initialize date/time/timestamp.
78       */
79      protected long timestampMillis = System.currentTimeMillis();
80  
81      /**
82       * Meta data for the result set.
83       */
84      protected ResultSetMetaData resultSetMetaData;
85  
86      /**
87       * Create a proxy ResultSet.
88       */
89      public TestResultSet() {
90          this(TestResultSetMetaData.createProxy());
91      }
92  
93      /**
94       * Create a proxy ResultSet with the specified meta data.
95       *
96       * @param resultSetMetaData The result set meta data
97       */
98      public TestResultSet(final ResultSetMetaData resultSetMetaData) {
99          this.resultSetMetaData = resultSetMetaData;
100     }
101 
102     public boolean absolute(final int row) throws SQLException {
103         throw new UnsupportedOperationException();
104     }
105 
106     public void afterLast() throws SQLException {
107         throw new UnsupportedOperationException();
108     }
109 
110     public void beforeFirst() throws SQLException {
111         throw new UnsupportedOperationException();
112     }
113 
114     public void cancelRowUpdates() throws SQLException {
115         throw new UnsupportedOperationException();
116     }
117 
118     public void clearWarnings() throws SQLException {
119         throw new UnsupportedOperationException();
120     }
121 
122     public void close() throws SQLException {
123         // No action required
124     }
125 
126     private String columnName(final Object arg) throws SQLException {
127         if (arg instanceof Integer) {
128             return resultSetMetaData.getColumnName(((Integer) arg).intValue());
129         }
130         return (String) arg;
131     }
132 
133     public void deleteRow() throws SQLException {
134         throw new UnsupportedOperationException();
135     }
136 
137     public int findColumn(final String columnName) throws SQLException {
138         throw new UnsupportedOperationException();
139     }
140 
141     public boolean first() throws SQLException {
142         throw new UnsupportedOperationException();
143     }
144 
145     public Array getArray(final int columnIndex) throws SQLException {
146         throw new UnsupportedOperationException();
147     }
148 
149     public Array getArray(final String columnName) throws SQLException {
150         throw new UnsupportedOperationException();
151     }
152 
153     public InputStream getAsciiStream(final int columnIndex) throws SQLException {
154         throw new UnsupportedOperationException();
155     }
156 
157     public InputStream getAsciiStream(final String columnName) throws SQLException {
158         throw new UnsupportedOperationException();
159     }
160 
161     public BigDecimal getBigDecimal(final int columnIndex) throws SQLException {
162         throw new UnsupportedOperationException();
163     }
164 
165     /** @deprecated */
166     @Deprecated
167     public BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException {
168         throw new UnsupportedOperationException();
169     }
170 
171     public BigDecimal getBigDecimal(final String columnName) throws SQLException {
172         throw new UnsupportedOperationException();
173     }
174 
175     /** @deprecated */
176     @Deprecated
177     public BigDecimal getBigDecimal(final String columnName, final int scale) throws SQLException {
178         throw new UnsupportedOperationException();
179     }
180 
181     public InputStream getBinaryStream(final int columnIndex) throws SQLException {
182         throw new UnsupportedOperationException();
183     }
184 
185     public InputStream getBinaryStream(final String columnName) throws SQLException {
186         throw new UnsupportedOperationException();
187     }
188 
189     public Blob getBlob(final int columnIndex) throws SQLException {
190         throw new UnsupportedOperationException();
191     }
192 
193     public Blob getBlob(final String columnName) throws SQLException {
194         throw new UnsupportedOperationException();
195     }
196 
197     public boolean getBoolean(final int columnIndex) throws SQLException {
198         throw new UnsupportedOperationException();
199     }
200 
201     public boolean getBoolean(final String columnName) throws SQLException {
202         throw new UnsupportedOperationException();
203     }
204 
205     public byte getByte(final int columnIndex) throws SQLException {
206         throw new UnsupportedOperationException();
207     }
208 
209     public byte getByte(final String columnName) throws SQLException {
210         throw new UnsupportedOperationException();
211     }
212 
213     public byte[] getBytes(final int columnIndex) throws SQLException {
214         throw new UnsupportedOperationException();
215     }
216 
217     public byte[] getBytes(final String columnName) throws SQLException {
218         throw new UnsupportedOperationException();
219     }
220 
221     public Reader getCharacterStream(final int columnIndex) throws SQLException {
222         throw new UnsupportedOperationException();
223     }
224 
225     public Reader getCharacterStream(final String columnName) throws SQLException {
226         throw new UnsupportedOperationException();
227     }
228 
229     public Clob getClob(final int columnIndex) throws SQLException {
230         throw new UnsupportedOperationException();
231     }
232 
233     public Clob getClob(final String columnName) throws SQLException {
234         throw new UnsupportedOperationException();
235     }
236 
237     public int getConcurrency() throws SQLException {
238         throw new UnsupportedOperationException();
239     }
240 
241     public String getCursorName() throws SQLException {
242         throw new UnsupportedOperationException();
243     }
244 
245     public Date getDate(final int columnIndex) throws SQLException {
246         throw new UnsupportedOperationException();
247     }
248 
249     public Date getDate(final int columnIndex, final Calendar cal) throws SQLException {
250         throw new UnsupportedOperationException();
251     }
252 
253     public Date getDate(final String columnName) throws SQLException {
254         return new Date(timestampMillis);
255     }
256 
257     public Date getDate(final String columnName, final Calendar cal) throws SQLException {
258         throw new UnsupportedOperationException();
259     }
260 
261     public double getDouble(final int columnIndex) throws SQLException {
262         throw new UnsupportedOperationException();
263     }
264 
265     public double getDouble(final String columnName) throws SQLException {
266         throw new UnsupportedOperationException();
267     }
268 
269     public int getFetchDirection() throws SQLException {
270         throw new UnsupportedOperationException();
271     }
272 
273     public int getFetchSize() throws SQLException {
274         throw new UnsupportedOperationException();
275     }
276 
277     public float getFloat(final int columnIndex) throws SQLException {
278         throw new UnsupportedOperationException();
279     }
280 
281     public float getFloat(final String columnName) throws SQLException {
282         throw new UnsupportedOperationException();
283     }
284 
285     public int getInt(final int columnIndex) throws SQLException {
286         throw new UnsupportedOperationException();
287     }
288 
289     public int getInt(final String columnName) throws SQLException {
290         throw new UnsupportedOperationException();
291     }
292 
293     public long getLong(final int columnIndex) throws SQLException {
294         throw new UnsupportedOperationException();
295     }
296 
297     public long getLong(final String columnName) throws SQLException {
298         throw new UnsupportedOperationException();
299     }
300 
301     public ResultSetMetaData getMetaData() throws SQLException {
302         return resultSetMetaData;
303     }
304 
305     public Object getObject(final int columnIndex) throws SQLException {
306         throw new UnsupportedOperationException();
307     }
308 
309     public Object getObject(final int columnIndex, final Map<?, ?> map) throws SQLException {
310         throw new UnsupportedOperationException();
311     }
312 
313     public Object getObject(final String columnName) throws SQLException {
314         if (row > 5) {
315             throw new SQLException("No current row");
316         }
317         switch (columnName) {
318         case "bigDecimalProperty":
319             return new BigDecimal(123.45);
320         case "booleanProperty":
321             if (row % 2 == 0) {
322                 return Boolean.TRUE;
323             }
324             return Boolean.FALSE;
325         case "byteProperty":
326             return Byte.valueOf((byte) row);
327         case "dateProperty":
328             return new Date(timestampMillis);
329         case "doubleProperty":
330             return Double.valueOf(321.0);
331         case "floatProperty":
332             return Float.valueOf((float) 123.0);
333         case "intProperty":
334             return Integer.valueOf(100 + row);
335         case "longProperty":
336             return Long.valueOf(200 + row);
337         case "nullProperty":
338             return null;
339         case "shortProperty":
340             return Short.valueOf((short) (300 + row));
341         case "stringProperty":
342             return "This is a string";
343         case "timeProperty":
344             return new Time(timestampMillis);
345         case "timestampProperty":
346             return new Timestamp(timestampMillis);
347         default:
348             break;
349         }
350         throw new SQLException("Unknown column name " + columnName);
351     }
352 
353     public Object getObject(final String columnName, final Map<?, ?> map) throws SQLException {
354         throw new UnsupportedOperationException();
355     }
356 
357     public Ref getRef(final int columnIndex) throws SQLException {
358         throw new UnsupportedOperationException();
359     }
360 
361     public Ref getRef(final String columnName) throws SQLException {
362         throw new UnsupportedOperationException();
363     }
364 
365     public int getRow() throws SQLException {
366         throw new UnsupportedOperationException();
367     }
368 
369     public short getShort(final int columnIndex) throws SQLException {
370         throw new UnsupportedOperationException();
371     }
372 
373     public short getShort(final String columnName) throws SQLException {
374         throw new UnsupportedOperationException();
375     }
376 
377     public Statement getStatement() throws SQLException {
378         throw new UnsupportedOperationException();
379     }
380 
381     public String getString(final int columnIndex) throws SQLException {
382         throw new UnsupportedOperationException();
383     }
384 
385     public String getString(final String columnName) throws SQLException {
386         throw new UnsupportedOperationException();
387     }
388 
389     public Time getTime(final int columnIndex) throws SQLException {
390         throw new UnsupportedOperationException();
391     }
392 
393     public Time getTime(final int columnIndex, final Calendar cal) throws SQLException {
394         throw new UnsupportedOperationException();
395     }
396 
397     public Time getTime(final String columnName) throws SQLException {
398         return new Time(timestampMillis);
399     }
400 
401     public Time getTime(final String columnName, final Calendar cal) throws SQLException {
402         throw new UnsupportedOperationException();
403     }
404 
405     public Timestamp getTimestamp(final int columnIndex) throws SQLException {
406         throw new UnsupportedOperationException();
407     }
408 
409     public Timestamp getTimestamp(final int columnIndex, final Calendar cal) throws SQLException {
410         throw new UnsupportedOperationException();
411     }
412 
413     public Timestamp getTimestamp(final String columnName) throws SQLException {
414         return new Timestamp(timestampMillis);
415     }
416 
417     public Timestamp getTimestamp(final String columnName, final Calendar cal) throws SQLException {
418         throw new UnsupportedOperationException();
419     }
420 
421     public int getType() throws SQLException {
422         throw new UnsupportedOperationException();
423     }
424 
425     /** @deprecated */
426     @Deprecated
427     public InputStream getUnicodeStream(final int columnIndex) throws SQLException {
428         throw new UnsupportedOperationException();
429     }
430 
431     /** @deprecated */
432     @Deprecated
433     public InputStream getUnicodeStream(final String columnName) throws SQLException {
434         throw new UnsupportedOperationException();
435     }
436 
437     public URL getURL(final int columnIndex) throws SQLException {
438         throw new UnsupportedOperationException();
439     }
440 
441     public URL getURL(final String columnName) throws SQLException {
442         throw new UnsupportedOperationException();
443     }
444 
445     public SQLWarning getWarnings() throws SQLException {
446         throw new UnsupportedOperationException();
447     }
448 
449     public void insertRow() throws SQLException {
450         throw new UnsupportedOperationException();
451     }
452 
453     /**
454      * Handles method invocation on the ResultSet proxy.
455      *
456      * @param proxy  The proxy ResultSet object
457      * @param method the method being invoked
458      * @param args   The method arguments
459      * @return The result of invoking the method.
460      * @throws Throwable if an error occurs.
461      */
462     @Override
463     public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
464         final String methodName = method.getName();
465         switch (methodName) {
466         case "close":
467             return null;
468         case "getMetaData":
469             return getMetaData();
470         case "getObject":
471             return getObject(columnName(args[0]));
472         case "getDate":
473             return getDate(columnName(args[0]));
474         case "getTime":
475             return getTime(columnName(args[0]));
476         case "getTimestamp":
477             return getTimestamp(columnName(args[0]));
478         case "next":
479             return next() ? Boolean.TRUE : Boolean.FALSE;
480         case "updateObject":
481             updateObject((String) args[0], args[1]);
482             return null;
483         default:
484             break;
485         }
486 
487         throw new UnsupportedOperationException(methodName + " not implemented");
488     }
489 
490     public boolean isAfterLast() throws SQLException {
491         throw new UnsupportedOperationException();
492     }
493 
494     public boolean isBeforeFirst() throws SQLException {
495         throw new UnsupportedOperationException();
496     }
497 
498     public boolean isFirst() throws SQLException {
499         throw new UnsupportedOperationException();
500     }
501 
502     public boolean isLast() throws SQLException {
503         throw new UnsupportedOperationException();
504     }
505 
506     public boolean last() throws SQLException {
507         throw new UnsupportedOperationException();
508     }
509 
510     public void moveToCurrentRow() throws SQLException {
511         throw new UnsupportedOperationException();
512     }
513 
514     public void moveToInsertRow() throws SQLException {
515         throw new UnsupportedOperationException();
516     }
517 
518     public boolean next() throws SQLException {
519         if (row++ < 5) {
520             return true;
521         }
522         return false;
523     }
524 
525     public boolean previous() throws SQLException {
526         throw new UnsupportedOperationException();
527     }
528 
529     public void refreshRow() throws SQLException {
530         throw new UnsupportedOperationException();
531     }
532 
533     public boolean relative(final int rows) throws SQLException {
534         throw new UnsupportedOperationException();
535     }
536 
537     public boolean rowDeleted() throws SQLException {
538         throw new UnsupportedOperationException();
539     }
540 
541     public boolean rowInserted() throws SQLException {
542         throw new UnsupportedOperationException();
543     }
544 
545     public boolean rowUpdated() throws SQLException {
546         throw new UnsupportedOperationException();
547     }
548 
549     public void setFetchDirection(final int direction) throws SQLException {
550         throw new UnsupportedOperationException();
551     }
552 
553     public void setFetchSize(final int size) throws SQLException {
554         throw new UnsupportedOperationException();
555     }
556 
557     public void updateArray(final int columnPosition, final Array x) throws SQLException {
558         throw new UnsupportedOperationException();
559     }
560 
561     public void updateArray(final String columnName, final Array x) throws SQLException {
562         throw new UnsupportedOperationException();
563     }
564 
565     public void updateAsciiStream(final int columnPosition, final InputStream x, final int len) throws SQLException {
566         throw new UnsupportedOperationException();
567     }
568 
569     public void updateAsciiStream(final String columnName, final InputStream x, final int len) throws SQLException {
570         throw new UnsupportedOperationException();
571     }
572 
573     public void updateBigDecimal(final int columnPosition, final BigDecimal x) throws SQLException {
574         throw new UnsupportedOperationException();
575     }
576 
577     public void updateBigDecimal(final String columnName, final BigDecimal x) throws SQLException {
578         throw new UnsupportedOperationException();
579     }
580 
581     public void updateBinaryStream(final int columnPosition, final InputStream x, final int len) throws SQLException {
582         throw new UnsupportedOperationException();
583     }
584 
585     public void updateBinaryStream(final String columnName, final InputStream x, final int len) throws SQLException {
586         throw new UnsupportedOperationException();
587     }
588 
589     public void updateBlob(final int columnPosition, final Blob x) throws SQLException {
590         throw new UnsupportedOperationException();
591     }
592 
593     public void updateBlob(final String columnName, final Blob x) throws SQLException {
594         throw new UnsupportedOperationException();
595     }
596 
597     public void updateBoolean(final int columnPosition, final boolean x) throws SQLException {
598         throw new UnsupportedOperationException();
599     }
600 
601     public void updateBoolean(final String columnName, final boolean x) throws SQLException {
602         throw new UnsupportedOperationException();
603     }
604 
605     public void updateByte(final int columnPosition, final byte x) throws SQLException {
606         throw new UnsupportedOperationException();
607     }
608 
609     public void updateByte(final String columnName, final byte x) throws SQLException {
610         throw new UnsupportedOperationException();
611     }
612 
613     public void updateBytes(final int columnPosition, final byte[] x) throws SQLException {
614         throw new UnsupportedOperationException();
615     }
616 
617     public void updateBytes(final String columnName, final byte[] x) throws SQLException {
618         throw new UnsupportedOperationException();
619     }
620 
621     public void updateCharacterStream(final int columnPosition, final Reader x, final int len) throws SQLException {
622         throw new UnsupportedOperationException();
623     }
624 
625     public void updateCharacterStream(final String columnName, final Reader x, final int len) throws SQLException {
626         throw new UnsupportedOperationException();
627     }
628 
629     public void updateClob(final int columnPosition, final Clob x) throws SQLException {
630         throw new UnsupportedOperationException();
631     }
632 
633     public void updateClob(final String columnName, final Clob x) throws SQLException {
634         throw new UnsupportedOperationException();
635     }
636 
637     public void updateDate(final int columnPosition, final Date x) throws SQLException {
638         throw new UnsupportedOperationException();
639     }
640 
641     public void updateDate(final String columnName, final Date x) throws SQLException {
642         throw new UnsupportedOperationException();
643     }
644 
645     public void updateDouble(final int columnPosition, final double x) throws SQLException {
646         throw new UnsupportedOperationException();
647     }
648 
649     public void updateDouble(final String columnName, final double x) throws SQLException {
650         throw new UnsupportedOperationException();
651     }
652 
653     public void updateFloat(final int columnPosition, final float x) throws SQLException {
654         throw new UnsupportedOperationException();
655     }
656 
657     public void updateFloat(final String columnName, final float x) throws SQLException {
658         throw new UnsupportedOperationException();
659     }
660 
661     public void updateInt(final int columnPosition, final int x) throws SQLException {
662         throw new UnsupportedOperationException();
663     }
664 
665     public void updateInt(final String columnName, final int x) throws SQLException {
666         throw new UnsupportedOperationException();
667     }
668 
669     public void updateLong(final int columnPosition, final long x) throws SQLException {
670         throw new UnsupportedOperationException();
671     }
672 
673     public void updateLong(final String columnName, final long x) throws SQLException {
674         throw new UnsupportedOperationException();
675     }
676 
677     public void updateNull(final int columnPosition) throws SQLException {
678         throw new UnsupportedOperationException();
679     }
680 
681     public void updateNull(final String columnName) throws SQLException {
682         throw new UnsupportedOperationException();
683     }
684 
685     public void updateObject(final int columnPosition, final Object x) throws SQLException {
686         throw new UnsupportedOperationException();
687     }
688 
689     public void updateObject(final int columnPosition, final Object x, final int scale) throws SQLException {
690         throw new UnsupportedOperationException();
691     }
692 
693     public void updateObject(final String columnName, final Object x) throws SQLException {
694         if (row > 5) {
695             throw new SQLException("No current row");
696         }
697         // FIXME - updateObject()
698     }
699 
700     public void updateObject(final String columnName, final Object x, final int scale) throws SQLException {
701         throw new UnsupportedOperationException();
702     }
703 
704     public void updateRef(final int columnPosition, final Ref x) throws SQLException {
705         throw new UnsupportedOperationException();
706     }
707 
708     public void updateRef(final String columnName, final Ref x) throws SQLException {
709         throw new UnsupportedOperationException();
710     }
711 
712     public void updateRow() throws SQLException {
713         throw new UnsupportedOperationException();
714     }
715 
716     public void updateShort(final int columnPosition, final short x) throws SQLException {
717         throw new UnsupportedOperationException();
718     }
719 
720     public void updateShort(final String columnName, final short x) throws SQLException {
721         throw new UnsupportedOperationException();
722     }
723 
724     public void updateString(final int columnPosition, final String x) throws SQLException {
725         throw new UnsupportedOperationException();
726     }
727 
728     public void updateString(final String columnName, final String x) throws SQLException {
729         throw new UnsupportedOperationException();
730     }
731 
732     public void updateTime(final int columnPosition, final Time x) throws SQLException {
733         throw new UnsupportedOperationException();
734     }
735 
736     public void updateTime(final String columnName, final Time x) throws SQLException {
737         throw new UnsupportedOperationException();
738     }
739 
740     public void updateTimestamp(final int columnPosition, final Timestamp x) throws SQLException {
741         throw new UnsupportedOperationException();
742     }
743 
744     public void updateTimestamp(final String columnName, final Timestamp x) throws SQLException {
745         throw new UnsupportedOperationException();
746     }
747 
748     public boolean wasNull() throws SQLException {
749         throw new UnsupportedOperationException();
750     }
751 
752 }