1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.sql.tests.java.sql; 19 20 import java.io.Serializable; 21 import java.sql.DataTruncation; 22 import org.apache.harmony.testframework.serialization.SerializationTest; 23 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 24 25 import junit.framework.TestCase; 26 27 public class DataTruncationTest extends TestCase { 28 29 /* 30 * ConstructorTest 31 */ 32 public void testDataTruncationintbooleanbooleanintint() { 33 34 int[] init1 = { -2147483648, 2147483647, 0, 329751502, 318587557, 35 -1217247045, 329474146 }; 36 boolean[] init2 = { false, true, false, false, false, true, false }; 37 boolean[] init3 = { false, true, false, false, false, false, true }; 38 int[] init4 = { -2147483648, 2147483647, 0, 1761409290, -1331044048, 39 -576231606, 661635011 }; 40 int[] init5 = { -2147483648, 2147483647, 0, 540816689, -1890783845, 41 -105552912, -85923935 }; 42 43 String[] theFinalStates1 = { "22001", "01004", "22001", "22001", 44 "22001", "22001", "01004" }; 45 String state2 = "Data truncation"; 46 String[] theFinalStates2 = { state2, state2, state2, state2, state2, 47 state2, state2 }; 48 int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 }; 49 int[] theFinalStates4 = init1; 50 int[] theFinalStates5 = init4; 51 int[] theFinalStates6 = init5; 52 boolean[] theFinalStates7 = init2; 53 boolean[] theFinalStates8 = init3; 54 55 Exception[] theExceptions = { null, null, null, null, null, null, null }; 56 57 DataTruncation aDataTruncation; 58 int loopCount = init1.length; 59 for (int i = 0; i < loopCount; i++) { 60 try { 61 aDataTruncation = new DataTruncation(init1[i], init2[i], 62 init3[i], init4[i], init5[i]); 63 if (theExceptions[i] != null) { 64 fail(); 65 } 66 assertEquals(i + " Final state mismatch", aDataTruncation 67 .getSQLState(), theFinalStates1[i]); 68 assertEquals(i + " Final state mismatch", aDataTruncation 69 .getMessage(), theFinalStates2[i]); 70 assertEquals(i + " Final state mismatch", aDataTruncation 71 .getErrorCode(), theFinalStates3[i]); 72 assertEquals(i + " Final state mismatch", aDataTruncation 73 .getIndex(), theFinalStates4[i]); 74 assertEquals(i + " Final state mismatch", aDataTruncation 75 .getDataSize(), theFinalStates5[i]); 76 assertEquals(i + " Final state mismatch", aDataTruncation 77 .getTransferSize(), theFinalStates6[i]); 78 assertEquals(i + " Final state mismatch", aDataTruncation 79 .getParameter(), theFinalStates7[i]); 80 assertEquals(i + " Final state mismatch", aDataTruncation 81 .getRead(), theFinalStates8[i]); 82 83 } catch (Exception e) { 84 if (theExceptions[i] == null) { 85 fail(i + "Unexpected exception"); 86 } 87 assertEquals(i + "Exception mismatch", e.getClass(), 88 theExceptions[i].getClass()); 89 assertEquals(i + "Exception mismatch", e.getMessage(), 90 theExceptions[i].getMessage()); 91 } // end try 92 } // end for 93 94 } // end method testDataTruncationintbooleanbooleanintint 95 96 /** 97 * @tests {@link java.sql.DataTruncation#DataTruncation(int, boolean, boolean, int, int, Throwable)} 98 * 99 * @since 1.6 100 */ 101 public void testConstructor_IBBIILjava_lang_Throwable() { 102 Exception e = new Exception("error message"); 103 DataTruncation dataTruncation = new DataTruncation(2147483647, true, 104 false, 0, 2147483647, e); 105 assertNotNull(dataTruncation); 106 assertEquals(e, dataTruncation.getCause()); 107 assertEquals("error message", dataTruncation.getCause().getMessage()); 108 } 109 110 /* 111 * Method test for getIndex 112 */ 113 public void testGetIndex() { 114 115 DataTruncation aDataTruncation; 116 int[] init1 = { -2147483648, 2147483647, 0, -2045829673, 1977156911, 117 478985827, 1687271915 }; 118 boolean[] init2 = { false, true, false, false, true, true, true }; 119 boolean[] init3 = { false, true, false, false, true, true, true }; 120 int[] init4 = { -2147483648, 2147483647, 0, -631377748, 21025030, 121 1215194589, 1064137121 }; 122 int[] init5 = { -2147483648, 2147483647, 0, -897998505, 997578180, 123 735015866, 264619424 }; 124 125 int theReturn; 126 int[] theReturns = init1; 127 String[] theFinalStates1 = { "22001", "01004", "22001", "22001", 128 "01004", "01004", "01004" }; 129 String state2 = "Data truncation"; 130 String[] theFinalStates2 = { state2, state2, state2, state2, state2, 131 state2, state2 }; 132 int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 }; 133 int[] theFinalStates4 = init1; 134 int[] theFinalStates5 = init4; 135 int[] theFinalStates6 = init5; 136 boolean[] theFinalStates7 = init2; 137 boolean[] theFinalStates8 = init3; 138 139 Exception[] theExceptions = { null, null, null, null, null, null, null }; 140 141 int loopCount = 1; 142 for (int i = 0; i < loopCount; i++) { 143 try { 144 aDataTruncation = new DataTruncation(init1[i], init2[i], 145 init3[i], init4[i], init5[i]); 146 theReturn = aDataTruncation.getIndex(); 147 if (theExceptions[i] != null) { 148 fail(i + "Exception missed"); 149 } 150 assertEquals(i + "Return value mismatch", theReturn, 151 theReturns[i]); 152 assertEquals(i + " Final state mismatch", aDataTruncation 153 .getSQLState(), theFinalStates1[i]); 154 assertEquals(i + " Final state mismatch", aDataTruncation 155 .getMessage(), theFinalStates2[i]); 156 assertEquals(i + " Final state mismatch", aDataTruncation 157 .getErrorCode(), theFinalStates3[i]); 158 assertEquals(i + " Final state mismatch", aDataTruncation 159 .getIndex(), theFinalStates4[i]); 160 assertEquals(i + " Final state mismatch", aDataTruncation 161 .getDataSize(), theFinalStates5[i]); 162 assertEquals(i + " Final state mismatch", aDataTruncation 163 .getTransferSize(), theFinalStates6[i]); 164 assertEquals(i + " Final state mismatch", aDataTruncation 165 .getParameter(), theFinalStates7[i]); 166 assertEquals(i + " Final state mismatch", aDataTruncation 167 .getRead(), theFinalStates8[i]); 168 169 } catch (Exception e) { 170 if (theExceptions[i] == null) { 171 fail(i + "Unexpected exception"); 172 } 173 assertEquals(i + "Exception mismatch", e.getClass(), 174 theExceptions[i].getClass()); 175 assertEquals(i + "Exception mismatch", e.getMessage(), 176 theExceptions[i].getMessage()); 177 } // end try 178 } // end for 179 180 } // end method testGetIndex 181 182 /* 183 * Method test for getParameter 184 */ 185 public void testGetParameter() { 186 187 DataTruncation aDataTruncation; 188 int[] init1 = { -2147483648, 2147483647, 0, -492314242, 1637665948, 189 -305785075, 258819883 }; 190 boolean[] init2 = { false, true, false, true, true, false, true }; 191 boolean[] init3 = { false, true, false, false, false, true, true }; 192 int[] init4 = { -2147483648, 2147483647, 0, 1134512579, 533874007, 193 1709608139, 990656593 }; 194 int[] init5 = { -2147483648, 2147483647, 0, -1566784226, -744009101, 195 -444614454, 356465980 }; 196 197 boolean theReturn; 198 boolean[] theReturns = init2; 199 String[] theFinalStates1 = { "22001", "01004", "22001", "22001", 200 "22001", "01004", "01004" }; 201 String state2 = "Data truncation"; 202 String[] theFinalStates2 = { state2, state2, state2, state2, state2, 203 state2, state2 }; 204 int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 }; 205 int[] theFinalStates4 = init1; 206 int[] theFinalStates5 = init4; 207 int[] theFinalStates6 = init5; 208 boolean[] theFinalStates7 = init2; 209 boolean[] theFinalStates8 = init3; 210 211 Exception[] theExceptions = { null, null, null, null, null, null, null }; 212 213 int loopCount = 1; 214 for (int i = 0; i < loopCount; i++) { 215 try { 216 aDataTruncation = new DataTruncation(init1[i], init2[i], 217 init3[i], init4[i], init5[i]); 218 theReturn = aDataTruncation.getParameter(); 219 if (theExceptions[i] != null) { 220 fail(i + "Exception missed"); 221 } 222 assertEquals(i + "Return value mismatch", theReturn, 223 theReturns[i]); 224 assertEquals(i + " Final state mismatch", aDataTruncation 225 .getSQLState(), theFinalStates1[i]); 226 assertEquals(i + " Final state mismatch", aDataTruncation 227 .getMessage(), theFinalStates2[i]); 228 assertEquals(i + " Final state mismatch", aDataTruncation 229 .getErrorCode(), theFinalStates3[i]); 230 assertEquals(i + " Final state mismatch", aDataTruncation 231 .getIndex(), theFinalStates4[i]); 232 assertEquals(i + " Final state mismatch", aDataTruncation 233 .getDataSize(), theFinalStates5[i]); 234 assertEquals(i + " Final state mismatch", aDataTruncation 235 .getTransferSize(), theFinalStates6[i]); 236 assertEquals(i + " Final state mismatch", aDataTruncation 237 .getParameter(), theFinalStates7[i]); 238 assertEquals(i + " Final state mismatch", aDataTruncation 239 .getRead(), theFinalStates8[i]); 240 241 } catch (Exception e) { 242 if (theExceptions[i] == null) { 243 fail(i + "Unexpected exception"); 244 } 245 assertEquals(i + "Exception mismatch", e.getClass(), 246 theExceptions[i].getClass()); 247 assertEquals(i + "Exception mismatch", e.getMessage(), 248 theExceptions[i].getMessage()); 249 } // end try 250 } // end for 251 252 } // end method testGetParameter 253 254 /* 255 * Method test for getRead 256 */ 257 public void testGetRead() { 258 259 DataTruncation aDataTruncation; 260 int[] init1 = { -2147483648, 2147483647, 0, 2092420209, -1695764964, 261 1832837995, -80199594 }; 262 boolean[] init2 = { false, true, false, false, false, true, true }; 263 boolean[] init3 = { false, true, false, false, true, true, false }; 264 int[] init4 = { -2147483648, 2147483647, 0, 1762375167, -604897453, 265 1362491587, 1007466498 }; 266 int[] init5 = { -2147483648, 2147483647, 0, 1494407222, -1696982311, 267 -940493360, -1777579868 }; 268 269 boolean theReturn; 270 boolean[] theReturns = init3; 271 String[] theFinalStates1 = { "22001", "01004", "22001", "22001", 272 "01004", "01004", "22001" }; 273 String state2 = "Data truncation"; 274 String[] theFinalStates2 = { state2, state2, state2, state2, state2, 275 state2, state2 }; 276 int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 }; 277 int[] theFinalStates4 = init1; 278 int[] theFinalStates5 = init4; 279 int[] theFinalStates6 = init5; 280 boolean[] theFinalStates7 = init2; 281 boolean[] theFinalStates8 = init3; 282 283 Exception[] theExceptions = { null, null, null, null, null, null, null }; 284 285 int loopCount = 1; 286 for (int i = 0; i < loopCount; i++) { 287 try { 288 aDataTruncation = new DataTruncation(init1[i], init2[i], 289 init3[i], init4[i], init5[i]); 290 theReturn = aDataTruncation.getRead(); 291 if (theExceptions[i] != null) { 292 fail(i + "Exception missed"); 293 } 294 assertEquals(i + "Return value mismatch", theReturn, 295 theReturns[i]); 296 assertEquals(i + " Final state mismatch", aDataTruncation 297 .getSQLState(), theFinalStates1[i]); 298 assertEquals(i + " Final state mismatch", aDataTruncation 299 .getMessage(), theFinalStates2[i]); 300 assertEquals(i + " Final state mismatch", aDataTruncation 301 .getErrorCode(), theFinalStates3[i]); 302 assertEquals(i + " Final state mismatch", aDataTruncation 303 .getIndex(), theFinalStates4[i]); 304 assertEquals(i + " Final state mismatch", aDataTruncation 305 .getDataSize(), theFinalStates5[i]); 306 assertEquals(i + " Final state mismatch", aDataTruncation 307 .getTransferSize(), theFinalStates6[i]); 308 assertEquals(i + " Final state mismatch", aDataTruncation 309 .getParameter(), theFinalStates7[i]); 310 assertEquals(i + " Final state mismatch", aDataTruncation 311 .getRead(), theFinalStates8[i]); 312 313 } catch (Exception e) { 314 if (theExceptions[i] == null) { 315 fail(i + "Unexpected exception"); 316 } 317 assertEquals(i + "Exception mismatch", e.getClass(), 318 theExceptions[i].getClass()); 319 assertEquals(i + "Exception mismatch", e.getMessage(), 320 theExceptions[i].getMessage()); 321 } // end try 322 } // end for 323 324 } // end method testGetRead 325 326 /* 327 * Method test for getDataSize 328 */ 329 public void testGetDataSize() { 330 331 DataTruncation aDataTruncation; 332 int[] init1 = { -2147483648, 2147483647, 0, 1146707040, -2020665632, 333 1268632617, -1595624039 }; 334 boolean[] init2 = { false, true, false, true, false, true, true }; 335 boolean[] init3 = { false, true, false, true, true, false, false }; 336 int[] init4 = { -2147483648, 2147483647, 0, -367493363, 328996907, 337 -1581326731, 835022052 }; 338 int[] init5 = { -2147483648, 2147483647, 0, -886134194, 908213800, 339 1123419516, -429606389 }; 340 341 int theReturn; 342 int[] theReturns = init4; 343 String[] theFinalStates1 = { "22001", "01004", "22001", "01004", 344 "01004", "22001", "22001" }; 345 String state2 = "Data truncation"; 346 String[] theFinalStates2 = { state2, state2, state2, state2, state2, 347 state2, state2 }; 348 int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 }; 349 int[] theFinalStates4 = init1; 350 int[] theFinalStates5 = init4; 351 int[] theFinalStates6 = init5; 352 boolean[] theFinalStates7 = init2; 353 boolean[] theFinalStates8 = init3; 354 355 Exception[] theExceptions = { null, null, null, null, null, null, null }; 356 357 int loopCount = 1; 358 for (int i = 0; i < loopCount; i++) { 359 try { 360 aDataTruncation = new DataTruncation(init1[i], init2[i], 361 init3[i], init4[i], init5[i]); 362 theReturn = aDataTruncation.getDataSize(); 363 if (theExceptions[i] != null) { 364 fail(i + "Exception missed"); 365 } 366 assertEquals(i + "Return value mismatch", theReturn, 367 theReturns[i]); 368 assertEquals(i + " Final state mismatch", aDataTruncation 369 .getSQLState(), theFinalStates1[i]); 370 assertEquals(i + " Final state mismatch", aDataTruncation 371 .getMessage(), theFinalStates2[i]); 372 assertEquals(i + " Final state mismatch", aDataTruncation 373 .getErrorCode(), theFinalStates3[i]); 374 assertEquals(i + " Final state mismatch", aDataTruncation 375 .getIndex(), theFinalStates4[i]); 376 assertEquals(i + " Final state mismatch", aDataTruncation 377 .getDataSize(), theFinalStates5[i]); 378 assertEquals(i + " Final state mismatch", aDataTruncation 379 .getTransferSize(), theFinalStates6[i]); 380 assertEquals(i + " Final state mismatch", aDataTruncation 381 .getParameter(), theFinalStates7[i]); 382 assertEquals(i + " Final state mismatch", aDataTruncation 383 .getRead(), theFinalStates8[i]); 384 385 } catch (Exception e) { 386 if (theExceptions[i] == null) { 387 fail(i + "Unexpected exception"); 388 } 389 assertEquals(i + "Exception mismatch", e.getClass(), 390 theExceptions[i].getClass()); 391 assertEquals(i + "Exception mismatch", e.getMessage(), 392 theExceptions[i].getMessage()); 393 } // end try 394 } // end for 395 396 } // end method testGetDataSize 397 398 /* 399 * Method test for getTransferSize 400 */ 401 public void testGetTransferSize() { 402 403 DataTruncation aDataTruncation; 404 int[] init1 = { -2147483648, 2147483647, 0, 78096124, 1719192600, 405 -1661234694, -1205825753 }; 406 boolean[] init2 = { false, true, false, false, true, false, true }; 407 boolean[] init3 = { false, true, false, false, false, false, false }; 408 int[] init4 = { -2147483648, 2147483647, 0, -493779295, -2042560243, 409 -217347438, 1357818664 }; 410 int[] init5 = { -2147483648, 2147483647, 0, -1647009002, -717544563, 411 -1368171905, -918209633 }; 412 413 int theReturn; 414 int[] theReturns = init5; 415 String[] theFinalStates1 = { "22001", "01004", "22001", "22001", 416 "22001", "22001", "22001" }; 417 String state2 = "Data truncation"; 418 String[] theFinalStates2 = { state2, state2, state2, state2, state2, 419 state2, state2 }; 420 int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 }; 421 int[] theFinalStates4 = init1; 422 int[] theFinalStates5 = init4; 423 int[] theFinalStates6 = init5; 424 boolean[] theFinalStates7 = init2; 425 boolean[] theFinalStates8 = init3; 426 427 Exception[] theExceptions = { null, null, null, null, null, null, null }; 428 429 int loopCount = 1; 430 for (int i = 0; i < loopCount; i++) { 431 try { 432 aDataTruncation = new DataTruncation(init1[i], init2[i], 433 init3[i], init4[i], init5[i]); 434 theReturn = aDataTruncation.getTransferSize(); 435 if (theExceptions[i] != null) { 436 fail(i + "Exception missed"); 437 } 438 assertEquals(i + "Return value mismatch", theReturn, 439 theReturns[i]); 440 assertEquals(i + " Final state mismatch", aDataTruncation 441 .getSQLState(), theFinalStates1[i]); 442 assertEquals(i + " Final state mismatch", aDataTruncation 443 .getMessage(), theFinalStates2[i]); 444 assertEquals(i + " Final state mismatch", aDataTruncation 445 .getErrorCode(), theFinalStates3[i]); 446 assertEquals(i + " Final state mismatch", aDataTruncation 447 .getIndex(), theFinalStates4[i]); 448 assertEquals(i + " Final state mismatch", aDataTruncation 449 .getDataSize(), theFinalStates5[i]); 450 assertEquals(i + " Final state mismatch", aDataTruncation 451 .getTransferSize(), theFinalStates6[i]); 452 assertEquals(i + " Final state mismatch", aDataTruncation 453 .getParameter(), theFinalStates7[i]); 454 assertEquals(i + " Final state mismatch", aDataTruncation 455 .getRead(), theFinalStates8[i]); 456 457 } catch (Exception e) { 458 if (theExceptions[i] == null) { 459 fail(i + "Unexpected exception"); 460 } 461 assertEquals(i + "Exception mismatch", e.getClass(), 462 theExceptions[i].getClass()); 463 assertEquals(i + "Exception mismatch", e.getMessage(), 464 theExceptions[i].getMessage()); 465 } // end try 466 } // end for 467 468 } // end method testGetTransferSize 469 470 /** 471 * @tests serialization/deserialization compatibility. 472 */ 473 public void testSerializationSelf() throws Exception { 474 DataTruncation object = new DataTruncation(10, true, true, 10, 10); 475 SerializationTest.verifySelf(object, DATATRUNCATION_COMPARATOR); 476 } 477 478 /** 479 * @tests serialization/deserialization compatibility with RI. 480 */ 481 public void testSerializationCompatibility() throws Exception { 482 DataTruncation object = new DataTruncation(10, true, true, 10, 10); 483 SerializationTest.verifyGolden(this, object, DATATRUNCATION_COMPARATOR); 484 } 485 486 // comparator for DataTruncation objects 487 private static final SerializableAssert DATATRUNCATION_COMPARATOR = new SerializableAssert() { 488 public void assertDeserialized(Serializable initial, 489 Serializable deserialized) { 490 491 // do common checks for all throwable objects 492 SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial, 493 deserialized); 494 495 DataTruncation initThr = (DataTruncation) initial; 496 DataTruncation dserThr = (DataTruncation) deserialized; 497 498 // verify index 499 assertEquals(initThr.getIndex(), dserThr.getIndex()); 500 501 // verify parameter 502 assertEquals(initThr.getParameter(), dserThr.getParameter()); 503 504 // verify read 505 assertEquals(initThr.getRead(), dserThr.getRead()); 506 507 // verify dataSize 508 assertEquals(initThr.getDataSize(), dserThr.getDataSize()); 509 510 // verify transferSize 511 assertEquals(initThr.getTransferSize(), dserThr.getTransferSize()); 512 } 513 }; 514 515 } // end class DataTruncationTest 516