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.SQLDataException;
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 SQLDataExceptionTests {
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 SQLDataExceptionTests() {
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 SQLDataException and setting all objects to null
72 */
73 @Test
74 public void test() {
75 SQLDataException e = new SQLDataException(null, null, errorCode, null);
76 assertTrue(e.getMessage() == null && e.getSQLState() == null
77 && e.getCause() == null && e.getErrorCode() == errorCode);
78 }
79
80 /**
81 * Create SQLDataException with no-arg constructor
82 */
83 @Test
84 public void test1() {
85 SQLDataException ex = new SQLDataException();
86 assertTrue(ex.getMessage() == null
87 && ex.getSQLState() == null
88 && ex.getCause() == null
174 }
175
176 /**
177 * Create SQLDataException with Throwable
178 */
179 @Test
180 public void test9() {
181 SQLDataException ex = new SQLDataException(t);
182 assertTrue(ex.getMessage().equals(cause)
183 && ex.getSQLState() == null
184 && cause.equals(ex.getCause().toString())
185 && ex.getErrorCode() == 0);
186 }
187
188 /**
189 * Serialize a SQLDataException and make sure you can read it back properly
190 */
191 @Test
192 public void test10() throws Exception {
193 SQLDataException e = new SQLDataException(reason, state, errorCode, t);
194 ObjectOutputStream out
195 = new ObjectOutputStream(
196 new FileOutputStream("SQLDataException.ser"));
197 out.writeObject(e);
198 ObjectInputStream is = new ObjectInputStream(
199 new FileInputStream("SQLDataException.ser"));
200 SQLDataException ex1 = (SQLDataException) is.readObject();
201 assertTrue(reason.equals(ex1.getMessage())
202 && ex1.getSQLState().equals(state)
203 && cause.equals(ex1.getCause().toString())
204 && ex1.getErrorCode() == errorCode);
205 }
206
207 /**
208 * Validate that the ordering of the returned Exceptions is correct
209 * using for-each loop
210 */
211 @Test
212 public void test11() {
213 SQLDataException ex = new SQLDataException("Exception 1", t1);
214 SQLDataException ex1 = new SQLDataException("Exception 2");
215 SQLDataException ex2 = new SQLDataException("Exception 3", t2);
216 ex.setNextException(ex1);
217 ex.setNextException(ex2);
218 int num = 0;
219 for (Throwable e : ex) {
220 assertTrue(msgs[num++].equals(e.getMessage()));
|
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.SQLDataException;
26 import java.sql.SQLException;
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 SQLDataExceptionTests extends BaseTest {
33
34 /**
35 * Create SQLDataException and setting all objects to null
36 */
37 @Test
38 public void test() {
39 SQLDataException e = new SQLDataException(null, null, errorCode, null);
40 assertTrue(e.getMessage() == null && e.getSQLState() == null
41 && e.getCause() == null && e.getErrorCode() == errorCode);
42 }
43
44 /**
45 * Create SQLDataException with no-arg constructor
46 */
47 @Test
48 public void test1() {
49 SQLDataException ex = new SQLDataException();
50 assertTrue(ex.getMessage() == null
51 && ex.getSQLState() == null
52 && ex.getCause() == null
138 }
139
140 /**
141 * Create SQLDataException with Throwable
142 */
143 @Test
144 public void test9() {
145 SQLDataException ex = new SQLDataException(t);
146 assertTrue(ex.getMessage().equals(cause)
147 && ex.getSQLState() == null
148 && cause.equals(ex.getCause().toString())
149 && ex.getErrorCode() == 0);
150 }
151
152 /**
153 * Serialize a SQLDataException and make sure you can read it back properly
154 */
155 @Test
156 public void test10() throws Exception {
157 SQLDataException e = new SQLDataException(reason, state, errorCode, t);
158 SQLDataException ex1 = createSerializedException(e);
159 assertTrue(reason.equals(ex1.getMessage())
160 && ex1.getSQLState().equals(state)
161 && cause.equals(ex1.getCause().toString())
162 && ex1.getErrorCode() == errorCode);
163 }
164
165 /**
166 * Validate that the ordering of the returned Exceptions is correct
167 * using for-each loop
168 */
169 @Test
170 public void test11() {
171 SQLDataException ex = new SQLDataException("Exception 1", t1);
172 SQLDataException ex1 = new SQLDataException("Exception 2");
173 SQLDataException ex2 = new SQLDataException("Exception 3", t2);
174 ex.setNextException(ex1);
175 ex.setNextException(ex2);
176 int num = 0;
177 for (Throwable e : ex) {
178 assertTrue(msgs[num++].equals(e.getMessage()));
|