KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > sql > CommonMockSingleRowResultSet


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

58
59 // ------------------------------------------------------------------------ 78
60

61 package com.mockobjects.sql;
62
63 import java.sql.SQLException JavaDoc;
64 import java.util.Map JavaDoc;
65
66 /**
67  * Implementation of a MockResultSet that uses the ExpectationRow
68  * object to simulate a ResultSet that returns a single row.
69  * It verifies that the values fed to it have been retrieved
70  * throw a call to next and getRow.
71  * Column values can be retrieved using any of the getters
72  * declared in MockResultSet.
73  * Depending on which constructor is used,
74  * these can be found by either column index or column name.
75  * For basic java types (e.g. int, boolean), insert an instance of
76  * the appropriate object (e.g. Integer, Boolean).
77  * To force throwing a SQLException on a getter,
78  * set the corresponding value to be of type SQLException.
79  * @author Jeff Martin
80  * @author Ted Husted
81  * @version $Revision: 1.3 $
82  */

83 abstract class CommonMockSingleRowResultSet extends MockResultSet {
84
85 // -------------------------------------------------------------------- fields
86

87     private ExpectationSqlRow myRow =
88         new ExpectationSqlRow("CommonMockSingleRowResultSet.SqlRow");
89
90     private int myNextCallCount = 0;
91
92 // -------------------------------------------------------------- addExpected*
93

94
95     /**
96      * Add expected values from an array to be retrieved by index.
97      */

98     public void addExpectedIndexedValues(Object JavaDoc[] values) {
99         myRow.addExpectedIndexedValues(values);
100     }
101
102     /**
103      * Add expected values from an array to be retrieved by name or
104      * index.
105      */

106     public void addExpectedNamedValues(String JavaDoc[] names, Object JavaDoc[] values) {
107         myRow.addExpectedNamedValues(names, values);
108     }
109
110     /**
111      * Add expected values from a Map to be retrieved by name or
112      * index.
113      */

114     public void addExpectedNamedValues(Map JavaDoc map) {
115         myRow.addExpectedNamedValues(map);
116     }
117
118 // -------------------------------------------------------------- constructors
119

120     /**
121      * Default constructor.
122      * Call one of the AddExpected* methods before use,
123      * or use one of the alternate constructors instead.
124      */

125     public CommonMockSingleRowResultSet(){
126         super();
127     }
128
129     /**
130      * Constructor to create a single row ResultSet
131      * that can retrieve passed values by index.
132      */

133     public CommonMockSingleRowResultSet(Object JavaDoc[] values) {
134         addExpectedIndexedValues(values);
135     }
136
137     /**
138      * Constructor to create a single row ResultSet
139      * that can retrieve passed values by name.
140      */

141     public CommonMockSingleRowResultSet(String JavaDoc[] names, Object JavaDoc[] values) {
142         addExpectedNamedValues(names, values);
143     }
144
145     /**
146      * Constructor to create a single row ResultSet
147      * from a Map.
148      */

149     public CommonMockSingleRowResultSet(Map JavaDoc map) {
150         addExpectedNamedValues(map);
151     }
152
153 // --------------------------------------------------------------- implemented
154

155
156     /**
157      * Return true if this is the first time next is called.
158      */

159     public boolean next() throws SQLException JavaDoc {
160         myNextCalls.inc();
161         myNextCallCount++;
162         return myNextCallCount == 1;
163     }
164
165     /**
166      * Return 1 if next has been called exactly once.
167      */

168     public int getRow() throws SQLException JavaDoc {
169         return myNextCallCount;
170     }
171
172     /**
173      * Return value set for given index by an addExpected* method.
174      * Called by superclass to cast values to appropriate type.
175      */

176     public Object JavaDoc getObject(int columnIndex) throws SQLException JavaDoc {
177         return myRow.get(columnIndex);
178     }
179
180     /**
181      * Return value set for given name by an addExpectedNamedValue
182      * method.
183      * Called by superclass to cast values to appropriate type.
184      */

185     public Object JavaDoc getObject(String JavaDoc columnName) throws SQLException JavaDoc {
186         return myRow.get(columnName);
187     }
188 } // end CommonMockSingleRowResultSet
189
Popular Tags