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.FileInputStream;
  26 import java.io.FileOutputStream;
  27 import java.io.ObjectInputStream;
  28 import java.io.ObjectOutputStream;
  29 import java.sql.SQLRecoverableException;
  30 import java.sql.SQLException;
  31 import static org.testng.Assert.*;
  32 import org.testng.annotations.AfterClass;
  33 import org.testng.annotations.AfterMethod;
  34 import org.testng.annotations.BeforeClass;
  35 import org.testng.annotations.BeforeMethod;
  36 import org.testng.annotations.Test;
  37 
  38 public class SQLRecoverableExceptionTests {
  39 
  40     private final String reason = "reason";
  41     private final String state = "SQLState";
  42     private final String cause = "java.lang.Throwable: cause";
  43     private final Throwable t = new Throwable("cause");
  44     private final Throwable t1 = new Throwable("cause 1");
  45     private final Throwable t2 = new Throwable("cause 2");
  46     private final int errorCode = 21;
  47     private final String[] msgs = {"Exception 1", "cause 1", "Exception 2",
  48         "Exception 3", "cause 2"};
  49 
  50     public SQLRecoverableExceptionTests() {
  51     }
  52 
  53     @BeforeClass
  54     public static void setUpClass() throws Exception {
  55     }
  56 
  57     @AfterClass
  58     public static void tearDownClass() throws Exception {
  59     }
  60 
  61     @BeforeMethod
  62     public void setUpMethod() throws Exception {
  63     }
  64 
  65     @AfterMethod
  66     public void tearDownMethod() throws Exception {
  67     }
  68 
  69     /**
  70      * Create SQLRecoverableException and setting all objects to null
  71      */
  72     @Test
  73     public void test() {
  74         SQLRecoverableException e = new SQLRecoverableException(null,
  75                null, errorCode, null);
  76         assertTrue(e.getMessage() == null && e.getSQLState() == null
  77                 && e.getCause() == null && e.getErrorCode() == errorCode);
  78     }
  79 
  80     /**
  81      * Create SQLRecoverableException with no-arg constructor
  82      */
  83     @Test
  84     public void test1() {
  85         SQLRecoverableException ex = new SQLRecoverableException();
  86         assertTrue(ex.getMessage() == null
  87                 && ex.getSQLState() == null
  88                 && ex.getCause() == null
  89                 && ex.getErrorCode() == 0);
  90     }
  91 
  92     /**
  93      * Create SQLRecoverableException with message
  94      */
  95     @Test
  96     public void test2() {
  97         SQLRecoverableException ex = new SQLRecoverableException(reason);
  98         assertTrue(ex.getMessage().equals(reason)
  99                 && ex.getSQLState() == null
 100                 && ex.getCause() == null
 101                 && ex.getErrorCode() == 0);
 102     }
 103 
 104     /**
 105      * Create SQLRecoverableException with message, and SQLState
 106      */
 107     @Test
 108     public void test3() {
 109         SQLRecoverableException ex = new SQLRecoverableException(reason, state);
 110         assertTrue(ex.getMessage().equals(reason)
 111                 && ex.getSQLState().equals(state)
 112                 && ex.getCause() == null
 113                 && ex.getErrorCode() == 0);
 114     }
 115 
 116     /**
 117      * Create SQLRecoverableException with message, SQLState, and error code
 118      */
 119     @Test
 120     public void test4() {
 121         SQLRecoverableException ex =
 122                 new SQLRecoverableException(reason, state, errorCode);
 123         assertTrue(ex.getMessage().equals(reason)
 124                 && ex.getSQLState().equals(state)
 125                 && ex.getCause() == null
 126                 && ex.getErrorCode() == errorCode);
 127     }
 128 
 129     /**
 130      * Create SQLRecoverableException with message, SQLState, errorCode, and Throwable
 131      */
 132     @Test
 133     public void test5() {
 134         SQLRecoverableException ex =
 135                 new SQLRecoverableException(reason, state, errorCode, t);
 136         assertTrue(ex.getMessage().equals(reason)
 137                 && ex.getSQLState().equals(state)
 138                 && cause.equals(ex.getCause().toString())
 139                 && ex.getErrorCode() == errorCode);
 140     }
 141 
 142     /**
 143      * Create SQLRecoverableException with message, SQLState, and Throwable
 144      */
 145     @Test
 146     public void test6() {
 147         SQLRecoverableException ex = new SQLRecoverableException(reason, state, t);
 148         assertTrue(ex.getMessage().equals(reason)
 149                 && ex.getSQLState().equals(state)
 150                 && cause.equals(ex.getCause().toString())
 151                 && ex.getErrorCode() == 0);
 152     }
 153 
 154     /**
 155      * Create SQLRecoverableException with message, and Throwable
 156      */
 157     @Test
 158     public void test7() {
 159         SQLRecoverableException ex = new SQLRecoverableException(reason, t);
 160         assertTrue(ex.getMessage().equals(reason)
 161                 && ex.getSQLState() == null
 162                 && cause.equals(ex.getCause().toString())
 163                 && ex.getErrorCode() == 0);
 164     }
 165 
 166     /**
 167      * Create SQLRecoverableException with null Throwable
 168      */
 169     @Test
 170     public void test8() {
 171         SQLRecoverableException ex = new SQLRecoverableException((Throwable)null);
 172         assertTrue(ex.getMessage() == null
 173                 && ex.getSQLState() == null
 174                 && ex.getCause() == null
 175                 && ex.getErrorCode() == 0);
 176     }
 177 
 178     /**
 179      * Create SQLRecoverableException with Throwable
 180      */
 181     @Test
 182     public void test9() {
 183         SQLRecoverableException ex = new SQLRecoverableException(t);
 184         assertTrue(ex.getMessage().equals(cause)
 185                 && ex.getSQLState() == null
 186                 && cause.equals(ex.getCause().toString())
 187                 && ex.getErrorCode() == 0);
 188     }
 189 
 190     /**
 191      * Serialize a SQLRecoverableException and make sure you can read it back properly
 192      */
 193     @Test
 194     public void test10() throws Exception {
 195         SQLRecoverableException e =
 196                 new SQLRecoverableException(reason, state, errorCode, t);
 197         ObjectOutputStream out
 198                 = new ObjectOutputStream(
 199                         new FileOutputStream("SQLRecoverableException.ser"));
 200         out.writeObject(e);
 201         ObjectInputStream is = new ObjectInputStream(
 202                 new FileInputStream("SQLRecoverableException.ser"));
 203         SQLRecoverableException ex1 = (SQLRecoverableException) is.readObject();
 204         assertTrue(reason.equals(ex1.getMessage())
 205                 && ex1.getSQLState().equals(state)
 206                 && cause.equals(ex1.getCause().toString())
 207                 && ex1.getErrorCode() == errorCode);
 208     }
 209 
 210     /**
 211      * Validate that the ordering of the returned Exceptions is correct
 212      * using for-each loop
 213      */
 214     @Test
 215     public void test11() {
 216         SQLRecoverableException ex = new SQLRecoverableException("Exception 1", t1);
 217         SQLRecoverableException ex1 = new SQLRecoverableException("Exception 2");
 218         SQLRecoverableException ex2 = new SQLRecoverableException("Exception 3", t2);
 219         ex.setNextException(ex1);
 220         ex.setNextException(ex2);
 221         int num = 0;
 222         for (Throwable e : ex) {
 223             assertTrue(msgs[num++].equals(e.getMessage()));
 224         }
 225     }
 226 
 227     /**
 228      * Validate that the ordering of the returned Exceptions is correct
 229      * using traditional while loop
 230      */
 231     @Test
 232     public void test12() {
 233         SQLRecoverableException ex = new SQLRecoverableException("Exception 1", t1);
 234         SQLRecoverableException ex1 = new SQLRecoverableException("Exception 2");
 235         SQLRecoverableException ex2 = new SQLRecoverableException("Exception 3", t2);
 236         ex.setNextException(ex1);
 237         ex.setNextException(ex2);
 238         int num = 0;
 239         SQLException sqe = ex;
 240         while (sqe != null) {
 241             assertTrue(msgs[num++].equals(sqe.getMessage()));
 242             Throwable c = sqe.getCause();
 243             while (c != null) {
 244                 assertTrue(msgs[num++].equals(c.getMessage()));
 245                 c = c.getCause();
 246             }
 247             sqe = sqe.getNextException();
 248         }
 249     }
 250 }