1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.sql;
  24 
  25 import java.io.ByteArrayInputStream;
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FileOutputStream;
  29 import java.io.ObjectInputStream;
  30 import java.io.ObjectOutputStream;
  31 import java.sql.BatchUpdateException;
  32 import java.sql.SQLException;
  33 import java.util.Arrays;
  34 import static org.testng.Assert.*;
  35 import org.testng.annotations.AfterClass;
  36 import org.testng.annotations.AfterMethod;
  37 import org.testng.annotations.BeforeClass;
  38 import org.testng.annotations.BeforeMethod;
  39 import org.testng.annotations.Test;
  40 import util.SerializedBatchUpdateException;
  41 
  42 public class BatchUpdateExceptionTests {
  43 
  44     private final String reason = "reason";
  45     private final String state = "SQLState";
  46     private final String cause = "java.lang.Throwable: cause";
  47     private final Throwable t1 = new Throwable("cause 1");
  48     private final Throwable t2 = new Throwable("cause 2");
  49     private final Throwable t = new Throwable("cause");
  50     private final int errorCode = 21;
  51     private final int[] uc = {1, 2, 3};
  52     private final long[] luc = {1, 2, 3};
  53     private final String[] msgs = {"Exception 1", "cause 1", "Exception 2",
  54         "Exception 3", "cause 2"};
  55     private final String testSrcDir = System.getProperty("test.src", ".")
  56             + File.separatorChar;
  57 
  58     public BatchUpdateExceptionTests() {
  59     }
  60 
  61     @BeforeClass
  62     public static void setUpClass() throws Exception {
  63     }
  64 
  65     @AfterClass
  66     public static void tearDownClass() throws Exception {
  67     }
  68 
  69     @BeforeMethod
  70     public void setUpMethod() throws Exception {
  71     }
  72 
  73     @AfterMethod
  74     public void tearDownMethod() throws Exception {
  75     }
  76 
  77     /**
  78      * Create BatchUpdateException and setting all objects to null
  79      */
  80     @Test
  81     public void test() {
  82         BatchUpdateException be = new BatchUpdateException(null,
  83                 null, errorCode, (int[]) null, null);
  84         assertTrue(be.getMessage() == null && be.getSQLState() == null
  85                 && be.getUpdateCounts() == null && be.getCause() == null
  86                 && be.getLargeUpdateCounts() == null
  87                 && be.getErrorCode() == errorCode);
  88     }
  89 
  90     /**
  91      * Create BatchUpdateException with no-arg constructor
  92      */
  93     @Test
  94     public void test1() {
  95         BatchUpdateException ex = new BatchUpdateException();
  96         assertTrue(ex.getMessage() == null
  97                 && ex.getSQLState() == null
  98                 && ex.getCause() == null
  99                 && ex.getErrorCode() == 0
 100                 && ex.getUpdateCounts() == null
 101                 && ex.getLargeUpdateCounts() == null);
 102     }
 103 
 104     /**
 105      * Create BatchUpdateException with null Throwable
 106      */
 107     @Test
 108     public void test2() {
 109         BatchUpdateException ex = new BatchUpdateException((Throwable) null);
 110         assertTrue(ex.getMessage() == null
 111                 && ex.getSQLState() == null
 112                 && ex.getCause() == null
 113                 && ex.getErrorCode() == 0
 114                 && ex.getUpdateCounts() == null
 115                 && ex.getLargeUpdateCounts() == null);
 116     }
 117 
 118     /**
 119      * Create BatchUpdateException with message and update counts
 120      */
 121     @Test
 122     public void test3() {
 123 
 124         BatchUpdateException ex = new BatchUpdateException(reason, uc);
 125         assertTrue(ex.getMessage().equals(reason)
 126                 && ex.getSQLState() == null
 127                 && ex.getCause() == null
 128                 && ex.getErrorCode() == 0
 129                 && Arrays.equals(ex.getUpdateCounts(), uc)
 130                 && Arrays.equals(ex.getLargeUpdateCounts(), luc)
 131         );
 132     }
 133 
 134     /**
 135      * Create BatchUpdateException with update counts
 136      */
 137     @Test
 138     public void test4() {
 139         BatchUpdateException ex = new BatchUpdateException(uc);
 140         assertTrue(ex.getMessage() == null
 141                 && ex.getSQLState() == null
 142                 && ex.getCause() == null
 143                 && ex.getErrorCode() == 0
 144                 && Arrays.equals(ex.getUpdateCounts(), uc)
 145                 && Arrays.equals(ex.getLargeUpdateCounts(), luc)
 146         );
 147     }
 148 
 149     /**
 150      * Create BatchUpdateException with Throwable and update counts
 151      */
 152     @Test
 153     public void test5() {
 154         BatchUpdateException ex = new BatchUpdateException(uc, t);
 155         assertTrue(ex.getMessage().equals(cause)
 156                 && ex.getSQLState() == null
 157                 && cause.equals(ex.getCause().toString())
 158                 && ex.getErrorCode() == 0
 159                 && Arrays.equals(ex.getUpdateCounts(), uc)
 160                 && Arrays.equals(ex.getLargeUpdateCounts(), luc)
 161         );
 162     }
 163 
 164     /**
 165      * Create BatchUpdateException with message, Throwable, and update counts
 166      */
 167     @Test
 168     public void test6() {
 169         BatchUpdateException ex = new BatchUpdateException(reason, uc, t);
 170         assertTrue(ex.getMessage().equals(reason)
 171                 && ex.getSQLState() == null
 172                 && cause.equals(ex.getCause().toString())
 173                 && ex.getErrorCode() == 0
 174                 && Arrays.equals(ex.getUpdateCounts(), uc)
 175                 && Arrays.equals(ex.getLargeUpdateCounts(), luc)
 176         );
 177     }
 178 
 179     /**
 180      * Create BatchUpdateException with message, SQLState, Throwable, and update
 181      * counts
 182      */
 183     @Test
 184     public void test7() {
 185         BatchUpdateException ex = new BatchUpdateException(reason, state, uc, t);
 186         assertTrue(ex.getMessage().equals(reason)
 187                 && ex.getSQLState().equals(state)
 188                 && cause.equals(ex.getCause().toString())
 189                 && ex.getErrorCode() == 0
 190                 && Arrays.equals(ex.getUpdateCounts(), uc)
 191                 && Arrays.equals(ex.getLargeUpdateCounts(), luc)
 192         );
 193     }
 194 
 195     /**
 196      * Create BatchUpdateException with message, SQLState, errorCode code
 197      * Throwable, and update counts
 198      */
 199     @Test
 200     public void test8() {
 201         BatchUpdateException ex = new BatchUpdateException(reason, state, errorCode,
 202                 uc, t);
 203         assertTrue(ex.getMessage().equals(reason)
 204                 && ex.getSQLState().equals(state)
 205                 && cause.equals(ex.getCause().toString())
 206                 && ex.getErrorCode() == errorCode
 207                 && Arrays.equals(ex.getUpdateCounts(), uc)
 208                 && Arrays.equals(ex.getLargeUpdateCounts(), luc)
 209         );
 210     }
 211 
 212     /**
 213      * Create BatchUpdateException with message, SQLState, errorCode code
 214      * Throwable, and long [] update counts
 215      */
 216     @Test
 217     public void test9() {
 218         BatchUpdateException ex = new BatchUpdateException(reason, state, errorCode,
 219                 luc, t);
 220         assertTrue(ex.getMessage().equals(reason)
 221                 && ex.getSQLState().equals(state)
 222                 && cause.equals(ex.getCause().toString())
 223                 && ex.getErrorCode() == errorCode
 224                 && Arrays.equals(ex.getUpdateCounts(), uc)
 225                 && Arrays.equals(ex.getLargeUpdateCounts(), luc)
 226         );
 227     }
 228 
 229     /**
 230      * Validate that a copy of the update counts array is made
 231      */
 232     @Test
 233     public void test10() {
 234         int[] uc1 = {1, 2};
 235         BatchUpdateException ex = new BatchUpdateException(uc1);
 236         assertTrue(Arrays.equals(ex.getUpdateCounts(), uc1));
 237         uc1[0] = 6689;
 238         assertFalse(Arrays.equals(ex.getUpdateCounts(), uc1));
 239     }
 240 
 241     /**
 242      * Validate that if null is specified for the update count, it is returned
 243      * as null
 244      */
 245     @Test
 246     public void test11() {
 247         BatchUpdateException ex = new BatchUpdateException((int[]) null);
 248         assertTrue(ex.getMessage() == null && ex.getSQLState() == null
 249                 && ex.getErrorCode() == 0 && ex.getUpdateCounts() == null
 250                 && ex.getLargeUpdateCounts() == null);
 251     }
 252 
 253     /**
 254      * Serialize a BatchUpdateException and make sure you can read it back
 255      * properly
 256      */
 257     @Test
 258     public void test12() throws Exception {
 259         BatchUpdateException be = new BatchUpdateException(reason, state, errorCode,
 260                 uc, t);
 261         ObjectOutputStream out
 262                 = new ObjectOutputStream(
 263                         new FileOutputStream("BatchUpdateException_JDBC42.ser"));
 264         out.writeObject(be);
 265         ObjectInputStream is = new ObjectInputStream(
 266                 new FileInputStream("BatchUpdateException_JDBC42.ser"));
 267         BatchUpdateException bue = (BatchUpdateException) is.readObject();
 268         assertTrue(reason.equals(bue.getMessage())
 269                 && bue.getSQLState().equals(state)
 270                 && cause.equals(bue.getCause().toString())
 271                 && bue.getErrorCode() == errorCode
 272                 && Arrays.equals(bue.getLargeUpdateCounts(), luc)
 273                 && Arrays.equals(bue.getUpdateCounts(), uc));
 274     }
 275 
 276     /**
 277      * De-Serialize a BatchUpdateException from JDBC 4.0 and make sure you can
 278      * read it back properly
 279      */
 280     @Test
 281     public void test13() throws Exception {
 282         String reason1 = "This was the error msg";
 283         String state1 = "user defined sqlState";
 284         String cause1 = "java.lang.Throwable: throw 1";
 285         int errorCode1 = 99999;
 286         Throwable t = new Throwable("throw 1");
 287         int[] uc1 = {1, 2, 21};
 288         long[] luc1 = {1, 2, 21};
 289 
 290         ObjectInputStream ois = new ObjectInputStream(
 291                 new ByteArrayInputStream(SerializedBatchUpdateException.DATA));
 292         BatchUpdateException bue = (BatchUpdateException) ois.readObject();
 293         assertTrue(reason1.equals(bue.getMessage())
 294                 && bue.getSQLState().equals(state1)
 295                 && bue.getErrorCode() == errorCode1
 296                 && cause1.equals(bue.getCause().toString())
 297                 && Arrays.equals(bue.getLargeUpdateCounts(), luc1)
 298                 && Arrays.equals(bue.getUpdateCounts(), uc1));
 299     }
 300 
 301     /**
 302      * Serialize a BatchUpdateException with an Integer.MAX_VALUE + 1 and
 303      * validate you can read it back properly
 304      */
 305     @Test
 306     public void test14() throws Exception {
 307         int[] uc1 = {Integer.MAX_VALUE, Integer.MAX_VALUE + 1};
 308         long[] luc1 = {Integer.MAX_VALUE, Integer.MAX_VALUE + 1};
 309         BatchUpdateException be = new BatchUpdateException(reason, state, errorCode,
 310                 luc1, t);
 311         ObjectOutputStream out
 312                 = new ObjectOutputStream(
 313                         new FileOutputStream("BatchUpdateException_MAX_INT.ser"));
 314         out.writeObject(be);
 315         ObjectInputStream is = new ObjectInputStream(
 316                 new FileInputStream("BatchUpdateException_MAX_INT.ser"));
 317         BatchUpdateException bue = (BatchUpdateException) is.readObject();
 318         assertTrue(reason.equals(bue.getMessage())
 319                 && bue.getSQLState().equals(state)
 320                 && cause.equals(bue.getCause().toString())
 321                 && bue.getErrorCode() == errorCode
 322                 && Arrays.equals(bue.getLargeUpdateCounts(), luc1)
 323                 && Arrays.equals(bue.getUpdateCounts(), uc1));
 324     }
 325 
 326     /**
 327      * Validate that the ordering of the returned Exceptions is correct
 328      * using for-each loop
 329      */
 330     @Test
 331     public void test15() {
 332         BatchUpdateException ex = new BatchUpdateException("Exception 1", uc, t1);
 333         BatchUpdateException ex1 = new BatchUpdateException("Exception 2", uc);
 334         BatchUpdateException ex2 = new BatchUpdateException("Exception 3", uc, t2);
 335         ex.setNextException(ex1);
 336         ex.setNextException(ex2);
 337         int num = 0;
 338         for (Throwable e : ex) {
 339             assertTrue(msgs[num++].equals(e.getMessage()));
 340         }
 341     }
 342 
 343     /**
 344      * Validate that the ordering of the returned Exceptions is correct
 345      * using traditional while loop
 346      */
 347     @Test
 348     public void test16() {
 349         BatchUpdateException ex = new BatchUpdateException("Exception 1", uc,  t1);
 350         BatchUpdateException ex1 = new BatchUpdateException("Exception 2", uc);
 351         BatchUpdateException ex2 = new BatchUpdateException("Exception 3", uc, t2);
 352         ex.setNextException(ex1);
 353         ex.setNextException(ex2);
 354         SQLException sqe = ex;
 355         int num = 0;
 356         while (sqe != null) {
 357             assertTrue(msgs[num++].equals(sqe.getMessage()));
 358             Throwable c = sqe.getCause();
 359             while (c != null) {
 360                 assertTrue(msgs[num++].equals(c.getMessage()));
 361                 c = c.getCause();
 362             }
 363             sqe = sqe.getNextException();
 364         }
 365     }
 366 
 367 }