1 16 17 package org.springframework.orm.ibatis; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.sql.DataSource ; 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 48 public class SqlMapClientTests extends TestCase { 49 50 public void testSqlMapClientFactoryBeanWithoutConfig() throws Exception { 51 SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean(); 52 factory.setConfigLocation(null); 54 try { 55 factory.afterPropertiesSet(); 56 fail("Should have thrown IllegalArgumentException"); 57 } 58 catch (IllegalArgumentException ex) { 59 } 61 } 62 63 public void testSqlMapClientTemplate() throws SQLException { 64 MockControl dsControl = MockControl.createControl(DataSource .class); 65 DataSource ds = (DataSource ) dsControl.getMock(); 66 MockControl conControl = MockControl.createControl(Connection .class); 67 final Connection con = (Connection ) 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 result = template.execute(new SqlMapClientCallback() { 93 public Object 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 { 106 DataSource 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 { 121 MockControl dsControl = MockControl.createControl(DataSource .class); 122 DataSource ds = (DataSource ) dsControl.getMock(); 123 MockControl conControl = MockControl.createControl(Connection .class); 124 Connection con = (Connection ) 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 { 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 { 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 { 177 List result = new ArrayList (); 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 { 187 List result = new ArrayList (); 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 { 197 RowHandler rowHandler = new RowHandler() { 198 public void handleRow(Object 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 { 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 { 220 Map result = new HashMap (); 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 { 230 Map result = new HashMap (); 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 { 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 { 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 { 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 { 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 { 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 assertEquals(10, ex.getExpectedRowsAffected()); 287 assertEquals(20, ex.getActualRowsAffected()); 288 } 289 template.executorControl.verify(); 290 } 291 292 public void testDeleteWithRequiredRowsAffected() throws SQLException { 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 { 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 assertEquals(10, ex.getExpectedRowsAffected()); 313 assertEquals(20, ex.getActualRowsAffected()); 314 } 315 template.executorControl.verify(); 316 } 317 318 public void testSqlMapClientDaoSupport() throws Exception { 319 MockControl dsControl = MockControl.createControl(DataSource .class); 320 DataSource ds = (DataSource ) 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 execute(SqlMapClientCallback action) throws DataAccessException { 349 try { 350 return action.doInSqlMapClient(executor); 351 } 352 catch (SQLException ex) { 353 throw getExceptionTranslator().translate("SqlMapClient operation", null, ex); 354 } 355 } 356 } 357 358 } 359 | Popular Tags |