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.File;
  26 import java.io.FileInputStream;
  27 import java.io.FileOutputStream;
  28 import java.io.IOException;
  29 import java.io.ObjectInputStream;
  30 import java.io.ObjectOutputStream;
  31 import java.net.URI;
  32 import java.net.URISyntaxException;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 import java.sql.SQLException;
  37 import org.testng.annotations.AfterClass;
  38 import org.testng.annotations.AfterMethod;
  39 import org.testng.annotations.BeforeClass;
  40 import org.testng.annotations.BeforeMethod;
  41 
  42 public class BaseTest {
  43 
  44     protected final String reason = "reason";
  45     protected final String state = "SQLState";
  46     protected final String cause = "java.lang.Throwable: cause";
  47     protected final Throwable t = new Throwable("cause");
  48     protected final Throwable t1 = new Throwable("cause 1");
  49     protected final Throwable t2 = new Throwable("cause 2");
  50     protected final int errorCode = 21;
  51     protected final String[] msgs = {"Exception 1", "cause 1", "Exception 2",
  52         "Exception 3", "cause 2"};
  53 
  54     @BeforeClass
  55     public static void setUpClass() throws Exception {
  56     }
  57 
  58     @AfterClass
  59     public static void tearDownClass() throws Exception {
  60     }
  61 
  62     @BeforeMethod
  63     public void setUpMethod() throws Exception {
  64     }
  65 
  66     @AfterMethod
  67     public void tearDownMethod() throws Exception {
  68     }
  69 
  70     /**
  71      * Take some form of SQLException, serialize and deserialize it
  72      *
  73      * @param <T> SQLException
  74      * @param ex SQLException
  75      * @param fname name of file
  76      * @return
  77      * @throws IOException
  78      * @throws ClassNotFoundException
  79      */
  80     @SuppressWarnings("unchecked")
  81     protected <T extends SQLException> T
  82             createSerializedException(T ex, String fname)
  83             throws IOException, ClassNotFoundException, URISyntaxException {
  84         T ex1;
  85         try (ObjectOutputStream out = new ObjectOutputStream(
  86                 new FileOutputStream(fname));
  87                 ObjectInputStream is  = new ObjectInputStream(
  88                     new FileInputStream(fname))) {
  89             out.writeObject(ex); 
  90             ex1 = (T) is.readObject();
  91         }
  92         
  93         File f = new File(fname);
  94         if (!f.delete()) {
  95             System.out.println("path=" + f.getAbsolutePath()
  96                     + " could not be deleted");
  97         }
  98         return ex1;
  99     }
 100 
 101 }