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.rowset;
  24 
  25 import java.sql.SQLException;
  26 import javax.sql.rowset.RowSetWarning;
  27 import static org.testng.Assert.*;
  28 import org.testng.annotations.Test;
  29 import util.BaseTest;
  30 
  31 public class RowSetWarningTests extends BaseTest {
  32 
  33     private final String[] warnings = {"Warning 1", "cause 1", "Warning 2",
  34         "Warning 3", "cause 2"};
  35 
  36     /*
  37      * Create RowSetWarning and setting all objects to null
  38      */
  39     @Test
  40     public void test() {
  41         RowSetWarning e = new RowSetWarning(null, null, errorCode);
  42         assertTrue(e.getMessage() == null && e.getSQLState() == null
  43                 && e.getCause() == null && e.getErrorCode() == errorCode);
  44     }
  45 
  46     /*
  47      * Create RowSetWarning with no-arg constructor
  48      */
  49     @Test
  50     public void test01() {
  51         RowSetWarning ex = new RowSetWarning();
  52         assertTrue(ex.getMessage() == null
  53                 && ex.getSQLState() == null
  54                 && ex.getCause() == null
  55                 && ex.getErrorCode() == 0);
  56     }
  57 
  58     /*
  59      * Create RowSetWarning with message
  60      */
  61     @Test
  62     public void test02() {
  63         RowSetWarning ex = new RowSetWarning(reason);
  64         assertTrue(ex.getMessage().equals(reason)
  65                 && ex.getSQLState() == null
  66                 && ex.getCause() == null
  67                 && ex.getErrorCode() == 0);
  68     }
  69 
  70     /*
  71      * Create RowSetWarning with message, and SQLState
  72      */
  73     @Test
  74     public void test03() {
  75 
  76         RowSetWarning ex = new RowSetWarning(reason, state);
  77         assertTrue(ex.getMessage().equals(reason)
  78                 && ex.getSQLState().equals(state)
  79                 && ex.getCause() == null
  80                 && ex.getErrorCode() == 0);
  81     }
  82 
  83     /*
  84      * Create RowSetWarning with message, SQLState, and error code
  85      */
  86     @Test
  87     public void test04() {
  88         RowSetWarning ex = new RowSetWarning(reason, state, errorCode);
  89         assertTrue(ex.getMessage().equals(reason)
  90                 && ex.getSQLState().equals(state)
  91                 && ex.getCause() == null
  92                 && ex.getErrorCode() == errorCode);
  93     }
  94 
  95     /*
  96      * Serialize a RowSetWarning and make sure you can read it back properly
  97      */
  98     @Test
  99     public void test05() throws Exception {
 100         RowSetWarning e = new RowSetWarning(reason, state, errorCode);
 101         e.initCause(t);
 102         RowSetWarning ex1 = createSerializedException(e);
 103         assertTrue(reason.equals(ex1.getMessage())
 104                 && ex1.getSQLState().equals(state)
 105                 && cause.equals(ex1.getCause().toString())
 106                 && ex1.getErrorCode() == errorCode);
 107     }
 108 
 109     /*
 110      * Validate that the ordering of the returned Exceptions is correct using
 111      * for-each loop
 112      */
 113     @Test
 114     public void test06() {
 115         RowSetWarning ex = new RowSetWarning("Exception 1");
 116         ex.initCause(t1);
 117         RowSetWarning ex1 = new RowSetWarning("Exception 2");
 118         RowSetWarning ex2 = new RowSetWarning("Exception 3");
 119         ex2.initCause(t2);
 120         ex.setNextException(ex1);
 121         ex.setNextException(ex2);
 122         int num = 0;
 123         for (Throwable e : ex) {
 124             assertTrue(msgs[num++].equals(e.getMessage()));
 125         }
 126     }
 127 
 128     /*
 129      * Validate that the ordering of the returned Exceptions is correct using
 130      * traditional while loop
 131      */
 132     @Test
 133     public void test07() {
 134         RowSetWarning ex = new RowSetWarning("Exception 1");
 135         ex.initCause(t1);
 136         RowSetWarning ex1 = new RowSetWarning("Exception 2");
 137         RowSetWarning ex2 = new RowSetWarning("Exception 3");
 138         ex2.initCause(t2);
 139         ex.setNextException(ex1);
 140         ex.setNextException(ex2);
 141         int num = 0;
 142         SQLException sqe = ex;
 143         while (sqe != null) {
 144             assertTrue(msgs[num++].equals(sqe.getMessage()));
 145             Throwable c = sqe.getCause();
 146             while (c != null) {
 147                 assertTrue(msgs[num++].equals(c.getMessage()));
 148                 c = c.getCause();
 149             }
 150             sqe = sqe.getNextException();
 151         }
 152     }
 153 
 154     /*
 155      * Validate that the ordering of the returned RowSetWarning is correct using
 156      * for-each loop
 157      */
 158     @Test
 159     public void test08() {
 160         RowSetWarning ex = new RowSetWarning("Warning 1");
 161         ex.initCause(t1);
 162         RowSetWarning ex1 = new RowSetWarning("Warning 2");
 163         RowSetWarning ex2 = new RowSetWarning("Warning 3");
 164         ex2.initCause(t2);
 165         ex.setNextWarning(ex1);
 166         ex.setNextWarning(ex2);
 167         int num = 0;
 168         for (Throwable e : ex) {
 169             assertTrue(warnings[num++].equals(e.getMessage()));
 170         }
 171     }
 172 
 173     /**
 174      * Validate that the ordering of the returned RowSetWarning is correct using
 175      * traditional while loop
 176      */
 177     @Test
 178     public void test09() {
 179         RowSetWarning ex = new RowSetWarning("Warning 1");
 180         ex.initCause(t1);
 181         RowSetWarning ex1 = new RowSetWarning("Warning 2");
 182         RowSetWarning ex2 = new RowSetWarning("Warning 3");
 183         ex2.initCause(t2);
 184         ex.setNextWarning(ex1);
 185         ex.setNextWarning(ex2);
 186         int num = 0;
 187         RowSetWarning sqe = ex;
 188         while (sqe != null) {
 189             assertTrue(warnings[num++].equals(sqe.getMessage()));
 190             Throwable c = sqe.getCause();
 191             while (c != null) {
 192                 assertTrue(msgs[num++].equals(c.getMessage()));
 193                 c = c.getCause();
 194             }
 195             sqe = sqe.getNextWarning();
 196         }
 197     }
 198 
 199     /*
 200      * Serialize a RowSetWarning and make sure you can read it back properly
 201      */
 202     @Test
 203     public void test10() throws Exception {
 204         RowSetWarning e = new RowSetWarning(reason, state, errorCode);
 205         RowSetWarning ex1 = createSerializedException(e);
 206         assertTrue(reason.equals(ex1.getMessage())
 207                 && ex1.getSQLState().equals(state)
 208                 && ex1.getCause() == null
 209                 && ex1.getErrorCode() == errorCode);
 210     }
 211 
 212     /*
 213      *  Serialize a RowSetWarning and make sure you can read it back properly.
 214      * Validate that the ordering of the returned RowSetWarning is correct using
 215      * traditional while loop
 216      */
 217     @Test
 218     public void test11() throws Exception {
 219         RowSetWarning ex = new RowSetWarning("Warning 1");
 220         ex.initCause(t1);
 221         RowSetWarning ex1 = new RowSetWarning("Warning 2");
 222         RowSetWarning ex2 = new RowSetWarning("Warning 3");
 223         ex2.initCause(t2);
 224         ex.setNextWarning(ex1);
 225         ex.setNextWarning(ex2);
 226         int num = 0;
 227         RowSetWarning sqe = createSerializedException(ex);
 228         while (sqe != null) {
 229             assertTrue(warnings[num++].equals(sqe.getMessage()));
 230             Throwable c = sqe.getCause();
 231             while (c != null) {
 232                 assertTrue(msgs[num++].equals(c.getMessage()));
 233                 c = c.getCause();
 234             }
 235             sqe = sqe.getNextWarning();
 236         }
 237     }
 238 }