Path: blob/master/test/jdk/java/sql/testng/util/BaseTest.java
41152 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package util;2324import java.io.ByteArrayInputStream;25import java.io.ByteArrayOutputStream;26import java.io.IOException;27import java.io.ObjectInputStream;28import java.io.ObjectOutputStream;29import java.security.Policy;30import java.sql.JDBCType;31import java.sql.SQLException;32import org.testng.annotations.AfterClass;33import org.testng.annotations.AfterMethod;34import org.testng.annotations.BeforeClass;35import org.testng.annotations.BeforeMethod;36import org.testng.annotations.DataProvider;3738public class BaseTest {3940protected final String reason = "reason";41protected final String state = "SQLState";42protected final String cause = "java.lang.Throwable: cause";43protected final Throwable t = new Throwable("cause");44protected final Throwable t1 = new Throwable("cause 1");45protected final Throwable t2 = new Throwable("cause 2");46protected final int errorCode = 21;47protected final String[] msgs = {"Exception 1", "cause 1", "Exception 2",48"Exception 3", "cause 2"};4950@BeforeClass51public static void setUpClass() throws Exception {52}5354@AfterClass55public static void tearDownClass() throws Exception {56}5758@BeforeMethod59public void setUpMethod() throws Exception {60}6162@AfterMethod63public void tearDownMethod() throws Exception {64}6566/*67* Take some form of SQLException, serialize and deserialize it68*/69@SuppressWarnings("unchecked")70protected <T extends SQLException> T71createSerializedException(T ex)72throws IOException, ClassNotFoundException {73return (T) serializeDeserializeObject(ex);74}7576/*77* Utility method to serialize and deserialize an object78*/79@SuppressWarnings("unchecked")80protected <T> T serializeDeserializeObject(T o)81throws IOException, ClassNotFoundException {82T o1;83ByteArrayOutputStream baos = new ByteArrayOutputStream();84try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {85oos.writeObject(o);86}87try (ObjectInputStream ois88= new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {89o1 = (T) ois.readObject();90}91return o1;92}9394/*95* Utility Method used to set the current Policy96*/97protected static void setPolicy(Policy p) {98Policy.setPolicy(p);99}100101/*102* DataProvider used to specify the value to set and check for103* methods using boolean values104*/105@DataProvider(name = "trueFalse")106protected Object[][] trueFalse() {107return new Object[][]{108{true},109{false}110};111}112113/*114* DataProvider used to specify the standard JDBC Types115*/116@DataProvider(name = "jdbcTypes")117protected Object[][] jdbcTypes() {118Object[][] o = new Object[JDBCType.values().length][1];119int pos = 0;120for (JDBCType c : JDBCType.values()) {121o[pos++][0] = c.getVendorTypeNumber();122}123return o;124}125}126127128