1 16 17 package org.springframework.jdbc.object; 18 19 import java.sql.PreparedStatement ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.sql.Types ; 23 24 import org.easymock.MockControl; 25 26 import org.springframework.dao.InvalidDataAccessApiUsageException; 27 import org.springframework.jdbc.AbstractJdbcTests; 28 29 32 public class SqlFunctionTests extends AbstractJdbcTests { 33 34 private static final String FUNCTION = "select count(id) from mytable"; 35 private static final String FUNCTION_INT = 36 "select count(id) from mytable where myparam = ?"; 37 private static final String FUNCTION_MIXED = 38 "select count(id) from mytable where myparam = ? and mystring = ?"; 39 40 private MockControl ctrlPreparedStatement; 41 private PreparedStatement mockPreparedStatement; 42 private MockControl ctrlResultSet; 43 private ResultSet mockResultSet; 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 48 ctrlPreparedStatement = 49 MockControl.createControl(PreparedStatement .class); 50 mockPreparedStatement = 51 (PreparedStatement ) ctrlPreparedStatement.getMock(); 52 ctrlResultSet = MockControl.createControl(ResultSet .class); 53 mockResultSet = (ResultSet ) ctrlResultSet.getMock(); 54 } 55 56 protected void tearDown() throws Exception { 57 super.tearDown(); 58 59 if (shouldVerify()) { 60 ctrlPreparedStatement.verify(); 61 ctrlResultSet.verify(); 62 } 63 } 64 65 protected void replay() { 66 super.replay(); 67 ctrlPreparedStatement.replay(); 68 ctrlResultSet.replay(); 69 } 70 71 public void testFunction() throws SQLException { 72 mockResultSet.next(); 73 ctrlResultSet.setReturnValue(true); 74 mockResultSet.getObject(1); 75 ctrlResultSet.setReturnValue(new Integer (14)); 76 mockResultSet.next(); 77 ctrlResultSet.setReturnValue(false); 78 mockResultSet.close(); 79 ctrlResultSet.setVoidCallable(); 80 81 mockPreparedStatement.executeQuery(); 82 ctrlPreparedStatement.setReturnValue(mockResultSet); 83 mockPreparedStatement.getWarnings(); 84 ctrlPreparedStatement.setReturnValue(null); 85 mockPreparedStatement.close(); 86 ctrlPreparedStatement.setVoidCallable(); 87 88 mockConnection.prepareStatement(FUNCTION); 89 ctrlConnection.setReturnValue(mockPreparedStatement); 90 91 replay(); 92 93 SqlFunction function = new SqlFunction(); 94 function.setDataSource(mockDataSource); 95 function.setSql(FUNCTION); 96 function.compile(); 97 98 int count = function.run(); 99 assertTrue("Function returned value 14", count == 14); 100 } 101 102 public void testTooManyRows() throws SQLException { 103 mockResultSet.next(); 104 ctrlResultSet.setReturnValue(true); 105 mockResultSet.getInt(1); 106 ctrlResultSet.setReturnValue(14); 107 mockResultSet.next(); 108 ctrlResultSet.setReturnValue(true); 109 mockResultSet.close(); 110 ctrlResultSet.setVoidCallable(); 111 112 mockPreparedStatement.executeQuery(); 113 ctrlPreparedStatement.setReturnValue(mockResultSet); 114 mockPreparedStatement.close(); 115 ctrlPreparedStatement.setVoidCallable(); 116 117 mockConnection.prepareStatement(FUNCTION); 118 ctrlConnection.setReturnValue(mockPreparedStatement); 119 120 replay(); 121 122 SqlFunction function = new SqlFunction(mockDataSource, FUNCTION); 123 function.compile(); 124 125 try { 126 int count = function.run(); 127 fail("Shouldn't continue when too many rows returned"); 128 } 129 catch (InvalidDataAccessApiUsageException idaauex) { 130 } 132 } 133 134 public void testFunctionInt() throws SQLException { 135 mockResultSet.next(); 136 ctrlResultSet.setReturnValue(true); 137 mockResultSet.getInt(1); 138 ctrlResultSet.setReturnValue(14); 139 mockResultSet.next(); 140 ctrlResultSet.setReturnValue(false); 141 mockResultSet.close(); 142 ctrlResultSet.setVoidCallable(); 143 144 mockPreparedStatement.setObject(1, new Integer (1), Types.INTEGER); 145 ctrlPreparedStatement.setVoidCallable(); 146 mockPreparedStatement.executeQuery(); 147 ctrlPreparedStatement.setReturnValue(mockResultSet); 148 mockPreparedStatement.getWarnings(); 149 ctrlPreparedStatement.setReturnValue(null); 150 mockPreparedStatement.close(); 151 ctrlPreparedStatement.setVoidCallable(); 152 153 mockConnection.prepareStatement(FUNCTION_INT); 154 ctrlConnection.setReturnValue(mockPreparedStatement); 155 156 replay(); 157 158 SqlFunction function = 159 new SqlFunction( 160 mockDataSource, 161 FUNCTION_INT, 162 new int[] { Types.INTEGER }); 163 function.compile(); 164 165 int count = function.run(1); 166 assertTrue("Function returned value 14", count == 14); 167 } 168 169 public void testFunctionMixed() throws SQLException { 170 mockResultSet.next(); 171 ctrlResultSet.setReturnValue(true); 172 mockResultSet.getInt(1); 173 ctrlResultSet.setReturnValue(14); 174 mockResultSet.next(); 175 ctrlResultSet.setReturnValue(false); 176 mockResultSet.close(); 177 ctrlResultSet.setVoidCallable(); 178 179 mockPreparedStatement.setObject(1, new Integer (1), Types.INTEGER); 180 ctrlPreparedStatement.setVoidCallable(); 181 mockPreparedStatement.setString(2, "rod"); 182 ctrlPreparedStatement.setVoidCallable(); 183 mockPreparedStatement.executeQuery(); 184 ctrlPreparedStatement.setReturnValue(mockResultSet); 185 mockPreparedStatement.getWarnings(); 186 ctrlPreparedStatement.setReturnValue(null); 187 mockPreparedStatement.close(); 188 ctrlPreparedStatement.setVoidCallable(); 189 190 mockConnection.prepareStatement(FUNCTION_MIXED); 191 ctrlConnection.setReturnValue(mockPreparedStatement); 192 193 replay(); 194 195 SqlFunction function = 196 new SqlFunction( 197 mockDataSource, 198 FUNCTION_MIXED, 199 new int[] { Types.INTEGER, Types.VARCHAR }); 200 function.compile(); 201 202 int count = function.run(new Object [] { new Integer (1), "rod" }); 203 assertTrue("Function returned value 14", count == 14); 204 } 205 206 } 207 | Popular Tags |