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.SQLIntegrityConstraintViolationException;
  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 SQLIntegrityConstraintViolationExceptionTests {
  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 SQLIntegrityConstraintViolationExceptionTests() {
  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 SQLIntegrityConstraintViolationException and setting all objects to null
  72      */
  73     @Test
  74     public void test() {
  75         SQLIntegrityConstraintViolationException e =
  76                 new SQLIntegrityConstraintViolationException(null,
  77                 null, errorCode, null);
  78         assertTrue(e.getMessage() == null && e.getSQLState() == null
  79                 && e.getCause() == null && e.getErrorCode() == errorCode);
  80     }
  81 
  82     /**
  83      * Create SQLIntegrityConstraintViolationException with no-arg constructor
  84      */
  85     @Test
  86     public void test1() {
  87         SQLIntegrityConstraintViolationException ex =
  88                 new SQLIntegrityConstraintViolationException();
  89         assertTrue(ex.getMessage() == null
  90                 && ex.getSQLState() == null
  91                 && ex.getCause() == null
  92                 && ex.getErrorCode() == 0);
  93     }
  94 
  95     /**
  96      * Create SQLIntegrityConstraintViolationException with message
  97      */
  98     @Test
  99     public void test2() {
 100         SQLIntegrityConstraintViolationException ex =
 101                 new SQLIntegrityConstraintViolationException(reason);
 102         assertTrue(ex.getMessage().equals(reason)
 103                 && ex.getSQLState() == null
 104                 && ex.getCause() == null
 105                 && ex.getErrorCode() == 0);
 106     }
 107 
 108     /**
 109      * Create SQLIntegrityConstraintViolationException with message, and SQLState
 110      */
 111     @Test
 112     public void test3() {
 113         SQLIntegrityConstraintViolationException ex =
 114                 new SQLIntegrityConstraintViolationException(reason, state);
 115         assertTrue(ex.getMessage().equals(reason)
 116                 && ex.getSQLState().equals(state)
 117                 && ex.getCause() == null
 118                 && ex.getErrorCode() == 0);
 119     }
 120 
 121     /**
 122      * Create SQLIntegrityConstraintViolationException with message, SQLState, and error code
 123      */
 124     @Test
 125     public void test4() {
 126         SQLIntegrityConstraintViolationException ex =
 127                 new SQLIntegrityConstraintViolationException(reason, state, errorCode);
 128         assertTrue(ex.getMessage().equals(reason)
 129                 && ex.getSQLState().equals(state)
 130                 && ex.getCause() == null
 131                 && ex.getErrorCode() == errorCode);
 132     }
 133 
 134     /**
 135      * Create SQLIntegrityConstraintViolationException with message, SQLState, errorCode, and Throwable
 136      */
 137     @Test
 138     public void test5() {
 139         SQLIntegrityConstraintViolationException ex =
 140                 new SQLIntegrityConstraintViolationException(reason, state, errorCode, t);
 141         assertTrue(ex.getMessage().equals(reason)
 142                 && ex.getSQLState().equals(state)
 143                 && cause.equals(ex.getCause().toString())
 144                 && ex.getErrorCode() == errorCode);
 145     }
 146 
 147     /**
 148      * Create SQLIntegrityConstraintViolationException with message, SQLState, and Throwable
 149      */
 150     @Test
 151     public void test6() {
 152         SQLIntegrityConstraintViolationException ex =
 153                 new SQLIntegrityConstraintViolationException(reason, state, t);
 154         assertTrue(ex.getMessage().equals(reason)
 155                 && ex.getSQLState().equals(state)
 156                 && cause.equals(ex.getCause().toString())
 157                 && ex.getErrorCode() == 0);
 158     }
 159 
 160     /**
 161      * Create SQLIntegrityConstraintViolationException with message, and Throwable
 162      */
 163     @Test
 164     public void test7() {
 165         SQLIntegrityConstraintViolationException ex =
 166                 new SQLIntegrityConstraintViolationException(reason, t);
 167         assertTrue(ex.getMessage().equals(reason)
 168                 && ex.getSQLState() == null
 169                 && cause.equals(ex.getCause().toString())
 170                 && ex.getErrorCode() == 0);
 171     }
 172 
 173     /**
 174      * Create SQLIntegrityConstraintViolationException with null Throwable
 175      */
 176     @Test
 177     public void test8() {
 178         SQLIntegrityConstraintViolationException ex =
 179                 new SQLIntegrityConstraintViolationException((Throwable)null);
 180         assertTrue(ex.getMessage() == null
 181                 && ex.getSQLState() == null
 182                 && ex.getCause() == null
 183                 && ex.getErrorCode() == 0);
 184     }
 185 
 186     /**
 187      * Create SQLIntegrityConstraintViolationException with Throwable
 188      */
 189     @Test
 190     public void test9() {
 191         SQLIntegrityConstraintViolationException ex =
 192                 new SQLIntegrityConstraintViolationException(t);
 193         assertTrue(ex.getMessage().equals(cause)
 194                 && ex.getSQLState() == null
 195                 && cause.equals(ex.getCause().toString())
 196                 && ex.getErrorCode() == 0);
 197     }
 198 
 199     /**
 200      * Serialize a SQLIntegrityConstraintViolationException and make sure
 201      * you can read it back properly
 202      */
 203     @Test
 204     public void test10() throws Exception {
 205         SQLIntegrityConstraintViolationException e =
 206                 new SQLIntegrityConstraintViolationException(reason, state, errorCode, t);
 207         ObjectOutputStream out
 208                 = new ObjectOutputStream(
 209                         new FileOutputStream("SQLIntegrityConstraintViolationException.ser"));
 210         out.writeObject(e);
 211         ObjectInputStream is = new ObjectInputStream(
 212                 new FileInputStream("SQLIntegrityConstraintViolationException.ser"));
 213         SQLIntegrityConstraintViolationException ex1 =
 214                 (SQLIntegrityConstraintViolationException) is.readObject();
 215         assertTrue(reason.equals(ex1.getMessage())
 216                 && ex1.getSQLState().equals(state)
 217                 && cause.equals(ex1.getCause().toString())
 218                 && ex1.getErrorCode() == errorCode);
 219     }
 220 
 221     /**
 222      * Validate that the ordering of the returned Exceptions is correct
 223      * using for-each loop
 224      */
 225     @Test
 226     public void test11() {
 227         SQLIntegrityConstraintViolationException ex =
 228                 new SQLIntegrityConstraintViolationException("Exception 1", t1);
 229         SQLIntegrityConstraintViolationException ex1 =
 230                 new SQLIntegrityConstraintViolationException("Exception 2");
 231         SQLIntegrityConstraintViolationException ex2 =
 232                 new SQLIntegrityConstraintViolationException("Exception 3", t2);
 233         ex.setNextException(ex1);
 234         ex.setNextException(ex2);
 235         int num = 0;
 236         for (Throwable e : ex) {
 237             assertTrue(msgs[num++].equals(e.getMessage()));
 238         }
 239     }
 240 
 241     /**
 242      * Validate that the ordering of the returned Exceptions is correct
 243      * using traditional while loop
 244      */
 245     @Test
 246     public void test12() {
 247         SQLIntegrityConstraintViolationException ex =
 248                 new SQLIntegrityConstraintViolationException("Exception 1", t1);
 249         SQLIntegrityConstraintViolationException ex1 =
 250                 new SQLIntegrityConstraintViolationException("Exception 2");
 251         SQLIntegrityConstraintViolationException ex2 =
 252                 new SQLIntegrityConstraintViolationException("Exception 3", t2);
 253         ex.setNextException(ex1);
 254         ex.setNextException(ex2);
 255         int num = 0;
 256         SQLException sqe = ex;
 257         while (sqe != null) {
 258             assertTrue(msgs[num++].equals(sqe.getMessage()));
 259             Throwable c = sqe.getCause();
 260             while (c != null) {
 261                 assertTrue(msgs[num++].equals(c.getMessage()));
 262                 c = c.getCause();
 263             }
 264             sqe = sqe.getNextException();
 265         }
 266     }
 267 
 268     /**
 269      * Create SQLIntegrityConstraintViolationException and validate it is an instance of
 270      * SQLNonTransientException
 271      */
 272     @Test
 273     public void test13() {
 274         Exception ex = new SQLIntegrityConstraintViolationException();
 275         assertTrue(ex instanceof SQLNonTransientException);
 276     }
 277 }