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.SQLException;
  32 import org.testng.annotations.AfterClass;
  33 import org.testng.annotations.AfterMethod;
  34 import org.testng.annotations.BeforeClass;
  35 import org.testng.annotations.BeforeMethod;
  36 
  37 public class BaseTest {
  38 
  39     protected final String reason = "reason";
  40     protected final String state = "SQLState";
  41     protected final String cause = "java.lang.Throwable: cause";
  42     protected final Throwable t = new Throwable("cause");
  43     protected final Throwable t1 = new Throwable("cause 1");
  44     protected final Throwable t2 = new Throwable("cause 2");
  45     protected final int errorCode = 21;
  46     protected final String[] msgs = {"Exception 1", "cause 1", "Exception 2",
  47         "Exception 3", "cause 2"};
  48 
  49     @BeforeClass
  50     public static void setUpClass() throws Exception {
  51     }
  52 
  53     @AfterClass
  54     public static void tearDownClass() throws Exception {
  55     }
  56 
  57     @BeforeMethod
  58     public void setUpMethod() throws Exception {
  59     }
  60 
  61     @AfterMethod
  62     public void tearDownMethod() throws Exception {
  63     }
  64 
  65     /*
  66      * Take some form of SQLException, serialize and deserialize it
  67      */
  68     @SuppressWarnings("unchecked")
  69     protected <T extends SQLException> T
  70             createSerializedException(T ex)
  71             throws IOException, ClassNotFoundException {
  72         return (T) serializeDeserializeObject(ex);
  73     }
  74 
  75     /*
  76      * Utility method to serialize and deserialize an object
  77      */
  78     @SuppressWarnings("unchecked")
  79     protected <T> T serializeDeserializeObject(T o)
  80             throws IOException, ClassNotFoundException {
  81         T o1;
  82         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  83         try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  84             oos.writeObject(o);
  85         }
  86         try (ObjectInputStream ois
  87                 = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
  88             o1 = (T) ois.readObject();
  89         }
  90         return o1;
  91     }
  92 
  93     /*
  94      * Utility Method used to set the current Policy
  95      */
  96     protected static void setPolicy(Policy p) {
  97         Policy.setPolicy(p);
  98         //System.out.println("\n" + Policy.getPolicy());
  99     }
 100 }