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