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.security.Policy; 31 import java.sql.JDBCType; 32 import java.sql.SQLException; 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.DataProvider; 38 39 public class BaseTest { 40 41 protected final String reason = "reason"; 42 protected final String state = "SQLState"; 43 protected final String cause = "java.lang.Throwable: cause"; 44 protected final Throwable t = new Throwable("cause"); 45 protected final Throwable t1 = new Throwable("cause 1"); 46 protected final Throwable t2 = new Throwable("cause 2"); 47 protected final int errorCode = 21; 48 protected final String[] msgs = {"Exception 1", "cause 1", "Exception 2", 49 "Exception 3", "cause 2"}; 50 51 @BeforeClass 52 public static void setUpClass() throws Exception { 53 } 54 55 @AfterClass 56 public static void tearDownClass() throws Exception { 57 } 58 59 @BeforeMethod 60 public void setUpMethod() throws Exception { 61 } 62 63 @AfterMethod 64 public void tearDownMethod() throws Exception { 65 } 66 67 /* 68 * Take some form of SQLException, serialize and deserialize it 69 */ 70 @SuppressWarnings("unchecked") 71 protected <T extends SQLException> T 72 createSerializedException(T ex) 73 throws IOException, ClassNotFoundException { 74 return (T) serializeDeserializeObject(ex); 75 } 76 77 /* 78 * Utility method to serialize and deserialize an object 79 */ 80 @SuppressWarnings("unchecked") 81 protected <T> T serializeDeserializeObject(T o) 82 throws IOException, ClassNotFoundException { 83 T o1; 84 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 85 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { 86 oos.writeObject(o); 87 } 88 try (ObjectInputStream ois 89 = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { 90 o1 = (T) ois.readObject(); 91 } 92 return o1; 93 } 94 95 /* 96 * Utility Method used to set the current Policy 97 */ 98 protected static void setPolicy(Policy p) { 99 Policy.setPolicy(p); 100 } 101 102 /* 103 * DataProvider used to specify the value to set and check for 104 * methods using boolean values 105 */ 106 @DataProvider(name = "trueFalse") 107 protected Object[][] trueFalse() { 108 return new Object[][]{ 109 {true}, 110 {false} 111 }; 112 } 113 114 /* 115 * DataProvider used to specify the standard JDBC Types 116 */ 117 @DataProvider(name = "jdbcTypes") 118 protected Object[][] jdbcTypes() { 119 Object[][] o = new Object[JDBCType.values().length][1]; 120 int pos = 0; 121 for (JDBCType c : JDBCType.values()) { 122 o[pos++][0] = c.getVendorTypeNumber(); 123 } 124 return o; 125 } 126 }