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