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