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.SQLTimeoutException;
  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 SQLTimeoutExceptionTests {
  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 SQLTimeoutExceptionTests() {
  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 SQLTimeoutException and setting all objects to null
  72      */
  73     @Test
  74     public void test() {
  75         SQLTimeoutException e = new SQLTimeoutException(null,
  76                 null, errorCode, null);
  77         assertTrue(e.getMessage() == null && e.getSQLState() == null
  78                 && e.getCause() == null && e.getErrorCode() == errorCode);
  79     }
  80 
  81     /**
  82      * Create SQLTimeoutException with no-arg constructor
  83      */
  84     @Test
  85     public void test1() {
  86         SQLTimeoutException ex = new SQLTimeoutException();
  87         assertTrue(ex.getMessage() == null
  88                 && ex.getSQLState() == null
  89                 && ex.getCause() == null
  90                 && ex.getErrorCode() == 0);
  91     }
  92 
  93     /**
  94      * Create SQLTimeoutException with message
  95      */
  96     @Test
  97     public void test2() {
  98         SQLTimeoutException ex = new SQLTimeoutException(reason);
  99         assertTrue(ex.getMessage().equals(reason)
 100                 && ex.getSQLState() == null
 101                 && ex.getCause() == null
 102                 && ex.getErrorCode() == 0);
 103     }
 104 
 105     /**
 106      * Create SQLTimeoutException with message, and SQLState
 107      */
 108     @Test
 109     public void test3() {
 110         SQLTimeoutException ex = new SQLTimeoutException(reason, state);
 111         assertTrue(ex.getMessage().equals(reason)
 112                 && ex.getSQLState().equals(state)
 113                 && ex.getCause() == null
 114                 && ex.getErrorCode() == 0);
 115     }
 116 
 117     /**
 118      * Create SQLTimeoutException with message, SQLState, and error code
 119      */
 120     @Test
 121     public void test4() {
 122         SQLTimeoutException ex = new SQLTimeoutException(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 SQLTimeoutException with message, SQLState, errorCode, and Throwable
 131      */
 132     @Test
 133     public void test5() {
 134         SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode, t);
 135         assertTrue(ex.getMessage().equals(reason)
 136                 && ex.getSQLState().equals(state)
 137                 && cause.equals(ex.getCause().toString())
 138                 && ex.getErrorCode() == errorCode);
 139     }
 140 
 141     /**
 142      * Create SQLTimeoutException with message, SQLState, and Throwable
 143      */
 144     @Test
 145     public void test6() {
 146         SQLTimeoutException ex = new SQLTimeoutException(reason, state, t);
 147         assertTrue(ex.getMessage().equals(reason)
 148                 && ex.getSQLState().equals(state)
 149                 && cause.equals(ex.getCause().toString())
 150                 && ex.getErrorCode() == 0);
 151     }
 152 
 153     /**
 154      * Create SQLTimeoutException with message, and Throwable
 155      */
 156     @Test
 157     public void test7() {
 158         SQLTimeoutException ex = new SQLTimeoutException(reason, t);
 159         assertTrue(ex.getMessage().equals(reason)
 160                 && ex.getSQLState() == null
 161                 && cause.equals(ex.getCause().toString())
 162                 && ex.getErrorCode() == 0);
 163     }
 164 
 165     /**
 166      * Create SQLTimeoutException with null Throwable
 167      */
 168     @Test
 169     public void test8() {
 170         SQLTimeoutException ex = new SQLTimeoutException((Throwable)null);
 171         assertTrue(ex.getMessage() == null
 172                 && ex.getSQLState() == null
 173                 && ex.getCause() == null
 174                 && ex.getErrorCode() == 0);
 175     }
 176 
 177     /**
 178      * Create SQLTimeoutException with Throwable
 179      */
 180     @Test
 181     public void test9() {
 182         SQLTimeoutException ex = new SQLTimeoutException(t);
 183         assertTrue(ex.getMessage().equals(cause)
 184                 && ex.getSQLState() == null
 185                 && cause.equals(ex.getCause().toString())
 186                 && ex.getErrorCode() == 0);
 187     }
 188 
 189     /**
 190      * Serialize a SQLTimeoutException and make sure you can read it back properly
 191      */
 192     @Test
 193     public void test10() throws Exception {
 194         SQLTimeoutException e =
 195                 new SQLTimeoutException(reason, state, errorCode, t);
 196         ObjectOutputStream out
 197                 = new ObjectOutputStream(
 198                         new FileOutputStream("SQLTimeoutException.ser"));
 199         out.writeObject(e);
 200         ObjectInputStream is = new ObjectInputStream(
 201                 new FileInputStream("SQLTimeoutException.ser"));
 202         SQLTimeoutException ex1 = (SQLTimeoutException) is.readObject();
 203         assertTrue(reason.equals(ex1.getMessage())
 204                 && ex1.getSQLState().equals(state)
 205                 && cause.equals(ex1.getCause().toString())
 206                 && ex1.getErrorCode() == errorCode);
 207     }
 208 
 209     /**
 210      * Validate that the ordering of the returned Exceptions is correct
 211      * using for-each loop
 212      */
 213     @Test
 214     public void test11() {
 215         SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
 216         SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
 217         SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
 218         ex.setNextException(ex1);
 219         ex.setNextException(ex2);
 220         int num = 0;
 221         for (Throwable e : ex) {
 222             assertTrue(msgs[num++].equals(e.getMessage()));
 223         }
 224     }
 225 
 226     /**
 227      * Validate that the ordering of the returned Exceptions is correct
 228      * using traditional while loop
 229      */
 230     @Test
 231     public void test12() {
 232         SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
 233         SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
 234         SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
 235         ex.setNextException(ex1);
 236         ex.setNextException(ex2);
 237         int num = 0;
 238         SQLException sqe = ex;
 239         while (sqe != null) {
 240             assertTrue(msgs[num++].equals(sqe.getMessage()));
 241             Throwable c = sqe.getCause();
 242             while (c != null) {
 243                 assertTrue(msgs[num++].equals(c.getMessage()));
 244                 c = c.getCause();
 245             }
 246             sqe = sqe.getNextException();
 247         }
 248     }
 249 
 250     /**
 251      * Create SQLTimeoutException and validate it is an instance of
 252      * SQLNonTransientException
 253      */
 254     @Test
 255     public void test13() {
 256         Exception ex = new SQLTimeoutException();
 257         assertTrue(ex instanceof SQLTransientException);
 258     }
 259 }