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.ByteArrayInputStream; 26 import java.io.File; 27 import java.io.ObjectInputStream; 28 import java.sql.BatchUpdateException; 29 import java.sql.SQLException; 30 import java.util.Arrays; 31 import static org.testng.Assert.*; 32 import org.testng.annotations.Test; 33 import util.SerializedBatchUpdateException; 34 import util.BaseTest; 35 36 public class BatchUpdateExceptionTests extends BaseTest { 37 38 private final int[] uc = {1, 2, 3}; 39 private final long[] luc = {1, 2, 3}; 40 41 private final String testSrcDir = System.getProperty("test.src", ".") 42 + File.separatorChar; 43 44 /** 45 * Create BatchUpdateException and setting all objects to null 46 */ 47 @Test 48 public void test() { 49 BatchUpdateException be = new BatchUpdateException(null, 50 null, errorCode, (int[]) null, null); 51 assertTrue(be.getMessage() == null && be.getSQLState() == null 52 && be.getUpdateCounts() == null && be.getCause() == null 53 && be.getLargeUpdateCounts() == null 54 && be.getErrorCode() == errorCode); 55 } 56 57 /** 58 * Create BatchUpdateException with no-arg constructor 59 */ 60 @Test 61 public void test1() { 62 BatchUpdateException ex = new BatchUpdateException(); 63 assertTrue(ex.getMessage() == null 64 && ex.getSQLState() == null 65 && ex.getCause() == null 66 && ex.getErrorCode() == 0 67 && ex.getUpdateCounts() == null 68 && ex.getLargeUpdateCounts() == null); 69 } 70 71 /** 72 * Create BatchUpdateException with null Throwable 73 */ 74 @Test 75 public void test2() { 76 BatchUpdateException ex = new BatchUpdateException((Throwable) null); 77 assertTrue(ex.getMessage() == null 78 && ex.getSQLState() == null 79 && ex.getCause() == null 80 && ex.getErrorCode() == 0 81 && ex.getUpdateCounts() == null 82 && ex.getLargeUpdateCounts() == null); 83 } 84 85 /** 86 * Create BatchUpdateException with message and update counts 87 */ 88 @Test 89 public void test3() { 90 91 BatchUpdateException ex = new BatchUpdateException(reason, uc); 92 assertTrue(ex.getMessage().equals(reason) 93 && ex.getSQLState() == null 94 && ex.getCause() == null 95 && ex.getErrorCode() == 0 96 && Arrays.equals(ex.getUpdateCounts(), uc) 97 && Arrays.equals(ex.getLargeUpdateCounts(), luc) 98 ); 99 } 100 101 /** 102 * Create BatchUpdateException with update counts 103 */ 104 @Test 105 public void test4() { 106 BatchUpdateException ex = new BatchUpdateException(uc); 107 assertTrue(ex.getMessage() == null 108 && ex.getSQLState() == null 109 && ex.getCause() == null 110 && ex.getErrorCode() == 0 111 && Arrays.equals(ex.getUpdateCounts(), uc) 112 && Arrays.equals(ex.getLargeUpdateCounts(), luc) 113 ); 114 } 115 116 /** 117 * Create BatchUpdateException with Throwable and update counts 118 */ 119 @Test 120 public void test5() { 121 BatchUpdateException ex = new BatchUpdateException(uc, t); 122 assertTrue(ex.getMessage().equals(cause) 123 && ex.getSQLState() == null 124 && cause.equals(ex.getCause().toString()) 125 && ex.getErrorCode() == 0 126 && Arrays.equals(ex.getUpdateCounts(), uc) 127 && Arrays.equals(ex.getLargeUpdateCounts(), luc) 128 ); 129 } 130 131 /** 132 * Create BatchUpdateException with message, Throwable, and update counts 133 */ 134 @Test 135 public void test6() { 136 BatchUpdateException ex = new BatchUpdateException(reason, uc, t); 137 assertTrue(ex.getMessage().equals(reason) 138 && ex.getSQLState() == null 139 && cause.equals(ex.getCause().toString()) 140 && ex.getErrorCode() == 0 141 && Arrays.equals(ex.getUpdateCounts(), uc) 142 && Arrays.equals(ex.getLargeUpdateCounts(), luc) 143 ); 144 } 145 146 /** 147 * Create BatchUpdateException with message, SQLState, Throwable, and update 148 * counts 149 */ 150 @Test 151 public void test7() { 152 BatchUpdateException ex = new BatchUpdateException(reason, state, uc, t); 153 assertTrue(ex.getMessage().equals(reason) 154 && ex.getSQLState().equals(state) 155 && cause.equals(ex.getCause().toString()) 156 && ex.getErrorCode() == 0 157 && Arrays.equals(ex.getUpdateCounts(), uc) 158 && Arrays.equals(ex.getLargeUpdateCounts(), luc) 159 ); 160 } 161 162 /** 163 * Create BatchUpdateException with message, SQLState, errorCode code 164 * Throwable, and update counts 165 */ 166 @Test 167 public void test8() { 168 BatchUpdateException ex = new BatchUpdateException(reason, state, errorCode, 169 uc, t); 170 assertTrue(ex.getMessage().equals(reason) 171 && ex.getSQLState().equals(state) 172 && cause.equals(ex.getCause().toString()) 173 && ex.getErrorCode() == errorCode 174 && Arrays.equals(ex.getUpdateCounts(), uc) 175 && Arrays.equals(ex.getLargeUpdateCounts(), luc) 176 ); 177 } 178 179 /** 180 * Create BatchUpdateException with message, SQLState, errorCode code 181 * Throwable, and long [] update counts 182 */ 183 @Test 184 public void test9() { 185 BatchUpdateException ex = new BatchUpdateException(reason, state, errorCode, 186 luc, t); 187 assertTrue(ex.getMessage().equals(reason) 188 && ex.getSQLState().equals(state) 189 && cause.equals(ex.getCause().toString()) 190 && ex.getErrorCode() == errorCode 191 && Arrays.equals(ex.getUpdateCounts(), uc) 192 && Arrays.equals(ex.getLargeUpdateCounts(), luc) 193 ); 194 } 195 196 /** 197 * Validate that a copy of the update counts array is made 198 */ 199 @Test 200 public void test10() { 201 int[] uc1 = {1, 2}; 202 BatchUpdateException ex = new BatchUpdateException(uc1); 203 assertTrue(Arrays.equals(ex.getUpdateCounts(), uc1)); 204 uc1[0] = 6689; 205 assertFalse(Arrays.equals(ex.getUpdateCounts(), uc1)); 206 } 207 208 /** 209 * Validate that if null is specified for the update count, it is returned 210 * as null 211 */ 212 @Test 213 public void test11() { 214 BatchUpdateException ex = new BatchUpdateException((int[]) null); 215 assertTrue(ex.getMessage() == null && ex.getSQLState() == null 216 && ex.getErrorCode() == 0 && ex.getUpdateCounts() == null 217 && ex.getLargeUpdateCounts() == null); 218 } 219 220 /** 221 * Serialize a BatchUpdateException and make sure you can read it back 222 * properly 223 */ 224 @Test 225 public void test12() throws Exception { 226 BatchUpdateException be = new BatchUpdateException(reason, state, errorCode, 227 uc, t); 228 BatchUpdateException bue 229 = createSerializedException(be); 230 assertTrue(reason.equals(bue.getMessage()) 231 && bue.getSQLState().equals(state) 232 && cause.equals(bue.getCause().toString()) 233 && bue.getErrorCode() == errorCode 234 && Arrays.equals(bue.getLargeUpdateCounts(), luc) 235 && Arrays.equals(bue.getUpdateCounts(), uc)); 236 } 237 238 239 240 /** 241 * De-Serialize a BatchUpdateException from JDBC 4.0 and make sure you can 242 * read it back properly 243 */ 244 @Test 245 public void test13() throws Exception { 246 String reason1 = "This was the error msg"; 247 String state1 = "user defined sqlState"; 248 String cause1 = "java.lang.Throwable: throw 1"; 249 int errorCode1 = 99999; 250 Throwable t = new Throwable("throw 1"); 251 int[] uc1 = {1, 2, 21}; 252 long[] luc1 = {1, 2, 21}; 253 254 ObjectInputStream ois = new ObjectInputStream( 255 new ByteArrayInputStream(SerializedBatchUpdateException.DATA)); 256 BatchUpdateException bue = (BatchUpdateException) ois.readObject(); 257 assertTrue(reason1.equals(bue.getMessage()) 258 && bue.getSQLState().equals(state1) 259 && bue.getErrorCode() == errorCode1 260 && cause1.equals(bue.getCause().toString()) 261 && Arrays.equals(bue.getLargeUpdateCounts(), luc1) 262 && Arrays.equals(bue.getUpdateCounts(), uc1)); 263 } 264 265 /** 266 * Serialize a BatchUpdateException with an Integer.MAX_VALUE + 1 and 267 * validate you can read it back properly 268 */ 269 @Test 270 public void test14() throws Exception { 271 int[] uc1 = {Integer.MAX_VALUE, Integer.MAX_VALUE + 1}; 272 long[] luc1 = {Integer.MAX_VALUE, Integer.MAX_VALUE + 1}; 273 BatchUpdateException be = new BatchUpdateException(reason, state, errorCode, 274 luc1, t); 275 BatchUpdateException bue 276 = createSerializedException(be); 277 assertTrue(reason.equals(bue.getMessage()) 278 && bue.getSQLState().equals(state) 279 && cause.equals(bue.getCause().toString()) 280 && bue.getErrorCode() == errorCode 281 && Arrays.equals(bue.getLargeUpdateCounts(), luc1) 282 && Arrays.equals(bue.getUpdateCounts(), uc1)); 283 } 284 285 /** 286 * Validate that the ordering of the returned Exceptions is correct 287 * using for-each loop 288 */ 289 @Test 290 public void test15() { 291 BatchUpdateException ex = new BatchUpdateException("Exception 1", uc, t1); 292 BatchUpdateException ex1 = new BatchUpdateException("Exception 2", uc); 293 BatchUpdateException ex2 = new BatchUpdateException("Exception 3", uc, t2); 294 ex.setNextException(ex1); 295 ex.setNextException(ex2); 296 int num = 0; 297 for (Throwable e : ex) { 298 assertTrue(msgs[num++].equals(e.getMessage())); 299 } 300 } 301 302 /** 303 * Validate that the ordering of the returned Exceptions is correct 304 * using traditional while loop 305 */ 306 @Test 307 public void test16() { 308 BatchUpdateException ex = new BatchUpdateException("Exception 1", uc, t1); 309 BatchUpdateException ex1 = new BatchUpdateException("Exception 2", uc); 310 BatchUpdateException ex2 = new BatchUpdateException("Exception 3", uc, t2); 311 ex.setNextException(ex1); 312 ex.setNextException(ex2); 313 SQLException sqe = ex; 314 int num = 0; 315 while (sqe != null) { 316 assertTrue(msgs[num++].equals(sqe.getMessage())); 317 Throwable c = sqe.getCause(); 318 while (c != null) { 319 assertTrue(msgs[num++].equals(c.getMessage())); 320 c = c.getCause(); 321 } 322 sqe = sqe.getNextException(); 323 } 324 } 325 326 }