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.SQLTransientException;
  27 import static org.testng.Assert.*;




  28 import org.testng.annotations.Test;
  29 import util.BaseTest;
  30 
  31 public class SQLTransientExceptionTests extends BaseTest {





























  32 
  33     /**
  34      * Create SQLTransientException and setting all objects to null
  35      */
  36     @Test
  37     public void test() {
  38         SQLTransientException e = new SQLTransientException(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 SQLTransientException with no-arg constructor
  46      */
  47     @Test
  48     public void test1() {
  49         SQLTransientException ex = new SQLTransientException();
  50         assertTrue(ex.getMessage() == null
  51                 && ex.getSQLState() == null
  52                 && ex.getCause() == null
  53                 && ex.getErrorCode() == 0);
  54     }
  55 
  56     /**
  57      * Create SQLTransientException with message
  58      */
  59     @Test
  60     public void test2() {
  61         SQLTransientException ex = new SQLTransientException(reason);
  62         assertTrue(ex.getMessage().equals(reason)
  63                 && ex.getSQLState() == null
  64                 && ex.getCause() == null
  65                 && ex.getErrorCode() == 0);
  66     }
  67 
  68     /**
  69      * Create SQLTransientException with message, and SQLState
  70      */
  71     @Test
  72     public void test3() {
  73         SQLTransientException ex = new SQLTransientException(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 SQLTransientException with message, SQLState, and error code
  82      */
  83     @Test
  84     public void test4() {
  85         SQLTransientException ex = new SQLTransientException(reason, state, errorCode);
  86         assertTrue(ex.getMessage().equals(reason)
  87                 && ex.getSQLState().equals(state)
  88                 && ex.getCause() == null
  89                 && ex.getErrorCode() == errorCode);
  90     }
  91 
  92     /**
  93      * Create SQLTransientException with message, SQLState, errorCode, and Throwable
  94      */
  95     @Test
  96     public void test5() {
  97         SQLTransientException ex =
  98                 new SQLTransientException(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 SQLTransientException with message, SQLState, and Throwable
 107      */
 108     @Test
 109     public void test6() {
 110         SQLTransientException ex = new SQLTransientException(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 SQLTransientException with message, and Throwable
 119      */
 120     @Test
 121     public void test7() {
 122         SQLTransientException ex = new SQLTransientException(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 SQLTransientException with null Throwable
 131      */
 132     @Test
 133     public void test8() {
 134         SQLTransientException ex = new SQLTransientException((Throwable)null);
 135         assertTrue(ex.getMessage() == null
 136                 && ex.getSQLState() == null
 137                 && ex.getCause() == null
 138                 && ex.getErrorCode() == 0);
 139     }
 140 
 141     /**
 142      * Create SQLTransientException with Throwable
 143      */
 144     @Test
 145     public void test9() {
 146         SQLTransientException ex = new SQLTransientException(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 SQLTransientException and make sure you can read it back properly
 155      */
 156     @Test
 157     public void test10() throws Exception {
 158         SQLTransientException e =
 159                 new SQLTransientException(reason, state, errorCode, t);
 160         SQLTransientException ex1 = createSerializedException(e);






 161         assertTrue(reason.equals(ex1.getMessage())
 162                 && ex1.getSQLState().equals(state)
 163                 && cause.equals(ex1.getCause().toString())
 164                 && ex1.getErrorCode() == errorCode);
 165     }
 166 
 167     /**
 168      * Validate that the ordering of the returned Exceptions is correct
 169      * using for-each loop
 170      */
 171     @Test
 172     public void test11() {
 173         SQLTransientException ex = new SQLTransientException("Exception 1", t1);
 174         SQLTransientException ex1 = new SQLTransientException("Exception 2");
 175         SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
 176         ex.setNextException(ex1);
 177         ex.setNextException(ex2);
 178         int num = 0;
 179         for (Throwable e : ex) {
 180             assertTrue(msgs[num++].equals(e.getMessage()));
 181         }
 182     }
 183 
 184     /**
 185      * Validate that the ordering of the returned Exceptions is correct
 186      * using traditional while loop
 187      */
 188     @Test
 189     public void test12() {
 190         SQLTransientException ex = new SQLTransientException("Exception 1", t1);
 191         SQLTransientException ex1 = new SQLTransientException("Exception 2");
 192         SQLTransientException ex2 = new SQLTransientException("Exception 3", t2);
 193         ex.setNextException(ex1);
 194         ex.setNextException(ex2);
 195         int num = 0;
 196         SQLException sqe = ex;
 197         while (sqe != null) {
 198             assertTrue(msgs[num++].equals(sqe.getMessage()));
 199             Throwable c = sqe.getCause();
 200             while (c != null) {
 201                 assertTrue(msgs[num++].equals(c.getMessage()));
 202                 c = c.getCause();
 203             }
 204             sqe = sqe.getNextException();
 205         }
 206     }
 207 }
--- EOF ---