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