Home | History | Annotate | Download | only in sql
      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      * @since 1.6
     99      */
    100     public void testConstructor_IBBIILjava_lang_Throwable() {
    101         Exception e = new Exception("error message");
    102         DataTruncation dataTruncation = new DataTruncation(2147483647, true,
    103                 false, 0, 2147483647, e);
    104         assertNotNull(dataTruncation);
    105         assertEquals(e, dataTruncation.getCause());
    106         assertEquals("error message", dataTruncation.getCause().getMessage());
    107     }
    108 
    109     /*
    110      * Method test for getIndex
    111      */
    112     public void testGetIndex() {
    113 
    114         DataTruncation aDataTruncation;
    115         int[] init1 = { -2147483648, 2147483647, 0, -2045829673, 1977156911,
    116                 478985827, 1687271915 };
    117         boolean[] init2 = { false, true, false, false, true, true, true };
    118         boolean[] init3 = { false, true, false, false, true, true, true };
    119         int[] init4 = { -2147483648, 2147483647, 0, -631377748, 21025030,
    120                 1215194589, 1064137121 };
    121         int[] init5 = { -2147483648, 2147483647, 0, -897998505, 997578180,
    122                 735015866, 264619424 };
    123 
    124         int theReturn;
    125         int[] theReturns = init1;
    126         String[] theFinalStates1 = { "22001", "01004", "22001", "22001",
    127                 "01004", "01004", "01004" };
    128         String state2 = "Data truncation";
    129         String[] theFinalStates2 = { state2, state2, state2, state2, state2,
    130                 state2, state2 };
    131         int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 };
    132         int[] theFinalStates4 = init1;
    133         int[] theFinalStates5 = init4;
    134         int[] theFinalStates6 = init5;
    135         boolean[] theFinalStates7 = init2;
    136         boolean[] theFinalStates8 = init3;
    137 
    138         Exception[] theExceptions = { null, null, null, null, null, null, null };
    139 
    140         int loopCount = 1;
    141         for (int i = 0; i < loopCount; i++) {
    142             try {
    143                 aDataTruncation = new DataTruncation(init1[i], init2[i],
    144                         init3[i], init4[i], init5[i]);
    145                 theReturn = aDataTruncation.getIndex();
    146                 if (theExceptions[i] != null) {
    147                     fail(i + "Exception missed");
    148                 }
    149                 assertEquals(i + "Return value mismatch", theReturn,
    150                         theReturns[i]);
    151                 assertEquals(i + "  Final state mismatch", aDataTruncation
    152                         .getSQLState(), theFinalStates1[i]);
    153                 assertEquals(i + "  Final state mismatch", aDataTruncation
    154                         .getMessage(), theFinalStates2[i]);
    155                 assertEquals(i + "  Final state mismatch", aDataTruncation
    156                         .getErrorCode(), theFinalStates3[i]);
    157                 assertEquals(i + "  Final state mismatch", aDataTruncation
    158                         .getIndex(), theFinalStates4[i]);
    159                 assertEquals(i + "  Final state mismatch", aDataTruncation
    160                         .getDataSize(), theFinalStates5[i]);
    161                 assertEquals(i + "  Final state mismatch", aDataTruncation
    162                         .getTransferSize(), theFinalStates6[i]);
    163                 assertEquals(i + "  Final state mismatch", aDataTruncation
    164                         .getParameter(), theFinalStates7[i]);
    165                 assertEquals(i + "  Final state mismatch", aDataTruncation
    166                         .getRead(), theFinalStates8[i]);
    167 
    168             } catch (Exception e) {
    169                 if (theExceptions[i] == null) {
    170                     fail(i + "Unexpected exception");
    171                 }
    172                 assertEquals(i + "Exception mismatch", e.getClass(),
    173                         theExceptions[i].getClass());
    174                 assertEquals(i + "Exception mismatch", e.getMessage(),
    175                         theExceptions[i].getMessage());
    176             } // end try
    177         } // end for
    178 
    179     } // end method testGetIndex
    180 
    181     /*
    182      * Method test for getParameter
    183      */
    184     public void testGetParameter() {
    185 
    186         DataTruncation aDataTruncation;
    187         int[] init1 = { -2147483648, 2147483647, 0, -492314242, 1637665948,
    188                 -305785075, 258819883 };
    189         boolean[] init2 = { false, true, false, true, true, false, true };
    190         boolean[] init3 = { false, true, false, false, false, true, true };
    191         int[] init4 = { -2147483648, 2147483647, 0, 1134512579, 533874007,
    192                 1709608139, 990656593 };
    193         int[] init5 = { -2147483648, 2147483647, 0, -1566784226, -744009101,
    194                 -444614454, 356465980 };
    195 
    196         boolean theReturn;
    197         boolean[] theReturns = init2;
    198         String[] theFinalStates1 = { "22001", "01004", "22001", "22001",
    199                 "22001", "01004", "01004" };
    200         String state2 = "Data truncation";
    201         String[] theFinalStates2 = { state2, state2, state2, state2, state2,
    202                 state2, state2 };
    203         int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 };
    204         int[] theFinalStates4 = init1;
    205         int[] theFinalStates5 = init4;
    206         int[] theFinalStates6 = init5;
    207         boolean[] theFinalStates7 = init2;
    208         boolean[] theFinalStates8 = init3;
    209 
    210         Exception[] theExceptions = { null, null, null, null, null, null, null };
    211 
    212         int loopCount = 1;
    213         for (int i = 0; i < loopCount; i++) {
    214             try {
    215                 aDataTruncation = new DataTruncation(init1[i], init2[i],
    216                         init3[i], init4[i], init5[i]);
    217                 theReturn = aDataTruncation.getParameter();
    218                 if (theExceptions[i] != null) {
    219                     fail(i + "Exception missed");
    220                 }
    221                 assertEquals(i + "Return value mismatch", theReturn,
    222                         theReturns[i]);
    223                 assertEquals(i + "  Final state mismatch", aDataTruncation
    224                         .getSQLState(), theFinalStates1[i]);
    225                 assertEquals(i + "  Final state mismatch", aDataTruncation
    226                         .getMessage(), theFinalStates2[i]);
    227                 assertEquals(i + "  Final state mismatch", aDataTruncation
    228                         .getErrorCode(), theFinalStates3[i]);
    229                 assertEquals(i + "  Final state mismatch", aDataTruncation
    230                         .getIndex(), theFinalStates4[i]);
    231                 assertEquals(i + "  Final state mismatch", aDataTruncation
    232                         .getDataSize(), theFinalStates5[i]);
    233                 assertEquals(i + "  Final state mismatch", aDataTruncation
    234                         .getTransferSize(), theFinalStates6[i]);
    235                 assertEquals(i + "  Final state mismatch", aDataTruncation
    236                         .getParameter(), theFinalStates7[i]);
    237                 assertEquals(i + "  Final state mismatch", aDataTruncation
    238                         .getRead(), theFinalStates8[i]);
    239 
    240             } catch (Exception e) {
    241                 if (theExceptions[i] == null) {
    242                     fail(i + "Unexpected exception");
    243                 }
    244                 assertEquals(i + "Exception mismatch", e.getClass(),
    245                         theExceptions[i].getClass());
    246                 assertEquals(i + "Exception mismatch", e.getMessage(),
    247                         theExceptions[i].getMessage());
    248             } // end try
    249         } // end for
    250 
    251     } // end method testGetParameter
    252 
    253     /*
    254      * Method test for getRead
    255      */
    256     public void testGetRead() {
    257 
    258         DataTruncation aDataTruncation;
    259         int[] init1 = { -2147483648, 2147483647, 0, 2092420209, -1695764964,
    260                 1832837995, -80199594 };
    261         boolean[] init2 = { false, true, false, false, false, true, true };
    262         boolean[] init3 = { false, true, false, false, true, true, false };
    263         int[] init4 = { -2147483648, 2147483647, 0, 1762375167, -604897453,
    264                 1362491587, 1007466498 };
    265         int[] init5 = { -2147483648, 2147483647, 0, 1494407222, -1696982311,
    266                 -940493360, -1777579868 };
    267 
    268         boolean theReturn;
    269         boolean[] theReturns = init3;
    270         String[] theFinalStates1 = { "22001", "01004", "22001", "22001",
    271                 "01004", "01004", "22001" };
    272         String state2 = "Data truncation";
    273         String[] theFinalStates2 = { state2, state2, state2, state2, state2,
    274                 state2, state2 };
    275         int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 };
    276         int[] theFinalStates4 = init1;
    277         int[] theFinalStates5 = init4;
    278         int[] theFinalStates6 = init5;
    279         boolean[] theFinalStates7 = init2;
    280         boolean[] theFinalStates8 = init3;
    281 
    282         Exception[] theExceptions = { null, null, null, null, null, null, null };
    283 
    284         int loopCount = 1;
    285         for (int i = 0; i < loopCount; i++) {
    286             try {
    287                 aDataTruncation = new DataTruncation(init1[i], init2[i],
    288                         init3[i], init4[i], init5[i]);
    289                 theReturn = aDataTruncation.getRead();
    290                 if (theExceptions[i] != null) {
    291                     fail(i + "Exception missed");
    292                 }
    293                 assertEquals(i + "Return value mismatch", theReturn,
    294                         theReturns[i]);
    295                 assertEquals(i + "  Final state mismatch", aDataTruncation
    296                         .getSQLState(), theFinalStates1[i]);
    297                 assertEquals(i + "  Final state mismatch", aDataTruncation
    298                         .getMessage(), theFinalStates2[i]);
    299                 assertEquals(i + "  Final state mismatch", aDataTruncation
    300                         .getErrorCode(), theFinalStates3[i]);
    301                 assertEquals(i + "  Final state mismatch", aDataTruncation
    302                         .getIndex(), theFinalStates4[i]);
    303                 assertEquals(i + "  Final state mismatch", aDataTruncation
    304                         .getDataSize(), theFinalStates5[i]);
    305                 assertEquals(i + "  Final state mismatch", aDataTruncation
    306                         .getTransferSize(), theFinalStates6[i]);
    307                 assertEquals(i + "  Final state mismatch", aDataTruncation
    308                         .getParameter(), theFinalStates7[i]);
    309                 assertEquals(i + "  Final state mismatch", aDataTruncation
    310                         .getRead(), theFinalStates8[i]);
    311 
    312             } catch (Exception e) {
    313                 if (theExceptions[i] == null) {
    314                     fail(i + "Unexpected exception");
    315                 }
    316                 assertEquals(i + "Exception mismatch", e.getClass(),
    317                         theExceptions[i].getClass());
    318                 assertEquals(i + "Exception mismatch", e.getMessage(),
    319                         theExceptions[i].getMessage());
    320             } // end try
    321         } // end for
    322 
    323     } // end method testGetRead
    324 
    325     /*
    326      * Method test for getDataSize
    327      */
    328     public void testGetDataSize() {
    329 
    330         DataTruncation aDataTruncation;
    331         int[] init1 = { -2147483648, 2147483647, 0, 1146707040, -2020665632,
    332                 1268632617, -1595624039 };
    333         boolean[] init2 = { false, true, false, true, false, true, true };
    334         boolean[] init3 = { false, true, false, true, true, false, false };
    335         int[] init4 = { -2147483648, 2147483647, 0, -367493363, 328996907,
    336                 -1581326731, 835022052 };
    337         int[] init5 = { -2147483648, 2147483647, 0, -886134194, 908213800,
    338                 1123419516, -429606389 };
    339 
    340         int theReturn;
    341         int[] theReturns = init4;
    342         String[] theFinalStates1 = { "22001", "01004", "22001", "01004",
    343                 "01004", "22001", "22001" };
    344         String state2 = "Data truncation";
    345         String[] theFinalStates2 = { state2, state2, state2, state2, state2,
    346                 state2, state2 };
    347         int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 };
    348         int[] theFinalStates4 = init1;
    349         int[] theFinalStates5 = init4;
    350         int[] theFinalStates6 = init5;
    351         boolean[] theFinalStates7 = init2;
    352         boolean[] theFinalStates8 = init3;
    353 
    354         Exception[] theExceptions = { null, null, null, null, null, null, null };
    355 
    356         int loopCount = 1;
    357         for (int i = 0; i < loopCount; i++) {
    358             try {
    359                 aDataTruncation = new DataTruncation(init1[i], init2[i],
    360                         init3[i], init4[i], init5[i]);
    361                 theReturn = aDataTruncation.getDataSize();
    362                 if (theExceptions[i] != null) {
    363                     fail(i + "Exception missed");
    364                 }
    365                 assertEquals(i + "Return value mismatch", theReturn,
    366                         theReturns[i]);
    367                 assertEquals(i + "  Final state mismatch", aDataTruncation
    368                         .getSQLState(), theFinalStates1[i]);
    369                 assertEquals(i + "  Final state mismatch", aDataTruncation
    370                         .getMessage(), theFinalStates2[i]);
    371                 assertEquals(i + "  Final state mismatch", aDataTruncation
    372                         .getErrorCode(), theFinalStates3[i]);
    373                 assertEquals(i + "  Final state mismatch", aDataTruncation
    374                         .getIndex(), theFinalStates4[i]);
    375                 assertEquals(i + "  Final state mismatch", aDataTruncation
    376                         .getDataSize(), theFinalStates5[i]);
    377                 assertEquals(i + "  Final state mismatch", aDataTruncation
    378                         .getTransferSize(), theFinalStates6[i]);
    379                 assertEquals(i + "  Final state mismatch", aDataTruncation
    380                         .getParameter(), theFinalStates7[i]);
    381                 assertEquals(i + "  Final state mismatch", aDataTruncation
    382                         .getRead(), theFinalStates8[i]);
    383 
    384             } catch (Exception e) {
    385                 if (theExceptions[i] == null) {
    386                     fail(i + "Unexpected exception");
    387                 }
    388                 assertEquals(i + "Exception mismatch", e.getClass(),
    389                         theExceptions[i].getClass());
    390                 assertEquals(i + "Exception mismatch", e.getMessage(),
    391                         theExceptions[i].getMessage());
    392             } // end try
    393         } // end for
    394 
    395     } // end method testGetDataSize
    396 
    397     /*
    398      * Method test for getTransferSize
    399      */
    400     public void testGetTransferSize() {
    401 
    402         DataTruncation aDataTruncation;
    403         int[] init1 = { -2147483648, 2147483647, 0, 78096124, 1719192600,
    404                 -1661234694, -1205825753 };
    405         boolean[] init2 = { false, true, false, false, true, false, true };
    406         boolean[] init3 = { false, true, false, false, false, false, false };
    407         int[] init4 = { -2147483648, 2147483647, 0, -493779295, -2042560243,
    408                 -217347438, 1357818664 };
    409         int[] init5 = { -2147483648, 2147483647, 0, -1647009002, -717544563,
    410                 -1368171905, -918209633 };
    411 
    412         int theReturn;
    413         int[] theReturns = init5;
    414         String[] theFinalStates1 = { "22001", "01004", "22001", "22001",
    415                 "22001", "22001", "22001" };
    416         String state2 = "Data truncation";
    417         String[] theFinalStates2 = { state2, state2, state2, state2, state2,
    418                 state2, state2 };
    419         int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0 };
    420         int[] theFinalStates4 = init1;
    421         int[] theFinalStates5 = init4;
    422         int[] theFinalStates6 = init5;
    423         boolean[] theFinalStates7 = init2;
    424         boolean[] theFinalStates8 = init3;
    425 
    426         Exception[] theExceptions = { null, null, null, null, null, null, null };
    427 
    428         int loopCount = 1;
    429         for (int i = 0; i < loopCount; i++) {
    430             try {
    431                 aDataTruncation = new DataTruncation(init1[i], init2[i],
    432                         init3[i], init4[i], init5[i]);
    433                 theReturn = aDataTruncation.getTransferSize();
    434                 if (theExceptions[i] != null) {
    435                     fail(i + "Exception missed");
    436                 }
    437                 assertEquals(i + "Return value mismatch", theReturn,
    438                         theReturns[i]);
    439                 assertEquals(i + "  Final state mismatch", aDataTruncation
    440                         .getSQLState(), theFinalStates1[i]);
    441                 assertEquals(i + "  Final state mismatch", aDataTruncation
    442                         .getMessage(), theFinalStates2[i]);
    443                 assertEquals(i + "  Final state mismatch", aDataTruncation
    444                         .getErrorCode(), theFinalStates3[i]);
    445                 assertEquals(i + "  Final state mismatch", aDataTruncation
    446                         .getIndex(), theFinalStates4[i]);
    447                 assertEquals(i + "  Final state mismatch", aDataTruncation
    448                         .getDataSize(), theFinalStates5[i]);
    449                 assertEquals(i + "  Final state mismatch", aDataTruncation
    450                         .getTransferSize(), theFinalStates6[i]);
    451                 assertEquals(i + "  Final state mismatch", aDataTruncation
    452                         .getParameter(), theFinalStates7[i]);
    453                 assertEquals(i + "  Final state mismatch", aDataTruncation
    454                         .getRead(), theFinalStates8[i]);
    455 
    456             } catch (Exception e) {
    457                 if (theExceptions[i] == null) {
    458                     fail(i + "Unexpected exception");
    459                 }
    460                 assertEquals(i + "Exception mismatch", e.getClass(),
    461                         theExceptions[i].getClass());
    462                 assertEquals(i + "Exception mismatch", e.getMessage(),
    463                         theExceptions[i].getMessage());
    464             } // end try
    465         } // end for
    466 
    467     } // end method testGetTransferSize
    468 
    469     /**
    470      * @tests serialization/deserialization compatibility.
    471      */
    472     public void testSerializationSelf() throws Exception {
    473         DataTruncation object = new DataTruncation(10, true, true, 10, 10);
    474         SerializationTest.verifySelf(object, DATATRUNCATION_COMPARATOR);
    475     }
    476 
    477     /**
    478      * @tests serialization/deserialization compatibility with RI.
    479      */
    480     public void testSerializationCompatibility() throws Exception {
    481         DataTruncation object = new DataTruncation(10, true, true, 10, 10);
    482         SerializationTest.verifyGolden(this, object, DATATRUNCATION_COMPARATOR);
    483     }
    484 
    485     // comparator for DataTruncation objects
    486     private static final SerializableAssert DATATRUNCATION_COMPARATOR = new SerializableAssert() {
    487         public void assertDeserialized(Serializable initial,
    488                 Serializable deserialized) {
    489 
    490             // do common checks for all throwable objects
    491             SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
    492                     deserialized);
    493 
    494             DataTruncation initThr = (DataTruncation) initial;
    495             DataTruncation dserThr = (DataTruncation) deserialized;
    496 
    497             // verify index
    498             assertEquals(initThr.getIndex(), dserThr.getIndex());
    499 
    500             // verify parameter
    501             assertEquals(initThr.getParameter(), dserThr.getParameter());
    502 
    503             // verify read
    504             assertEquals(initThr.getRead(), dserThr.getRead());
    505 
    506             // verify dataSize
    507             assertEquals(initThr.getDataSize(), dserThr.getDataSize());
    508 
    509             // verify transferSize
    510             assertEquals(initThr.getTransferSize(), dserThr.getTransferSize());
    511         }
    512     };
    513 
    514 } // end class DataTruncationTest
    515