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     /**
  97      * Serialize a RowSetWarning and make sure you can read it back properly
  98      */
  99     @Test
 100     public void test05() throws Exception {
 101         RowSetWarning e = new RowSetWarning(reason, state, errorCode);
 102         e.initCause(t);
 103         RowSetWarning ex1 = createSerializedException(e);
 104         assertTrue(reason.equals(ex1.getMessage())
 105                 && ex1.getSQLState().equals(state)
 106                 && cause.equals(ex1.getCause().toString())
 107                 && ex1.getErrorCode() == errorCode);
 108     }
 109 
 110     /**
 111      * Validate that the ordering of the returned Exceptions is correct using
 112      * for-each loop
 113      */
 114     @Test
 115     public void test06() {
 116         RowSetWarning ex = new RowSetWarning("Exception 1");
 117         ex.initCause(t1);
 118         RowSetWarning ex1 = new RowSetWarning("Exception 2");
 119         RowSetWarning ex2 = new RowSetWarning("Exception 3");
 120         ex2.initCause(t2);
 121         ex.setNextException(ex1);
 122         ex.setNextException(ex2);
 123         int num = 0;
 124         for (Throwable e : ex) {
 125             assertTrue(msgs[num++].equals(e.getMessage()));
 126         }
 127     }
 128 
 129     /**
 130      * Validate that the ordering of the returned Exceptions is correct using
 131      * traditional while loop
 132      */
 133     @Test
 134     public void test07() {
 135         RowSetWarning ex = new RowSetWarning("Exception 1");
 136         ex.initCause(t1);
 137         RowSetWarning ex1 = new RowSetWarning("Exception 2");
 138         RowSetWarning ex2 = new RowSetWarning("Exception 3");
 139         ex2.initCause(t2);
 140         ex.setNextException(ex1);
 141         ex.setNextException(ex2);
 142         int num = 0;
 143         SQLException sqe = ex;
 144         while (sqe != null) {
 145             assertTrue(msgs[num++].equals(sqe.getMessage()));
 146             Throwable c = sqe.getCause();
 147             while (c != null) {
 148                 assertTrue(msgs[num++].equals(c.getMessage()));
 149                 c = c.getCause();
 150             }
 151             sqe = sqe.getNextException();
 152         }
 153     }
 154 
 155     /**
 156      * Validate that the ordering of the returned RowSetWarning is correct using
 157      * for-each loop
 158      */
 159     @Test
 160     public void test08() {
 161         RowSetWarning ex = new RowSetWarning("Warning 1");
 162         ex.initCause(t1);
 163         RowSetWarning ex1 = new RowSetWarning("Warning 2");
 164         RowSetWarning ex2 = new RowSetWarning("Warning 3");
 165         ex2.initCause(t2);
 166         ex.setNextWarning(ex1);
 167         ex.setNextWarning(ex2);
 168         int num = 0;
 169         for (Throwable e : ex) {
 170             assertTrue(warnings[num++].equals(e.getMessage()));
 171         }
 172     }
 173 
 174     /**
 175      * Validate that the ordering of the returned RowSetWarning is correct using
 176      * traditional while loop
 177      */
 178     @Test
 179     public void test09() {
 180         RowSetWarning ex = new RowSetWarning("Warning 1");
 181         ex.initCause(t1);
 182         RowSetWarning ex1 = new RowSetWarning("Warning 2");
 183         RowSetWarning ex2 = new RowSetWarning("Warning 3");
 184         ex2.initCause(t2);
 185         ex.setNextWarning(ex1);
 186         ex.setNextWarning(ex2);
 187         int num = 0;
 188         RowSetWarning sqe = ex;
 189         while (sqe != null) {
 190             System.out.println(warnings[num] + " " + sqe.getMessage());
 191             assertTrue(warnings[num++].equals(sqe.getMessage()));
 192             Throwable c = sqe.getCause();
 193             while (c != null) {
 194                 assertTrue(msgs[num++].equals(c.getMessage()));
 195                 c = c.getCause();
 196             }
 197             sqe = sqe.getNextWarning();
 198         }
 199     }
 200 }