test/java/sql/test/sql/SQLNonTransientExceptionTests.java

Print this page




   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 test.sql;
  24 
  25 import java.io.FileInputStream;
  26 import java.io.FileOutputStream;
  27 import java.io.ObjectInputStream;
  28 import java.io.ObjectOutputStream;
  29 import java.sql.SQLException;
  30 import java.sql.SQLNonTransientException;
  31 import static org.testng.Assert.*;
  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 import org.testng.annotations.Test;

  37 
  38 public class SQLNonTransientExceptionTests {
  39 
  40     private final String reason = "reason";
  41     private final String state = "SQLState";
  42     private final String cause = "java.lang.Throwable: cause";
  43     private final Throwable t = new Throwable("cause");
  44     private final Throwable t1 = new Throwable("cause 1");
  45     private final Throwable t2 = new Throwable("cause 2");
  46     private final int errorCode = 21;
  47     private final String[] msgs = {"Exception 1", "cause 1", "Exception 2",
  48         "Exception 3", "cause 2"};
  49 
  50     public SQLNonTransientExceptionTests() {
  51     }
  52 
  53     @BeforeClass
  54     public static void setUpClass() throws Exception {
  55     }
  56 
  57     @AfterClass
  58     public static void tearDownClass() throws Exception {
  59     }
  60 
  61     @BeforeMethod
  62     public void setUpMethod() throws Exception {
  63     }
  64 
  65     @AfterMethod
  66     public void tearDownMethod() throws Exception {
  67     }
  68 
  69     /**
  70      * Create SQLNonTransientException and setting all objects to null
  71      */
  72     @Test
  73     public void test() {
  74         SQLNonTransientException e = new SQLNonTransientException(null,
  75                 null, errorCode, null);
  76         assertTrue(e.getMessage() == null && e.getSQLState() == null
  77                 && e.getCause() == null && e.getErrorCode() == errorCode);
  78     }
  79 
  80     /**
  81      * Create SQLNonTransientException with no-arg constructor
  82      */
  83     @Test
  84     public void test1() {
  85         SQLNonTransientException ex = new SQLNonTransientException();
  86         assertTrue(ex.getMessage() == null
  87                 && ex.getSQLState() == null


 177 
 178     /**
 179      * Create SQLNonTransientException with Throwable
 180      */
 181     @Test
 182     public void test9() {
 183         SQLNonTransientException ex = new SQLNonTransientException(t);
 184         assertTrue(ex.getMessage().equals(cause)
 185                 && ex.getSQLState() == null
 186                 && cause.equals(ex.getCause().toString())
 187                 && ex.getErrorCode() == 0);
 188     }
 189 
 190     /**
 191      * Serialize a SQLNonTransientException and make sure you can read it back properly
 192      */
 193     @Test
 194     public void test10() throws Exception {
 195         SQLNonTransientException e =
 196                 new SQLNonTransientException(reason, state, errorCode, t);
 197         ObjectOutputStream out
 198                 = new ObjectOutputStream(
 199                         new FileOutputStream("SQLNonTransientException.ser"));
 200         out.writeObject(e);
 201         ObjectInputStream is = new ObjectInputStream(
 202                 new FileInputStream("SQLNonTransientException.ser"));
 203         SQLNonTransientException ex1 = (SQLNonTransientException) is.readObject();
 204         assertTrue(reason.equals(ex1.getMessage())
 205                 && ex1.getSQLState().equals(state)
 206                 && cause.equals(ex1.getCause().toString())
 207                 && ex1.getErrorCode() == errorCode);
 208     }
 209 
 210     /**
 211      * Validate that the ordering of the returned Exceptions is correct
 212      * using for-each loop
 213      */
 214     @Test
 215     public void test11() {
 216         SQLNonTransientException ex = new SQLNonTransientException("Exception 1", t1);
 217         SQLNonTransientException ex1 = new SQLNonTransientException("Exception 2");
 218         SQLNonTransientException ex2 = new SQLNonTransientException("Exception 3", t2);
 219         ex.setNextException(ex1);
 220         ex.setNextException(ex2);
 221         int num = 0;
 222         for (Throwable e : ex) {
 223             assertTrue(msgs[num++].equals(e.getMessage()));




   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 test.sql;
  24 




  25 import java.sql.SQLException;
  26 import java.sql.SQLNonTransientException;
  27 import static org.testng.Assert.*;




  28 import org.testng.annotations.Test;
  29 import util.BaseTest;
  30 
  31 public class SQLNonTransientExceptionTests extends BaseTest {





























  32 
  33     /**
  34      * Create SQLNonTransientException and setting all objects to null
  35      */
  36     @Test
  37     public void test() {
  38         SQLNonTransientException e = new SQLNonTransientException(null,
  39                 null, errorCode, null);
  40         assertTrue(e.getMessage() == null && e.getSQLState() == null
  41                 && e.getCause() == null && e.getErrorCode() == errorCode);
  42     }
  43 
  44     /**
  45      * Create SQLNonTransientException with no-arg constructor
  46      */
  47     @Test
  48     public void test1() {
  49         SQLNonTransientException ex = new SQLNonTransientException();
  50         assertTrue(ex.getMessage() == null
  51                 && ex.getSQLState() == null


 141 
 142     /**
 143      * Create SQLNonTransientException with Throwable
 144      */
 145     @Test
 146     public void test9() {
 147         SQLNonTransientException ex = new SQLNonTransientException(t);
 148         assertTrue(ex.getMessage().equals(cause)
 149                 && ex.getSQLState() == null
 150                 && cause.equals(ex.getCause().toString())
 151                 && ex.getErrorCode() == 0);
 152     }
 153 
 154     /**
 155      * Serialize a SQLNonTransientException and make sure you can read it back properly
 156      */
 157     @Test
 158     public void test10() throws Exception {
 159         SQLNonTransientException e =
 160                 new SQLNonTransientException(reason, state, errorCode, t);
 161         SQLNonTransientException ex1 =
 162                 createSerializedException(e);





 163         assertTrue(reason.equals(ex1.getMessage())
 164                 && ex1.getSQLState().equals(state)
 165                 && cause.equals(ex1.getCause().toString())
 166                 && ex1.getErrorCode() == errorCode);
 167     }
 168 
 169     /**
 170      * Validate that the ordering of the returned Exceptions is correct
 171      * using for-each loop
 172      */
 173     @Test
 174     public void test11() {
 175         SQLNonTransientException ex = new SQLNonTransientException("Exception 1", t1);
 176         SQLNonTransientException ex1 = new SQLNonTransientException("Exception 2");
 177         SQLNonTransientException ex2 = new SQLNonTransientException("Exception 3", t2);
 178         ex.setNextException(ex1);
 179         ex.setNextException(ex2);
 180         int num = 0;
 181         for (Throwable e : ex) {
 182             assertTrue(msgs[num++].equals(e.getMessage()));