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