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