KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > RowMapperTests


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.core;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Statement JavaDoc;
24 import java.sql.Types JavaDoc;
25 import java.util.List JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.easymock.MockControl;
29
30 import org.springframework.beans.TestBean;
31 import org.springframework.jdbc.datasource.SingleConnectionDataSource;
32 import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
33
34 /**
35  * @author Juergen Hoeller
36  * @since 02.08.2004
37  */

38 public class RowMapperTests extends TestCase {
39
40     private MockControl conControl;
41
42     private Connection JavaDoc con;
43
44     private MockControl rsControl;
45
46     private ResultSet JavaDoc rs;
47
48     private JdbcTemplate jdbcTemplate;
49
50     private List JavaDoc result;
51
52     protected void setUp() throws SQLException JavaDoc {
53         conControl = MockControl.createControl(Connection JavaDoc.class);
54         con = (Connection JavaDoc) conControl.getMock();
55         con.isClosed();
56         conControl.setDefaultReturnValue(false);
57
58         rsControl = MockControl.createControl(ResultSet JavaDoc.class);
59         rs = (ResultSet JavaDoc) rsControl.getMock();
60         rs.next();
61         rsControl.setReturnValue(true, 1);
62         rs.getString(1);
63         rsControl.setReturnValue("tb1", 1);
64         rs.getInt(2);
65         rsControl.setReturnValue(1, 1);
66         rs.next();
67         rsControl.setReturnValue(true, 1);
68         rs.getString(1);
69         rsControl.setReturnValue("tb2", 1);
70         rs.getInt(2);
71         rsControl.setReturnValue(2, 1);
72         rs.next();
73         rsControl.setReturnValue(false, 1);
74         rs.close();
75         rsControl.setVoidCallable(1);
76         rsControl.replay();
77
78         jdbcTemplate = new JdbcTemplate();
79         jdbcTemplate.setDataSource(new SingleConnectionDataSource(con, false));
80         jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
81         jdbcTemplate.afterPropertiesSet();
82     }
83
84     public void testStaticQueryWithRowMapper() throws SQLException JavaDoc {
85         MockControl stmtControl = MockControl.createControl(Statement JavaDoc.class);
86         Statement JavaDoc stmt = (Statement JavaDoc) stmtControl.getMock();
87
88         con.createStatement();
89         conControl.setReturnValue(stmt, 1);
90         stmt.executeQuery("some SQL");
91         stmtControl.setReturnValue(rs, 1);
92         stmt.getWarnings();
93         stmtControl.setReturnValue(null, 1);
94         stmt.close();
95         stmtControl.setVoidCallable(1);
96
97         conControl.replay();
98         stmtControl.replay();
99
100         result = jdbcTemplate.query("some SQL", new TestRowMapper());
101
102         stmtControl.verify();
103         verify();
104     }
105
106     public void testPreparedStatementCreatorWithRowMapper() throws SQLException JavaDoc {
107         MockControl psControl = MockControl.createControl(PreparedStatement JavaDoc.class);
108         final PreparedStatement JavaDoc ps = (PreparedStatement JavaDoc) psControl.getMock();
109
110         ps.executeQuery();
111         psControl.setReturnValue(rs, 1);
112         ps.getWarnings();
113         psControl.setReturnValue(null, 1);
114         ps.close();
115         psControl.setVoidCallable(1);
116
117         conControl.replay();
118         psControl.replay();
119
120         result = jdbcTemplate.query(
121                 new PreparedStatementCreator() {
122                     public PreparedStatement JavaDoc createPreparedStatement(Connection JavaDoc con) throws SQLException JavaDoc {
123                         return ps;
124                     }
125                 }, new TestRowMapper());
126
127         psControl.verify();
128         verify();
129     }
130
131     public void testPreparedStatementSetterWithRowMapper() throws SQLException JavaDoc {
132         MockControl psControl = MockControl.createControl(PreparedStatement JavaDoc.class);
133         final PreparedStatement JavaDoc ps = (PreparedStatement JavaDoc) psControl.getMock();
134
135         con.prepareStatement("some SQL");
136         conControl.setReturnValue(ps, 1);
137         ps.setString(1, "test");
138         psControl.setVoidCallable(1);
139         ps.executeQuery();
140         psControl.setReturnValue(rs, 1);
141         ps.getWarnings();
142         psControl.setReturnValue(null, 1);
143         ps.close();
144         psControl.setVoidCallable(1);
145
146         conControl.replay();
147         psControl.replay();
148
149         result = jdbcTemplate.query(
150                 "some SQL",
151                 new PreparedStatementSetter() {
152                     public void setValues(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
153                         ps.setString(1, "test");
154                     }
155                 }, new TestRowMapper());
156
157         psControl.verify();
158         verify();
159     }
160
161     public void testQueryWithArgsAndRowMapper() throws SQLException JavaDoc {
162         MockControl psControl = MockControl.createControl(PreparedStatement JavaDoc.class);
163         final PreparedStatement JavaDoc ps = (PreparedStatement JavaDoc) psControl.getMock();
164
165         con.prepareStatement("some SQL");
166         conControl.setReturnValue(ps, 1);
167         ps.setObject(1, "test1");
168         ps.setObject(2, "test2");
169         psControl.setVoidCallable(1);
170         ps.executeQuery();
171         psControl.setReturnValue(rs, 1);
172         ps.getWarnings();
173         psControl.setReturnValue(null, 1);
174         ps.close();
175         psControl.setVoidCallable(1);
176
177         conControl.replay();
178         psControl.replay();
179
180         result = jdbcTemplate.query(
181                 "some SQL",
182                 new Object JavaDoc[] {"test1", "test2"},
183                 new TestRowMapper());
184
185         psControl.verify();
186         verify();
187     }
188
189     public void testQueryWithArgsAndTypesAndRowMapper() throws SQLException JavaDoc {
190         MockControl psControl = MockControl.createControl(PreparedStatement JavaDoc.class);
191         final PreparedStatement JavaDoc ps = (PreparedStatement JavaDoc) psControl.getMock();
192
193         con.prepareStatement("some SQL");
194         conControl.setReturnValue(ps, 1);
195         ps.setString(1, "test1");
196         ps.setString(2, "test2");
197         psControl.setVoidCallable(1);
198         ps.executeQuery();
199         psControl.setReturnValue(rs, 1);
200         ps.getWarnings();
201         psControl.setReturnValue(null, 1);
202         ps.close();
203         psControl.setVoidCallable(1);
204
205         conControl.replay();
206         psControl.replay();
207
208         result = jdbcTemplate.query(
209                 "some SQL",
210                 new Object JavaDoc[] {"test1", "test2"},
211                 new int[] {Types.VARCHAR, Types.VARCHAR},
212                 new TestRowMapper());
213
214         psControl.verify();
215         verify();
216     }
217
218     protected void verify() {
219         conControl.verify();
220         rsControl.verify();
221
222         assertTrue(result != null);
223         assertEquals(2, result.size());
224         TestBean tb1 = (TestBean) result.get(0);
225         TestBean tb2 = (TestBean) result.get(1);
226         assertEquals("tb1", tb1.getName());
227         assertEquals(1, tb1.getAge());
228         assertEquals("tb2", tb2.getName());
229         assertEquals(2, tb2.getAge());
230     }
231
232
233     private static class TestRowMapper implements RowMapper {
234
235         public Object JavaDoc mapRow(ResultSet JavaDoc rs, int rowNum) throws SQLException JavaDoc {
236             return new TestBean(rs.getString(1), rs.getInt(2));
237         }
238     }
239
240 }
241
Popular Tags