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