001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.sql.tests.java.sql;
019:
020: import java.io.Serializable;
021: import java.lang.reflect.Field;
022: import java.sql.SQLException;
023:
024: import org.apache.harmony.testframework.serialization.SerializationTest;
025: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
026:
027: import junit.framework.Assert;
028: import junit.framework.TestCase;
029:
030: public class SQLExceptionTest extends TestCase {
031:
032: static long theFixedSUID = 2135244094396331484L;
033:
034: /*
035: * SUID test
036: */
037: public void testSUID() {
038:
039: try {
040: Class<?> theClass = Class.forName("java.sql.SQLException");
041: Field theField = theClass
042: .getDeclaredField("serialVersionUID");
043: theField.setAccessible(true);
044: long theSUID = theField.getLong(null);
045: assertEquals("SUID mismatch: ", theFixedSUID, theSUID);
046: } catch (Exception e) {
047: System.out.println("SUID check got exception: "
048: + e.getMessage());
049: // assertTrue("Exception while testing SUID ", false );
050: } // end catch
051:
052: } // end method testSUID
053:
054: /*
055: * ConstructorTest
056: */
057: public void testSQLExceptionStringStringint() {
058:
059: String[] init1 = { "a", "1", "valid1", "----", "&valid*", "1",
060: "a", null, "", " ", "a", "a", "a" };
061: String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a",
062: "&valid*", "a", "a", "a", null, "", " " };
063: int[] init3 = { -2147483648, 2147483647, 0, 48429456,
064: 1770127344, 1047282235, -545472907, -2147483648,
065: -2147483648, -2147483648, -2147483648, -2147483648,
066: -2147483648 };
067:
068: String[] theFinalStates1 = init1;
069: String[] theFinalStates2 = init2;
070: int[] theFinalStates3 = init3;
071: SQLException[] theFinalStates4 = { null, null, null, null,
072: null, null, null, null, null, null, null, null, null };
073:
074: Exception[] theExceptions = { null, null, null, null, null,
075: null, null, null, null, null, null, null, null };
076:
077: SQLException aSQLException;
078: int loopCount = init1.length;
079: for (int i = 0; i < loopCount; i++) {
080: try {
081: aSQLException = new SQLException(init1[i], init2[i],
082: init3[i]);
083: if (theExceptions[i] != null) {
084: fail();
085: }
086: assertEquals(i + " Final state mismatch",
087: aSQLException.getMessage(), theFinalStates1[i]);
088: assertEquals(i + " Final state mismatch",
089: aSQLException.getSQLState(), theFinalStates2[i]);
090: assertEquals(i + " Final state mismatch",
091: aSQLException.getErrorCode(),
092: theFinalStates3[i]);
093: assertEquals(i + " Final state mismatch",
094: aSQLException.getNextException(),
095: theFinalStates4[i]);
096:
097: } catch (Exception e) {
098: if (theExceptions[i] == null) {
099: fail(i + "Unexpected exception");
100: }
101: assertEquals(i + "Exception mismatch", e.getClass(),
102: theExceptions[i].getClass());
103: assertEquals(i + "Exception mismatch", e.getMessage(),
104: theExceptions[i].getMessage());
105: } // end try
106: } // end for
107:
108: } // end method testSQLExceptionStringStringint
109:
110: /*
111: * ConstructorTest
112: */
113: public void testSQLExceptionStringString() {
114:
115: String[] init1 = { "a", "1", "valid1", "----", "&valid*", null,
116: "", " ", "a", "a", "a" };
117: String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a",
118: "a", "a", null, "", " " };
119:
120: String[] theFinalStates1 = init1;
121: String[] theFinalStates2 = init2;
122: int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
123: SQLException[] theFinalStates4 = { null, null, null, null,
124: null, null, null, null, null, null, null };
125:
126: Exception[] theExceptions = { null, null, null, null, null,
127: null, null, null, null, null, null };
128:
129: SQLException aSQLException;
130: int loopCount = init1.length;
131: for (int i = 0; i < loopCount; i++) {
132: try {
133: aSQLException = new SQLException(init1[i], init2[i]);
134: if (theExceptions[i] != null) {
135: fail();
136: }
137: assertEquals(i + " Final state mismatch",
138: aSQLException.getMessage(), theFinalStates1[i]);
139: assertEquals(i + " Final state mismatch",
140: aSQLException.getSQLState(), theFinalStates2[i]);
141: assertEquals(i + " Final state mismatch",
142: aSQLException.getErrorCode(),
143: theFinalStates3[i]);
144: assertEquals(i + " Final state mismatch",
145: aSQLException.getNextException(),
146: theFinalStates4[i]);
147:
148: } catch (Exception e) {
149: if (theExceptions[i] == null) {
150: fail(i + "Unexpected exception");
151: }
152: assertEquals(i + "Exception mismatch", e.getClass(),
153: theExceptions[i].getClass());
154: assertEquals(i + "Exception mismatch", e.getMessage(),
155: theExceptions[i].getMessage());
156: } // end try
157: } // end for
158:
159: } // end method testSQLExceptionStringString
160:
161: /*
162: * ConstructorTest
163: */
164: public void testSQLExceptionString() {
165:
166: String[] init1 = { "a", "1", "valid1", "----", "&valid*", null,
167: "", " " };
168:
169: String[] theFinalStates1 = init1;
170: String[] theFinalStates2 = { null, null, null, null, null,
171: null, null, null };
172: int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
173: SQLException[] theFinalStates4 = { null, null, null, null,
174: null, null, null, null };
175:
176: Exception[] theExceptions = { null, null, null, null, null,
177: null, null, null };
178:
179: SQLException aSQLException;
180: int loopCount = init1.length;
181: for (int i = 0; i < loopCount; i++) {
182: try {
183: aSQLException = new SQLException(init1[i]);
184: if (theExceptions[i] != null) {
185: fail();
186: }
187: assertEquals(i + " Final state mismatch",
188: aSQLException.getMessage(), theFinalStates1[i]);
189: assertEquals(i + " Final state mismatch",
190: aSQLException.getSQLState(), theFinalStates2[i]);
191: assertEquals(i + " Final state mismatch",
192: aSQLException.getErrorCode(),
193: theFinalStates3[i]);
194: assertEquals(i + " Final state mismatch",
195: aSQLException.getNextException(),
196: theFinalStates4[i]);
197:
198: } catch (Exception e) {
199: if (theExceptions[i] == null) {
200: fail(i + "Unexpected exception");
201: }
202: assertEquals(i + "Exception mismatch", e.getClass(),
203: theExceptions[i].getClass());
204: assertEquals(i + "Exception mismatch", e.getMessage(),
205: theExceptions[i].getMessage());
206: } // end try
207: } // end for
208:
209: } // end method testSQLExceptionString
210:
211: /*
212: * ConstructorTest
213: */
214: public void testSQLException() {
215:
216: String[] theFinalStates1 = { null };
217: String[] theFinalStates2 = { null };
218: int[] theFinalStates3 = { 0 };
219: SQLException[] theFinalStates4 = { null };
220:
221: Exception[] theExceptions = { null };
222:
223: SQLException aSQLException;
224: int loopCount = 1;
225: for (int i = 0; i < loopCount; i++) {
226: try {
227: aSQLException = new SQLException();
228: if (theExceptions[i] != null) {
229: fail();
230: }
231: assertEquals(i + " Final state mismatch",
232: aSQLException.getMessage(), theFinalStates1[i]);
233: assertEquals(i + " Final state mismatch",
234: aSQLException.getSQLState(), theFinalStates2[i]);
235: assertEquals(i + " Final state mismatch",
236: aSQLException.getErrorCode(),
237: theFinalStates3[i]);
238: assertEquals(i + " Final state mismatch",
239: aSQLException.getNextException(),
240: theFinalStates4[i]);
241:
242: } catch (Exception e) {
243: if (theExceptions[i] == null) {
244: fail(i + "Unexpected exception");
245: }
246: assertEquals(i + "Exception mismatch", e.getClass(),
247: theExceptions[i].getClass());
248: assertEquals(i + "Exception mismatch", e.getMessage(),
249: theExceptions[i].getMessage());
250: } // end try
251: } // end for
252:
253: } // end method testSQLException
254:
255: /*
256: * Method test for getErrorCode
257: */
258: public void testGetErrorCode() {
259:
260: SQLException aSQLException;
261: String[] init1 = { "a", "1", "valid1", "----", null, "&valid*",
262: "1" };
263: String[] init2 = { "a", "1", "valid1", "----", "&valid*", null,
264: "a" };
265: int[] init3 = { -2147483648, 2147483647, 0, 48429456,
266: 1770127344, 1047282235, -545472907 };
267:
268: int theReturn;
269: int[] theReturns = init3;
270: String[] theFinalStates1 = init1;
271: String[] theFinalStates2 = init2;
272: int[] theFinalStates3 = init3;
273: SQLException[] theFinalStates4 = { null, null, null, null,
274: null, null, null };
275:
276: Exception[] theExceptions = { null, null, null, null, null,
277: null, null };
278:
279: int loopCount = 1;
280: for (int i = 0; i < loopCount; i++) {
281: try {
282: aSQLException = new SQLException(init1[i], init2[i],
283: init3[i]);
284: theReturn = aSQLException.getErrorCode();
285: if (theExceptions[i] != null) {
286: fail(i + "Exception missed");
287: }
288: assertEquals(i + "Return value mismatch", theReturn,
289: theReturns[i]);
290: assertEquals(i + " Final state mismatch",
291: aSQLException.getMessage(), theFinalStates1[i]);
292: assertEquals(i + " Final state mismatch",
293: aSQLException.getSQLState(), theFinalStates2[i]);
294: assertEquals(i + " Final state mismatch",
295: aSQLException.getErrorCode(),
296: theFinalStates3[i]);
297: assertEquals(i + " Final state mismatch",
298: aSQLException.getNextException(),
299: theFinalStates4[i]);
300:
301: } catch (Exception e) {
302: if (theExceptions[i] == null) {
303: fail(i + "Unexpected exception");
304: }
305: assertEquals(i + "Exception mismatch", e.getClass(),
306: theExceptions[i].getClass());
307: assertEquals(i + "Exception mismatch", e.getMessage(),
308: theExceptions[i].getMessage());
309: } // end try
310: } // end for
311:
312: } // end method testGetErrorCode
313:
314: /*
315: * Method test for getNextException
316: */
317: public void testGetNextException() {
318:
319: SQLException aSQLException;
320: String[] init1 = { "a", "1", "valid1", "----", null, "&valid*",
321: "1" };
322: String[] init2 = { "a", "1", "valid1", "----", "&valid*", null,
323: "a" };
324: int[] init3 = { -2147483648, 2147483647, 0, 48429456,
325: 1770127344, 1047282235, -545472907 };
326: SQLException[] init4 = { new SQLException(), null,
327: new SQLException(), new SQLException(),
328: new SQLException(), null, new SQLException() };
329:
330: SQLException theReturn;
331: SQLException[] theReturns = init4;
332: String[] theFinalStates1 = init1;
333: String[] theFinalStates2 = init2;
334: int[] theFinalStates3 = init3;
335: SQLException[] theFinalStates4 = init4;
336:
337: Exception[] theExceptions = { null, null, null, null, null,
338: null, null };
339:
340: int loopCount = init1.length;
341: for (int i = 0; i < loopCount; i++) {
342: try {
343: aSQLException = new SQLException(init1[i], init2[i],
344: init3[i]);
345: aSQLException.setNextException(init4[i]);
346: theReturn = aSQLException.getNextException();
347: if (theExceptions[i] != null) {
348: fail(i + "Exception missed");
349: }
350: assertEquals(i + "Return value mismatch", theReturn,
351: theReturns[i]);
352: assertEquals(i + " Final state mismatch",
353: aSQLException.getMessage(), theFinalStates1[i]);
354: assertEquals(i + " Final state mismatch",
355: aSQLException.getSQLState(), theFinalStates2[i]);
356: assertEquals(i + " Final state mismatch",
357: aSQLException.getErrorCode(),
358: theFinalStates3[i]);
359: assertEquals(i + " Final state mismatch",
360: aSQLException.getNextException(),
361: theFinalStates4[i]);
362:
363: } catch (Exception e) {
364: if (theExceptions[i] == null) {
365: fail(i + "Unexpected exception");
366: }
367: assertEquals(i + "Exception mismatch", e.getClass(),
368: theExceptions[i].getClass());
369: assertEquals(i + "Exception mismatch", e.getMessage(),
370: theExceptions[i].getMessage());
371: } // end try
372: } // end for
373:
374: } // end method testGetNextException
375:
376: /*
377: * Method test for getSQLState
378: */
379: public void testGetSQLState() {
380:
381: SQLException aSQLException;
382: String[] init1 = { "a", "1", "valid1", "----", null, "&valid*",
383: "1" };
384: String[] init2 = { "a", "1", "valid1", "----", "&valid*", null,
385: "a" };
386: int[] init3 = { -2147483648, 2147483647, 0, 48429456,
387: 1770127344, 1047282235, -545472907 };
388:
389: String theReturn;
390: String[] theReturns = init2;
391: String[] theFinalStates1 = init1;
392: String[] theFinalStates2 = init2;
393: int[] theFinalStates3 = init3;
394: SQLException[] theFinalStates4 = { null, null, null, null,
395: null, null, null };
396:
397: Exception[] theExceptions = { null, null, null, null, null,
398: null, null };
399:
400: int loopCount = 1;
401: for (int i = 0; i < loopCount; i++) {
402: try {
403: aSQLException = new SQLException(init1[i], init2[i],
404: init3[i]);
405: theReturn = aSQLException.getSQLState();
406: if (theExceptions[i] != null) {
407: fail(i + "Exception missed");
408: }
409: assertEquals(i + "Return value mismatch", theReturn,
410: theReturns[i]);
411: assertEquals(i + " Final state mismatch",
412: aSQLException.getMessage(), theFinalStates1[i]);
413: assertEquals(i + " Final state mismatch",
414: aSQLException.getSQLState(), theFinalStates2[i]);
415: assertEquals(i + " Final state mismatch",
416: aSQLException.getErrorCode(),
417: theFinalStates3[i]);
418: assertEquals(i + " Final state mismatch",
419: aSQLException.getNextException(),
420: theFinalStates4[i]);
421:
422: } catch (Exception e) {
423: if (theExceptions[i] == null) {
424: fail(i + "Unexpected exception");
425: }
426: assertEquals(i + "Exception mismatch", e.getClass(),
427: theExceptions[i].getClass());
428: assertEquals(i + "Exception mismatch", e.getMessage(),
429: theExceptions[i].getMessage());
430: } // end try
431: } // end for
432:
433: } // end method testGetSQLState
434:
435: /*
436: * Method test for setNextException
437: */
438: public void testSetNextExceptionSQLException() {
439:
440: SQLException[] parm1 = { new SQLException(), null,
441: new SQLException(), new SQLException(),
442: new SQLException(), null, new SQLException() };
443:
444: SQLException aSQLException;
445:
446: String[] init1 = { "a", "1", "valid1", "----", null, "&valid*",
447: "1" };
448: String[] init2 = { "a", "1", "valid1", "----", "&valid*", null,
449: "a" };
450: int[] init3 = { -2147483648, 2147483647, 0, 48429456,
451: 1770127344, 1047282235, -545472907 };
452:
453: String[] theFinalStates1 = init1;
454: String[] theFinalStates2 = init2;
455: int[] theFinalStates3 = init3;
456: SQLException[] theFinalStates4 = parm1;
457:
458: Exception[] theExceptions = { null, null, null, null, null,
459: null, null, null, null, null, null };
460:
461: int loopCount = parm1.length;
462: for (int i = 0; i < loopCount; i++) {
463: try {
464: aSQLException = new SQLException(init1[i], init2[i],
465: init3[i]);
466: aSQLException.setNextException(parm1[i]);
467: if (theExceptions[i] != null) {
468: fail(i + "Exception missed");
469: }
470: assertEquals(i + " Final state mismatch",
471: aSQLException.getMessage(), theFinalStates1[i]);
472: assertEquals(i + " Final state mismatch",
473: aSQLException.getSQLState(), theFinalStates2[i]);
474: assertEquals(i + " Final state mismatch",
475: aSQLException.getErrorCode(),
476: theFinalStates3[i]);
477: assertEquals(i + " Final state mismatch",
478: aSQLException.getNextException(),
479: theFinalStates4[i]);
480:
481: } catch (Exception e) {
482: if (theExceptions[i] == null) {
483: fail(i + "Unexpected exception");
484: }
485: assertEquals(i + "Exception mismatch", e.getClass(),
486: theExceptions[i].getClass());
487: assertEquals(i + "Exception mismatch", e.getMessage(),
488: theExceptions[i].getMessage());
489: } // end try
490: } // end for
491:
492: } // end method testSetNextExceptionSQLException
493:
494: /**
495: * @tests serialization/deserialization compatibility.
496: */
497: public void testSerializationSelf() throws Exception {
498: SQLException object = new SQLException();
499: SerializationTest.verifySelf(object, SQLEXCEPTION_COMPARATOR);
500: }
501:
502: /**
503: * @tests serialization/deserialization compatibility with RI.
504: */
505: public void testSerializationCompatibility() throws Exception {
506:
507: SQLException nextSQLException = new SQLException("nextReason",
508: "nextSQLState", 33);
509:
510: int vendorCode = 10;
511: SQLException object = new SQLException("reason", "SQLState",
512: vendorCode);
513:
514: object.setNextException(nextSQLException);
515:
516: SerializationTest.verifyGolden(this , object,
517: SQLEXCEPTION_COMPARATOR);
518: }
519:
520: // comparator for SQLException objects
521: private static final SerializableAssert SQLEXCEPTION_COMPARATOR = new SerializableAssert() {
522: public void assertDeserialized(Serializable initial,
523: Serializable deserialized) {
524:
525: // do common checks for all throwable objects
526: SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(
527: initial, deserialized);
528:
529: SQLException initThr = (SQLException) initial;
530: SQLException dserThr = (SQLException) deserialized;
531:
532: // verify SQLState
533: Assert.assertEquals("SQLState", initThr.getSQLState(),
534: dserThr.getSQLState());
535:
536: // verify vendorCode
537: Assert.assertEquals("vendorCode", initThr.getErrorCode(),
538: dserThr.getErrorCode());
539:
540: // verify next
541: if (initThr.getNextException() == null) {
542: assertNull(dserThr.getNextException());
543: } else {
544: // use the same comparator
545: SQLEXCEPTION_COMPARATOR
546: .assertDeserialized(initThr.getNextException(),
547: dserThr.getNextException());
548: }
549: }
550: };
551:
552: /**
553: * @tests java.sql.SQLException#setNextException(java.sql.SQLException)
554: */
555: public void test_setNextException_LSQLException() {
556: SQLException se1 = new SQLException("reason", "SQLState", 1);
557: SQLException se2 = new SQLException("reason", "SQLState", 2);
558: SQLException se3 = new SQLException("reason", "SQLState", 3);
559: SQLException se4 = new SQLException("reason", "SQLState", 4);
560:
561: se1.setNextException(se2);
562: assertSame(se2, se1.getNextException());
563:
564: se1.setNextException(se3);
565: assertSame(se2, se1.getNextException());
566: assertSame(se3, se2.getNextException());
567: assertNull(se3.getNextException());
568:
569: se3.setNextException(null);
570: assertNull(se3.getNextException());
571:
572: se3.setNextException(se4);
573: assertSame(se4, se3.getNextException());
574: }
575:
576: } // end class SQLExceptionTest
|