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

  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 SQLWarningTests {
  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     private final String[] warnings = {"Warning 1", "cause 1", "Warning 2",
  50             "Warning 3", "cause 2"};
  51 
  52     public SQLWarningTests() {
  53     }
  54 
  55     @BeforeClass
  56     public static void setUpClass() throws Exception {
  57     }
  58 
  59     @AfterClass
  60     public static void tearDownClass() throws Exception {
  61     }
  62 
  63     @BeforeMethod
  64     public void setUpMethod() throws Exception {
  65     }
  66 
  67     @AfterMethod
  68     public void tearDownMethod() throws Exception {
  69     }
  70 
  71     /**
  72      * Create SQLWarning and setting all objects to null
  73      */
  74     @Test
  75     public void test() {
  76         SQLWarning e = new SQLWarning(null, null, errorCode, null);
  77         assertTrue(e.getMessage() == null && e.getSQLState() == null
  78                 && e.getCause() == null && e.getErrorCode() == errorCode);
  79     }
  80 
  81     /**
  82      * Create SQLWarning with no-arg constructor
  83      */
  84     @Test
  85     public void test1() {
  86         SQLWarning ex = new SQLWarning();
  87         assertTrue(ex.getMessage() == null
  88                 && ex.getSQLState() == null
  89                 && ex.getCause() == null
  90                 && ex.getErrorCode() == 0);


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




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









  33     private final String[] warnings = {"Warning 1", "cause 1", "Warning 2",
  34             "Warning 3", "cause 2"};
  35 



















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


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






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