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.BatchUpdateException;
     22 import java.util.Arrays;
     23 
     24 import junit.framework.TestCase;
     25 
     26 import org.apache.harmony.testframework.serialization.SerializationTest;
     27 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
     28 
     29 public class BatchUpdateExceptionTest extends TestCase {
     30 
     31     /*
     32      * ConstructorTest
     33      */
     34     public void testBatchUpdateException() {
     35 
     36         int[] theFinalStates1 = { 0 }; // Error Code state
     37         int[][] theFinalStates2 = { null }; // Update Counts array state
     38         String[] theFinalStates3 = { null }; // SQL State state value
     39         String[] theFinalStates4 = { null }; // Message state
     40 
     41         Exception[] theExceptions = { null };
     42 
     43         BatchUpdateException aBatchUpdateException;
     44         int loopCount = 1;
     45         for (int i = 0; i < loopCount; i++) {
     46             try {
     47                 aBatchUpdateException = new BatchUpdateException();
     48                 if (theExceptions[i] != null) {
     49                     fail();
     50                 }
     51                 assertEquals(i + " Final state mismatch: ",
     52                         aBatchUpdateException.getErrorCode(),
     53                         theFinalStates1[i]);
     54                 assertEquals(i + " Final state mismatch: ",
     55                         aBatchUpdateException.getUpdateCounts(),
     56                         theFinalStates2[i]);
     57                 assertEquals(i + " Final state mismatch: ",
     58                         aBatchUpdateException.getSQLState(), theFinalStates3[i]);
     59                 assertEquals(i + " Final state mismatch: ",
     60                         aBatchUpdateException.getMessage(), theFinalStates4[i]);
     61 
     62             } catch (Exception e) {
     63                 if (theExceptions[i] == null) {
     64                     fail(i + "Unexpected exception");
     65                 }
     66                 assertEquals(i + "Exception mismatch", e.getClass(),
     67                         theExceptions[i].getClass());
     68                 assertEquals(i + "Exception mismatch", e.getMessage(),
     69                         theExceptions[i].getMessage());
     70             } // end try
     71         } // end for
     72 
     73     } // end method testBatchUpdateException
     74 
     75     /*
     76      * ConstructorTest
     77      */
     78     public void testBatchUpdateExceptionintArray() {
     79 
     80         int[][] init1 = { { 1, 2, 3 }, null };
     81 
     82         int[] theFinalStates1 = { 0, 0 }; // Error Code state
     83         int[][] theFinalStates2 = init1; // Update Counts array state
     84         String[] theFinalStates3 = { null, null }; // SQL State state value
     85         String[] theFinalStates4 = { null, null }; // Message state
     86 
     87         Exception[] theExceptions = { null, null };
     88 
     89         BatchUpdateException aBatchUpdateException;
     90         int loopCount = init1.length;
     91         for (int i = 0; i < loopCount; i++) {
     92             try {
     93                 aBatchUpdateException = new BatchUpdateException(init1[i]);
     94                 if (theExceptions[i] != null) {
     95                     fail();
     96                 }
     97                 assertEquals(i + " Final state mismatch: ",
     98                         aBatchUpdateException.getErrorCode(),
     99                         theFinalStates1[i]);
    100                 assertTrue(i + " Final state mismatch: ",
    101                         Arrays.equals(aBatchUpdateException.getUpdateCounts(), theFinalStates2[i]));
    102                 assertEquals(i + " Final state mismatch: ",
    103                         aBatchUpdateException.getSQLState(), theFinalStates3[i]);
    104                 assertEquals(i + " Final state mismatch: ",
    105                         aBatchUpdateException.getMessage(), theFinalStates4[i]);
    106 
    107             } catch (Exception e) {
    108                 if (theExceptions[i] == null) {
    109                     fail(i + "Unexpected exception");
    110                 }
    111                 assertEquals(i + "Exception mismatch", e.getClass(),
    112                         theExceptions[i].getClass());
    113                 assertEquals(i + "Exception mismatch", e.getMessage(),
    114                         theExceptions[i].getMessage());
    115             } // end try
    116         } // end for
    117 
    118     } // end method testBatchUpdateExceptionintArray
    119 
    120     /*
    121      * ConstructorTest
    122      */
    123     public void testBatchUpdateExceptionStringintArray() {
    124 
    125         String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "",
    126                 ".", "a" };
    127         int[][] init2 = { { 1, 2, 3 }, { }, { 3 }, null, { 5, 5 }, { 6 },
    128                 { 121, 2, 1 }, { 1 }, { 1, 2 } };
    129 
    130         int[] theFinalStates1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Error Code
    131         // state
    132         // Update Counts array state
    133         int[][] theFinalStates2 = init2;
    134         // SQL State state value
    135         String[] theFinalStates3 = { null, null, null, null, null, null, null,
    136                 null, null };
    137         String[] theFinalStates4 = init1; // Message state
    138 
    139         Exception[] theExceptions = { null, null, null, null, null, null, null,
    140                 null, null };
    141 
    142         BatchUpdateException aBatchUpdateException;
    143         int loopCount = init1.length;
    144         for (int i = 0; i < loopCount; i++) {
    145             try {
    146                 aBatchUpdateException = new BatchUpdateException(init1[i],
    147                         init2[i]);
    148                 if (theExceptions[i] != null) {
    149                     fail();
    150                 }
    151                 assertEquals(i + " Final state mismatch: ",
    152                         aBatchUpdateException.getErrorCode(),
    153                         theFinalStates1[i]);
    154                 assertTrue(i + " Final state mismatch: ",
    155                         Arrays.equals(aBatchUpdateException.getUpdateCounts(), theFinalStates2[i]));
    156                 assertEquals(i + " Final state mismatch: ",
    157                         aBatchUpdateException.getSQLState(), theFinalStates3[i]);
    158                 assertEquals(i + " Final state mismatch: ",
    159                         aBatchUpdateException.getMessage(), theFinalStates4[i]);
    160 
    161             } catch (Exception e) {
    162                 if (theExceptions[i] == null) {
    163                     fail(i + "Unexpected exception");
    164                 }
    165                 assertEquals(i + "Exception mismatch", e.getClass(),
    166                         theExceptions[i].getClass());
    167                 assertEquals(i + "Exception mismatch", e.getMessage(),
    168                         theExceptions[i].getMessage());
    169             } // end try
    170         } // end for
    171 
    172     } // end method testBatchUpdateExceptionStringintArray
    173 
    174     /*
    175      * ConstructorTest
    176      */
    177     public void testBatchUpdateExceptionStringStringintArray() {
    178 
    179         String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "",
    180                 ".", "a", "a" };
    181         String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a", null,
    182                 "", ".", "a" };
    183         int[][] init3 = { { 1, 2, 3 }, { }, { 3 }, { 5, 5 }, { 6 },
    184                 { 121, 2, 1 }, { 1 }, { 1, 2 }, { 1 }, { 2 }, null };
    185 
    186         int[] theFinalStates1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Error
    187         // Code
    188         // state
    189         // Update Counts array state
    190         int[][] theFinalStates2 = init3;
    191         // SQL State state value
    192         String[] theFinalStates3 = init2;
    193         String[] theFinalStates4 = init1; // Message state
    194 
    195         Exception[] theExceptions = { null, null, null, null, null, null, null,
    196                 null, null, null, null };
    197 
    198         BatchUpdateException aBatchUpdateException;
    199         int loopCount = init1.length;
    200         for (int i = 0; i < loopCount; i++) {
    201             try {
    202                 aBatchUpdateException = new BatchUpdateException(init1[i],
    203                         init2[i], init3[i]);
    204                 if (theExceptions[i] != null) {
    205                     fail();
    206                 }
    207                 assertEquals(i + " Final state mismatch: ",
    208                         aBatchUpdateException.getErrorCode(),
    209                         theFinalStates1[i]);
    210                 assertTrue(i + " Final state mismatch: ",
    211                         Arrays.equals(aBatchUpdateException.getUpdateCounts(), theFinalStates2[i]));
    212                 assertEquals(i + " Final state mismatch: ",
    213                         aBatchUpdateException.getSQLState(), theFinalStates3[i]);
    214                 assertEquals(i + " Final state mismatch: ",
    215                         aBatchUpdateException.getMessage(), theFinalStates4[i]);
    216 
    217             } catch (Exception e) {
    218                 if (theExceptions[i] == null) {
    219                     fail(i + "Unexpected exception");
    220                 }
    221                 assertEquals(i + "Exception mismatch", e.getClass(),
    222                         theExceptions[i].getClass());
    223                 assertEquals(i + "Exception mismatch", e.getMessage(),
    224                         theExceptions[i].getMessage());
    225             } // end try
    226         } // end for
    227 
    228     } // end method testBatchUpdateExceptionStringStringintArray
    229 
    230     /*
    231      * ConstructorTest
    232      */
    233     public void testBatchUpdateExceptionStringStringintintArray() {
    234 
    235         String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "",
    236                 ".", "a", "a" };
    237         String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a", null,
    238                 "", ".", "a" };
    239         int[] init3 = { -2147483648, 2147483647, 0, -492417162, -156220255,
    240                 -173012890, -631026360, -2147483648, -2147483648, -2147483648,
    241                 -2147483648 };
    242         int[][] init4 = { { 1, 2, 3 }, { }, { 3 }, { 5, 5 }, { 6 },
    243                 { 121, 2, 1 }, { 1 }, { 1, 2 }, { 1 }, { 2 }, null };
    244 
    245         int[] theFinalStates1 = init3; // Error Code state
    246         // Update Counts array state
    247         int[][] theFinalStates2 = init4;
    248         // SQL State state value
    249         String[] theFinalStates3 = init2;
    250         String[] theFinalStates4 = init1; // Message state
    251 
    252         Exception[] theExceptions = { null, null, null, null, null, null, null,
    253                 null, null, null, null };
    254 
    255         BatchUpdateException aBatchUpdateException;
    256         int loopCount = init1.length;
    257         for (int i = 0; i < loopCount; i++) {
    258             try {
    259                 aBatchUpdateException = new BatchUpdateException(init1[i],
    260                         init2[i], init3[i], init4[i]);
    261                 if (theExceptions[i] != null) {
    262                     fail();
    263                 }
    264                 assertEquals(i + " Final state mismatch: ",
    265                         aBatchUpdateException.getErrorCode(),
    266                         theFinalStates1[i]);
    267                 assertTrue(i + " Final state mismatch: ",
    268                         Arrays.equals(aBatchUpdateException.getUpdateCounts(), theFinalStates2[i]));
    269                 assertEquals(i + " Final state mismatch: ",
    270                         aBatchUpdateException.getSQLState(), theFinalStates3[i]);
    271                 assertEquals(i + " Final state mismatch: ",
    272                         aBatchUpdateException.getMessage(), theFinalStates4[i]);
    273 
    274             } catch (Exception e) {
    275                 if (theExceptions[i] == null) {
    276                     fail(i + "Unexpected exception");
    277                 }
    278                 assertEquals(i + "Exception mismatch", e.getClass(),
    279                         theExceptions[i].getClass());
    280                 assertEquals(i + "Exception mismatch", e.getMessage(),
    281                         theExceptions[i].getMessage());
    282             } // end try
    283         } // end for
    284 
    285     } // end method testBatchUpdateExceptionStringStringintintArray
    286 
    287     /**
    288      * @tests {@link java.sql.BatchUpdateException#BatchUpdateException(Throwable)}
    289      * @since 1.6
    290      */
    291     public void testBatchUpdateExceptionLThrowable() {
    292         Throwable cause = new Exception("MYTHROWABLE");
    293         BatchUpdateException batchUpdateException = new BatchUpdateException(
    294                 cause);
    295         assertNotNull(batchUpdateException);
    296         assertEquals("java.lang.Exception: MYTHROWABLE", batchUpdateException
    297                 .getMessage());
    298         assertNull(batchUpdateException.getSQLState());
    299         assertEquals(0, batchUpdateException.getErrorCode());
    300         assertEquals(cause, batchUpdateException.getCause());
    301     }
    302 
    303     /**
    304      * @tests {@link java.sql.BatchUpdateException#BatchUpdateException(int[], Throwable)}
    305      * @since 1.6
    306      */
    307     public void testBatchUpdateException$ILThrowable() {
    308         Throwable cause = new Exception("MYTHROWABLE");
    309         int[] updateCounts = new int[] { 1, 2, 3 };
    310         BatchUpdateException batchUpdateException = new BatchUpdateException(
    311                 updateCounts, cause);
    312         assertNotNull(batchUpdateException);
    313         assertEquals("java.lang.Exception: MYTHROWABLE", batchUpdateException
    314                 .getMessage());
    315         int[] result = batchUpdateException.getUpdateCounts();
    316         for (int i = 0; i < updateCounts.length; i++) {
    317             assertEquals(updateCounts[i], result[i]);
    318         }
    319         assertEquals(cause, batchUpdateException.getCause());
    320     }
    321 
    322     /**
    323      * @tests {@link java.sql.BatchUpdateException#BatchUpdateException(String, int[], Throwable)}
    324      * @since 1.6
    325      */
    326     public void testBatchUpdateExceptionLString$ILThrowable() {
    327         Throwable cause = new Exception("MYTHROWABLE");
    328         int[] updateCounts = new int[] { 1, 2, 3 };
    329         BatchUpdateException batchUpdateException = new BatchUpdateException(
    330                 "MYTESTSTRING1", "MYTESTSTRING2", updateCounts, cause);
    331         assertNotNull(batchUpdateException);
    332         assertEquals("MYTESTSTRING2", batchUpdateException.getSQLState());
    333         assertEquals("MYTESTSTRING1", batchUpdateException.getMessage());
    334         assertEquals(0, batchUpdateException.getErrorCode());
    335         int[] result = batchUpdateException.getUpdateCounts();
    336         for (int i = 0; i < updateCounts.length; i++) {
    337             assertEquals(updateCounts[i], result[i]);
    338         }
    339         assertEquals(cause, batchUpdateException.getCause());
    340     }
    341 
    342     /**
    343      * @tests {@link java.sql.BatchUpdateException#BatchUpdateException(String, String, int[], Throwable)}
    344      * @since 1.6
    345      */
    346     public void testBatchUpdateExceptionLStringLStringILThrowable() {
    347         int[] updateCounts = new int[] { 1, 2, 3 };
    348         BatchUpdateException batchUpdateException = new BatchUpdateException(
    349                 null, null, updateCounts, null);
    350         assertNotNull(batchUpdateException);
    351         assertNull(batchUpdateException.getSQLState());
    352         assertNull(batchUpdateException.getMessage());
    353         assertEquals(0, batchUpdateException.getErrorCode());
    354         assertNull(batchUpdateException.getCause());
    355     }
    356 
    357     /**
    358      * @tests {@link java.sql.BatchUpdateException#BatchUpdateException(String, String, int, int[], Throwable)}
    359      * @since 1.6
    360      */
    361     public void testBatchUpdateExceptionLStringLStringI$ILThrowable() {
    362         BatchUpdateException batchUpdateException = new BatchUpdateException(
    363                 "MYTESTSTRING1", "MYTESTSTRING2", 1, null);
    364         assertNotNull(batchUpdateException);
    365         assertEquals("MYTESTSTRING2", batchUpdateException.getSQLState());
    366         assertEquals("MYTESTSTRING1", batchUpdateException.getMessage());
    367         assertEquals(batchUpdateException.getErrorCode(), 1);
    368         assertNull(batchUpdateException.getCause());
    369     }
    370 
    371     /*
    372      * Method test for getUpdateCounts
    373      */
    374     public void testGetUpdateCounts() {
    375 
    376         BatchUpdateException aBatchUpdateException;
    377         int[][] init1 = { { 1, 2, 3 }, { }, null };
    378 
    379         int[] theReturn;
    380         int[][] theReturns = init1;
    381 
    382         int[] theFinalStates1 = { 0, 0, 0 }; // Error Code state
    383         int[][] theFinalStates2 = init1; // Update Counts array state
    384         String[] theFinalStates3 = { null, null, null }; // SQL State state
    385         // value
    386         String[] theFinalStates4 = { null, null, null }; // Message state
    387 
    388         Exception[] theExceptions = { null, null, null };
    389 
    390         int loopCount = init1.length;
    391         for (int i = 0; i < loopCount; i++) {
    392             try {
    393                 aBatchUpdateException = new BatchUpdateException(init1[i]);
    394                 theReturn = aBatchUpdateException.getUpdateCounts();
    395                 if (theExceptions[i] != null) {
    396                     fail(i + "Exception missed");
    397                 }
    398                 assertTrue(i + "Return value mismatch", Arrays.equals(theReturn, theReturns[i]));
    399                 assertEquals(i + " Final state mismatch: ",
    400                         aBatchUpdateException.getErrorCode(),
    401                         theFinalStates1[i]);
    402                 assertTrue(i + " Final state mismatch: ",
    403                         Arrays.equals(aBatchUpdateException.getUpdateCounts(), theFinalStates2[i]));
    404                 assertEquals(i + " Final state mismatch: ",
    405                         aBatchUpdateException.getSQLState(), theFinalStates3[i]);
    406                 assertEquals(i + " Final state mismatch: ",
    407                         aBatchUpdateException.getMessage(), theFinalStates4[i]);
    408 
    409             } catch (Exception e) {
    410                 if (theExceptions[i] == null) {
    411                     fail(i + "Unexpected exception");
    412                 }
    413                 assertEquals(i + "Exception mismatch", e.getClass(),
    414                         theExceptions[i].getClass());
    415                 assertEquals(i + "Exception mismatch", e.getMessage(),
    416                         theExceptions[i].getMessage());
    417             } // end try
    418         } // end for
    419 
    420     } // end method testGetUpdateCounts
    421 
    422     /**
    423      * @tests serialization/deserialization compatibility.
    424      */
    425     public void testSerializationSelf() throws Exception {
    426         BatchUpdateException object = new BatchUpdateException();
    427         SerializationTest.verifySelf(object, BATCHUPDATEEXCEPTION_COMPARATOR);
    428     }
    429 
    430     /**
    431      * @tests serialization/deserialization compatibility with RI.
    432      */
    433     public void testSerializationCompatibility() throws Exception {
    434         int vendorCode = 10;
    435         int[] updateCounts = { 1, 2, 3, 4 };
    436         BatchUpdateException object = new BatchUpdateException("reason",
    437                 "SQLState", vendorCode, updateCounts);
    438         SerializationTest.verifyGolden(this, object,
    439                 BATCHUPDATEEXCEPTION_COMPARATOR);
    440     }
    441 
    442     // comparator for BatchUpdateException field updateCounts
    443     private static final SerializableAssert BATCHUPDATEEXCEPTION_COMPARATOR = new SerializableAssert() {
    444         public void assertDeserialized(Serializable initial,
    445                 Serializable deserialized) {
    446 
    447             // do common checks for all throwable objects
    448             SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
    449                     deserialized);
    450 
    451             BatchUpdateException initThr = (BatchUpdateException) initial;
    452             BatchUpdateException dserThr = (BatchUpdateException) deserialized;
    453 
    454             // verify updateCounts
    455             int[] initUpdateCounts = initThr.getUpdateCounts();
    456             int[] dserUpdateCounts = dserThr.getUpdateCounts();
    457             assertTrue(Arrays.equals(initUpdateCounts, dserUpdateCounts));
    458         }
    459     };
    460 
    461 } // end class BatchUpdateExceptionTest
    462 
    463