KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbutils > MockResultSet


1 /*
2  * $Header: /home/cvs/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/MockResultSet.java,v 1.1 2003/11/02 19:15:23 dgraham Exp $
3  * $Revision: 1.1 $
4  * $Date: 2003/11/02 19:15:23 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2003 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowledgement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowledgement may appear in the software itself,
30  * if and wherever such third-party acknowledgements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Software Foundation.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62 package org.apache.commons.dbutils;
63
64 import java.lang.reflect.InvocationHandler JavaDoc;
65 import java.lang.reflect.Method JavaDoc;
66 import java.sql.ResultSet JavaDoc;
67 import java.sql.ResultSetMetaData JavaDoc;
68 import java.sql.SQLException JavaDoc;
69 import java.util.Arrays JavaDoc;
70 import java.util.Collections JavaDoc;
71 import java.util.Iterator JavaDoc;
72
73 /**
74  * MockResultSet dynamically implements the ResultSet interface.
75  *
76  * @author David Graham
77  */

78 public class MockResultSet implements InvocationHandler JavaDoc {
79
80     private ResultSetMetaData JavaDoc metaData = null;
81
82     private Iterator JavaDoc iter = null;
83
84     private Object JavaDoc[] currentRow = null;
85
86     private Boolean JavaDoc wasNull = Boolean.FALSE;
87
88     /**
89      * Create a <code>MockResultSet</code> proxy object. This is equivalent to:
90      * <pre>
91      * ProxyFactory.instance().createResultSet(new MockResultSet(metaData, rows));
92      * </pre>
93      *
94      * @param metaData
95      * @param rows A null value indicates an empty <code>ResultSet</code>.
96      * @return
97      */

98     public static ResultSet JavaDoc create(
99         ResultSetMetaData JavaDoc metaData,
100         Object JavaDoc[][] rows) {
101
102         return ProxyFactory.instance().createResultSet(
103             new MockResultSet(metaData, rows));
104     }
105
106     /**
107      * MockResultSet constructor.
108      * @param metaData
109      * @param rows A null value indicates an empty <code>ResultSet</code>.
110      */

111     public MockResultSet(ResultSetMetaData JavaDoc metaData, Object JavaDoc[][] rows) {
112         super();
113         this.metaData = metaData;
114         this.iter =
115             (rows == null)
116                 ? Collections.EMPTY_LIST.iterator()
117                 : Arrays.asList(rows).iterator();
118     }
119
120     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
121         throws Throwable JavaDoc {
122
123         String JavaDoc methodName = method.getName();
124
125         if (methodName.equals("getMetaData")) {
126             return this.getMetaData();
127
128         } else if (methodName.equals("next")) {
129             return this.next();
130
131         } else if (methodName.equals("previous")) {
132
133         } else if (methodName.equals("close")) {
134
135         } else if (methodName.equals("getObject")) {
136
137             if (args[0] instanceof Integer JavaDoc) {
138                 int col = ((Integer JavaDoc) args[0]).intValue();
139                 return this.getObject(col);
140
141             } else if (args[0] instanceof String JavaDoc) {
142                 return this.getObject((String JavaDoc) args[0]);
143             }
144
145         } else if (methodName.equals("getString")) {
146
147             if (args[0] instanceof Integer JavaDoc) {
148                 int col = ((Integer JavaDoc) args[0]).intValue();
149                 return this.getString(col);
150
151             } else if (args[0] instanceof String JavaDoc) {
152                 return this.getString((String JavaDoc) args[0]);
153             }
154
155         } else if (methodName.equals("wasNull")) {
156             return this.wasNull();
157
158         } else if (methodName.equals("isLast")) {
159             return this.isLast();
160         }
161
162         return null;
163     }
164
165     protected Boolean JavaDoc isLast() throws SQLException JavaDoc {
166         return this.iter.hasNext() ? Boolean.FALSE : Boolean.TRUE;
167     }
168
169     /**
170      * Gets the object at the given column index.
171      * @param columnIndex A 1 based index.
172      * @throws SQLException
173      */

174     protected Object JavaDoc getObject(int columnIndex) throws SQLException JavaDoc {
175         Object JavaDoc obj = this.currentRow[columnIndex - 1];
176         if (obj == null) {
177             this.wasNull = (obj == null) ? Boolean.TRUE : Boolean.FALSE;
178         }
179
180         return obj;
181     }
182
183     protected Object JavaDoc getObject(String JavaDoc columnName) throws SQLException JavaDoc {
184         return this.getObject(this.findColumnIndex(columnName));
185     }
186
187     /**
188      * Returns the column index for the given column name.
189      * @return A 1 based index
190      * @throws SQLException if the column name is invalid
191      */

192     private int findColumnIndex(String JavaDoc columnName) throws SQLException JavaDoc {
193         for (int i = 0; i < this.currentRow.length; i++) {
194             int c = i + 1;
195             if (this.metaData.getColumnName(c).equalsIgnoreCase(columnName)) {
196                 return c;
197             }
198         }
199
200         throw new SQLException JavaDoc(columnName + " is not a valid column name.");
201     }
202
203     /**
204      * Gets the String at the given column index.
205      * @param columnIndex A 1 based index.
206      * @throws SQLException
207      */

208     protected String JavaDoc getString(int columnIndex) throws SQLException JavaDoc {
209         Object JavaDoc obj = this.getObject(columnIndex);
210         return (obj == null) ? null : obj.toString();
211     }
212
213     protected String JavaDoc getString(String JavaDoc columnName) throws SQLException JavaDoc {
214         Object JavaDoc obj = this.getObject(this.findColumnIndex(columnName));
215         return (obj == null) ? null : obj.toString();
216     }
217
218     protected Boolean JavaDoc next() throws SQLException JavaDoc {
219         if (!this.iter.hasNext()) {
220             return Boolean.FALSE;
221         } else {
222             this.currentRow = (Object JavaDoc[]) iter.next();
223             return Boolean.TRUE;
224         }
225     }
226
227     protected ResultSetMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
228         return this.metaData;
229     }
230
231     protected Boolean JavaDoc wasNull() throws SQLException JavaDoc {
232         return this.wasNull;
233     }
234 }
Popular Tags