1 16 17 package org.springframework.orm.ibatis; 18 19 import java.io.IOException ; 20 import java.sql.Connection ; 21 import java.sql.SQLException ; 22 23 import javax.sql.DataSource ; 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 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 ex) { 47 } 49 } 50 51 public void testSqlMapTemplate() throws SQLException { 52 MockControl dsControl = MockControl.createControl(DataSource .class); 53 DataSource ds = (DataSource ) dsControl.getMock(); 54 MockControl conControl = MockControl.createControl(Connection .class); 55 final Connection con = (Connection ) 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 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 result = template.execute("stmt", new SqlMapCallback() { 78 public Object doInMappedStatement(MappedStatement s, Connection 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 { 90 MockControl dsControl = MockControl.createControl(DataSource .class); 91 DataSource ds = (DataSource ) 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 |