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.sql.SQLException;
022: import java.sql.SQLWarning;
023:
024: import org.apache.harmony.testframework.serialization.SerializationTest;
025: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
026:
027: import junit.framework.TestCase;
028:
029: public class SQLWarningTest extends TestCase {
030:
031: /*
032: * ConstructorTest
033: */
034: public void testSQLWarning() {
035:
036: String[] theFinalStates1 = { null };
037: String[] theFinalStates2 = { null };
038: int[] theFinalStates3 = { 0 };
039: SQLWarning[] theFinalStates4 = { null };
040:
041: Exception[] theExceptions = { null };
042:
043: SQLWarning aSQLWarning;
044: int loopCount = 1;
045: for (int i = 0; i < loopCount; i++) {
046: try {
047: aSQLWarning = new SQLWarning();
048: if (theExceptions[i] != null) {
049: fail();
050: }
051: assertEquals(i + " Final state mismatch", aSQLWarning
052: .getMessage(), theFinalStates1[i]);
053: assertEquals(i + " Final state mismatch", aSQLWarning
054: .getSQLState(), theFinalStates2[i]);
055: assertEquals(i + " Final state mismatch", aSQLWarning
056: .getErrorCode(), theFinalStates3[i]);
057: assertEquals(i + " Final state mismatch", aSQLWarning
058: .getNextWarning(), theFinalStates4[i]);
059:
060: } catch (Exception e) {
061: if (theExceptions[i] == null) {
062: fail(i + "Unexpected exception");
063: }
064: assertEquals(i + "Exception mismatch", e.getClass(),
065: theExceptions[i].getClass());
066: assertEquals(i + "Exception mismatch", e.getMessage(),
067: theExceptions[i].getMessage());
068: } // end try
069: } // end for
070:
071: } // end method testSQLWarning
072:
073: /*
074: * ConstructorTest
075: */
076: public void testSQLWarningString() {
077:
078: String[] init1 = { "a", "1", "valid1", "----", "&valid*", null,
079: "", " " };
080:
081: String[] theFinalStates1 = init1;
082: String[] theFinalStates2 = { null, null, null, null, null,
083: null, null, null };
084: int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
085: SQLWarning[] theFinalStates4 = { null, null, null, null, null,
086: null, null, null };
087:
088: Exception[] theExceptions = { null, null, null, null, null,
089: null, null, null };
090:
091: SQLWarning aSQLWarning;
092: int loopCount = init1.length;
093: for (int i = 0; i < loopCount; i++) {
094: try {
095: aSQLWarning = new SQLWarning(init1[i]);
096: if (theExceptions[i] != null) {
097: fail();
098: }
099: assertEquals(i + " Final state mismatch", aSQLWarning
100: .getMessage(), theFinalStates1[i]);
101: assertEquals(i + " Final state mismatch", aSQLWarning
102: .getSQLState(), theFinalStates2[i]);
103: assertEquals(i + " Final state mismatch", aSQLWarning
104: .getErrorCode(), theFinalStates3[i]);
105: assertEquals(i + " Final state mismatch", aSQLWarning
106: .getNextWarning(), theFinalStates4[i]);
107:
108: } catch (Exception e) {
109: if (theExceptions[i] == null) {
110: fail(i + "Unexpected exception");
111: }
112: assertEquals(i + "Exception mismatch", e.getClass(),
113: theExceptions[i].getClass());
114: assertEquals(i + "Exception mismatch", e.getMessage(),
115: theExceptions[i].getMessage());
116: } // end try
117: } // end for
118:
119: } // end method testSQLWarningString
120:
121: /*
122: * ConstructorTest
123: */
124: public void testSQLWarningStringString() {
125:
126: String[] init1 = { "a", "1", "valid1", "----", "&valid*", null,
127: "", " ", "a", "a", "a" };
128: String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a",
129: "a", "a", null, "", " " };
130:
131: String[] theFinalStates1 = init1;
132: String[] theFinalStates2 = init2;
133: int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
134: SQLWarning[] theFinalStates4 = { null, null, null, null, null,
135: null, null, null, null, null, null };
136:
137: Exception[] theExceptions = { null, null, null, null, null,
138: null, null, null, null, null, null };
139:
140: SQLWarning aSQLWarning;
141: int loopCount = init1.length;
142: for (int i = 0; i < loopCount; i++) {
143: try {
144: aSQLWarning = new SQLWarning(init1[i], init2[i]);
145: if (theExceptions[i] != null) {
146: fail();
147: }
148: assertEquals(i + " Final state mismatch", aSQLWarning
149: .getMessage(), theFinalStates1[i]);
150: assertEquals(i + " Final state mismatch", aSQLWarning
151: .getSQLState(), theFinalStates2[i]);
152: assertEquals(i + " Final state mismatch", aSQLWarning
153: .getErrorCode(), theFinalStates3[i]);
154: assertEquals(i + " Final state mismatch", aSQLWarning
155: .getNextWarning(), theFinalStates4[i]);
156:
157: } catch (Exception e) {
158: if (theExceptions[i] == null) {
159: fail(i + "Unexpected exception");
160: }
161: assertEquals(i + "Exception mismatch", e.getClass(),
162: theExceptions[i].getClass());
163: assertEquals(i + "Exception mismatch", e.getMessage(),
164: theExceptions[i].getMessage());
165: } // end try
166: } // end for
167:
168: } // end method testSQLWarningStringString
169:
170: /*
171: * ConstructorTest
172: */
173: public void testSQLWarningStringStringint() {
174:
175: String[] init1 = { "a", "1", "valid1", "----", "&valid*",
176: "----", "----", null, "", " ", "a", "a", "a" };
177: String[] init2 = { "a", "1", "valid1", "----", "&valid*",
178: "valid1", "----", "a", "a", "a", null, "", " " };
179: int[] init3 = { -2147483648, 2147483647, 0, 1412862821,
180: -733923487, 488067774, -1529953616, -2147483648,
181: -2147483648, -2147483648, -2147483648, -2147483648,
182: -2147483648 };
183:
184: String[] theFinalStates1 = init1;
185: String[] theFinalStates2 = init2;
186: int[] theFinalStates3 = init3;
187: SQLWarning[] theFinalStates4 = { null, null, null, null, null,
188: null, null, null, null, null, null, null, null };
189:
190: Exception[] theExceptions = { null, null, null, null, null,
191: null, null, null, null, null, null, null, null };
192:
193: SQLWarning aSQLWarning;
194: int loopCount = init1.length;
195: for (int i = 0; i < loopCount; i++) {
196: try {
197: aSQLWarning = new SQLWarning(init1[i], init2[i],
198: init3[i]);
199: if (theExceptions[i] != null) {
200: fail();
201: }
202: assertEquals(i + " Final state mismatch", aSQLWarning
203: .getMessage(), theFinalStates1[i]);
204: assertEquals(i + " Final state mismatch", aSQLWarning
205: .getSQLState(), theFinalStates2[i]);
206: assertEquals(i + " Final state mismatch", aSQLWarning
207: .getErrorCode(), theFinalStates3[i]);
208: assertEquals(i + " Final state mismatch", aSQLWarning
209: .getNextWarning(), theFinalStates4[i]);
210:
211: } catch (Exception e) {
212: if (theExceptions[i] == null) {
213: fail(i + "Unexpected exception");
214: }
215: assertEquals(i + "Exception mismatch", e.getClass(),
216: theExceptions[i].getClass());
217: assertEquals(i + "Exception mismatch", e.getMessage(),
218: theExceptions[i].getMessage());
219: } // end try
220: } // end for
221:
222: } // end method testSQLWarningStringStringint
223:
224: /*
225: * Method test for getNextWarning
226: */
227: public void testGetNextWarning() {
228:
229: SQLWarning aSQLWarning;
230: String[] init1 = { "a", "1", "valid1", "----", "&valid*" };
231:
232: SQLWarning theReturn;
233: SQLWarning[] theReturns = { null };
234: String[] theFinalStates1 = init1;
235: String[] theFinalStates2 = { null };
236: int[] theFinalStates3 = { 0 };
237: SQLWarning[] theFinalStates4 = { null };
238:
239: Exception[] theExceptions = { null };
240:
241: int loopCount = 1;
242: for (int i = 0; i < loopCount; i++) {
243: try {
244: aSQLWarning = new SQLWarning(init1[i]);
245: theReturn = aSQLWarning.getNextWarning();
246: if (theExceptions[i] != null) {
247: fail(i + "Exception missed");
248: }
249: assertEquals(i + "Return value mismatch", theReturn,
250: theReturns[i]);
251: assertEquals(i + " Final state mismatch", aSQLWarning
252: .getMessage(), theFinalStates1[i]);
253: assertEquals(i + " Final state mismatch", aSQLWarning
254: .getSQLState(), theFinalStates2[i]);
255: assertEquals(i + " Final state mismatch", aSQLWarning
256: .getErrorCode(), theFinalStates3[i]);
257: assertEquals(i + " Final state mismatch", aSQLWarning
258: .getNextWarning(), theFinalStates4[i]);
259:
260: } catch (Exception e) {
261: if (theExceptions[i] == null) {
262: fail(i + "Unexpected exception");
263: }
264: assertEquals(i + "Exception mismatch", e.getClass(),
265: theExceptions[i].getClass());
266: assertEquals(i + "Exception mismatch", e.getMessage(),
267: theExceptions[i].getMessage());
268: } // end try
269: } // end for
270:
271: } // end method testGetNextWarning
272:
273: /*
274: * Method test for setNextWarning
275: */
276: public void testSetNextWarningSQLWarning() {
277:
278: SQLWarning[] parm1 = { new SQLWarning(), null };
279:
280: SQLWarning aSQLWarning;
281: String[] init1 = { "a", "1" };
282:
283: String[] theFinalStates1 = init1;
284: String[] theFinalStates2 = { null, null };
285: int[] theFinalStates3 = { 0, 0 };
286: SQLWarning[] theFinalStates4 = parm1;
287:
288: Exception[] theExceptions = { null, null };
289:
290: int loopCount = parm1.length;
291: for (int i = 0; i < loopCount; i++) {
292: try {
293: aSQLWarning = new SQLWarning(init1[i]);
294: aSQLWarning.setNextWarning(parm1[i]);
295: if (theExceptions[i] != null) {
296: fail(i + "Exception missed");
297: }
298: assertEquals(i + " Final state mismatch", aSQLWarning
299: .getMessage(), theFinalStates1[i]);
300: assertEquals(i + " Final state mismatch", aSQLWarning
301: .getSQLState(), theFinalStates2[i]);
302: assertEquals(i + " Final state mismatch", aSQLWarning
303: .getErrorCode(), theFinalStates3[i]);
304: assertEquals(i + " Final state mismatch", aSQLWarning
305: .getNextWarning(), theFinalStates4[i]);
306:
307: } catch (Exception e) {
308: if (theExceptions[i] == null) {
309: fail(i + "Unexpected exception");
310: }
311: assertEquals(i + "Exception mismatch", e.getClass(),
312: theExceptions[i].getClass());
313: assertEquals(i + "Exception mismatch", e.getMessage(),
314: theExceptions[i].getMessage());
315: } // end try
316: } // end for
317:
318: } // end method testSetNextWarningSQLWarning
319:
320: /**
321: * @tests java.sql.SQLWarning#setNextWarning(java.sql.SQLWarning)
322: */
323: public void test_setNextWarning_SQLWarning() {
324: SQLWarning sw = new SQLWarning("reason", "SQLState", 0);
325: SQLWarning sw1 = new SQLWarning("reason", "SQLState", 1);
326: SQLWarning sw2 = new SQLWarning("reason", "SQLState", 2);
327: SQLWarning sw3 = new SQLWarning("reason", "SQLState", 3);
328:
329: SQLException se = new SQLException("reason", "SQLState", 4);
330:
331: sw.setNextWarning(sw1);
332: assertSame(sw1, sw.getNextException());
333: assertSame(sw1, sw.getNextWarning());
334:
335: sw.setNextWarning(sw2);
336: assertSame(sw2, sw1.getNextException());
337: assertSame(sw2, sw1.getNextWarning());
338:
339: sw.setNextException(sw3);
340: assertSame(sw3, sw2.getNextException());
341: assertSame(sw3, sw2.getNextWarning());
342:
343: sw.setNextException(se);
344: assertSame(se, sw3.getNextException());
345: try {
346: sw3.getNextWarning();
347: fail("should throw Error");
348: } catch (Error e) {
349: // expected
350: }
351: }
352:
353: /**
354: * @tests serialization/deserialization compatibility.
355: */
356: public void testSerializationSelf() throws Exception {
357: SQLWarning object = new SQLWarning();
358: SerializationTest.verifySelf(object, SQLWARNING_COMPARATOR);
359: }
360:
361: /**
362: * @tests serialization/deserialization compatibility with RI.
363: */
364: public void testSerializationCompatibility() throws Exception {
365: SQLWarning object = new SQLWarning();
366:
367: SQLWarning nextSQLWarning = new SQLWarning("nextReason",
368: "nextSQLState", 10);
369:
370: object.setNextWarning(nextSQLWarning);
371:
372: SerializationTest.verifyGolden(this , object,
373: SQLWARNING_COMPARATOR);
374: }
375:
376: // comparator for SQLWarning objects
377: private static final SerializableAssert SQLWARNING_COMPARATOR = new SerializableAssert() {
378: public void assertDeserialized(Serializable initial,
379: Serializable deserialized) {
380:
381: // do common checks for all throwable objects
382: SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(
383: initial, deserialized);
384:
385: SQLWarning initThr = (SQLWarning) initial;
386: SQLWarning dserThr = (SQLWarning) deserialized;
387:
388: // verify getNextWarning() method
389: if (initThr.getNextWarning() == null) {
390: assertNull(dserThr.getNextWarning());
391: } else {
392: // use the same comparator
393: SQLWARNING_COMPARATOR.assertDeserialized(initThr
394: .getNextWarning(), dserThr.getNextWarning());
395: }
396: }
397: };
398:
399: } // end class SQLWarningTest
|