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