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