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