1 25 package testsuite.regression; 26 27 import java.sql.ResultSetMetaData ; 28 import java.sql.SQLException ; 29 30 import testsuite.BaseTestCase; 31 32 38 public class NumbersRegressionTest extends BaseTestCase { 39 45 public NumbersRegressionTest(String name) { 46 super(name); 47 } 48 49 58 public static void main(String [] args) { 59 junit.textui.TestRunner.run(NumbersRegressionTest.class); 60 } 61 62 68 public void testBigInt() throws Exception { 69 try { 70 this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression"); 71 this.stmt 72 .executeUpdate("CREATE TABLE bigIntRegression ( val BIGINT NOT NULL)"); 73 this.stmt 74 .executeUpdate("INSERT INTO bigIntRegression VALUES (6692730313872877584)"); 75 this.rs = this.stmt 76 .executeQuery("SELECT val FROM bigIntRegression"); 77 78 while (this.rs.next()) { 79 long retrieveAsLong = this.rs.getLong(1); 81 assertTrue(retrieveAsLong == 6692730313872877584L); 82 } 83 84 this.rs.close(); 85 this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression"); 86 87 String bigIntAsString = "6692730313872877584"; 88 89 long parsedBigIntAsLong = Long.parseLong(bigIntAsString); 90 91 assertTrue(bigIntAsString 93 .equals(String.valueOf(parsedBigIntAsLong))); 94 } finally { 95 this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression"); 96 } 97 } 98 99 105 public void testFloatsAndReals() throws Exception { 106 try { 107 this.stmt.executeUpdate("DROP TABLE IF EXISTS floatsAndReals"); 108 this.stmt 109 .executeUpdate("CREATE TABLE IF NOT EXISTS floatsAndReals(floatCol FLOAT, realCol REAL, doubleCol DOUBLE)"); 110 this.stmt 111 .executeUpdate("INSERT INTO floatsAndReals VALUES (0, 0, 0)"); 112 113 this.rs = this.stmt 114 .executeQuery("SELECT floatCol, realCol, doubleCol FROM floatsAndReals"); 115 116 ResultSetMetaData rsmd = this.rs.getMetaData(); 117 118 this.rs.next(); 119 120 assertTrue(rsmd.getColumnClassName(1).equals("java.lang.Float")); 121 assertTrue(this.rs.getObject(1).getClass().getName().equals( 122 "java.lang.Float")); 123 124 assertTrue(rsmd.getColumnClassName(2).equals("java.lang.Double")); 125 assertTrue(this.rs.getObject(2).getClass().getName().equals( 126 "java.lang.Double")); 127 128 assertTrue(rsmd.getColumnClassName(3).equals("java.lang.Double")); 129 assertTrue(this.rs.getObject(3).getClass().getName().equals( 130 "java.lang.Double")); 131 132 } finally { 133 this.stmt.executeUpdate("DROP TABLE IF EXISTS floatsAndReals"); 134 } 135 } 136 137 144 public void testPrecisionAndScale() throws Exception { 145 testPrecisionForType("TINYINT", 8, -1, false); 146 testPrecisionForType("TINYINT", 8, -1, true); 147 testPrecisionForType("SMALLINT", 8, -1, false); 148 testPrecisionForType("SMALLINT", 8, -1, true); 149 testPrecisionForType("MEDIUMINT", 8, -1, false); 150 testPrecisionForType("MEDIUMINT", 8, -1, true); 151 testPrecisionForType("INT", 8, -1, false); 152 testPrecisionForType("INT", 8, -1, true); 153 testPrecisionForType("BIGINT", 8, -1, false); 154 testPrecisionForType("BIGINT", 8, -1, true); 155 156 testPrecisionForType("FLOAT", 8, 4, false); 157 testPrecisionForType("FLOAT", 8, 4, true); 158 testPrecisionForType("DOUBLE", 8, 4, false); 159 testPrecisionForType("DOUBLE", 8, 4, true); 160 161 testPrecisionForType("DECIMAL", 8, 4, false); 162 testPrecisionForType("DECIMAL", 8, 4, true); 163 164 testPrecisionForType("DECIMAL", 9, 0, false); 165 testPrecisionForType("DECIMAL", 9, 0, true); 166 } 167 168 private void testPrecisionForType(String typeName, int m, int d, 169 boolean unsigned) throws Exception { 170 try { 171 this.stmt 172 .executeUpdate("DROP TABLE IF EXISTS precisionAndScaleRegression"); 173 174 StringBuffer createStatement = new StringBuffer ( 175 "CREATE TABLE precisionAndScaleRegression ( val "); 176 createStatement.append(typeName); 177 createStatement.append("("); 178 createStatement.append(m); 179 180 if (d != -1) { 181 createStatement.append(","); 182 createStatement.append(d); 183 } 184 185 createStatement.append(")"); 186 187 if (unsigned) { 188 createStatement.append(" UNSIGNED "); 189 } 190 191 createStatement.append(" NOT NULL)"); 192 193 this.stmt.executeUpdate(createStatement.toString()); 194 195 this.rs = this.stmt 196 .executeQuery("SELECT val FROM precisionAndScaleRegression"); 197 198 ResultSetMetaData rsmd = this.rs.getMetaData(); 199 assertTrue("Precision returned incorrectly for type " + typeName 200 + ", " + m + " != rsmd.getPrecision() = " 201 + rsmd.getPrecision(1), rsmd.getPrecision(1) == m); 202 203 if (d != -1) { 204 assertTrue("Scale returned incorrectly for type " + typeName 205 + ", " + d + " != rsmd.getScale() = " 206 + rsmd.getScale(1), rsmd.getScale(1) == d); 207 } 208 } finally { 209 if (this.rs != null) { 210 try { 211 this.rs.close(); 212 } catch (Exception ex) { 213 } 215 } 216 217 this.stmt 218 .executeUpdate("DROP TABLE IF EXISTS precisionAndScaleRegression"); 219 } 220 } 221 222 public void testIntShouldReturnLong() throws Exception { 223 try { 224 this.stmt.executeUpdate("DROP TABLE IF EXISTS testIntRetLong"); 225 this.stmt.executeUpdate("CREATE TABLE testIntRetLong(field1 INT)"); 226 this.stmt.executeUpdate("INSERT INTO testIntRetLong VALUES (1)"); 227 228 this.rs = this.stmt.executeQuery("SELECT * FROM testIntRetLong"); 229 this.rs.next(); 230 231 assertTrue(this.rs.getObject(1).getClass().equals( 232 java.lang.Integer .class)); 233 } finally { 234 if (this.rs != null) { 235 try { 236 this.rs.close(); 237 } catch (SQLException sqlEx) { 238 } 240 241 this.rs = null; 242 } 243 244 this.stmt.executeUpdate("DROP TABLE IF EXISTS testIntRetLong"); 245 246 } 247 } 248 249 255 public void testBug5729() throws Exception { 256 if (versionMeetsMinimum(4, 1)) { 257 String valueAsString = "1095923280000"; 258 259 try { 260 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5729"); 261 this.stmt 262 .executeUpdate("CREATE TABLE testBug5729(field1 BIGINT UNSIGNED)"); 263 this.stmt.executeUpdate("INSERT INTO testBug5729 VALUES (" 264 + valueAsString + ")"); 265 266 this.rs = this.conn.prepareStatement( 267 "SELECT * FROM testBug5729").executeQuery(); 268 this.rs.next(); 269 270 assertTrue(this.rs.getObject(1).toString() 271 .equals(valueAsString)); 272 } finally { 273 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5729"); 274 } 275 } 276 } 277 278 286 public void testBug8484() throws Exception { 287 try { 288 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug8484"); 289 this.stmt 290 .executeUpdate("CREATE TABLE testBug8484 (field1 DECIMAL(16, 8), field2 varchar(32))"); 291 this.stmt 292 .executeUpdate("INSERT INTO testBug8484 VALUES (12345678.12345678, '')"); 293 this.rs = this.stmt 294 .executeQuery("SELECT field1, field2 FROM testBug8484"); 295 this.rs.next(); 296 assertEquals("12345678.123", this.rs.getBigDecimal(1, 3).toString()); 297 assertEquals("0.000", this.rs.getBigDecimal(2, 3).toString()); 298 299 this.pstmt = this.conn 300 .prepareStatement("SELECT field1, field2 FROM testBug8484"); 301 this.rs = this.pstmt.executeQuery(); 302 this.rs.next(); 303 assertEquals("12345678.123", this.rs.getBigDecimal(1, 3).toString()); 304 assertEquals("0.000", this.rs.getBigDecimal(2, 3).toString()); 305 } finally { 306 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug8484"); 307 } 308 } 309 } 310 | Popular Tags |