1 16 17 package org.springframework.jdbc.core; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.sql.Statement ; 24 import java.sql.Types ; 25 import java.util.List ; 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 38 public class RowMapperTests extends TestCase { 39 40 private MockControl conControl; 41 42 private Connection con; 43 44 private MockControl rsControl; 45 46 private ResultSet rs; 47 48 private JdbcTemplate jdbcTemplate; 49 50 private List result; 51 52 protected void setUp() throws SQLException { 53 conControl = MockControl.createControl(Connection .class); 54 con = (Connection ) conControl.getMock(); 55 con.isClosed(); 56 conControl.setDefaultReturnValue(false); 57 58 rsControl = MockControl.createControl(ResultSet .class); 59 rs = (ResultSet ) 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 { 85 MockControl stmtControl = MockControl.createControl(Statement .class); 86 Statement stmt = (Statement ) 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 { 107 MockControl psControl = MockControl.createControl(PreparedStatement .class); 108 final PreparedStatement ps = (PreparedStatement ) 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 createPreparedStatement(Connection con) throws SQLException { 123 return ps; 124 } 125 }, new TestRowMapper()); 126 127 psControl.verify(); 128 verify(); 129 } 130 131 public void testPreparedStatementSetterWithRowMapper() throws SQLException { 132 MockControl psControl = MockControl.createControl(PreparedStatement .class); 133 final PreparedStatement ps = (PreparedStatement ) 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 ps) throws SQLException { 153 ps.setString(1, "test"); 154 } 155 }, new TestRowMapper()); 156 157 psControl.verify(); 158 verify(); 159 } 160 161 public void testQueryWithArgsAndRowMapper() throws SQLException { 162 MockControl psControl = MockControl.createControl(PreparedStatement .class); 163 final PreparedStatement ps = (PreparedStatement ) 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 [] {"test1", "test2"}, 183 new TestRowMapper()); 184 185 psControl.verify(); 186 verify(); 187 } 188 189 public void testQueryWithArgsAndTypesAndRowMapper() throws SQLException { 190 MockControl psControl = MockControl.createControl(PreparedStatement .class); 191 final PreparedStatement ps = (PreparedStatement ) 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 [] {"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 mapRow(ResultSet rs, int rowNum) throws SQLException { 236 return new TestBean(rs.getString(1), rs.getInt(2)); 237 } 238 } 239 240 } 241 | Popular Tags |