001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.io.InputStream;
021: import java.io.Reader;
022: import java.lang.reflect.InvocationHandler;
023: import java.lang.reflect.Method;
024: import java.lang.reflect.Proxy;
025: import java.math.BigDecimal;
026: import java.net.URL;
027: import java.sql.Array;
028: import java.sql.Blob;
029: import java.sql.Clob;
030: import java.sql.Date;
031: import java.sql.Ref;
032: import java.sql.ResultSet;
033: import java.sql.ResultSetMetaData;
034: import java.sql.SQLException;
035: import java.sql.SQLWarning;
036: import java.sql.Statement;
037: import java.sql.Time;
038: import java.sql.Timestamp;
039: import java.util.Calendar;
040: import java.util.Map;
041:
042: /**
043: * <p>Mock object that implements enough of <code>java.sql.ResultSet</code>
044: * to exercise the {@link ResultSetDyaClass} functionality.</p>
045: *
046: * @author Craig R. McClanahan
047: * @version $Revision: 556233 $ $Date: 2007-07-14 07:37:06 +0100 (Sat, 14 Jul 2007) $
048: */
049:
050: public class TestResultSet implements InvocationHandler {
051:
052: // ----------------------------------------------------- Instance Variables
053:
054: /**
055: * Current row number (0 means "before the first one").
056: */
057: protected int row = 0;
058:
059: /**
060: * The constant (per run) value used to initialize date/time/timestamp.
061: */
062: protected long timestamp = System.currentTimeMillis();
063:
064: /**
065: * Meta data for the result set.
066: */
067: protected ResultSetMetaData resultSetMetaData;
068:
069: /**
070: * Factory method for creating {@link ResultSet} proxies.
071: *
072: * @return A result set proxy
073: */
074: public static ResultSet createProxy() {
075: return TestResultSet.createProxy(new TestResultSet());
076: }
077:
078: /**
079: * Factory method for creating {@link ResultSet} proxies.
080: *
081: * @param invocationHandler Invocation Handler
082: * @return A result set proxy
083: */
084: public static ResultSet createProxy(
085: InvocationHandler invocationHandler) {
086: ClassLoader classLoader = ResultSet.class.getClassLoader();
087: Class[] interfaces = new Class[] { ResultSet.class };
088: return (ResultSet) Proxy.newProxyInstance(classLoader,
089: interfaces, invocationHandler);
090: }
091:
092: /**
093: * Create a proxy ResultSet.
094: */
095: public TestResultSet() {
096: this (TestResultSetMetaData.createProxy());
097: }
098:
099: /**
100: * Create a proxy ResultSet with the specified meta data.
101: *
102: * @param resultSetMetaData The result set meta data
103: */
104: public TestResultSet(ResultSetMetaData resultSetMetaData) {
105: this .resultSetMetaData = resultSetMetaData;
106: }
107:
108: /**
109: * Handles method invocation on the ResultSet proxy.
110: *
111: * @param proxy The proxy ResultSet object
112: * @param method the method being invoked
113: * @param args The method arguments
114: * @return The result of invoking the method.
115: * @throws Throwable if an error occurs.
116: */
117: public Object invoke(Object proxy, Method method, Object[] args)
118: throws Throwable {
119: String methodName = method.getName();
120: if ("close".equals(methodName)) {
121: return null;
122: }
123: if ("getMetaData".equals(methodName)) {
124: return getMetaData();
125: }
126: if ("getObject".equals(methodName)) {
127: return getObject(columnName(args[0]));
128: }
129: if ("getDate".equals(methodName)) {
130: return getDate(columnName(args[0]));
131: }
132: if ("getTime".equals(methodName)) {
133: return getTime(columnName(args[0]));
134: }
135: if ("getTimestamp".equals(methodName)) {
136: return getTimestamp(columnName(args[0]));
137: }
138: if ("next".equals(methodName)) {
139: return (next() ? Boolean.TRUE : Boolean.FALSE);
140: }
141: if ("updateObject".equals(methodName)) {
142: updateObject((String) args[0], args[1]);
143: return null;
144: }
145:
146: throw new UnsupportedOperationException(methodName
147: + " not implemented");
148: }
149:
150: private String columnName(Object arg) throws SQLException {
151: if (arg instanceof Integer) {
152: return resultSetMetaData.getColumnName(((Integer) arg)
153: .intValue());
154: } else {
155: return (String) arg;
156: }
157: }
158:
159: // ---------------------------------------------------- Implemented Methods
160:
161: public void close() throws SQLException {
162: // No action required
163: }
164:
165: public ResultSetMetaData getMetaData() throws SQLException {
166: return resultSetMetaData;
167: }
168:
169: public Object getObject(String columnName) throws SQLException {
170: if (row > 5) {
171: throw new SQLException("No current row");
172: }
173: if ("bigDecimalProperty".equals(columnName)) {
174: return (new BigDecimal(123.45));
175: } else if ("booleanProperty".equals(columnName)) {
176: if ((row % 2) == 0) {
177: return (Boolean.TRUE);
178: } else {
179: return (Boolean.FALSE);
180: }
181: } else if ("byteProperty".equals(columnName)) {
182: return (new Byte((byte) row));
183: } else if ("dateProperty".equals(columnName)) {
184: return (new Date(timestamp));
185: } else if ("doubleProperty".equals(columnName)) {
186: return (new Double(321.0));
187: } else if ("floatProperty".equals(columnName)) {
188: return (new Float((float) 123.0));
189: } else if ("intProperty".equals(columnName)) {
190: return (new Integer(100 + row));
191: } else if ("longProperty".equals(columnName)) {
192: return (new Long(200 + row));
193: } else if ("nullProperty".equals(columnName)) {
194: return (null);
195: } else if ("shortProperty".equals(columnName)) {
196: return (new Short((short) (300 + row)));
197: } else if ("stringProperty".equals(columnName)) {
198: return ("This is a string");
199: } else if ("timeProperty".equals(columnName)) {
200: return (new Time(timestamp));
201: } else if ("timestampProperty".equals(columnName)) {
202: return (new Timestamp(timestamp));
203: } else {
204: throw new SQLException("Unknown column name " + columnName);
205: }
206: }
207:
208: public Date getDate(String columnName) throws SQLException {
209: return (new Date(timestamp));
210: }
211:
212: public Time getTime(String columnName) throws SQLException {
213: return (new Time(timestamp));
214: }
215:
216: public Timestamp getTimestamp(String columnName)
217: throws SQLException {
218: return (new Timestamp(timestamp));
219: }
220:
221: public boolean next() throws SQLException {
222: if (row++ < 5) {
223: return (true);
224: } else {
225: return (false);
226: }
227: }
228:
229: public void updateObject(String columnName, Object x)
230: throws SQLException {
231: if (row > 5) {
232: throw new SQLException("No current row");
233: }
234: // FIXME - updateObject()
235: }
236:
237: // -------------------------------------------------- Unimplemented Methods
238:
239: public boolean absolute(int row) throws SQLException {
240: throw new UnsupportedOperationException();
241: }
242:
243: public void afterLast() throws SQLException {
244: throw new UnsupportedOperationException();
245: }
246:
247: public void beforeFirst() throws SQLException {
248: throw new UnsupportedOperationException();
249: }
250:
251: public void cancelRowUpdates() throws SQLException {
252: throw new UnsupportedOperationException();
253: }
254:
255: public void clearWarnings() throws SQLException {
256: throw new UnsupportedOperationException();
257: }
258:
259: public void deleteRow() throws SQLException {
260: throw new UnsupportedOperationException();
261: }
262:
263: public int findColumn(String columnName) throws SQLException {
264: throw new UnsupportedOperationException();
265: }
266:
267: public boolean first() throws SQLException {
268: throw new UnsupportedOperationException();
269: }
270:
271: public Array getArray(int columnIndex) throws SQLException {
272: throw new UnsupportedOperationException();
273: }
274:
275: public Array getArray(String columnName) throws SQLException {
276: throw new UnsupportedOperationException();
277: }
278:
279: public InputStream getAsciiStream(int columnIndex)
280: throws SQLException {
281: throw new UnsupportedOperationException();
282: }
283:
284: public InputStream getAsciiStream(String columnName)
285: throws SQLException {
286: throw new UnsupportedOperationException();
287: }
288:
289: public BigDecimal getBigDecimal(int columnIndex)
290: throws SQLException {
291: throw new UnsupportedOperationException();
292: }
293:
294: /** @deprecated */
295: public BigDecimal getBigDecimal(int columnIndex, int scale)
296: throws SQLException {
297: throw new UnsupportedOperationException();
298: }
299:
300: public BigDecimal getBigDecimal(String columnName)
301: throws SQLException {
302: throw new UnsupportedOperationException();
303: }
304:
305: /** @deprecated */
306: public BigDecimal getBigDecimal(String columnName, int scale)
307: throws SQLException {
308: throw new UnsupportedOperationException();
309: }
310:
311: public InputStream getBinaryStream(int columnIndex)
312: throws SQLException {
313: throw new UnsupportedOperationException();
314: }
315:
316: public InputStream getBinaryStream(String columnName)
317: throws SQLException {
318: throw new UnsupportedOperationException();
319: }
320:
321: public Blob getBlob(int columnIndex) throws SQLException {
322: throw new UnsupportedOperationException();
323: }
324:
325: public Blob getBlob(String columnName) throws SQLException {
326: throw new UnsupportedOperationException();
327: }
328:
329: public boolean getBoolean(int columnIndex) throws SQLException {
330: throw new UnsupportedOperationException();
331: }
332:
333: public boolean getBoolean(String columnName) throws SQLException {
334: throw new UnsupportedOperationException();
335: }
336:
337: public byte getByte(int columnIndex) throws SQLException {
338: throw new UnsupportedOperationException();
339: }
340:
341: public byte getByte(String columnName) throws SQLException {
342: throw new UnsupportedOperationException();
343: }
344:
345: public byte[] getBytes(int columnIndex) throws SQLException {
346: throw new UnsupportedOperationException();
347: }
348:
349: public byte[] getBytes(String columnName) throws SQLException {
350: throw new UnsupportedOperationException();
351: }
352:
353: public Reader getCharacterStream(int columnIndex)
354: throws SQLException {
355: throw new UnsupportedOperationException();
356: }
357:
358: public Reader getCharacterStream(String columnName)
359: throws SQLException {
360: throw new UnsupportedOperationException();
361: }
362:
363: public Clob getClob(int columnIndex) throws SQLException {
364: throw new UnsupportedOperationException();
365: }
366:
367: public Clob getClob(String columnName) throws SQLException {
368: throw new UnsupportedOperationException();
369: }
370:
371: public int getConcurrency() throws SQLException {
372: throw new UnsupportedOperationException();
373: }
374:
375: public String getCursorName() throws SQLException {
376: throw new UnsupportedOperationException();
377: }
378:
379: public Date getDate(int columnIndex) throws SQLException {
380: throw new UnsupportedOperationException();
381: }
382:
383: public Date getDate(int columnIndex, Calendar cal)
384: throws SQLException {
385: throw new UnsupportedOperationException();
386: }
387:
388: public Date getDate(String columnName, Calendar cal)
389: throws SQLException {
390: throw new UnsupportedOperationException();
391: }
392:
393: public double getDouble(int columnIndex) throws SQLException {
394: throw new UnsupportedOperationException();
395: }
396:
397: public double getDouble(String columnName) throws SQLException {
398: throw new UnsupportedOperationException();
399: }
400:
401: public int getFetchDirection() throws SQLException {
402: throw new UnsupportedOperationException();
403: }
404:
405: public int getFetchSize() throws SQLException {
406: throw new UnsupportedOperationException();
407: }
408:
409: public float getFloat(int columnIndex) throws SQLException {
410: throw new UnsupportedOperationException();
411: }
412:
413: public float getFloat(String columnName) throws SQLException {
414: throw new UnsupportedOperationException();
415: }
416:
417: public int getInt(int columnIndex) throws SQLException {
418: throw new UnsupportedOperationException();
419: }
420:
421: public int getInt(String columnName) throws SQLException {
422: throw new UnsupportedOperationException();
423: }
424:
425: public long getLong(int columnIndex) throws SQLException {
426: throw new UnsupportedOperationException();
427: }
428:
429: public long getLong(String columnName) throws SQLException {
430: throw new UnsupportedOperationException();
431: }
432:
433: public Object getObject(int columnIndex) throws SQLException {
434: throw new UnsupportedOperationException();
435: }
436:
437: public Object getObject(int columnIndex, Map map)
438: throws SQLException {
439: throw new UnsupportedOperationException();
440: }
441:
442: public Object getObject(String columnName, Map map)
443: throws SQLException {
444: throw new UnsupportedOperationException();
445: }
446:
447: public Ref getRef(int columnIndex) throws SQLException {
448: throw new UnsupportedOperationException();
449: }
450:
451: public Ref getRef(String columnName) throws SQLException {
452: throw new UnsupportedOperationException();
453: }
454:
455: public int getRow() throws SQLException {
456: throw new UnsupportedOperationException();
457: }
458:
459: public short getShort(int columnIndex) throws SQLException {
460: throw new UnsupportedOperationException();
461: }
462:
463: public short getShort(String columnName) throws SQLException {
464: throw new UnsupportedOperationException();
465: }
466:
467: public Statement getStatement() throws SQLException {
468: throw new UnsupportedOperationException();
469: }
470:
471: public String getString(int columnIndex) throws SQLException {
472: throw new UnsupportedOperationException();
473: }
474:
475: public String getString(String columnName) throws SQLException {
476: throw new UnsupportedOperationException();
477: }
478:
479: public Time getTime(int columnIndex) throws SQLException {
480: throw new UnsupportedOperationException();
481: }
482:
483: public Time getTime(int columnIndex, Calendar cal)
484: throws SQLException {
485: throw new UnsupportedOperationException();
486: }
487:
488: public Time getTime(String columnName, Calendar cal)
489: throws SQLException {
490: throw new UnsupportedOperationException();
491: }
492:
493: public Timestamp getTimestamp(int columnIndex) throws SQLException {
494: throw new UnsupportedOperationException();
495: }
496:
497: public Timestamp getTimestamp(int columnIndex, Calendar cal)
498: throws SQLException {
499: throw new UnsupportedOperationException();
500: }
501:
502: public Timestamp getTimestamp(String columnName, Calendar cal)
503: throws SQLException {
504: throw new UnsupportedOperationException();
505: }
506:
507: public int getType() throws SQLException {
508: throw new UnsupportedOperationException();
509: }
510:
511: /** @deprecated */
512: public InputStream getUnicodeStream(int columnIndex)
513: throws SQLException {
514: throw new UnsupportedOperationException();
515: }
516:
517: /** @deprecated */
518: public InputStream getUnicodeStream(String columnName)
519: throws SQLException {
520: throw new UnsupportedOperationException();
521: }
522:
523: public URL getURL(int columnIndex) throws SQLException {
524: throw new UnsupportedOperationException();
525: }
526:
527: public URL getURL(String columnName) throws SQLException {
528: throw new UnsupportedOperationException();
529: }
530:
531: public SQLWarning getWarnings() throws SQLException {
532: throw new UnsupportedOperationException();
533: }
534:
535: public void insertRow() throws SQLException {
536: throw new UnsupportedOperationException();
537: }
538:
539: public boolean isAfterLast() throws SQLException {
540: throw new UnsupportedOperationException();
541: }
542:
543: public boolean isBeforeFirst() throws SQLException {
544: throw new UnsupportedOperationException();
545: }
546:
547: public boolean isFirst() throws SQLException {
548: throw new UnsupportedOperationException();
549: }
550:
551: public boolean isLast() throws SQLException {
552: throw new UnsupportedOperationException();
553: }
554:
555: public boolean last() throws SQLException {
556: throw new UnsupportedOperationException();
557: }
558:
559: public void moveToCurrentRow() throws SQLException {
560: throw new UnsupportedOperationException();
561: }
562:
563: public void moveToInsertRow() throws SQLException {
564: throw new UnsupportedOperationException();
565: }
566:
567: public boolean previous() throws SQLException {
568: throw new UnsupportedOperationException();
569: }
570:
571: public void refreshRow() throws SQLException {
572: throw new UnsupportedOperationException();
573: }
574:
575: public boolean relative(int rows) throws SQLException {
576: throw new UnsupportedOperationException();
577: }
578:
579: public boolean rowDeleted() throws SQLException {
580: throw new UnsupportedOperationException();
581: }
582:
583: public boolean rowInserted() throws SQLException {
584: throw new UnsupportedOperationException();
585: }
586:
587: public boolean rowUpdated() throws SQLException {
588: throw new UnsupportedOperationException();
589: }
590:
591: public void setFetchDirection(int direction) throws SQLException {
592: throw new UnsupportedOperationException();
593: }
594:
595: public void setFetchSize(int size) throws SQLException {
596: throw new UnsupportedOperationException();
597: }
598:
599: public void updateArray(int columnPosition, Array x)
600: throws SQLException {
601: throw new UnsupportedOperationException();
602: }
603:
604: public void updateArray(String columnName, Array x)
605: throws SQLException {
606: throw new UnsupportedOperationException();
607: }
608:
609: public void updateAsciiStream(int columnPosition, InputStream x,
610: int len) throws SQLException {
611: throw new UnsupportedOperationException();
612: }
613:
614: public void updateAsciiStream(String columnName, InputStream x,
615: int len) throws SQLException {
616: throw new UnsupportedOperationException();
617: }
618:
619: public void updateBigDecimal(int columnPosition, BigDecimal x)
620: throws SQLException {
621: throw new UnsupportedOperationException();
622: }
623:
624: public void updateBigDecimal(String columnName, BigDecimal x)
625: throws SQLException {
626: throw new UnsupportedOperationException();
627: }
628:
629: public void updateBinaryStream(int columnPosition, InputStream x,
630: int len) throws SQLException {
631: throw new UnsupportedOperationException();
632: }
633:
634: public void updateBinaryStream(String columnName, InputStream x,
635: int len) throws SQLException {
636: throw new UnsupportedOperationException();
637: }
638:
639: public void updateBlob(int columnPosition, Blob x)
640: throws SQLException {
641: throw new UnsupportedOperationException();
642: }
643:
644: public void updateBlob(String columnName, Blob x)
645: throws SQLException {
646: throw new UnsupportedOperationException();
647: }
648:
649: public void updateBoolean(int columnPosition, boolean x)
650: throws SQLException {
651: throw new UnsupportedOperationException();
652: }
653:
654: public void updateBoolean(String columnName, boolean x)
655: throws SQLException {
656: throw new UnsupportedOperationException();
657: }
658:
659: public void updateByte(int columnPosition, byte x)
660: throws SQLException {
661: throw new UnsupportedOperationException();
662: }
663:
664: public void updateByte(String columnName, byte x)
665: throws SQLException {
666: throw new UnsupportedOperationException();
667: }
668:
669: public void updateBytes(int columnPosition, byte x[])
670: throws SQLException {
671: throw new UnsupportedOperationException();
672: }
673:
674: public void updateBytes(String columnName, byte x[])
675: throws SQLException {
676: throw new UnsupportedOperationException();
677: }
678:
679: public void updateCharacterStream(int columnPosition, Reader x,
680: int len) throws SQLException {
681: throw new UnsupportedOperationException();
682: }
683:
684: public void updateCharacterStream(String columnName, Reader x,
685: int len) throws SQLException {
686: throw new UnsupportedOperationException();
687: }
688:
689: public void updateClob(int columnPosition, Clob x)
690: throws SQLException {
691: throw new UnsupportedOperationException();
692: }
693:
694: public void updateClob(String columnName, Clob x)
695: throws SQLException {
696: throw new UnsupportedOperationException();
697: }
698:
699: public void updateDate(int columnPosition, Date x)
700: throws SQLException {
701: throw new UnsupportedOperationException();
702: }
703:
704: public void updateDate(String columnName, Date x)
705: throws SQLException {
706: throw new UnsupportedOperationException();
707: }
708:
709: public void updateDouble(int columnPosition, double x)
710: throws SQLException {
711: throw new UnsupportedOperationException();
712: }
713:
714: public void updateDouble(String columnName, double x)
715: throws SQLException {
716: throw new UnsupportedOperationException();
717: }
718:
719: public void updateFloat(int columnPosition, float x)
720: throws SQLException {
721: throw new UnsupportedOperationException();
722: }
723:
724: public void updateFloat(String columnName, float x)
725: throws SQLException {
726: throw new UnsupportedOperationException();
727: }
728:
729: public void updateInt(int columnPosition, int x)
730: throws SQLException {
731: throw new UnsupportedOperationException();
732: }
733:
734: public void updateInt(String columnName, int x) throws SQLException {
735: throw new UnsupportedOperationException();
736: }
737:
738: public void updateLong(int columnPosition, long x)
739: throws SQLException {
740: throw new UnsupportedOperationException();
741: }
742:
743: public void updateLong(String columnName, long x)
744: throws SQLException {
745: throw new UnsupportedOperationException();
746: }
747:
748: public void updateNull(int columnPosition) throws SQLException {
749: throw new UnsupportedOperationException();
750: }
751:
752: public void updateNull(String columnName) throws SQLException {
753: throw new UnsupportedOperationException();
754: }
755:
756: public void updateObject(int columnPosition, Object x)
757: throws SQLException {
758: throw new UnsupportedOperationException();
759: }
760:
761: public void updateObject(int columnPosition, Object x, int scale)
762: throws SQLException {
763: throw new UnsupportedOperationException();
764: }
765:
766: public void updateObject(String columnName, Object x, int scale)
767: throws SQLException {
768: throw new UnsupportedOperationException();
769: }
770:
771: public void updateRef(int columnPosition, Ref x)
772: throws SQLException {
773: throw new UnsupportedOperationException();
774: }
775:
776: public void updateRef(String columnName, Ref x) throws SQLException {
777: throw new UnsupportedOperationException();
778: }
779:
780: public void updateRow() throws SQLException {
781: throw new UnsupportedOperationException();
782: }
783:
784: public void updateShort(int columnPosition, short x)
785: throws SQLException {
786: throw new UnsupportedOperationException();
787: }
788:
789: public void updateShort(String columnName, short x)
790: throws SQLException {
791: throw new UnsupportedOperationException();
792: }
793:
794: public void updateString(int columnPosition, String x)
795: throws SQLException {
796: throw new UnsupportedOperationException();
797: }
798:
799: public void updateString(String columnName, String x)
800: throws SQLException {
801: throw new UnsupportedOperationException();
802: }
803:
804: public void updateTime(int columnPosition, Time x)
805: throws SQLException {
806: throw new UnsupportedOperationException();
807: }
808:
809: public void updateTime(String columnName, Time x)
810: throws SQLException {
811: throw new UnsupportedOperationException();
812: }
813:
814: public void updateTimestamp(int columnPosition, Timestamp x)
815: throws SQLException {
816: throw new UnsupportedOperationException();
817: }
818:
819: public void updateTimestamp(String columnName, Timestamp x)
820: throws SQLException {
821: throw new UnsupportedOperationException();
822: }
823:
824: public boolean wasNull() throws SQLException {
825: throw new UnsupportedOperationException();
826: }
827:
828: }
|