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