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