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