test/java/sql/test/sql/SQLExceptionTests.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 static org.testng.Assert.*;
  31 import org.testng.annotations.AfterClass;
  32 import org.testng.annotations.AfterMethod;
  33 import org.testng.annotations.BeforeClass;
  34 import org.testng.annotations.BeforeMethod;
  35 import org.testng.annotations.Test;

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


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




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





























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


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






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