/luni/src/test/java/org/apache/harmony/sql/tests/java/sql/BatchUpdateExceptionTest.java

https://github.com/MIPS/libcore · Java · 438 lines · 329 code · 56 blank · 53 comment · 30 complexity · 6723516f21e99f763683a2d1884abdab 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.BatchUpdateException;
  24. import java.util.Arrays;
  25. import junit.framework.TestCase;
  26. import org.apache.harmony.testframework.serialization.SerializationTest;
  27. import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
  28. @TestTargetClass(BatchUpdateException.class)
  29. public class BatchUpdateExceptionTest extends TestCase {
  30. /*
  31. * ConstructorTest
  32. */
  33. @TestTargetNew(
  34. level = TestLevel.COMPLETE,
  35. notes = "",
  36. method = "BatchUpdateException",
  37. args = {}
  38. )
  39. public void testBatchUpdateException() {
  40. int[] theFinalStates1 = { 0 }; // Error Code state
  41. int[][] theFinalStates2 = { null }; // Update Counts array state
  42. String[] theFinalStates3 = { null }; // SQL State state value
  43. String[] theFinalStates4 = { null }; // Message state
  44. Exception[] theExceptions = { null };
  45. BatchUpdateException aBatchUpdateException;
  46. int loopCount = 1;
  47. for (int i = 0; i < loopCount; i++) {
  48. try {
  49. aBatchUpdateException = new BatchUpdateException();
  50. if (theExceptions[i] != null) {
  51. fail();
  52. }
  53. assertEquals(i + " Final state mismatch: ",
  54. aBatchUpdateException.getErrorCode(),
  55. theFinalStates1[i]);
  56. assertEquals(i + " Final state mismatch: ",
  57. aBatchUpdateException.getUpdateCounts(),
  58. theFinalStates2[i]);
  59. assertEquals(i + " Final state mismatch: ",
  60. aBatchUpdateException.getSQLState(), theFinalStates3[i]);
  61. assertEquals(i + " Final state mismatch: ",
  62. aBatchUpdateException.getMessage(), theFinalStates4[i]);
  63. } catch (Exception e) {
  64. if (theExceptions[i] == null) {
  65. fail(i + "Unexpected exception");
  66. }
  67. assertEquals(i + "Exception mismatch", e.getClass(),
  68. theExceptions[i].getClass());
  69. assertEquals(i + "Exception mismatch", e.getMessage(),
  70. theExceptions[i].getMessage());
  71. } // end try
  72. } // end for
  73. } // end method testBatchUpdateException
  74. /*
  75. * ConstructorTest
  76. */
  77. @TestTargetNew(
  78. level = TestLevel.COMPLETE,
  79. notes = "",
  80. method = "BatchUpdateException",
  81. args = {int[].class}
  82. )
  83. public void testBatchUpdateExceptionintArray() {
  84. int[][] init1 = { { 1, 2, 3 }, null };
  85. int[] theFinalStates1 = { 0, 0 }; // Error Code state
  86. int[][] theFinalStates2 = init1; // Update Counts array state
  87. String[] theFinalStates3 = { null, null }; // SQL State state value
  88. String[] theFinalStates4 = { null, null }; // Message state
  89. Exception[] theExceptions = { null, null };
  90. BatchUpdateException aBatchUpdateException;
  91. int loopCount = init1.length;
  92. for (int i = 0; i < loopCount; i++) {
  93. try {
  94. aBatchUpdateException = new BatchUpdateException(init1[i]);
  95. if (theExceptions[i] != null) {
  96. fail();
  97. }
  98. assertEquals(i + " Final state mismatch: ",
  99. aBatchUpdateException.getErrorCode(),
  100. theFinalStates1[i]);
  101. assertEquals(i + " Final state mismatch: ",
  102. aBatchUpdateException.getUpdateCounts(),
  103. theFinalStates2[i]);
  104. assertEquals(i + " Final state mismatch: ",
  105. aBatchUpdateException.getSQLState(), theFinalStates3[i]);
  106. assertEquals(i + " Final state mismatch: ",
  107. aBatchUpdateException.getMessage(), 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 testBatchUpdateExceptionintArray
  119. /*
  120. * ConstructorTest
  121. */
  122. @TestTargetNew(
  123. level = TestLevel.COMPLETE,
  124. notes = "",
  125. method = "BatchUpdateException",
  126. args = {java.lang.String.class, int[].class}
  127. )
  128. public void testBatchUpdateExceptionStringintArray() {
  129. String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "",
  130. ".", "a" };
  131. int[][] init2 = { { 1, 2, 3 }, {}, { 3 }, null, { 5, 5 }, { 6 },
  132. { 121, 2, 1 }, { 1 }, { 1, 2 } };
  133. int[] theFinalStates1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Error Code
  134. // state
  135. // Update Counts array state
  136. int[][] theFinalStates2 = init2;
  137. // SQL State state value
  138. String[] theFinalStates3 = { null, null, null, null, null, null, null,
  139. null, null };
  140. String[] theFinalStates4 = init1; // Message state
  141. Exception[] theExceptions = { null, null, null, null, null, null, null,
  142. null, null };
  143. BatchUpdateException aBatchUpdateException;
  144. int loopCount = init1.length;
  145. for (int i = 0; i < loopCount; i++) {
  146. try {
  147. aBatchUpdateException = new BatchUpdateException(init1[i],
  148. init2[i]);
  149. if (theExceptions[i] != null) {
  150. fail();
  151. }
  152. assertEquals(i + " Final state mismatch: ",
  153. aBatchUpdateException.getErrorCode(),
  154. theFinalStates1[i]);
  155. assertEquals(i + " Final state mismatch: ",
  156. aBatchUpdateException.getUpdateCounts(),
  157. theFinalStates2[i]);
  158. assertEquals(i + " Final state mismatch: ",
  159. aBatchUpdateException.getSQLState(), theFinalStates3[i]);
  160. assertEquals(i + " Final state mismatch: ",
  161. aBatchUpdateException.getMessage(), theFinalStates4[i]);
  162. } catch (Exception e) {
  163. if (theExceptions[i] == null) {
  164. fail(i + "Unexpected exception");
  165. }
  166. assertEquals(i + "Exception mismatch", e.getClass(),
  167. theExceptions[i].getClass());
  168. assertEquals(i + "Exception mismatch", e.getMessage(),
  169. theExceptions[i].getMessage());
  170. } // end try
  171. } // end for
  172. } // end method testBatchUpdateExceptionStringintArray
  173. /*
  174. * ConstructorTest
  175. */
  176. @TestTargetNew(
  177. level = TestLevel.COMPLETE,
  178. notes = "",
  179. method = "BatchUpdateException",
  180. args = {java.lang.String.class, java.lang.String.class, int[].class}
  181. )
  182. public void testBatchUpdateExceptionStringStringintArray() {
  183. String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "",
  184. ".", "a", "a" };
  185. String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a", null,
  186. "", ".", "a" };
  187. int[][] init3 = { { 1, 2, 3 }, {}, { 3 }, { 5, 5 }, { 6 },
  188. { 121, 2, 1 }, { 1 }, { 1, 2 }, { 1 }, { 2 }, null };
  189. int[] theFinalStates1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Error
  190. // Code
  191. // state
  192. // Update Counts array state
  193. int[][] theFinalStates2 = init3;
  194. // SQL State state value
  195. String[] theFinalStates3 = init2;
  196. String[] theFinalStates4 = init1; // Message state
  197. Exception[] theExceptions = { null, null, null, null, null, null, null,
  198. null, null, null, null };
  199. BatchUpdateException aBatchUpdateException;
  200. int loopCount = init1.length;
  201. for (int i = 0; i < loopCount; i++) {
  202. try {
  203. aBatchUpdateException = new BatchUpdateException(init1[i],
  204. init2[i], init3[i]);
  205. if (theExceptions[i] != null) {
  206. fail();
  207. }
  208. assertEquals(i + " Final state mismatch: ",
  209. aBatchUpdateException.getErrorCode(),
  210. theFinalStates1[i]);
  211. assertEquals(i + " Final state mismatch: ",
  212. aBatchUpdateException.getUpdateCounts(),
  213. theFinalStates2[i]);
  214. assertEquals(i + " Final state mismatch: ",
  215. aBatchUpdateException.getSQLState(), theFinalStates3[i]);
  216. assertEquals(i + " Final state mismatch: ",
  217. aBatchUpdateException.getMessage(), theFinalStates4[i]);
  218. } catch (Exception e) {
  219. if (theExceptions[i] == null) {
  220. fail(i + "Unexpected exception");
  221. }
  222. assertEquals(i + "Exception mismatch", e.getClass(),
  223. theExceptions[i].getClass());
  224. assertEquals(i + "Exception mismatch", e.getMessage(),
  225. theExceptions[i].getMessage());
  226. } // end try
  227. } // end for
  228. } // end method testBatchUpdateExceptionStringStringintArray
  229. /*
  230. * ConstructorTest
  231. */
  232. @TestTargetNew(
  233. level = TestLevel.COMPLETE,
  234. notes = "",
  235. method = "BatchUpdateException",
  236. args = {java.lang.String.class, java.lang.String.class, int.class, int[].class}
  237. )
  238. public void testBatchUpdateExceptionStringStringintintArray() {
  239. String[] init1 = { "a", "1", "valid1", "----", "&valid*", null, "",
  240. ".", "a", "a" };
  241. String[] init2 = { "a", "1", "valid1", "----", "&valid*", "a", null,
  242. "", ".", "a" };
  243. int[] init3 = { -2147483648, 2147483647, 0, -492417162, -156220255,
  244. -173012890, -631026360, -2147483648, -2147483648, -2147483648,
  245. -2147483648 };
  246. int[][] init4 = { { 1, 2, 3 }, {}, { 3 }, { 5, 5 }, { 6 },
  247. { 121, 2, 1 }, { 1 }, { 1, 2 }, { 1 }, { 2 }, null };
  248. int[] theFinalStates1 = init3; // Error Code state
  249. // Update Counts array state
  250. int[][] theFinalStates2 = init4;
  251. // SQL State state value
  252. String[] theFinalStates3 = init2;
  253. String[] theFinalStates4 = init1; // Message state
  254. Exception[] theExceptions = { null, null, null, null, null, null, null,
  255. null, null, null, null };
  256. BatchUpdateException aBatchUpdateException;
  257. int loopCount = init1.length;
  258. for (int i = 0; i < loopCount; i++) {
  259. try {
  260. aBatchUpdateException = new BatchUpdateException(init1[i],
  261. init2[i], init3[i], init4[i]);
  262. if (theExceptions[i] != null) {
  263. fail();
  264. }
  265. assertEquals(i + " Final state mismatch: ",
  266. aBatchUpdateException.getErrorCode(),
  267. theFinalStates1[i]);
  268. assertEquals(i + " Final state mismatch: ",
  269. aBatchUpdateException.getUpdateCounts(),
  270. theFinalStates2[i]);
  271. assertEquals(i + " Final state mismatch: ",
  272. aBatchUpdateException.getSQLState(), theFinalStates3[i]);
  273. assertEquals(i + " Final state mismatch: ",
  274. aBatchUpdateException.getMessage(), theFinalStates4[i]);
  275. } catch (Exception e) {
  276. if (theExceptions[i] == null) {
  277. fail(i + "Unexpected exception");
  278. }
  279. assertEquals(i + "Exception mismatch", e.getClass(),
  280. theExceptions[i].getClass());
  281. assertEquals(i + "Exception mismatch", e.getMessage(),
  282. theExceptions[i].getMessage());
  283. } // end try
  284. } // end for
  285. } // end method testBatchUpdateExceptionStringStringintintArray
  286. /*
  287. * Method test for getUpdateCounts
  288. */
  289. @TestTargetNew(
  290. level = TestLevel.COMPLETE,
  291. notes = "",
  292. method = "getUpdateCounts",
  293. args = {}
  294. )
  295. public void testGetUpdateCounts() {
  296. BatchUpdateException aBatchUpdateException;
  297. int[][] init1 = { { 1, 2, 3 }, {}, null };
  298. int[] theReturn;
  299. int[][] theReturns = init1;
  300. int[] theFinalStates1 = { 0, 0, 0 }; // Error Code state
  301. int[][] theFinalStates2 = init1; // Update Counts array state
  302. String[] theFinalStates3 = { null, null, null }; // SQL State state
  303. // value
  304. String[] theFinalStates4 = { null, null, null }; // Message state
  305. Exception[] theExceptions = { null, null, null };
  306. int loopCount = init1.length;
  307. for (int i = 0; i < loopCount; i++) {
  308. try {
  309. aBatchUpdateException = new BatchUpdateException(init1[i]);
  310. theReturn = aBatchUpdateException.getUpdateCounts();
  311. if (theExceptions[i] != null) {
  312. fail(i + "Exception missed");
  313. }
  314. assertEquals(i + "Return value mismatch", theReturn,
  315. theReturns[i]);
  316. assertEquals(i + " Final state mismatch: ",
  317. aBatchUpdateException.getErrorCode(),
  318. theFinalStates1[i]);
  319. assertEquals(i + " Final state mismatch: ",
  320. aBatchUpdateException.getUpdateCounts(),
  321. theFinalStates2[i]);
  322. assertEquals(i + " Final state mismatch: ",
  323. aBatchUpdateException.getSQLState(), theFinalStates3[i]);
  324. assertEquals(i + " Final state mismatch: ",
  325. aBatchUpdateException.getMessage(), theFinalStates4[i]);
  326. } catch (Exception e) {
  327. if (theExceptions[i] == null) {
  328. fail(i + "Unexpected exception");
  329. }
  330. assertEquals(i + "Exception mismatch", e.getClass(),
  331. theExceptions[i].getClass());
  332. assertEquals(i + "Exception mismatch", e.getMessage(),
  333. theExceptions[i].getMessage());
  334. } // end try
  335. } // end for
  336. } // end method testGetUpdateCounts
  337. /**
  338. * @tests serialization/deserialization compatibility.
  339. */
  340. @TestTargetNew(
  341. level = TestLevel.COMPLETE,
  342. notes = "Serialization test",
  343. method = "!SerializationSelf",
  344. args = {}
  345. )
  346. public void testSerializationSelf() throws Exception {
  347. BatchUpdateException object = new BatchUpdateException();
  348. SerializationTest.verifySelf(object, BATCHUPDATEEXCEPTION_COMPARATOR);
  349. }
  350. /**
  351. * @tests serialization/deserialization compatibility with RI.
  352. */
  353. @TestTargetNew(
  354. level = TestLevel.COMPLETE,
  355. notes = "Serialization test",
  356. method = "!SerializationGolden",
  357. args = {}
  358. )
  359. public void testSerializationCompatibility() throws Exception {
  360. int vendorCode = 10;
  361. int[] updateCounts = { 1, 2, 3, 4 };
  362. BatchUpdateException object = new BatchUpdateException("reason",
  363. "SQLState", vendorCode, updateCounts);
  364. SerializationTest.verifyGolden(this, object,
  365. BATCHUPDATEEXCEPTION_COMPARATOR);
  366. }
  367. // comparator for BatchUpdateException field updateCounts
  368. private static final SerializableAssert BATCHUPDATEEXCEPTION_COMPARATOR = new SerializableAssert() {
  369. public void assertDeserialized(Serializable initial,
  370. Serializable deserialized) {
  371. // do common checks for all throwable objects
  372. SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
  373. deserialized);
  374. BatchUpdateException initThr = (BatchUpdateException) initial;
  375. BatchUpdateException dserThr = (BatchUpdateException) deserialized;
  376. // verify updateCounts
  377. int[] initUpdateCounts = initThr.getUpdateCounts();
  378. int[] dserUpdateCounts = dserThr.getUpdateCounts();
  379. assertTrue(Arrays.equals(initUpdateCounts, dserUpdateCounts));
  380. }
  381. };
  382. } // end class BatchUpdateExceptionTest