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