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 util; 24 25 import java.io.ByteArrayInputStream; 26 import java.io.ByteArrayOutputStream; 27 import java.io.IOException; 28 import java.io.ObjectInputStream; 29 import java.io.ObjectOutputStream; 30 import java.sql.SQLException; 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 36 public class BaseTest { 37 38 protected final String reason = "reason"; 39 protected final String state = "SQLState"; 40 protected final String cause = "java.lang.Throwable: cause"; 41 protected final Throwable t = new Throwable("cause"); 42 protected final Throwable t1 = new Throwable("cause 1"); 43 protected final Throwable t2 = new Throwable("cause 2"); 44 protected final int errorCode = 21; 45 protected final String[] msgs = {"Exception 1", "cause 1", "Exception 2", 46 "Exception 3", "cause 2"}; 47 48 @BeforeClass 49 public static void setUpClass() throws Exception { 50 } 51 52 @AfterClass 53 public static void tearDownClass() throws Exception { 54 } 55 56 @BeforeMethod 57 public void setUpMethod() throws Exception { 58 } 59 60 @AfterMethod 61 public void tearDownMethod() throws Exception { 62 } 63 64 /* 65 * Take some form of SQLException, serialize and deserialize it 66 */ 67 @SuppressWarnings("unchecked") 68 protected <T extends SQLException> T 69 createSerializedException(T ex) 70 throws IOException, ClassNotFoundException { 71 return (T) serializeDeserializeObject(ex); 72 } 73 74 /* 75 * Utility method to serialize and deserialize an object 76 */ 77 @SuppressWarnings("unchecked") 78 protected <T> T serializeDeserializeObject(T o) 79 throws IOException, ClassNotFoundException { 80 T o1; 81 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 82 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { 83 oos.writeObject(o); 84 } 85 try (ObjectInputStream ois 86 = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { 87 o1 = (T) ois.readObject(); 88 } 89 return o1; 90 } 91 }