KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import com.ibatis.db.sqlmap.MappedStatement;
26 import com.ibatis.db.sqlmap.SqlMap;
27 import junit.framework.TestCase;
28 import org.easymock.MockControl;
29
30 import org.springframework.core.io.ClassPathResource;
31 import org.springframework.orm.ibatis.support.SqlMapDaoSupport;
32
33 /**
34  * @author Juergen Hoeller
35  * @since 28.11.2003
36  */

37 public class SqlMapTests extends TestCase {
38
39     public void testSqlMapFactoryBeanWithConfigNotFound() {
40         SqlMapFactoryBean factory = new SqlMapFactoryBean();
41         factory.setConfigLocation(new ClassPathResource("example/sql-map-config.xml"));
42         try {
43             factory.afterPropertiesSet();
44             fail("Should have thrown IOException");
45         }
46         catch (IOException JavaDoc ex) {
47             // expected
48
}
49     }
50     
51     public void testSqlMapTemplate() throws SQLException JavaDoc {
52         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
53         DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
54         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
55         final Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
56         ds.getConnection();
57         dsControl.setReturnValue(con, 1);
58         con.close();
59         conControl.setVoidCallable(1);
60         dsControl.replay();
61         conControl.replay();
62
63         final MappedStatement stmt = new MappedStatement();
64         SqlMap map = new SqlMap() {
65             public MappedStatement getMappedStatement(String JavaDoc name) {
66                 if ("stmt".equals(name)) {
67                     return stmt;
68                 }
69                 return null;
70             }
71         };
72
73         SqlMapTemplate template = new SqlMapTemplate();
74         template.setDataSource(ds);
75         template.setSqlMap(map);
76         template.afterPropertiesSet();
77         Object JavaDoc result = template.execute("stmt", new SqlMapCallback() {
78             public Object JavaDoc doInMappedStatement(MappedStatement s, Connection JavaDoc c) {
79                 assertTrue(stmt == s);
80                 assertTrue(con == c);
81                 return "done";
82             }
83         });
84         assertEquals("done", result);
85         dsControl.verify();
86         conControl.verify();
87     }
88     
89     public void testSqlMapDaoSupport() throws Exception JavaDoc {
90         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
91         DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
92         SqlMapDaoSupport testDao = new SqlMapDaoSupport() {
93         };
94         testDao.setDataSource(ds);
95         assertEquals(ds, testDao.getDataSource());
96
97         SqlMap map = new SqlMap();
98         testDao.setSqlMap(map);
99         assertEquals(map, testDao.getSqlMap());
100
101         SqlMapTemplate template = new SqlMapTemplate();
102         template.setDataSource(ds);
103         template.setSqlMap(map);
104         testDao.setSqlMapTemplate(template);
105         assertEquals(template, testDao.getSqlMapTemplate());
106
107         testDao.afterPropertiesSet();
108     }
109
110 }
111
Popular Tags