KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ibatis > SqlMapClientTests


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.orm.ibatis;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.sql.DataSource JavaDoc;
27
28 import com.ibatis.common.util.PaginatedArrayList;
29 import com.ibatis.common.util.PaginatedList;
30 import com.ibatis.sqlmap.client.SqlMapClient;
31 import com.ibatis.sqlmap.client.SqlMapExecutor;
32 import com.ibatis.sqlmap.client.SqlMapSession;
33 import com.ibatis.sqlmap.client.event.RowHandler;
34 import junit.framework.TestCase;
35 import org.easymock.MockControl;
36
37 import org.springframework.dao.DataAccessException;
38 import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;
39 import org.springframework.jdbc.datasource.DriverManagerDataSource;
40 import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
41 import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
42
43 /**
44  * @author Juergen Hoeller
45  * @author Alef Arendsen
46  * @since 09.10.2004
47  */

48 public class SqlMapClientTests extends TestCase {
49
50     public void testSqlMapClientFactoryBeanWithoutConfig() throws Exception JavaDoc {
51         SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
52         // explicitly set to null, don't know why ;-)
53
factory.setConfigLocation(null);
54         try {
55             factory.afterPropertiesSet();
56             fail("Should have thrown IllegalArgumentException");
57         }
58         catch (IllegalArgumentException JavaDoc ex) {
59             // expected
60
}
61     }
62
63     public void testSqlMapClientTemplate() throws SQLException JavaDoc {
64         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
65         DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
66         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
67         final Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
68         ds.getConnection();
69         dsControl.setReturnValue(con, 1);
70         con.close();
71         conControl.setVoidCallable(1);
72         dsControl.replay();
73         conControl.replay();
74
75         MockControl smsControl = MockControl.createControl(SqlMapSession.class);
76         final SqlMapSession sms = (SqlMapSession) smsControl.getMock();
77         MockControl smcControl = MockControl.createControl(SqlMapClient.class);
78         SqlMapClient smc = (SqlMapClient) smcControl.getMock();
79         smc.openSession();
80         smcControl.setReturnValue(sms);
81         sms.setUserConnection(con);
82         smsControl.setVoidCallable();
83         sms.close();
84         smsControl.setVoidCallable();
85         smsControl.replay();
86         smcControl.replay();
87
88         SqlMapClientTemplate template = new SqlMapClientTemplate();
89         template.setDataSource(ds);
90         template.setSqlMapClient(smc);
91         template.afterPropertiesSet();
92         Object JavaDoc result = template.execute(new SqlMapClientCallback() {
93             public Object JavaDoc doInSqlMapClient(SqlMapExecutor executor) {
94                 assertTrue(executor == sms);
95                 return "done";
96             }
97         });
98         assertEquals("done", result);
99         dsControl.verify();
100         conControl.verify();
101         smsControl.verify();
102         smcControl.verify();
103     }
104
105     public void testQueryForObjectOnSqlMapClient() throws SQLException JavaDoc {
106         DataSource JavaDoc dataSource = new TransactionAwareDataSourceProxy(new DriverManagerDataSource());
107         MockControl clientControl = MockControl.createControl(SqlMapClient.class);
108         SqlMapClient client = (SqlMapClient) clientControl.getMock();
109         client.getDataSource();
110         clientControl.setReturnValue(dataSource, 2);
111         client.queryForObject("myStatement", "myParameter");
112         clientControl.setReturnValue("myResult", 1);
113         clientControl.replay();
114         SqlMapClientTemplate template = new SqlMapClientTemplate();
115         template.setSqlMapClient(client);
116         assertEquals("myResult", template.queryForObject("myStatement", "myParameter"));
117         clientControl.verify();
118     }
119
120     public void testQueryForObjectOnSqlMapSession() throws SQLException JavaDoc {
121         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
122         DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
123         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
124         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
125         MockControl clientControl = MockControl.createControl(SqlMapClient.class);
126         SqlMapClient client = (SqlMapClient) clientControl.getMock();
127         MockControl sessionControl = MockControl.createControl(SqlMapSession.class);
128         SqlMapSession session = (SqlMapSession) sessionControl.getMock();
129
130         ds.getConnection();
131         dsControl.setReturnValue(con, 1);
132         con.close();
133         conControl.setVoidCallable(1);
134         client.getDataSource();
135         clientControl.setReturnValue(ds, 3);
136         client.openSession();
137         clientControl.setReturnValue(session, 1);
138         session.setUserConnection(con);
139         sessionControl.setVoidCallable(1);
140         session.queryForObject("myStatement", "myParameter");
141         sessionControl.setReturnValue("myResult", 1);
142         session.close();
143         sessionControl.setVoidCallable(1);
144
145         dsControl.replay();
146         conControl.replay();
147         clientControl.replay();
148         sessionControl.replay();
149
150         SqlMapClientTemplate template = new SqlMapClientTemplate();
151         template.setSqlMapClient(client);
152         assertEquals("myResult", template.queryForObject("myStatement", "myParameter"));
153
154         dsControl.verify();
155         clientControl.verify();
156     }
157
158     public void testQueryForObject() throws SQLException JavaDoc {
159         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
160         template.executor.queryForObject("myStatement", "myParameter");
161         template.executorControl.setReturnValue("myResult", 1);
162         template.executorControl.replay();
163         assertEquals("myResult", template.queryForObject("myStatement", "myParameter"));
164         template.executorControl.verify();
165     }
166
167     public void testQueryForObjectWithResultObject() throws SQLException JavaDoc {
168         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
169         template.executor.queryForObject("myStatement", "myParameter", "myResult");
170         template.executorControl.setReturnValue("myResult", 1);
171         template.executorControl.replay();
172         assertEquals("myResult", template.queryForObject("myStatement", "myParameter", "myResult"));
173         template.executorControl.verify();
174     }
175
176     public void testQueryForList() throws SQLException JavaDoc {
177         List JavaDoc result = new ArrayList JavaDoc();
178         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
179         template.executor.queryForList("myStatement", "myParameter");
180         template.executorControl.setReturnValue(result, 1);
181         template.executorControl.replay();
182         assertEquals(result, template.queryForList("myStatement", "myParameter"));
183         template.executorControl.verify();
184     }
185
186     public void testQueryForListWithResultSize() throws SQLException JavaDoc {
187         List JavaDoc result = new ArrayList JavaDoc();
188         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
189         template.executor.queryForList("myStatement", "myParameter", 10, 20);
190         template.executorControl.setReturnValue(result, 1);
191         template.executorControl.replay();
192         assertEquals(result, template.queryForList("myStatement", "myParameter", 10, 20));
193         template.executorControl.verify();
194     }
195
196     public void testQueryWithRowHandler() throws SQLException JavaDoc {
197         RowHandler rowHandler = new RowHandler() {
198             public void handleRow(Object JavaDoc row) {
199             }
200         };
201         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
202         template.executor.queryWithRowHandler("myStatement", "myParameter", rowHandler);
203         template.executorControl.setVoidCallable(1);
204         template.executorControl.replay();
205         template.queryWithRowHandler("myStatement", "myParameter", rowHandler);
206         template.executorControl.verify();
207     }
208
209     public void testQueryForPaginatedList() throws SQLException JavaDoc {
210         PaginatedList result = new PaginatedArrayList(10);
211         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
212         template.executor.queryForPaginatedList("myStatement", "myParameter", 10);
213         template.executorControl.setReturnValue(result, 1);
214         template.executorControl.replay();
215         assertEquals(result, template.queryForPaginatedList("myStatement", "myParameter", 10));
216         template.executorControl.verify();
217     }
218
219     public void testQueryForMap() throws SQLException JavaDoc {
220         Map JavaDoc result = new HashMap JavaDoc();
221         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
222         template.executor.queryForMap("myStatement", "myParameter", "myKey");
223         template.executorControl.setReturnValue(result, 1);
224         template.executorControl.replay();
225         assertEquals(result, template.queryForMap("myStatement", "myParameter", "myKey"));
226         template.executorControl.verify();
227     }
228
229     public void testQueryForMapWithValueProperty() throws SQLException JavaDoc {
230         Map JavaDoc result = new HashMap JavaDoc();
231         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
232         template.executor.queryForMap("myStatement", "myParameter", "myKey", "myValue");
233         template.executorControl.setReturnValue(result, 1);
234         template.executorControl.replay();
235         assertEquals(result, template.queryForMap("myStatement", "myParameter", "myKey", "myValue"));
236         template.executorControl.verify();
237     }
238
239     public void testInsert() throws SQLException JavaDoc {
240         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
241         template.executor.insert("myStatement", "myParameter");
242         template.executorControl.setReturnValue("myResult", 1);
243         template.executorControl.replay();
244         assertEquals("myResult", template.insert("myStatement", "myParameter"));
245         template.executorControl.verify();
246     }
247
248     public void testUpdate() throws SQLException JavaDoc {
249         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
250         template.executor.update("myStatement", "myParameter");
251         template.executorControl.setReturnValue(10, 1);
252         template.executorControl.replay();
253         assertEquals(10, template.update("myStatement", "myParameter"));
254         template.executorControl.verify();
255     }
256
257     public void testDelete() throws SQLException JavaDoc {
258         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
259         template.executor.delete("myStatement", "myParameter");
260         template.executorControl.setReturnValue(10, 1);
261         template.executorControl.replay();
262         assertEquals(10, template.delete("myStatement", "myParameter"));
263         template.executorControl.verify();
264     }
265
266     public void testUpdateWithRequiredRowsAffected() throws SQLException JavaDoc {
267         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
268         template.executor.update("myStatement", "myParameter");
269         template.executorControl.setReturnValue(10, 1);
270         template.executorControl.replay();
271         template.update("myStatement", "myParameter", 10);
272         template.executorControl.verify();
273     }
274
275     public void testUpdateWithRequiredRowsAffectedAndInvalidRowCount() throws SQLException JavaDoc {
276         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
277         template.executor.update("myStatement", "myParameter");
278         template.executorControl.setReturnValue(20, 1);
279         template.executorControl.replay();
280         try {
281             template.update("myStatement", "myParameter", 10);
282             fail("Should have thrown JdbcUpdateAffectedIncorrectNumberOfRowsException");
283         }
284         catch (JdbcUpdateAffectedIncorrectNumberOfRowsException ex) {
285             // expected
286
assertEquals(10, ex.getExpectedRowsAffected());
287             assertEquals(20, ex.getActualRowsAffected());
288         }
289         template.executorControl.verify();
290     }
291
292     public void testDeleteWithRequiredRowsAffected() throws SQLException JavaDoc {
293         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
294         template.executor.delete("myStatement", "myParameter");
295         template.executorControl.setReturnValue(10, 1);
296         template.executorControl.replay();
297         template.delete("myStatement", "myParameter", 10);
298         template.executorControl.verify();
299     }
300
301     public void testDeleteWithRequiredRowsAffectedAndInvalidRowCount() throws SQLException JavaDoc {
302         TestSqlMapClientTemplate template = new TestSqlMapClientTemplate();
303         template.executor.delete("myStatement", "myParameter");
304         template.executorControl.setReturnValue(20, 1);
305         template.executorControl.replay();
306         try {
307             template.delete("myStatement", "myParameter", 10);
308             fail("Should have thrown JdbcUpdateAffectedIncorrectNumberOfRowsException");
309         }
310         catch (JdbcUpdateAffectedIncorrectNumberOfRowsException ex) {
311             // expected
312
assertEquals(10, ex.getExpectedRowsAffected());
313             assertEquals(20, ex.getActualRowsAffected());
314         }
315         template.executorControl.verify();
316     }
317
318     public void testSqlMapClientDaoSupport() throws Exception JavaDoc {
319         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
320         DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
321         SqlMapClientDaoSupport testDao = new SqlMapClientDaoSupport() {
322         };
323         testDao.setDataSource(ds);
324         assertEquals(ds, testDao.getDataSource());
325
326         MockControl smcControl = MockControl.createControl(SqlMapClient.class);
327         SqlMapClient smc = (SqlMapClient) smcControl.getMock();
328         smcControl.replay();
329
330         testDao.setSqlMapClient(smc);
331         assertEquals(smc, testDao.getSqlMapClient());
332
333         SqlMapClientTemplate template = new SqlMapClientTemplate();
334         template.setDataSource(ds);
335         template.setSqlMapClient(smc);
336         testDao.setSqlMapClientTemplate(template);
337         assertEquals(template, testDao.getSqlMapClientTemplate());
338
339         testDao.afterPropertiesSet();
340     }
341
342
343     private static class TestSqlMapClientTemplate extends SqlMapClientTemplate {
344
345         public MockControl executorControl = MockControl.createControl(SqlMapExecutor.class);
346         public SqlMapExecutor executor = (SqlMapExecutor) executorControl.getMock();
347
348         public Object JavaDoc execute(SqlMapClientCallback action) throws DataAccessException {
349             try {
350                 return action.doInSqlMapClient(executor);
351             }
352             catch (SQLException JavaDoc ex) {
353                 throw getExceptionTranslator().translate("SqlMapClient operation", null, ex);
354             }
355         }
356     }
357
358 }
359
Popular Tags