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 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);
  91     }
  92 
  93     /**
  94      * Create SQLWarning with message
  95      */
  96     @Test
  97     public void test2() {
  98         SQLWarning ex = new SQLWarning(reason);
  99         assertTrue(ex.getMessage().equals(reason)
 100                 && ex.getSQLState() == null
 101                 && ex.getCause() == null
 102                 && ex.getErrorCode() == 0);
 103     }
 104 
 105     /**
 106      * Create SQLWarning with message, and SQLState
 107      */
 108     @Test
 109     public void test3() {
 110 
 111         SQLWarning ex = new SQLWarning(reason, state);
 112         assertTrue(ex.getMessage().equals(reason)
 113                 && ex.getSQLState().equals(state)
 114                 && ex.getCause() == null
 115                 && ex.getErrorCode() == 0);
 116     }
 117 
 118     /**
 119      * Create SQLWarning with message, SQLState, and error code
 120      */
 121     @Test
 122     public void test4() {
 123         SQLWarning ex = new SQLWarning(reason, state, errorCode);
 124         assertTrue(ex.getMessage().equals(reason)
 125                 && ex.getSQLState().equals(state)
 126                 && ex.getCause() == null
 127                 && ex.getErrorCode() == errorCode);
 128     }
 129 
 130     /**
 131      * Create SQLWarning with message, SQLState, errorCode, and Throwable
 132      */
 133     @Test
 134     public void test5() {
 135         SQLWarning ex = new SQLWarning(reason, state, errorCode, t);
 136         assertTrue(ex.getMessage().equals(reason)
 137                 && ex.getSQLState().equals(state)
 138                 && cause.equals(ex.getCause().toString())
 139                 && ex.getErrorCode() == errorCode);
 140     }
 141 
 142     /**
 143      * Create SQLWarning with message, SQLState, and Throwable
 144      */
 145     @Test
 146     public void test6() {
 147         SQLWarning ex = new SQLWarning(reason, state, t);
 148         assertTrue(ex.getMessage().equals(reason)
 149                 && ex.getSQLState().equals(state)
 150                 && cause.equals(ex.getCause().toString())
 151                 && ex.getErrorCode() == 0);
 152     }
 153 
 154     /**
 155      * Create SQLWarning with message, and Throwable
 156      */
 157     @Test
 158     public void test7() {
 159         SQLWarning ex = new SQLWarning(reason, t);
 160         assertTrue(ex.getMessage().equals(reason)
 161                 && ex.getSQLState() == null
 162                 && cause.equals(ex.getCause().toString())
 163                 && ex.getErrorCode() == 0);
 164     }
 165 
 166     /**
 167      * Create SQLWarning with null Throwable
 168      */
 169     @Test
 170     public void test8() {
 171         SQLWarning ex = new SQLWarning((Throwable) null);
 172         assertTrue(ex.getMessage() == null
 173                 && ex.getSQLState() == null
 174                 && ex.getCause() == null
 175                 && 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()));
 223         }
 224     }
 225 
 226     /**
 227      * Validate that the ordering of the returned Exceptions is correct using
 228      * traditional while loop
 229      */
 230     @Test
 231     public void test12() {
 232         SQLWarning ex = new SQLWarning("Exception 1", t1);
 233         SQLWarning ex1 = new SQLWarning("Exception 2");
 234         SQLWarning ex2 = new SQLWarning("Exception 3", t2);
 235         ex.setNextException(ex1);
 236         ex.setNextException(ex2);
 237         int num = 0;
 238         SQLException sqe = ex;
 239         while (sqe != null) {
 240             assertTrue(msgs[num++].equals(sqe.getMessage()));
 241             Throwable c = sqe.getCause();
 242             while (c != null) {
 243                 assertTrue(msgs[num++].equals(c.getMessage()));
 244                 c = c.getCause();
 245             }
 246             sqe = sqe.getNextException();
 247         }
 248     }
 249 
 250     /**
 251      * Validate that the ordering of the returned SQLWarning is correct using
 252      * for-each loop
 253      */
 254     @Test
 255     public void test13() {
 256         SQLWarning ex = new SQLWarning("Warning 1", t1);
 257         SQLWarning ex1 = new SQLWarning("Warning 2");
 258         SQLWarning ex2 = new SQLWarning("Warning 3", t2);
 259         ex.setNextWarning(ex1);
 260         ex.setNextWarning(ex2);
 261         int num = 0;
 262         for (Throwable e : ex) {
 263             assertTrue(warnings[num++].equals(e.getMessage()));
 264         }
 265     }
 266 
 267     /**
 268      * Validate that the ordering of the returned SQLWarning is correct using
 269      * traditional while loop
 270      */
 271     @Test
 272     public void test14() {
 273         SQLWarning ex = new SQLWarning("Warning 1", t1);
 274         SQLWarning ex1 = new SQLWarning("Warning 2");
 275         SQLWarning ex2 = new SQLWarning("Warning 3", t2);
 276         ex.setNextWarning(ex1);
 277         ex.setNextWarning(ex2);
 278         int num = 0;
 279         SQLWarning sqe = ex;
 280         while (sqe != null) {
 281             assertTrue(warnings[num++].equals(sqe.getMessage()));
 282             Throwable c = sqe.getCause();
 283             while (c != null) {
 284                 assertTrue(msgs[num++].equals(c.getMessage()));
 285                 c = c.getCause();
 286             }
 287             sqe = sqe.getNextWarning();
 288         }
 289     }
 290 }