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.SQLDataException; 30 import java.sql.SQLException; 31 import java.sql.SQLNonTransientException; 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 SQLDataExceptionTests { 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 SQLDataExceptionTests() { 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 SQLDataException and setting all objects to null 72 */ 73 @Test 74 public void test() { 75 SQLDataException e = new SQLDataException(null, null, errorCode, null); 76 assertTrue(e.getMessage() == null && e.getSQLState() == null 77 && e.getCause() == null && e.getErrorCode() == errorCode); 78 } 79 80 /** 81 * Create SQLDataException with no-arg constructor 82 */ 83 @Test 84 public void test1() { 85 SQLDataException ex = new SQLDataException(); 86 assertTrue(ex.getMessage() == null 87 && ex.getSQLState() == null 88 && ex.getCause() == null 89 && ex.getErrorCode() == 0); 90 } 91 92 /** 93 * Create SQLDataException with message 94 */ 95 @Test 96 public void test2() { 97 SQLDataException ex = new SQLDataException(reason); 98 assertTrue(ex.getMessage().equals(reason) 99 && ex.getSQLState() == null 100 && ex.getCause() == null 101 && ex.getErrorCode() == 0); 102 } 103 104 /** 105 * Create SQLDataException with message, and SQLState 106 */ 107 @Test 108 public void test3() { 109 SQLDataException ex = new SQLDataException(reason, state); 110 assertTrue(ex.getMessage().equals(reason) 111 && ex.getSQLState().equals(state) 112 && ex.getCause() == null 113 && ex.getErrorCode() == 0); 114 } 115 116 /** 117 * Create SQLDataException with message, SQLState, and error code 118 */ 119 @Test 120 public void test4() { 121 SQLDataException ex = new SQLDataException(reason, state, errorCode); 122 assertTrue(ex.getMessage().equals(reason) 123 && ex.getSQLState().equals(state) 124 && ex.getCause() == null 125 && ex.getErrorCode() == errorCode); 126 } 127 128 /** 129 * Create SQLDataException with message, SQLState, errorCode, and Throwable 130 */ 131 @Test 132 public void test5() { 133 SQLDataException ex = new SQLDataException(reason, state, errorCode, t); 134 assertTrue(ex.getMessage().equals(reason) 135 && ex.getSQLState().equals(state) 136 && cause.equals(ex.getCause().toString()) 137 && ex.getErrorCode() == errorCode); 138 } 139 140 /** 141 * Create SQLDataException with message, SQLState, and Throwable 142 */ 143 @Test 144 public void test6() { 145 SQLDataException ex = new SQLDataException(reason, state, t); 146 assertTrue(ex.getMessage().equals(reason) 147 && ex.getSQLState().equals(state) 148 && cause.equals(ex.getCause().toString()) 149 && ex.getErrorCode() == 0); 150 } 151 152 /** 153 * Create SQLDataException with message, and Throwable 154 */ 155 @Test 156 public void test7() { 157 SQLDataException ex = new SQLDataException(reason, t); 158 assertTrue(ex.getMessage().equals(reason) 159 && ex.getSQLState() == null 160 && cause.equals(ex.getCause().toString()) 161 && ex.getErrorCode() == 0); 162 } 163 164 /** 165 * Create SQLDataException with null Throwable 166 */ 167 @Test 168 public void test8() { 169 SQLDataException ex = new SQLDataException((Throwable)null); 170 assertTrue(ex.getMessage() == null 171 && ex.getSQLState() == null 172 && ex.getCause() == null 173 && ex.getErrorCode() == 0); 174 } 175 176 /** 177 * Create SQLDataException with Throwable 178 */ 179 @Test 180 public void test9() { 181 SQLDataException ex = new SQLDataException(t); 182 assertTrue(ex.getMessage().equals(cause) 183 && ex.getSQLState() == null 184 && cause.equals(ex.getCause().toString()) 185 && ex.getErrorCode() == 0); 186 } 187 188 /** 189 * Serialize a SQLDataException and make sure you can read it back properly 190 */ 191 @Test 192 public void test10() throws Exception { 193 SQLDataException e = new SQLDataException(reason, state, errorCode, t); 194 ObjectOutputStream out 195 = new ObjectOutputStream( 196 new FileOutputStream("SQLDataException.ser")); 197 out.writeObject(e); 198 ObjectInputStream is = new ObjectInputStream( 199 new FileInputStream("SQLDataException.ser")); 200 SQLDataException ex1 = (SQLDataException) is.readObject(); 201 assertTrue(reason.equals(ex1.getMessage()) 202 && ex1.getSQLState().equals(state) 203 && cause.equals(ex1.getCause().toString()) 204 && ex1.getErrorCode() == errorCode); 205 } 206 207 /** 208 * Validate that the ordering of the returned Exceptions is correct 209 * using for-each loop 210 */ 211 @Test 212 public void test11() { 213 SQLDataException ex = new SQLDataException("Exception 1", t1); 214 SQLDataException ex1 = new SQLDataException("Exception 2"); 215 SQLDataException ex2 = new SQLDataException("Exception 3", t2); 216 ex.setNextException(ex1); 217 ex.setNextException(ex2); 218 int num = 0; 219 for (Throwable e : ex) { 220 assertTrue(msgs[num++].equals(e.getMessage())); 221 } 222 } 223 224 /** 225 * Validate that the ordering of the returned Exceptions is correct 226 * using traditional while loop 227 */ 228 @Test 229 public void test12() { 230 SQLDataException ex = new SQLDataException("Exception 1", t1); 231 SQLDataException ex1 = new SQLDataException("Exception 2"); 232 SQLDataException ex2 = new SQLDataException("Exception 3", t2); 233 ex.setNextException(ex1); 234 ex.setNextException(ex2); 235 int num = 0; 236 SQLException sqe = ex; 237 while (sqe != null) { 238 assertTrue(msgs[num++].equals(sqe.getMessage())); 239 Throwable c = sqe.getCause(); 240 while (c != null) { 241 assertTrue(msgs[num++].equals(c.getMessage())); 242 c = c.getCause(); 243 } 244 sqe = sqe.getNextException(); 245 } 246 } 247 248 /** 249 * Create SQLDataException and validate it is an instance of 250 * SQLNonTransientException 251 */ 252 @Test 253 public void test13() { 254 Exception ex = new SQLDataException(); 255 assertTrue(ex instanceof SQLNonTransientException); 256 } 257 }