1 20 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 21 22 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 23 import org.apache.derbyTesting.junit.JDBC; 24 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.PreparedStatement ; 28 import java.sql.Statement ; 29 import java.sql.Connection ; 30 31 import java.math.BigDecimal ; 32 33 import junit.framework.Test; 34 import junit.framework.TestSuite; 35 36 37 43 final public class UpdateXXXTest extends BaseJDBCTestCase 44 { 45 49 public UpdateXXXTest(final String name) { 50 super(name); 51 } 52 53 public static Test suite() { 54 TestSuite suite = new TestSuite(); 55 56 if (usingDerbyNet()) 58 return suite; 59 60 suite.addTestSuite(UpdateXXXTest.class); 61 62 if (JDBC.vmSupportsJDBC2()) 64 suite.addTest(new UpdateXXXTest("jdbc2testUpdateBigDecimal")); 65 66 return suite; 67 } 68 69 70 76 public void setUp() 77 throws Exception 78 { 79 Connection con = getConnection(); 80 try { 81 82 con.setAutoCommit(false); 83 84 Statement stmt = con.createStatement(); 85 String createTableString = "CREATE TABLE " + TABLE_NAME + " (" + 86 "F01 SMALLINT," + 87 "F02 INTEGER," + 88 "F03 BIGINT," + 89 "F04 REAL," + 90 "F05 FLOAT," + 91 "F06 DOUBLE," + 92 "F07 DECIMAL," + 93 "F08 NUMERIC," + 94 "F09 CHAR(100)," + 95 "F10 VARCHAR(256) )"; 96 println(createTableString); 97 stmt.executeUpdate(createTableString); 98 PreparedStatement ps = con.prepareStatement 99 ("insert into " + TABLE_NAME + " values(?,?,?,?,?,?,?,?,?,?)"); 100 101 ps.setShort(1, (short) 1); 102 ps.setInt(2, 1); 103 ps.setLong(3, 1L); 104 ps.setFloat(4, 1.0f); 105 ps.setDouble(5, 1.0); 106 ps.setDouble(6, 1.0); 107 108 ps.setString(7, "1"); 111 ps.setString(8, "1"); 112 113 ps.setString(9, "1"); 114 ps.setString(10, "1"); 115 ps.executeUpdate(); 116 117 rs = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, 118 ResultSet.CONCUR_UPDATABLE). 119 executeQuery(SELECT_STMT); 120 rs.next(); 121 } catch (SQLException e) { 122 con.rollback(); 123 throw e; 124 } 125 } 126 127 132 public void testUpdateString() 133 throws SQLException 134 { 135 for (int i = 1; i <= COLUMNS; i++) { 136 rs.updateString(i, "2"); 137 assertEquals("Expected rs.getDouble(" + i + 138 ") to match updated value", 2, (int) rs.getDouble(i)); 139 } 140 rs.updateRow(); 141 checkColumnsAreUpdated(); 142 143 } 144 145 150 public void testUpdateInt() 151 throws SQLException 152 { 153 for (int i = 1; i <= COLUMNS; i++) { 154 rs.updateInt(i, 2); 155 assertEquals("Expected rs.getInt(" + i + 156 ") to match updated value", 2, rs.getInt(i)); 157 } 158 rs.updateRow(); 159 checkColumnsAreUpdated(); 160 } 161 162 167 public void testUpdateLong() 168 throws SQLException 169 { 170 for (int i = 1; i <= COLUMNS; i++) { 171 rs.updateLong(i, 2L); 172 assertEquals("Expected rs.getLong(" + i + 173 ") to match updated value", 2L, rs.getLong(i)); 174 } 175 rs.updateRow(); 176 checkColumnsAreUpdated(); 177 } 178 179 184 public void testUpdateShort() 185 throws SQLException 186 { 187 for (int i = 1; i <= COLUMNS; i++) { 188 rs.updateShort(i, (short) 2); 189 assertEquals("Expected rs.getShort(" + i + 190 ") to match updated value", 2, (int) rs.getShort(i)); 191 } 192 rs.updateRow(); 193 checkColumnsAreUpdated(); 194 } 195 196 201 public void testUpdateFloat() 202 throws SQLException 203 { 204 for (int i = 1; i <= COLUMNS; i++) { 205 rs.updateFloat(i, 2.0f); 206 assertEquals("Expected rs.getFloat(" + i + 207 ") to match updated value", 2, (int) rs.getFloat(i)); 208 } 209 rs.updateRow(); 210 checkColumnsAreUpdated(); 211 } 212 213 218 public void testUpdateDouble() 219 throws SQLException 220 { 221 for (int i = 1; i <= COLUMNS; i++) { 222 rs.updateDouble(i, 2.0); 223 assertEquals("Expected rs.getDouble(" + i + 224 ") to match updated value", 2, (int) rs.getDouble(i)); 225 } 226 rs.updateRow(); 227 checkColumnsAreUpdated(); 228 } 229 230 235 public void jdbc2testUpdateBigDecimal() 236 throws SQLException 237 { 238 for (int i = 1; i <= COLUMNS; i++) { 239 rs.updateBigDecimal(i, BigDecimal.valueOf(2L)); 240 assertEquals("Expected rs.getBigDecimal(" + i + 241 ") to match updated value", 2, 242 rs.getBigDecimal(i).intValue()); 243 } 244 rs.updateRow(); 245 checkColumnsAreUpdated(); 246 } 247 248 253 public void testUpdateObjectWithNull() 254 throws SQLException 255 { 256 Object value = null; 257 258 for (int i = 1; i <= COLUMNS; i++) { 259 rs.updateObject(i, value); 260 assertNull("Expected rs.getObject(" + i + ") to be null", 261 rs.getObject(i)); 262 assertTrue("Expected rs.wasNull() to return true", 263 rs.wasNull()); 264 } 265 rs.updateRow(); 266 checkColumnsAreNull(); 267 } 268 269 274 public void testUpdateNull() 275 throws SQLException 276 { 277 for (int i = 1; i <= COLUMNS; i++) { 278 rs.updateNull(i); 279 assertNull("Expected rs.getObject(" + i + ") to be null", 280 rs.getObject(i)); 281 assertTrue("Expected rs.wasNull() to return true", 282 rs.wasNull()); 283 } 284 rs.updateRow(); 285 checkColumnsAreNull(); 286 } 287 288 293 private void checkColumnsAreNull() 294 throws SQLException 295 { 296 rs.close(); 297 298 rs = createStatement(ResultSet.TYPE_FORWARD_ONLY, 299 ResultSet.CONCUR_READ_ONLY). 300 executeQuery(SELECT_STMT); 301 302 rs.next(); 303 304 for (int i = 1; i <= COLUMNS; i++) { 305 assertNull("Expected column " + i + " to be null", 306 rs.getObject(i)); 307 assertTrue("Expected wasNull() after reading column " + i + 308 " to be true when data is SQL Null on column", 309 rs.wasNull()); 310 } 311 } 312 313 319 private void checkColumnsAreUpdated() 320 throws SQLException 321 { 322 rs.close(); 323 324 rs = createStatement(ResultSet.TYPE_FORWARD_ONLY, 325 ResultSet.CONCUR_READ_ONLY). 326 executeQuery(SELECT_STMT); 327 328 rs.next(); 329 for (int i = 1; i <= COLUMNS; i++) { 330 int expectedVal = 2; 331 332 int actualVal = (int) rs.getDouble(i); 337 assertEquals("Unexpected value from rs.getDouble( + " + i + ")", 338 expectedVal, actualVal); 339 } 340 } 341 342 343 private ResultSet rs = null; 344 345 346 private static final String TABLE_NAME = "MultiTypeTable"; 347 348 349 private static final String SELECT_STMT = 350 "SELECT * FROM " + TABLE_NAME; 351 352 353 private static final int COLUMNS = 10; 354 } 355 | Popular Tags |