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