test/java/sql/test/sql/BatchUpdateExceptionTests.java

Print this page




   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


 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;




   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.ObjectInputStream;

  28 import java.sql.BatchUpdateException;
  29 import java.sql.SQLException;
  30 import java.util.Arrays;
  31 import static org.testng.Assert.*;




  32 import org.testng.annotations.Test;
  33 import util.SerializedBatchUpdateException;
  34 import util.BaseTest;
  35 
  36 public class BatchUpdateExceptionTests extends BaseTest {
  37 







  38     private final int[] uc = {1, 2, 3};
  39     private final long[] luc = {1, 2, 3};
  40 

  41     private final String testSrcDir = System.getProperty("test.src", ".")
  42             + File.separatorChar;
  43 



















  44     /**
  45      * Create BatchUpdateException and setting all objects to null
  46      */
  47     @Test
  48     public void test() {
  49         BatchUpdateException be = new BatchUpdateException(null,
  50                 null, errorCode, (int[]) null, null);
  51         assertTrue(be.getMessage() == null && be.getSQLState() == null
  52                 && be.getUpdateCounts() == null && be.getCause() == null
  53                 && be.getLargeUpdateCounts() == null
  54                 && be.getErrorCode() == errorCode);
  55     }
  56 
  57     /**
  58      * Create BatchUpdateException with no-arg constructor
  59      */
  60     @Test
  61     public void test1() {
  62         BatchUpdateException ex = new BatchUpdateException();
  63         assertTrue(ex.getMessage() == null


 208     /**
 209      * Validate that if null is specified for the update count, it is returned
 210      * as null
 211      */
 212     @Test
 213     public void test11() {
 214         BatchUpdateException ex = new BatchUpdateException((int[]) null);
 215         assertTrue(ex.getMessage() == null && ex.getSQLState() == null
 216                 && ex.getErrorCode() == 0 && ex.getUpdateCounts() == null
 217                 && ex.getLargeUpdateCounts() == null);
 218     }
 219 
 220     /**
 221      * Serialize a BatchUpdateException and make sure you can read it back
 222      * properly
 223      */
 224     @Test
 225     public void test12() throws Exception {
 226         BatchUpdateException be = new BatchUpdateException(reason, state, errorCode,
 227                 uc, t);
 228         BatchUpdateException bue
 229                 = createSerializedException(be);





 230         assertTrue(reason.equals(bue.getMessage())
 231                 && bue.getSQLState().equals(state)
 232                 && cause.equals(bue.getCause().toString())
 233                 && bue.getErrorCode() == errorCode
 234                 && Arrays.equals(bue.getLargeUpdateCounts(), luc)
 235                 && Arrays.equals(bue.getUpdateCounts(), uc));
 236     }
 237 
 238 
 239 
 240     /**
 241      * De-Serialize a BatchUpdateException from JDBC 4.0 and make sure you can
 242      * read it back properly
 243      */
 244     @Test
 245     public void test13() throws Exception {
 246         String reason1 = "This was the error msg";
 247         String state1 = "user defined sqlState";
 248         String cause1 = "java.lang.Throwable: throw 1";
 249         int errorCode1 = 99999;
 250         Throwable t = new Throwable("throw 1");
 251         int[] uc1 = {1, 2, 21};
 252         long[] luc1 = {1, 2, 21};
 253 
 254         ObjectInputStream ois = new ObjectInputStream(
 255                 new ByteArrayInputStream(SerializedBatchUpdateException.DATA));
 256         BatchUpdateException bue = (BatchUpdateException) ois.readObject();
 257         assertTrue(reason1.equals(bue.getMessage())
 258                 && bue.getSQLState().equals(state1)
 259                 && bue.getErrorCode() == errorCode1
 260                 && cause1.equals(bue.getCause().toString())
 261                 && Arrays.equals(bue.getLargeUpdateCounts(), luc1)
 262                 && Arrays.equals(bue.getUpdateCounts(), uc1));
 263     }
 264 
 265     /**
 266      * Serialize a BatchUpdateException with an Integer.MAX_VALUE + 1 and
 267      * validate you can read it back properly
 268      */
 269     @Test
 270     public void test14() throws Exception {
 271         int[] uc1 = {Integer.MAX_VALUE, Integer.MAX_VALUE + 1};
 272         long[] luc1 = {Integer.MAX_VALUE, Integer.MAX_VALUE + 1};
 273         BatchUpdateException be = new BatchUpdateException(reason, state, errorCode,
 274                 luc1, t);
 275                 BatchUpdateException bue
 276                 = createSerializedException(be);





 277         assertTrue(reason.equals(bue.getMessage())
 278                 && bue.getSQLState().equals(state)
 279                 && cause.equals(bue.getCause().toString())
 280                 && bue.getErrorCode() == errorCode
 281                 && Arrays.equals(bue.getLargeUpdateCounts(), luc1)
 282                 && Arrays.equals(bue.getUpdateCounts(), uc1));
 283     }
 284 
 285     /**
 286      * Validate that the ordering of the returned Exceptions is correct
 287      * using for-each loop
 288      */
 289     @Test
 290     public void test15() {
 291         BatchUpdateException ex = new BatchUpdateException("Exception 1", uc, t1);
 292         BatchUpdateException ex1 = new BatchUpdateException("Exception 2", uc);
 293         BatchUpdateException ex2 = new BatchUpdateException("Exception 3", uc, t2);
 294         ex.setNextException(ex1);
 295         ex.setNextException(ex2);
 296         int num = 0;