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.io.FileInputStream; 26 import java.io.FileOutputStream; 27 import java.io.ObjectInputStream; 28 import java.io.ObjectOutputStream; 29 import java.sql.SQLException; 30 import java.sql.SQLTransientConnectionException; 31 import java.sql.SQLTransientException; 32 import static org.testng.Assert.*; 33 import org.testng.annotations.AfterClass; 34 import org.testng.annotations.AfterMethod; 35 import org.testng.annotations.BeforeClass; 36 import org.testng.annotations.BeforeMethod; 37 import org.testng.annotations.Test; 38 39 public class SQLTransientConnectionExceptionTests { 40 41 private final String reason = "reason"; 42 private final String state = "SQLState"; 43 private final String cause = "java.lang.Throwable: cause"; 44 private final Throwable t = new Throwable("cause"); 45 private final Throwable t1 = new Throwable("cause 1"); 46 private final Throwable t2 = new Throwable("cause 2"); 47 private final int errorCode = 21; 48 private final String[] msgs = {"Exception 1", "cause 1", "Exception 2", 49 "Exception 3", "cause 2"}; 50 51 public SQLTransientConnectionExceptionTests() { 52 } 53 54 @BeforeClass 55 public static void setUpClass() throws Exception { 56 } 57 58 @AfterClass 59 public static void tearDownClass() throws Exception { 60 } 61 62 @BeforeMethod 63 public void setUpMethod() throws Exception { 64 } 65 66 @AfterMethod 67 public void tearDownMethod() throws Exception { 68 } 69 70 /** 71 * Create SQLTransientConnectionException and setting all objects to null 72 */ 73 @Test 74 public void test() { 75 SQLTransientConnectionException e = 76 new SQLTransientConnectionException( null, 77 null, errorCode, null); 78 assertTrue(e.getMessage() == null && e.getSQLState() == null 79 && e.getCause() == null && e.getErrorCode() == errorCode); 80 } 81 82 /** 83 * Create SQLTransientConnectionException with no-arg constructor 84 */ 85 @Test 86 public void test1() { 87 SQLTransientConnectionException ex = new SQLTransientConnectionException(); 88 assertTrue(ex.getMessage() == null 185 /** 186 * Create SQLTransientConnectionException with Throwable 187 */ 188 @Test 189 public void test9() { 190 SQLTransientConnectionException ex = 191 new SQLTransientConnectionException(t); 192 assertTrue(ex.getMessage().equals(cause) 193 && ex.getSQLState() == null 194 && cause.equals(ex.getCause().toString()) 195 && ex.getErrorCode() == 0); 196 } 197 198 /** 199 * Serialize a SQLTransientConnectionException and make sure you can read it back properly 200 */ 201 @Test 202 public void test10() throws Exception { 203 SQLTransientConnectionException e = 204 new SQLTransientConnectionException(reason, state, errorCode, t); 205 ObjectOutputStream out 206 = new ObjectOutputStream( 207 new FileOutputStream("SQLTransientConnectionException.ser")); 208 out.writeObject(e); 209 ObjectInputStream is = new ObjectInputStream( 210 new FileInputStream("SQLTransientConnectionException.ser")); 211 SQLTransientConnectionException ex1 = (SQLTransientConnectionException) is.readObject(); 212 assertTrue(reason.equals(ex1.getMessage()) 213 && ex1.getSQLState().equals(state) 214 && cause.equals(ex1.getCause().toString()) 215 && ex1.getErrorCode() == errorCode); 216 } 217 218 /** 219 * Validate that the ordering of the returned Exceptions is correct 220 * using for-each loop 221 */ 222 @Test 223 public void test11() { 224 SQLTransientConnectionException ex = 225 new SQLTransientConnectionException("Exception 1", t1); 226 SQLTransientConnectionException ex1 = 227 new SQLTransientConnectionException("Exception 2"); 228 SQLTransientConnectionException ex2 = 229 new SQLTransientConnectionException("Exception 3", t2); 230 ex.setNextException(ex1); 231 ex.setNextException(ex2); | 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.SQLTransientConnectionException; 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 SQLTransientConnectionExceptionTests extends BaseTest { 33 34 /** 35 * Create SQLTransientConnectionException and setting all objects to null 36 */ 37 @Test 38 public void test() { 39 SQLTransientConnectionException e = 40 new SQLTransientConnectionException( 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 SQLTransientConnectionException with no-arg constructor 48 */ 49 @Test 50 public void test1() { 51 SQLTransientConnectionException ex = new SQLTransientConnectionException(); 52 assertTrue(ex.getMessage() == null 149 /** 150 * Create SQLTransientConnectionException with Throwable 151 */ 152 @Test 153 public void test9() { 154 SQLTransientConnectionException ex = 155 new SQLTransientConnectionException(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 SQLTransientConnectionException and make sure you can read it back properly 164 */ 165 @Test 166 public void test10() throws Exception { 167 SQLTransientConnectionException e = 168 new SQLTransientConnectionException(reason, state, errorCode, t); 169 SQLTransientConnectionException ex1 = 170 createSerializedException(e, "SQLTransientConnectionException.ser"); 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 SQLTransientConnectionException ex = 184 new SQLTransientConnectionException("Exception 1", t1); 185 SQLTransientConnectionException ex1 = 186 new SQLTransientConnectionException("Exception 2"); 187 SQLTransientConnectionException ex2 = 188 new SQLTransientConnectionException("Exception 3", t2); 189 ex.setNextException(ex1); 190 ex.setNextException(ex2); |