KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > object > SqlFunctionTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.object;
18
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Types JavaDoc;
23
24 import org.easymock.MockControl;
25
26 import org.springframework.dao.InvalidDataAccessApiUsageException;
27 import org.springframework.jdbc.AbstractJdbcTests;
28
29 /**
30  * @author Trevor Cook
31  */

32 public class SqlFunctionTests extends AbstractJdbcTests {
33
34     private static final String JavaDoc FUNCTION = "select count(id) from mytable";
35     private static final String JavaDoc FUNCTION_INT =
36         "select count(id) from mytable where myparam = ?";
37     private static final String JavaDoc FUNCTION_MIXED =
38         "select count(id) from mytable where myparam = ? and mystring = ?";
39
40     private MockControl ctrlPreparedStatement;
41     private PreparedStatement JavaDoc mockPreparedStatement;
42     private MockControl ctrlResultSet;
43     private ResultSet JavaDoc mockResultSet;
44
45     protected void setUp() throws Exception JavaDoc {
46         super.setUp();
47
48         ctrlPreparedStatement =
49             MockControl.createControl(PreparedStatement JavaDoc.class);
50         mockPreparedStatement =
51             (PreparedStatement JavaDoc) ctrlPreparedStatement.getMock();
52         ctrlResultSet = MockControl.createControl(ResultSet JavaDoc.class);
53         mockResultSet = (ResultSet JavaDoc) ctrlResultSet.getMock();
54     }
55
56     protected void tearDown() throws Exception JavaDoc {
57         super.tearDown();
58
59         if (shouldVerify()) {
60             ctrlPreparedStatement.verify();
61             ctrlResultSet.verify();
62         }
63     }
64
65     protected void replay() {
66         super.replay();
67         ctrlPreparedStatement.replay();
68         ctrlResultSet.replay();
69     }
70
71     public void testFunction() throws SQLException JavaDoc {
72         mockResultSet.next();
73         ctrlResultSet.setReturnValue(true);
74         mockResultSet.getObject(1);
75         ctrlResultSet.setReturnValue(new Integer JavaDoc(14));
76         mockResultSet.next();
77         ctrlResultSet.setReturnValue(false);
78         mockResultSet.close();
79         ctrlResultSet.setVoidCallable();
80
81         mockPreparedStatement.executeQuery();
82         ctrlPreparedStatement.setReturnValue(mockResultSet);
83         mockPreparedStatement.getWarnings();
84         ctrlPreparedStatement.setReturnValue(null);
85         mockPreparedStatement.close();
86         ctrlPreparedStatement.setVoidCallable();
87
88         mockConnection.prepareStatement(FUNCTION);
89         ctrlConnection.setReturnValue(mockPreparedStatement);
90
91         replay();
92
93         SqlFunction function = new SqlFunction();
94         function.setDataSource(mockDataSource);
95         function.setSql(FUNCTION);
96         function.compile();
97
98         int count = function.run();
99         assertTrue("Function returned value 14", count == 14);
100     }
101
102     public void testTooManyRows() throws SQLException JavaDoc {
103         mockResultSet.next();
104         ctrlResultSet.setReturnValue(true);
105         mockResultSet.getInt(1);
106         ctrlResultSet.setReturnValue(14);
107         mockResultSet.next();
108         ctrlResultSet.setReturnValue(true);
109         mockResultSet.close();
110         ctrlResultSet.setVoidCallable();
111
112         mockPreparedStatement.executeQuery();
113         ctrlPreparedStatement.setReturnValue(mockResultSet);
114         mockPreparedStatement.close();
115         ctrlPreparedStatement.setVoidCallable();
116
117         mockConnection.prepareStatement(FUNCTION);
118         ctrlConnection.setReturnValue(mockPreparedStatement);
119
120         replay();
121
122         SqlFunction function = new SqlFunction(mockDataSource, FUNCTION);
123         function.compile();
124
125         try {
126             int count = function.run();
127             fail("Shouldn't continue when too many rows returned");
128         }
129         catch (InvalidDataAccessApiUsageException idaauex) {
130             // OK
131
}
132     }
133
134     public void testFunctionInt() throws SQLException JavaDoc {
135         mockResultSet.next();
136         ctrlResultSet.setReturnValue(true);
137         mockResultSet.getInt(1);
138         ctrlResultSet.setReturnValue(14);
139         mockResultSet.next();
140         ctrlResultSet.setReturnValue(false);
141         mockResultSet.close();
142         ctrlResultSet.setVoidCallable();
143
144         mockPreparedStatement.setObject(1, new Integer JavaDoc(1), Types.INTEGER);
145         ctrlPreparedStatement.setVoidCallable();
146         mockPreparedStatement.executeQuery();
147         ctrlPreparedStatement.setReturnValue(mockResultSet);
148         mockPreparedStatement.getWarnings();
149         ctrlPreparedStatement.setReturnValue(null);
150         mockPreparedStatement.close();
151         ctrlPreparedStatement.setVoidCallable();
152
153         mockConnection.prepareStatement(FUNCTION_INT);
154         ctrlConnection.setReturnValue(mockPreparedStatement);
155
156         replay();
157
158         SqlFunction function =
159             new SqlFunction(
160                 mockDataSource,
161                 FUNCTION_INT,
162                 new int[] { Types.INTEGER });
163         function.compile();
164
165         int count = function.run(1);
166         assertTrue("Function returned value 14", count == 14);
167     }
168
169     public void testFunctionMixed() throws SQLException JavaDoc {
170         mockResultSet.next();
171         ctrlResultSet.setReturnValue(true);
172         mockResultSet.getInt(1);
173         ctrlResultSet.setReturnValue(14);
174         mockResultSet.next();
175         ctrlResultSet.setReturnValue(false);
176         mockResultSet.close();
177         ctrlResultSet.setVoidCallable();
178
179         mockPreparedStatement.setObject(1, new Integer JavaDoc(1), Types.INTEGER);
180         ctrlPreparedStatement.setVoidCallable();
181         mockPreparedStatement.setString(2, "rod");
182         ctrlPreparedStatement.setVoidCallable();
183         mockPreparedStatement.executeQuery();
184         ctrlPreparedStatement.setReturnValue(mockResultSet);
185         mockPreparedStatement.getWarnings();
186         ctrlPreparedStatement.setReturnValue(null);
187         mockPreparedStatement.close();
188         ctrlPreparedStatement.setVoidCallable();
189
190         mockConnection.prepareStatement(FUNCTION_MIXED);
191         ctrlConnection.setReturnValue(mockPreparedStatement);
192
193         replay();
194
195         SqlFunction function =
196             new SqlFunction(
197                 mockDataSource,
198                 FUNCTION_MIXED,
199                 new int[] { Types.INTEGER, Types.VARCHAR });
200         function.compile();
201
202         int count = function.run(new Object JavaDoc[] { new Integer JavaDoc(1), "rod" });
203         assertTrue("Function returned value 14", count == 14);
204     }
205
206 }
207
Popular Tags