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