/platform/libcore/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/SQLWarningTest.java

https://github.com/lems111/Intercept-CM6-Kernel · Java · 474 lines · 360 code · 66 blank · 48 comment · 33 complexity · cc0030d36cf25753adeffea7e6d19b02 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.harmony.sql.tests.java.sql;
  18. import dalvik.annotation.TestTargetClass;
  19. import dalvik.annotation.TestTargets;
  20. import dalvik.annotation.TestLevel;
  21. import dalvik.annotation.TestTargetNew;
  22. import java.io.Serializable;
  23. import java.sql.SQLException;
  24. import java.sql.SQLWarning;
  25. import org.apache.harmony.testframework.serialization.SerializationTest;
  26. import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
  27. import junit.framework.TestCase;
  28. @TestTargetClass(SQLWarning.class)
  29. public class SQLWarningTest extends TestCase {
  30. /*
  31. * ConstructorTest
  32. */
  33. @TestTargetNew(
  34. level = TestLevel.PARTIAL_COMPLETE,
  35. notes = "",
  36. method = "SQLWarning",
  37. args = {}
  38. )
  39. public void testSQLWarning() {
  40. String[] theFinalStates1 = { null };
  41. String[] theFinalStates2 = { null };
  42. int[] theFinalStates3 = { 0 };
  43. SQLWarning[] theFinalStates4 = { null };
  44. Exception[] theExceptions = { null };
  45. SQLWarning aSQLWarning;
  46. int loopCount = 1;
  47. for (int i = 0; i < loopCount; i++) {
  48. try {
  49. aSQLWarning = new SQLWarning();
  50. if (theExceptions[i] != null) {
  51. fail();
  52. }
  53. assertEquals(i + " Final state mismatch", aSQLWarning
  54. .getMessage(), theFinalStates1[i]);
  55. assertEquals(i + " Final state mismatch", aSQLWarning
  56. .getSQLState(), theFinalStates2[i]);
  57. assertEquals(i + " Final state mismatch", aSQLWarning
  58. .getErrorCode(), theFinalStates3[i]);
  59. assertEquals(i + " Final state mismatch", aSQLWarning
  60. .getNextWarning(), theFinalStates4[i]);
  61. } catch (Exception e) {
  62. if (theExceptions[i] == null) {
  63. fail(i + "Unexpected exception");
  64. }
  65. assertEquals(i + "Exception mismatch", e.getClass(),
  66. theExceptions[i].getClass());
  67. assertEquals(i + "Exception mismatch", e.getMessage(),
  68. theExceptions[i].getMessage());
  69. } // end try
  70. } // end for
  71. } // end method testSQLWarning
  72. /*
  73. * ConstructorTest
  74. */
  75. @TestTargetNew(
  76. level = TestLevel.COMPLETE,
  77. notes = "",
  78. method = "SQLWarning",
  79. args = {java.lang.String.class}
  80. )
  81. public void testSQLWarningString() {
  82. String[] init1 = { "a", "1", "valid1", "----", "&valid*", null,
  83. "", "\u0000" };
  84. String[] theFinalStates1 = init1;
  85. String[] theFinalStates2 = { null, null, null, null, null, null, null,
  86. null };
  87. int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
  88. SQLWarning[] theFinalStates4 = { null, null, null, null, null, null,
  89. null, null };
  90. Exception[] theExceptions = { null, null, null, null, null, null, null,
  91. null };
  92. SQLWarning aSQLWarning;
  93. int loopCount = init1.length;
  94. for (int i = 0; i < loopCount; i++) {
  95. try {
  96. aSQLWarning = new SQLWarning(init1[i]);
  97. if (theExceptions[i] != null) {
  98. fail();
  99. }
  100. assertEquals(i + " Final state mismatch", aSQLWarning
  101. .getMessage(), theFinalStates1[i]);
  102. assertEquals(i + " Final state mismatch", aSQLWarning
  103. .getSQLState(), theFinalStates2[i]);
  104. assertEquals(i + " Final state mismatch", aSQLWarning
  105. .getErrorCode(), theFinalStates3[i]);
  106. assertEquals(i + " Final state mismatch", aSQLWarning
  107. .getNextWarning(), theFinalStates4[i]);
  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. } // end method testSQLWarningString
  119. /*
  120. * ConstructorTest
  121. */
  122. @TestTargetNew(
  123. level = TestLevel.COMPLETE,
  124. notes = "",
  125. method = "SQLWarning",
  126. args = {java.lang.String.class, java.lang.String.class}
  127. )
  128. public void testSQLWarningStringString() {
  129. String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "",
  130. "\u0000", "a", "a", "a" };
  131. String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a", "a",
  132. "a", null, "", "\u0000" };
  133. String[] theFinalStates1 = init1;
  134. String[] theFinalStates2 = init2;
  135. int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  136. SQLWarning[] theFinalStates4 = { null, null, null, null, null, null,
  137. null, null, null, null, null };
  138. Exception[] theExceptions = { null, null, null, null, null, null, null,
  139. null, null, null, null };
  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. } catch (Exception e) {
  157. if (theExceptions[i] == null) {
  158. fail(i + "Unexpected exception");
  159. }
  160. assertEquals(i + "Exception mismatch", e.getClass(),
  161. theExceptions[i].getClass());
  162. assertEquals(i + "Exception mismatch", e.getMessage(),
  163. theExceptions[i].getMessage());
  164. } // end try
  165. } // end for
  166. } // end method testSQLWarningStringString
  167. /*
  168. * ConstructorTest
  169. */
  170. @TestTargetNew(
  171. level = TestLevel.PARTIAL_COMPLETE,
  172. notes = "",
  173. method = "SQLWarning",
  174. args = {java.lang.String.class, java.lang.String.class, int.class}
  175. )
  176. public void testSQLWarningStringStringint() {
  177. String[] init1 = { "a", "1", "valid1", "----", "&valid*", "----",
  178. "----", null, "", "\u0000", "a", "a", "a" };
  179. String[] init2 = { "a", "1", "valid1", "----", "&valid*", "valid1",
  180. "----", "a", "a", "a", null, "", "\u0000" };
  181. int[] init3 = { -2147483648, 2147483647, 0, 1412862821, -733923487,
  182. 488067774, -1529953616, -2147483648, -2147483648, -2147483648,
  183. -2147483648, -2147483648, -2147483648 };
  184. String[] theFinalStates1 = init1;
  185. String[] theFinalStates2 = init2;
  186. int[] theFinalStates3 = init3;
  187. SQLWarning[] theFinalStates4 = { null, null, null, null, null, null,
  188. null, null, null, null, null, null, null };
  189. Exception[] theExceptions = { null, null, null, null, null, null, null,
  190. null, null, null, null, null, null };
  191. SQLWarning aSQLWarning;
  192. int loopCount = init1.length;
  193. for (int i = 0; i < loopCount; i++) {
  194. try {
  195. aSQLWarning = new SQLWarning(init1[i], init2[i], init3[i]);
  196. if (theExceptions[i] != null) {
  197. fail();
  198. }
  199. assertEquals(i + " Final state mismatch", aSQLWarning
  200. .getMessage(), theFinalStates1[i]);
  201. assertEquals(i + " Final state mismatch", aSQLWarning
  202. .getSQLState(), theFinalStates2[i]);
  203. assertEquals(i + " Final state mismatch", aSQLWarning
  204. .getErrorCode(), theFinalStates3[i]);
  205. assertEquals(i + " Final state mismatch", aSQLWarning
  206. .getNextWarning(), theFinalStates4[i]);
  207. } catch (Exception e) {
  208. if (theExceptions[i] == null) {
  209. fail(i + "Unexpected exception");
  210. }
  211. assertEquals(i + "Exception mismatch", e.getClass(),
  212. theExceptions[i].getClass());
  213. assertEquals(i + "Exception mismatch", e.getMessage(),
  214. theExceptions[i].getMessage());
  215. } // end try
  216. } // end for
  217. } // end method testSQLWarningStringStringint
  218. /*
  219. * Method test for getNextWarning
  220. */
  221. @TestTargetNew(
  222. level = TestLevel.PARTIAL_COMPLETE,
  223. notes = "",
  224. method = "getNextWarning",
  225. args = {}
  226. )
  227. public void testGetNextWarning() {
  228. SQLWarning aSQLWarning;
  229. String[] init1 = { "a", "1", "valid1", "----", "&valid*" };
  230. SQLWarning theReturn;
  231. SQLWarning[] theReturns = { null };
  232. String[] theFinalStates1 = init1;
  233. String[] theFinalStates2 = { null };
  234. int[] theFinalStates3 = { 0 };
  235. SQLWarning[] theFinalStates4 = { null };
  236. Exception[] theExceptions = { null };
  237. int loopCount = 1;
  238. for (int i = 0; i < loopCount; i++) {
  239. try {
  240. aSQLWarning = new SQLWarning(init1[i]);
  241. theReturn = aSQLWarning.getNextWarning();
  242. if (theExceptions[i] != null) {
  243. fail(i + "Exception missed");
  244. }
  245. assertEquals(i + "Return value mismatch", theReturn,
  246. theReturns[i]);
  247. assertEquals(i + " Final state mismatch", aSQLWarning
  248. .getMessage(), theFinalStates1[i]);
  249. assertEquals(i + " Final state mismatch", aSQLWarning
  250. .getSQLState(), theFinalStates2[i]);
  251. assertEquals(i + " Final state mismatch", aSQLWarning
  252. .getErrorCode(), theFinalStates3[i]);
  253. assertEquals(i + " Final state mismatch", aSQLWarning
  254. .getNextWarning(), theFinalStates4[i]);
  255. } catch (Exception e) {
  256. if (theExceptions[i] == null) {
  257. fail(i + "Unexpected exception");
  258. }
  259. assertEquals(i + "Exception mismatch", e.getClass(),
  260. theExceptions[i].getClass());
  261. assertEquals(i + "Exception mismatch", e.getMessage(),
  262. theExceptions[i].getMessage());
  263. } // end try
  264. } // end for
  265. } // end method testGetNextWarning
  266. /*
  267. * Method test for setNextWarning
  268. */
  269. @TestTargets({
  270. @TestTargetNew(
  271. level = TestLevel.PARTIAL_COMPLETE,
  272. notes = "",
  273. method = "setNextWarning",
  274. args = {java.sql.SQLWarning.class}
  275. ),
  276. @TestTargetNew(
  277. level = TestLevel.PARTIAL_COMPLETE,
  278. notes = "",
  279. method = "getNextWarning",
  280. args = {}
  281. )
  282. })
  283. public void testSetNextWarningSQLWarning() {
  284. SQLWarning[] parm1 = { new SQLWarning(), null };
  285. SQLWarning aSQLWarning;
  286. String[] init1 = { "a", "1" };
  287. String[] theFinalStates1 = init1;
  288. String[] theFinalStates2 = { null, null };
  289. int[] theFinalStates3 = { 0, 0 };
  290. SQLWarning[] theFinalStates4 = parm1;
  291. Exception[] theExceptions = { null, null };
  292. int loopCount = parm1.length;
  293. for (int i = 0; i < loopCount; i++) {
  294. try {
  295. aSQLWarning = new SQLWarning(init1[i]);
  296. aSQLWarning.setNextWarning(parm1[i]);
  297. if (theExceptions[i] != null) {
  298. fail(i + "Exception missed");
  299. }
  300. assertEquals(i + " Final state mismatch", aSQLWarning
  301. .getMessage(), theFinalStates1[i]);
  302. assertEquals(i + " Final state mismatch", aSQLWarning
  303. .getSQLState(), theFinalStates2[i]);
  304. assertEquals(i + " Final state mismatch", aSQLWarning
  305. .getErrorCode(), theFinalStates3[i]);
  306. assertEquals(i + " Final state mismatch", aSQLWarning
  307. .getNextWarning(), theFinalStates4[i]);
  308. } catch (Exception e) {
  309. if (theExceptions[i] == null) {
  310. fail(i + "Unexpected exception");
  311. }
  312. assertEquals(i + "Exception mismatch", e.getClass(),
  313. theExceptions[i].getClass());
  314. assertEquals(i + "Exception mismatch", e.getMessage(),
  315. theExceptions[i].getMessage());
  316. } // end try
  317. } // end for
  318. } // end method testSetNextWarningSQLWarning
  319. /**
  320. * @tests java.sql.SQLWarning#setNextWarning(java.sql.SQLWarning)
  321. */
  322. @TestTargets({
  323. @TestTargetNew(
  324. level = TestLevel.PARTIAL_COMPLETE,
  325. notes = "",
  326. method = "setNextWarning",
  327. args = {java.sql.SQLWarning.class}
  328. ),
  329. @TestTargetNew(
  330. level = TestLevel.PARTIAL_COMPLETE,
  331. notes = "",
  332. method = "getNextWarning",
  333. args = {}
  334. )
  335. })
  336. public void test_setNextWarning_SQLWarning() {
  337. SQLWarning sw = new SQLWarning("reason", "SQLState", 0);
  338. SQLWarning sw1 = new SQLWarning("reason", "SQLState", 1);
  339. SQLWarning sw2 = new SQLWarning("reason", "SQLState", 2);
  340. SQLWarning sw3 = new SQLWarning("reason", "SQLState", 3);
  341. SQLException se = new SQLException("reason", "SQLState", 4);
  342. sw.setNextWarning(sw1);
  343. assertSame(sw1, sw.getNextException());
  344. assertSame(sw1, sw.getNextWarning());
  345. sw.setNextWarning(sw2);
  346. assertSame(sw2, sw1.getNextException());
  347. assertSame(sw2, sw1.getNextWarning());
  348. sw.setNextException(sw3);
  349. assertSame(sw3, sw2.getNextException());
  350. assertSame(sw3, sw2.getNextWarning());
  351. sw.setNextException(se);
  352. assertSame(se, sw3.getNextException());
  353. try {
  354. sw3.getNextWarning();
  355. fail("should throw Error");
  356. } catch (Error e) {
  357. //expected
  358. }
  359. }
  360. /**
  361. * @tests serialization/deserialization compatibility.
  362. */
  363. @TestTargetNew(
  364. level = TestLevel.PARTIAL_COMPLETE,
  365. notes = "Serialization test",
  366. method = "SQLWarning",
  367. args = {}
  368. )
  369. public void testSerializationSelf() throws Exception {
  370. SQLWarning object = new SQLWarning();
  371. SerializationTest.verifySelf(object, SQLWARNING_COMPARATOR);
  372. }
  373. /**
  374. * @tests serialization/deserialization compatibility with RI.
  375. */
  376. @TestTargetNew(
  377. level = TestLevel.PARTIAL_COMPLETE,
  378. notes = "Serialization test",
  379. method = "SQLWarning",
  380. args = {java.lang.String.class, java.lang.String.class, int.class}
  381. )
  382. public void testSerializationCompatibility() throws Exception {
  383. SQLWarning object = new SQLWarning();
  384. SQLWarning nextSQLWarning = new SQLWarning("nextReason",
  385. "nextSQLState", 10);
  386. object.setNextWarning(nextSQLWarning);
  387. SerializationTest.verifyGolden(this, object, SQLWARNING_COMPARATOR);
  388. }
  389. // comparator for SQLWarning objects
  390. private static final SerializableAssert SQLWARNING_COMPARATOR = new SerializableAssert() {
  391. public void assertDeserialized(Serializable initial,
  392. Serializable deserialized) {
  393. // do common checks for all throwable objects
  394. SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
  395. deserialized);
  396. SQLWarning initThr = (SQLWarning) initial;
  397. SQLWarning dserThr = (SQLWarning) deserialized;
  398. // verify getNextWarning() method
  399. if (initThr.getNextWarning() == null) {
  400. assertNull(dserThr.getNextWarning());
  401. } else {
  402. // use the same comparator
  403. SQLWARNING_COMPARATOR.assertDeserialized(initThr
  404. .getNextWarning(), dserThr.getNextWarning());
  405. }
  406. }
  407. };
  408. } // end class SQLWarningTest