/external/apache-harmony/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/SQLWarningTest.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · Java · 450 lines · 314 code · 68 blank · 68 comment · 33 complexity · 376f2658a27d7e2f61c8daa8bebcf568 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 java.io.Serializable;
  19. import java.sql.SQLException;
  20. import java.sql.SQLWarning;
  21. import org.apache.harmony.testframework.serialization.SerializationTest;
  22. import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
  23. import junit.framework.TestCase;
  24. public class SQLWarningTest extends TestCase {
  25. /*
  26. * ConstructorTest
  27. */
  28. public void testSQLWarning() {
  29. String[] theFinalStates1 = { null };
  30. String[] theFinalStates2 = { null };
  31. int[] theFinalStates3 = { 0 };
  32. SQLWarning[] theFinalStates4 = { null };
  33. Exception[] theExceptions = { null };
  34. SQLWarning aSQLWarning;
  35. int loopCount = 1;
  36. for (int i = 0; i < loopCount; i++) {
  37. try {
  38. aSQLWarning = new SQLWarning();
  39. if (theExceptions[i] != null) {
  40. fail();
  41. }
  42. assertEquals(i + " Final state mismatch", aSQLWarning
  43. .getMessage(), theFinalStates1[i]);
  44. assertEquals(i + " Final state mismatch", aSQLWarning
  45. .getSQLState(), theFinalStates2[i]);
  46. assertEquals(i + " Final state mismatch", aSQLWarning
  47. .getErrorCode(), theFinalStates3[i]);
  48. assertEquals(i + " Final state mismatch", aSQLWarning
  49. .getNextWarning(), theFinalStates4[i]);
  50. } catch (Exception e) {
  51. if (theExceptions[i] == null) {
  52. fail(i + "Unexpected exception");
  53. }
  54. assertEquals(i + "Exception mismatch", e.getClass(),
  55. theExceptions[i].getClass());
  56. assertEquals(i + "Exception mismatch", e.getMessage(),
  57. theExceptions[i].getMessage());
  58. } // end try
  59. } // end for
  60. } // end method testSQLWarning
  61. /*
  62. * ConstructorTest
  63. */
  64. public void testSQLWarningString() {
  65. String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "", "\0" };
  66. String[] theFinalStates1 = init1;
  67. String[] theFinalStates2 = { null, null, null, null, null, null, null,
  68. null };
  69. int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
  70. SQLWarning[] theFinalStates4 = { null, null, null, null, null, null,
  71. null, null };
  72. Exception[] theExceptions = { null, null, null, null, null, null, null,
  73. null };
  74. SQLWarning aSQLWarning;
  75. int loopCount = init1.length;
  76. for (int i = 0; i < loopCount; i++) {
  77. try {
  78. aSQLWarning = new SQLWarning(init1[i]);
  79. if (theExceptions[i] != null) {
  80. fail();
  81. }
  82. assertEquals(i + " Final state mismatch", aSQLWarning
  83. .getMessage(), theFinalStates1[i]);
  84. assertEquals(i + " Final state mismatch", aSQLWarning
  85. .getSQLState(), theFinalStates2[i]);
  86. assertEquals(i + " Final state mismatch", aSQLWarning
  87. .getErrorCode(), theFinalStates3[i]);
  88. assertEquals(i + " Final state mismatch", aSQLWarning
  89. .getNextWarning(), theFinalStates4[i]);
  90. } catch (Exception e) {
  91. if (theExceptions[i] == null) {
  92. fail(i + "Unexpected exception");
  93. }
  94. assertEquals(i + "Exception mismatch", e.getClass(),
  95. theExceptions[i].getClass());
  96. assertEquals(i + "Exception mismatch", e.getMessage(),
  97. theExceptions[i].getMessage());
  98. } // end try
  99. } // end for
  100. } // end method testSQLWarningString
  101. /*
  102. * ConstructorTest
  103. */
  104. public void testSQLWarningStringString() {
  105. String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "",
  106. "\0", "a", "a", "a" };
  107. String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a", "a",
  108. "a", null, "", "\0" };
  109. String[] theFinalStates1 = init1;
  110. String[] theFinalStates2 = init2;
  111. int[] theFinalStates3 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  112. SQLWarning[] theFinalStates4 = { null, null, null, null, null, null,
  113. null, null, null, null, null };
  114. Exception[] theExceptions = { null, null, null, null, null, null, null,
  115. null, null, null, null };
  116. SQLWarning aSQLWarning;
  117. int loopCount = init1.length;
  118. for (int i = 0; i < loopCount; i++) {
  119. try {
  120. aSQLWarning = new SQLWarning(init1[i], init2[i]);
  121. if (theExceptions[i] != null) {
  122. fail();
  123. }
  124. assertEquals(i + " Final state mismatch", aSQLWarning
  125. .getMessage(), theFinalStates1[i]);
  126. assertEquals(i + " Final state mismatch", aSQLWarning
  127. .getSQLState(), theFinalStates2[i]);
  128. assertEquals(i + " Final state mismatch", aSQLWarning
  129. .getErrorCode(), theFinalStates3[i]);
  130. assertEquals(i + " Final state mismatch", aSQLWarning
  131. .getNextWarning(), theFinalStates4[i]);
  132. } catch (Exception e) {
  133. if (theExceptions[i] == null) {
  134. fail(i + "Unexpected exception");
  135. }
  136. assertEquals(i + "Exception mismatch", e.getClass(),
  137. theExceptions[i].getClass());
  138. assertEquals(i + "Exception mismatch", e.getMessage(),
  139. theExceptions[i].getMessage());
  140. } // end try
  141. } // end for
  142. } // end method testSQLWarningStringString
  143. /*
  144. * ConstructorTest
  145. */
  146. public void testSQLWarningStringStringint() {
  147. String[] init1 = { "a", "1", "valid1", "----", "&valid*", "----",
  148. "----", null, "", "\0", "a", "a", "a" };
  149. String[] init2 = { "a", "1", "valid1", "----", "&valid*", "valid1",
  150. "----", "a", "a", "a", null, "", "\0" };
  151. int[] init3 = { -2147483648, 2147483647, 0, 1412862821, -733923487,
  152. 488067774, -1529953616, -2147483648, -2147483648, -2147483648,
  153. -2147483648, -2147483648, -2147483648 };
  154. String[] theFinalStates1 = init1;
  155. String[] theFinalStates2 = init2;
  156. int[] theFinalStates3 = init3;
  157. SQLWarning[] theFinalStates4 = { null, null, null, null, null, null,
  158. null, null, null, null, null, null, null };
  159. Exception[] theExceptions = { null, null, null, null, null, null, null,
  160. null, null, null, null, null, null };
  161. SQLWarning aSQLWarning;
  162. int loopCount = init1.length;
  163. for (int i = 0; i < loopCount; i++) {
  164. try {
  165. aSQLWarning = new SQLWarning(init1[i], init2[i], init3[i]);
  166. if (theExceptions[i] != null) {
  167. fail();
  168. }
  169. assertEquals(i + " Final state mismatch", aSQLWarning
  170. .getMessage(), theFinalStates1[i]);
  171. assertEquals(i + " Final state mismatch", aSQLWarning
  172. .getSQLState(), theFinalStates2[i]);
  173. assertEquals(i + " Final state mismatch", aSQLWarning
  174. .getErrorCode(), theFinalStates3[i]);
  175. assertEquals(i + " Final state mismatch", aSQLWarning
  176. .getNextWarning(), theFinalStates4[i]);
  177. } catch (Exception e) {
  178. if (theExceptions[i] == null) {
  179. fail(i + "Unexpected exception");
  180. }
  181. assertEquals(i + "Exception mismatch", e.getClass(),
  182. theExceptions[i].getClass());
  183. assertEquals(i + "Exception mismatch", e.getMessage(),
  184. theExceptions[i].getMessage());
  185. } // end try
  186. } // end for
  187. } // end method testSQLWarningStringStringint
  188. /*
  189. * Method test for getNextWarning
  190. */
  191. public void testGetNextWarning() {
  192. SQLWarning aSQLWarning;
  193. String[] init1 = { "a", "1", "valid1", "----", "&valid*" };
  194. SQLWarning theReturn;
  195. SQLWarning[] theReturns = { null };
  196. String[] theFinalStates1 = init1;
  197. String[] theFinalStates2 = { null };
  198. int[] theFinalStates3 = { 0 };
  199. SQLWarning[] theFinalStates4 = { null };
  200. Exception[] theExceptions = { null };
  201. int loopCount = 1;
  202. for (int i = 0; i < loopCount; i++) {
  203. try {
  204. aSQLWarning = new SQLWarning(init1[i]);
  205. theReturn = aSQLWarning.getNextWarning();
  206. if (theExceptions[i] != null) {
  207. fail(i + "Exception missed");
  208. }
  209. assertEquals(i + "Return value mismatch", theReturn,
  210. theReturns[i]);
  211. assertEquals(i + " Final state mismatch", aSQLWarning
  212. .getMessage(), theFinalStates1[i]);
  213. assertEquals(i + " Final state mismatch", aSQLWarning
  214. .getSQLState(), theFinalStates2[i]);
  215. assertEquals(i + " Final state mismatch", aSQLWarning
  216. .getErrorCode(), theFinalStates3[i]);
  217. assertEquals(i + " Final state mismatch", aSQLWarning
  218. .getNextWarning(), theFinalStates4[i]);
  219. } catch (Exception e) {
  220. if (theExceptions[i] == null) {
  221. fail(i + "Unexpected exception");
  222. }
  223. assertEquals(i + "Exception mismatch", e.getClass(),
  224. theExceptions[i].getClass());
  225. assertEquals(i + "Exception mismatch", e.getMessage(),
  226. theExceptions[i].getMessage());
  227. } // end try
  228. } // end for
  229. } // end method testGetNextWarning
  230. /*
  231. * Method test for setNextWarning
  232. */
  233. public void testSetNextWarningSQLWarning() {
  234. SQLWarning[] parm1 = { new SQLWarning(), null };
  235. SQLWarning aSQLWarning;
  236. String[] init1 = { "a", "1" };
  237. String[] theFinalStates1 = init1;
  238. String[] theFinalStates2 = { null, null };
  239. int[] theFinalStates3 = { 0, 0 };
  240. SQLWarning[] theFinalStates4 = parm1;
  241. Exception[] theExceptions = { null, null };
  242. int loopCount = parm1.length;
  243. for (int i = 0; i < loopCount; i++) {
  244. try {
  245. aSQLWarning = new SQLWarning(init1[i]);
  246. aSQLWarning.setNextWarning(parm1[i]);
  247. if (theExceptions[i] != null) {
  248. fail(i + "Exception missed");
  249. }
  250. assertEquals(i + " Final state mismatch", aSQLWarning
  251. .getMessage(), theFinalStates1[i]);
  252. assertEquals(i + " Final state mismatch", aSQLWarning
  253. .getSQLState(), theFinalStates2[i]);
  254. assertEquals(i + " Final state mismatch", aSQLWarning
  255. .getErrorCode(), theFinalStates3[i]);
  256. assertEquals(i + " Final state mismatch", aSQLWarning
  257. .getNextWarning(), theFinalStates4[i]);
  258. } catch (Exception e) {
  259. if (theExceptions[i] == null) {
  260. fail(i + "Unexpected exception");
  261. }
  262. assertEquals(i + "Exception mismatch", e.getClass(),
  263. theExceptions[i].getClass());
  264. assertEquals(i + "Exception mismatch", e.getMessage(),
  265. theExceptions[i].getMessage());
  266. } // end try
  267. } // end for
  268. } // end method testSetNextWarningSQLWarning
  269. /**
  270. * @tests java.sql.SQLWarning#setNextWarning(java.sql.SQLWarning)
  271. */
  272. public void test_setNextWarning_SQLWarning() {
  273. SQLWarning sw = new SQLWarning("reason", "SQLState", 0);
  274. SQLWarning sw1 = new SQLWarning("reason", "SQLState", 1);
  275. SQLWarning sw2 = new SQLWarning("reason", "SQLState", 2);
  276. SQLWarning sw3 = new SQLWarning("reason", "SQLState", 3);
  277. SQLException se = new SQLException("reason", "SQLState", 4);
  278. sw.setNextWarning(sw1);
  279. assertSame(sw1, sw.getNextException());
  280. assertSame(sw1, sw.getNextWarning());
  281. sw.setNextWarning(sw2);
  282. assertSame(sw2, sw1.getNextException());
  283. assertSame(sw2, sw1.getNextWarning());
  284. sw.setNextException(sw3);
  285. assertSame(sw3, sw2.getNextException());
  286. assertSame(sw3, sw2.getNextWarning());
  287. sw.setNextException(se);
  288. assertSame(se, sw3.getNextException());
  289. try {
  290. sw3.getNextWarning();
  291. fail("should throw Error");
  292. } catch (Error e) {
  293. // expected
  294. }
  295. }
  296. /**
  297. * @tests {@link java.sql.SQLWarning#SQLWarning(Throwable)}
  298. *
  299. * @since 1.6
  300. */
  301. public void testConstructor_Ljava_lang_Throwable() {
  302. Exception e = new Exception("error");
  303. SQLWarning exception = new SQLWarning(e);
  304. assertEquals(e, exception.getCause());
  305. assertEquals("error", exception.getCause().getMessage());
  306. }
  307. /**
  308. * @tests {@link java.sql.SQLWarning#SQLWarning(String, Throwable)}
  309. *
  310. * @since 1.6
  311. */
  312. public void testConstructor_LString_Throwable() {
  313. Exception e = new Exception("error");
  314. SQLWarning exception = new SQLWarning("reason", e);
  315. assertEquals(e, exception.getCause());
  316. assertEquals("error", exception.getCause().getMessage());
  317. assertEquals("reason", exception.getMessage());
  318. }
  319. /**
  320. * @tests {@link java.sql.SQLWarning#SQLWarning(String, String, Throwable)}
  321. *
  322. * @since 1.6
  323. */
  324. public void testConstructor_LString_String_Throwable() {
  325. Exception e = new Exception("error");
  326. SQLWarning exception = new SQLWarning("reason", "state", e);
  327. assertEquals(e, exception.getCause());
  328. assertEquals("error", exception.getCause().getMessage());
  329. assertEquals("reason", exception.getMessage());
  330. assertEquals("state", exception.getSQLState());
  331. }
  332. /**
  333. * @tests {@link java.sql.SQLWarning#SQLWarning(String, String, int, Throwable)}
  334. *
  335. * @since 1.6
  336. */
  337. public void testConstructor_LString_String_I_Throwable() {
  338. Exception e = new Exception("error");
  339. SQLWarning exception = new SQLWarning("reason", "state", 18, e);
  340. assertEquals(e, exception.getCause());
  341. assertEquals("error", exception.getCause().getMessage());
  342. assertEquals("reason", exception.getMessage());
  343. assertEquals("state", exception.getSQLState());
  344. assertEquals(18, exception.getErrorCode());
  345. }
  346. /**
  347. * @tests serialization/deserialization compatibility.
  348. */
  349. public void testSerializationSelf() throws Exception {
  350. SQLWarning object = new SQLWarning();
  351. SerializationTest.verifySelf(object, SQLWARNING_COMPARATOR);
  352. }
  353. /**
  354. * @tests serialization/deserialization compatibility with RI.
  355. */
  356. public void testSerializationCompatibility() throws Exception {
  357. SQLWarning object = new SQLWarning();
  358. SQLWarning nextSQLWarning = new SQLWarning("nextReason",
  359. "nextSQLState", 10);
  360. object.setNextWarning(nextSQLWarning);
  361. SerializationTest.verifyGolden(this, object, SQLWARNING_COMPARATOR);
  362. }
  363. // comparator for SQLWarning objects
  364. private static final SerializableAssert SQLWARNING_COMPARATOR = new SerializableAssert() {
  365. public void assertDeserialized(Serializable initial,
  366. Serializable deserialized) {
  367. // do common checks for all throwable objects
  368. SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
  369. deserialized);
  370. SQLWarning initThr = (SQLWarning) initial;
  371. SQLWarning dserThr = (SQLWarning) deserialized;
  372. // verify getNextWarning() method
  373. if (initThr.getNextWarning() == null) {
  374. assertNull(dserThr.getNextWarning());
  375. } else {
  376. // use the same comparator
  377. SQLWARNING_COMPARATOR.assertDeserialized(initThr
  378. .getNextWarning(), dserThr.getNextWarning());
  379. }
  380. }
  381. };
  382. } // end class SQLWarningTest