test/java/sql/test/sql/SQLNonTransientConnectionExceptionTests.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.SQLNonTransientConnectionException;
  30 import java.sql.SQLException;

  31 import java.sql.SQLNonTransientException;
  32 import static org.testng.Assert.*;
  33 import org.testng.annotations.AfterClass;
  34 import org.testng.annotations.AfterMethod;
  35 import org.testng.annotations.BeforeClass;
  36 import org.testng.annotations.BeforeMethod;
  37 import org.testng.annotations.Test;

  38 
  39 public class SQLNonTransientConnectionExceptionTests {
  40 
  41     private final String reason = "reason";
  42     private final String state = "SQLState";
  43     private final String cause = "java.lang.Throwable: cause";
  44     private final Throwable t = new Throwable("cause");
  45     private final Throwable t1 = new Throwable("cause 1");
  46     private final Throwable t2 = new Throwable("cause 2");
  47     private final int errorCode = 21;
  48     private final String[] msgs = {"Exception 1", "cause 1", "Exception 2",
  49         "Exception 3", "cause 2"};
  50 
  51     public SQLNonTransientConnectionExceptionTests() {
  52     }
  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      * Create SQLNonTransientConnectionException and setting all objects to null
  72      */
  73     @Test
  74     public void test() {
  75         SQLNonTransientConnectionException e =
  76                 new SQLNonTransientConnectionException(null,
  77                 null, errorCode, null);
  78         assertTrue(e.getMessage() == null && e.getSQLState() == null
  79                 && e.getCause() == null && e.getErrorCode() == errorCode);
  80     }
  81 
  82     /**
  83      * Create SQLNonTransientConnectionException with no-arg constructor
  84      */
  85     @Test
  86     public void test1() {
  87         SQLNonTransientConnectionException ex =
  88                 new SQLNonTransientConnectionException();


 187      * Create SQLNonTransientConnectionException with Throwable
 188      */
 189     @Test
 190     public void test9() {
 191         SQLNonTransientConnectionException ex =
 192                 new SQLNonTransientConnectionException(t);
 193         assertTrue(ex.getMessage().equals(cause)
 194                 && ex.getSQLState() == null
 195                 && cause.equals(ex.getCause().toString())
 196                 && ex.getErrorCode() == 0);
 197     }
 198 
 199     /**
 200      * Serialize a SQLNonTransientConnectionException and make sure you can
 201      * read it back properly
 202      */
 203     @Test
 204     public void test10() throws Exception {
 205         SQLNonTransientConnectionException e =
 206                 new SQLNonTransientConnectionException(reason, state, errorCode, t);
 207         ObjectOutputStream out
 208                 = new ObjectOutputStream(
 209                         new FileOutputStream("SQLNonTransientConnectionException.ser"));
 210         out.writeObject(e);
 211         ObjectInputStream is = new ObjectInputStream(
 212                 new FileInputStream("SQLNonTransientConnectionException.ser"));
 213         SQLNonTransientConnectionException ex1 =
 214                 (SQLNonTransientConnectionException) is.readObject();
 215         assertTrue(reason.equals(ex1.getMessage())
 216                 && ex1.getSQLState().equals(state)
 217                 && cause.equals(ex1.getCause().toString())
 218                 && ex1.getErrorCode() == errorCode);
 219     }
 220 
 221     /**
 222      * Validate that the ordering of the returned Exceptions is correct
 223      * using for-each loop
 224      */
 225     @Test
 226     public void test11() {
 227         SQLNonTransientConnectionException ex =
 228                 new SQLNonTransientConnectionException("Exception 1", t1);
 229         SQLNonTransientConnectionException ex1 =
 230                 new SQLNonTransientConnectionException("Exception 2");
 231         SQLNonTransientConnectionException ex2 =
 232                 new SQLNonTransientConnectionException("Exception 3", t2);
 233         ex.setNextException(ex1);
 234         ex.setNextException(ex2);




   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.SQLNonTransientConnectionException;
  27 import java.sql.SQLNonTransientException;
  28 import static org.testng.Assert.*;




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





























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


 151      * Create SQLNonTransientConnectionException with Throwable
 152      */
 153     @Test
 154     public void test9() {
 155         SQLNonTransientConnectionException ex =
 156                 new SQLNonTransientConnectionException(t);
 157         assertTrue(ex.getMessage().equals(cause)
 158                 && ex.getSQLState() == null
 159                 && cause.equals(ex.getCause().toString())
 160                 && ex.getErrorCode() == 0);
 161     }
 162 
 163     /**
 164      * Serialize a SQLNonTransientConnectionException and make sure you can
 165      * read it back properly
 166      */
 167     @Test
 168     public void test10() throws Exception {
 169         SQLNonTransientConnectionException e =
 170                 new SQLNonTransientConnectionException(reason, state, errorCode, t);






 171         SQLNonTransientConnectionException ex1 =
 172                 createSerializedException(e);
 173         assertTrue(reason.equals(ex1.getMessage())
 174                 && ex1.getSQLState().equals(state)
 175                 && cause.equals(ex1.getCause().toString())
 176                 && ex1.getErrorCode() == errorCode);
 177     }
 178 
 179     /**
 180      * Validate that the ordering of the returned Exceptions is correct
 181      * using for-each loop
 182      */
 183     @Test
 184     public void test11() {
 185         SQLNonTransientConnectionException ex =
 186                 new SQLNonTransientConnectionException("Exception 1", t1);
 187         SQLNonTransientConnectionException ex1 =
 188                 new SQLNonTransientConnectionException("Exception 2");
 189         SQLNonTransientConnectionException ex2 =
 190                 new SQLNonTransientConnectionException("Exception 3", t2);
 191         ex.setNextException(ex1);
 192         ex.setNextException(ex2);