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 
  24 /**
  25  * @test
  26  * @modules java.sql.rowset/com.sun.rowset
  27  *          java.sql.rowset/com.sun.rowset.internal
  28  *          java.sql.rowset/com.sun.rowset.providers
  29  */
  30 
  31 package test.rowset.serial;
  32 
  33 import java.sql.SQLException;
  34 import javax.sql.rowset.serial.SerialException;
  35 import static org.testng.Assert.*;
  36 import org.testng.annotations.Test;
  37 import util.BaseTest;
  38 
  39 public class SerialExceptionTests extends BaseTest {
  40 
  41     /*
  42      * Create SerialException with no-arg constructor
  43      */
  44     @Test
  45     public void test01() {
  46         SerialException ex = new SerialException();
  47         assertTrue(ex.getMessage() == null
  48                 && ex.getSQLState() == null
  49                 && ex.getCause() == null
  50                 && ex.getErrorCode() == 0);
  51     }
  52 
  53     /*
  54      * Create SerialException with message
  55      */
  56     @Test
  57     public void test02() {
  58         SerialException ex = new SerialException(reason);
  59         assertTrue(ex.getMessage().equals(reason)
  60                 && ex.getSQLState() == null
  61                 && ex.getCause() == null
  62                 && ex.getErrorCode() == 0);
  63     }
  64 
  65     /*
  66      * Validate that the ordering of the returned Exceptions is correct using
  67      * for-each loop
  68      */
  69     @Test
  70     public void test03() {
  71         SerialException ex = new SerialException("Exception 1");
  72         ex.initCause(t1);
  73         SerialException ex1 = new SerialException("Exception 2");
  74         SerialException ex2 = new SerialException("Exception 3");
  75         ex2.initCause(t2);
  76         ex.setNextException(ex1);
  77         ex.setNextException(ex2);
  78         int num = 0;
  79         for (Throwable e : ex) {
  80             assertTrue(msgs[num++].equals(e.getMessage()));
  81         }
  82     }
  83 
  84     /*
  85      * Validate that the ordering of the returned Exceptions is correct using
  86      * traditional while loop
  87      */
  88     @Test
  89     public void test04() {
  90         SQLException ex = new SerialException("Exception 1");
  91         ex.initCause(t1);
  92         SerialException ex1 = new SerialException("Exception 2");
  93         SerialException ex2 = new SerialException("Exception 3");
  94         ex2.initCause(t2);
  95         ex.setNextException(ex1);
  96         ex.setNextException(ex2);
  97         int num = 0;
  98         while (ex != null) {
  99             assertTrue(msgs[num++].equals(ex.getMessage()));
 100             Throwable c = ex.getCause();
 101             while (c != null) {
 102                 assertTrue(msgs[num++].equals(c.getMessage()));
 103                 c = c.getCause();
 104             }
 105             ex = ex.getNextException();
 106         }
 107     }
 108 
 109     /*
 110      * Serialize a SerialException and make sure you can read it back properly
 111      */
 112     @Test
 113     public void test05() throws Exception {
 114         SerialException e = new SerialException(reason);
 115         SerialException ex1 = createSerializedException(e);
 116         assertTrue(ex1.getMessage().equals(reason)
 117                 && ex1.getSQLState() == null
 118                 && ex1.getCause() == null
 119                 && ex1.getErrorCode() == 0);;
 120     }
 121 }