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