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