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