PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/external/apache-harmony/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/SerializationStressTest1.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
Java | 1650 lines | 1186 code | 232 blank | 232 comment | 47 complexity | c7fbff3fd56b573597a22adb31a8bb64 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.luni.tests.java.io;
  18. import java.io.IOException;
  19. import java.io.NotSerializableException;
  20. import java.io.Serializable;
  21. import java.util.Arrays;
  22. import java.util.Hashtable;
  23. import java.util.Vector;
  24. @SuppressWarnings( { "serial", "unused" })
  25. public class SerializationStressTest1 extends SerializationStressTest {
  26. // The purpose of these two classes is to test if serialization, when
  27. // loading, runs the object's constructor (wrong) or the constructor defined
  28. // at the topmost Serializable superclass(correct).
  29. static final int INIT_INT_VALUE = 7;
  30. // HAS to be static class so that our constructor signature will remain
  31. // untouched (no synthetic param)
  32. private static class SerializationTest implements java.io.Serializable {
  33. int anInt = INIT_INT_VALUE;
  34. public SerializationTest() {
  35. super();
  36. }
  37. }
  38. static final String INIT_STR_VALUE = "a string that is blortz";
  39. // HAS to be static class so that our constructor signature will remain
  40. // untouched (no synthetic param)
  41. private static class SerializationTestSubclass1 extends SerializationTest {
  42. String aString = INIT_STR_VALUE;
  43. public SerializationTestSubclass1() {
  44. super();
  45. // Just to change default superclass init value
  46. anInt = INIT_INT_VALUE / 2;
  47. }
  48. }
  49. // -----------------------------------------------------------------------------------
  50. private static class SpecTestSuperClass implements Runnable {
  51. protected java.lang.String instVar;
  52. public void run() {
  53. }
  54. }
  55. private static class SpecTest extends SpecTestSuperClass implements
  56. Cloneable, Serializable {
  57. public java.lang.String instVar1;
  58. public static java.lang.String staticVar1;
  59. public static java.lang.String staticVar2;
  60. {
  61. instVar1 = "NonStaticInitialValue";
  62. }
  63. static {
  64. staticVar1 = "StaticInitialValue";
  65. staticVar1 = new String(staticVar1);
  66. }
  67. public Object method(Object objParam, Object objParam2) {
  68. return new Object();
  69. }
  70. public boolean method(boolean bParam, Object objParam) {
  71. return true;
  72. }
  73. public boolean method(boolean bParam, Object objParam, Object objParam2) {
  74. return true;
  75. }
  76. }
  77. private static class SpecTestSubclass extends SpecTest {
  78. public transient java.lang.String transientInstVar = "transientValue";
  79. }
  80. // -----------------------------------------------------------------------------------
  81. // This one tests what happens if the read/writeObject methods are defined
  82. // Serialization should work fine.
  83. private static class ReadWriteObject implements java.io.Serializable {
  84. public boolean calledWriteObject = false;
  85. public boolean calledReadObject = false;
  86. public ReadWriteObject() {
  87. super();
  88. }
  89. private void readObject(java.io.ObjectInputStream in)
  90. throws java.io.IOException, ClassNotFoundException {
  91. calledReadObject = true;
  92. in.readObject();
  93. }
  94. private void writeObject(java.io.ObjectOutputStream out)
  95. throws java.io.IOException {
  96. calledWriteObject = true;
  97. out.writeObject(FOO);
  98. }
  99. }
  100. // This one tests what happens if the read/writeObject methods are not
  101. // private.
  102. // Serialization should fail.
  103. private static class PublicReadWriteObject implements java.io.Serializable {
  104. public boolean calledWriteObject = false;
  105. public boolean calledReadObject = false;
  106. public PublicReadWriteObject() {
  107. super();
  108. }
  109. public void readObject(java.io.ObjectInputStream in)
  110. throws java.io.IOException, ClassNotFoundException {
  111. calledReadObject = true;
  112. in.readObject();
  113. }
  114. public void writeObject(java.io.ObjectOutputStream out)
  115. throws java.io.IOException {
  116. calledWriteObject = true;
  117. out.writeObject(FOO);
  118. }
  119. }
  120. // This one tests if field names are serialized in the same way (sorting)
  121. // across different VMs
  122. private static class FieldOrder implements Serializable {
  123. String aaa1NonPrimitive = "aaa1";
  124. int bbb1PrimitiveInt = 5;
  125. boolean aaa2PrimitiveBoolean = true;
  126. String bbb2NonPrimitive = "bbb2";
  127. }
  128. // This one tests what happens if you define just readObject, but not
  129. // writeObject.
  130. // Does it run or not ?
  131. private static class JustReadObject implements java.io.Serializable {
  132. public boolean calledReadObject = false;
  133. public JustReadObject() {
  134. super();
  135. }
  136. private void readObject(java.io.ObjectInputStream in)
  137. throws java.io.IOException, ClassNotFoundException {
  138. calledReadObject = true;
  139. in.defaultReadObject();
  140. }
  141. }
  142. // This one tests what happens if you define just writeObject, but not
  143. // readObject.
  144. // Does it run or not ?
  145. private static class JustWriteObject implements java.io.Serializable {
  146. public boolean calledWriteObject = false;
  147. public JustWriteObject() {
  148. super();
  149. }
  150. private void writeObject(java.io.ObjectOutputStream out)
  151. throws java.io.IOException, ClassNotFoundException {
  152. calledWriteObject = true;
  153. out.defaultWriteObject();
  154. }
  155. }
  156. // This one tests class-based replacement when dumping
  157. private static class ClassBasedReplacementWhenDumping implements
  158. java.io.Serializable {
  159. public boolean calledReplacement = false;
  160. public ClassBasedReplacementWhenDumping() {
  161. super();
  162. }
  163. private Object writeReplace() {
  164. calledReplacement = true;
  165. return FOO; // Replacement is a String
  166. }
  167. }
  168. // This one tests whether class-based replacement supports multiple levels.
  169. // MultipleClassBasedReplacementWhenDumping -> C1 -> C2 -> C3 -> FOO
  170. private static class MultipleClassBasedReplacementWhenDumping implements
  171. java.io.Serializable {
  172. private static class C1 implements java.io.Serializable {
  173. private Object writeReplace() {
  174. return new C2();
  175. }
  176. }
  177. private static class C2 implements java.io.Serializable {
  178. private Object writeReplace() {
  179. return new C3();
  180. }
  181. }
  182. private static class C3 implements java.io.Serializable {
  183. private Object writeReplace() {
  184. return FOO;
  185. }
  186. }
  187. public MultipleClassBasedReplacementWhenDumping() {
  188. super();
  189. }
  190. private Object writeReplace() {
  191. return new C1();
  192. }
  193. }
  194. // This one tests class-based replacement when loading
  195. private static class ClassBasedReplacementWhenLoading implements
  196. java.io.Serializable {
  197. public ClassBasedReplacementWhenLoading() {
  198. super();
  199. }
  200. private Object readResolve() {
  201. return FOO; // Replacement is a String
  202. }
  203. }
  204. // This one tests what happens if a loading-replacement is not
  205. // type-compatible with the original object
  206. private static class ClassBasedReplacementWhenLoadingViolatesFieldType
  207. implements java.io.Serializable {
  208. public ClassBasedReplacementWhenLoading classBasedReplacementWhenLoading = new ClassBasedReplacementWhenLoading();
  209. public ClassBasedReplacementWhenLoadingViolatesFieldType() {
  210. super();
  211. }
  212. }
  213. // What happens if dumping causes an error and you try to reload ?
  214. // Should the load throw the same exception ?
  215. private static class MyExceptionWhenDumping1 implements
  216. java.io.Serializable {
  217. private static class MyException extends java.io.IOException {
  218. };
  219. // A primitive instance variable exposes a bug in the serialization
  220. // spec.
  221. // Primitive instance variables are written without primitive data tags
  222. // and so are read without checking for tags. If an exception is
  223. // written, reading primitive data will just read bytes from the stream
  224. // which may be tags
  225. public boolean anInstanceVar = false;
  226. public MyExceptionWhenDumping1() {
  227. super();
  228. }
  229. private void readObject(java.io.ObjectInputStream in)
  230. throws java.io.IOException, ClassNotFoundException {
  231. in.defaultReadObject();
  232. }
  233. private void writeObject(java.io.ObjectOutputStream out)
  234. throws java.io.IOException, ClassNotFoundException {
  235. throw new MyException();
  236. }
  237. }
  238. // What happens if dumping causes an error and you try to reload ?
  239. // Should the load throw the same exception ?
  240. private static class MyExceptionWhenDumping2 implements
  241. java.io.Serializable {
  242. private static class MyException extends java.io.IOException {
  243. };
  244. public Integer anInstanceVar = new Integer(0xA1);
  245. public MyExceptionWhenDumping2() {
  246. super();
  247. }
  248. private void readObject(java.io.ObjectInputStream in)
  249. throws java.io.IOException, ClassNotFoundException {
  250. in.defaultReadObject();
  251. }
  252. private void writeObject(java.io.ObjectOutputStream out)
  253. throws java.io.IOException, ClassNotFoundException {
  254. throw new MyException();
  255. }
  256. }
  257. // What happens if dumping causes an error (NonSerializable inst var) and
  258. // you try to reload ?
  259. // Should the load throw the same exception ?
  260. private static class NonSerializableExceptionWhenDumping implements
  261. java.io.Serializable {
  262. public Object anInstanceVar = new Object();
  263. public NonSerializableExceptionWhenDumping() {
  264. super();
  265. }
  266. }
  267. // What happens if dumping causes an error (which is not serializable) and
  268. // you try to reload ?
  269. // Should the load throw the same exception ?
  270. private static class MyUnserializableExceptionWhenDumping implements
  271. java.io.Serializable {
  272. private static class MyException extends java.io.IOException {
  273. private Object notSerializable = new Object();
  274. };
  275. public boolean anInstanceVar = false;
  276. public MyUnserializableExceptionWhenDumping() {
  277. super();
  278. }
  279. private void readObject(java.io.ObjectInputStream in)
  280. throws java.io.IOException, ClassNotFoundException {
  281. in.defaultReadObject();
  282. }
  283. private void writeObject(java.io.ObjectOutputStream out)
  284. throws java.io.IOException, ClassNotFoundException {
  285. throw new MyException();
  286. }
  287. }
  288. public SerializationStressTest1(String name) {
  289. super(name);
  290. }
  291. public void test_18_1_writeObject() {
  292. // Test for method void
  293. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  294. Object objToSave = null;
  295. Object objLoaded;
  296. try {
  297. objToSave = "HelloWorld";
  298. if (DEBUG)
  299. System.out.println("Obj = " + objToSave);
  300. objLoaded = dumpAndReload(objToSave);
  301. assertTrue(MSG_TEST_FAILED + objToSave, (((String) objLoaded)
  302. .equals((String) objToSave)));
  303. } catch (IOException e) {
  304. fail("IOException serializing data : " + e.getMessage());
  305. } catch (ClassNotFoundException e) {
  306. fail("ClassNotFoundException reading Object type: "
  307. + e.getMessage());
  308. } catch (Error err) {
  309. System.out.println("Error when obj = " + objToSave);
  310. // err.printStackTrace();
  311. throw err;
  312. }
  313. }
  314. public void test_18_2_writeObject() {
  315. // Test for method void
  316. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  317. Object objToSave = null;
  318. Object objLoaded;
  319. try {
  320. objToSave = null;
  321. if (DEBUG)
  322. System.out.println("Obj = " + objToSave);
  323. objLoaded = dumpAndReload(objToSave);
  324. assertTrue(MSG_TEST_FAILED + objToSave, objLoaded == objToSave);
  325. } catch (IOException e) {
  326. fail("IOException serializing data : " + e.getMessage());
  327. } catch (ClassNotFoundException e) {
  328. fail("ClassNotFoundException reading Object type : "
  329. + e.getMessage());
  330. } catch (Error err) {
  331. System.out.println("Error when obj = " + objToSave);
  332. // err.printStackTrace();
  333. throw err;
  334. }
  335. }
  336. public void test_18_3_writeObject() {
  337. // Test for method void
  338. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  339. Object objToSave = null;
  340. Object objLoaded;
  341. try {
  342. byte[] bytes = { 0, 1, 2, 3 };
  343. objToSave = bytes;
  344. if (DEBUG)
  345. System.out.println("Obj = " + objToSave);
  346. objLoaded = dumpAndReload(objToSave);
  347. assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
  348. (byte[]) objLoaded, (byte[]) objToSave));
  349. } catch (IOException e) {
  350. fail("IOException serializing data : " + e.getMessage());
  351. } catch (ClassNotFoundException e) {
  352. fail("ClassNotFoundException reading Object type : "
  353. + e.getMessage());
  354. } catch (Error err) {
  355. System.out.println("Error when obj = " + objToSave);
  356. // err.printStackTrace();
  357. throw err;
  358. }
  359. }
  360. public void test_18_4_writeObject() {
  361. // Test for method void
  362. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  363. Object objToSave = null;
  364. Object objLoaded;
  365. try {
  366. int[] ints = { 0, 1, 2, 3 };
  367. objToSave = ints;
  368. if (DEBUG)
  369. System.out.println("Obj = " + objToSave);
  370. objLoaded = dumpAndReload(objToSave);
  371. assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
  372. (int[]) objLoaded, (int[]) objToSave));
  373. } catch (IOException e) {
  374. fail("IOException serializing data : " + e.getMessage());
  375. } catch (ClassNotFoundException e) {
  376. fail("ClassNotFoundException reading Object type : "
  377. + e.getMessage());
  378. } catch (Error err) {
  379. System.out.println("Error when obj = " + objToSave);
  380. throw err;
  381. }
  382. }
  383. public void test_18_5_writeObject() {
  384. // Test for method void
  385. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  386. Object objToSave = null;
  387. Object objLoaded;
  388. try {
  389. short[] shorts = { 0, 1, 2, 3 };
  390. objToSave = shorts;
  391. if (DEBUG)
  392. System.out.println("Obj = " + objToSave);
  393. objLoaded = dumpAndReload(objToSave);
  394. assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
  395. (short[]) objLoaded, (short[]) objToSave));
  396. } catch (IOException e) {
  397. fail("IOException serializing data : " + e.getMessage());
  398. } catch (ClassNotFoundException e) {
  399. fail("ClassNotFoundException reading Object type : "
  400. + e.getMessage());
  401. } catch (Error err) {
  402. System.out.println("Error when obj = " + objToSave);
  403. // err.printStackTrace();
  404. throw err;
  405. }
  406. }
  407. public void test_18_6_writeObject() {
  408. // Test for method void
  409. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  410. Object objToSave = null;
  411. Object objLoaded;
  412. try {
  413. long[] longs = { 0, 1, 2, 3 };
  414. objToSave = longs;
  415. if (DEBUG)
  416. System.out.println("Obj = " + objToSave);
  417. objLoaded = dumpAndReload(objToSave);
  418. assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
  419. (long[]) objLoaded, (long[]) objToSave));
  420. } catch (IOException e) {
  421. fail("IOException serializing data : " + e.getMessage());
  422. } catch (ClassNotFoundException e) {
  423. fail("ClassNotFoundException reading Object type : "
  424. + e.getMessage());
  425. } catch (Error err) {
  426. System.out.println("Error when obj = " + objToSave);
  427. // err.printStackTrace();
  428. throw err;
  429. }
  430. }
  431. public void test_18_7_writeObject() {
  432. // Test for method void
  433. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  434. Object objToSave = null;
  435. Object objLoaded;
  436. try {
  437. float[] floats = { 0.0f, 1.1f, 2.2f, 3.3f };
  438. objToSave = floats;
  439. if (DEBUG)
  440. System.out.println("Obj = " + objToSave);
  441. objLoaded = dumpAndReload(objToSave);
  442. assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
  443. (float[]) objLoaded, (float[]) objToSave));
  444. } catch (IOException e) {
  445. fail("IOException serializing data: " + e.getMessage());
  446. } catch (ClassNotFoundException e) {
  447. fail("ClassNotFoundException reading Object type : "
  448. + e.getMessage());
  449. } catch (Error err) {
  450. System.out.println("Error when obj = " + objToSave);
  451. // err.printStackTrace();
  452. throw err;
  453. }
  454. }
  455. public void test_18_8_writeObject() {
  456. // Test for method void
  457. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  458. Object objToSave = null;
  459. Object objLoaded;
  460. try {
  461. double[] doubles = { 0.0, 1.1, 2.2, 3.3 };
  462. objToSave = doubles;
  463. if (DEBUG)
  464. System.out.println("Obj = " + objToSave);
  465. objLoaded = dumpAndReload(objToSave);
  466. assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
  467. (double[]) objLoaded, (double[]) objToSave));
  468. } catch (IOException e) {
  469. fail("IOException serializing data : " + e.getMessage());
  470. } catch (ClassNotFoundException e) {
  471. fail("ClassNotFoundException reading Object type : "
  472. + e.getMessage());
  473. } catch (Error err) {
  474. System.out.println("Error when obj = " + objToSave);
  475. // err.printStackTrace();
  476. throw err;
  477. }
  478. }
  479. public void test_18_9_writeObject() {
  480. // Test for method void
  481. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  482. Object objToSave = null;
  483. Object objLoaded;
  484. try {
  485. boolean[] booleans = { true, false, false, true };
  486. objToSave = booleans;
  487. if (DEBUG)
  488. System.out.println("Obj = " + objToSave);
  489. objLoaded = dumpAndReload(objToSave);
  490. assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
  491. (boolean[]) objLoaded, (boolean[]) objToSave));
  492. } catch (IOException e) {
  493. fail("IOException serializing data : " + e.getMessage());
  494. } catch (ClassNotFoundException e) {
  495. fail("ClassNotFoundException reading Object type : " + e.getMessage());
  496. } catch (Error err) {
  497. System.out.println("Error when obj = " + objToSave);
  498. // err.printStackTrace();
  499. throw err;
  500. }
  501. }
  502. public void test_18_10_writeObject() {
  503. // Test for method void
  504. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  505. Object objToSave = null;
  506. Object objLoaded;
  507. try {
  508. String[] strings = { "foo", "bar", "java" };
  509. objToSave = strings;
  510. if (DEBUG)
  511. System.out.println("Obj = " + objToSave);
  512. objLoaded = dumpAndReload(objToSave);
  513. assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
  514. (Object[]) objLoaded, (Object[]) objToSave));
  515. } catch (IOException e) {
  516. fail("IOException serializing " + objToSave + " : "
  517. + e.getMessage());
  518. } catch (ClassNotFoundException e) {
  519. fail("Unable to read Object type: " + e.toString());
  520. } catch (Error err) {
  521. System.out.println("Error when obj = " + objToSave);
  522. // err.printStackTrace();
  523. throw err;
  524. }
  525. }
  526. public void test_18_11_writeObject() {
  527. // Test for method void
  528. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  529. Object objToSave = null;
  530. Object objLoaded;
  531. try {
  532. objToSave = new Object(); // Not serializable
  533. if (DEBUG)
  534. System.out.println("Obj = " + objToSave);
  535. boolean passed = false;
  536. Throwable t = null;
  537. try {
  538. objLoaded = dumpAndReload(objToSave);
  539. } catch (NotSerializableException ns) {
  540. passed = true;
  541. t = ns;
  542. } catch (Exception wrongExc) {
  543. passed = false;
  544. t = wrongExc;
  545. }
  546. assertTrue(
  547. "Failed to throw NotSerializableException when serializing "
  548. + objToSave + " Threw(if non-null) this: " + t,
  549. passed);
  550. } catch (Error err) {
  551. System.out.println("Error when obj = " + objToSave);
  552. // err.printStackTrace();
  553. throw err;
  554. }
  555. }
  556. public void test_18_12_writeObject() {
  557. // Test for method void
  558. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  559. try {
  560. if (DEBUG)
  561. System.out.println("Obj = <mixed>");
  562. t_MixPrimitivesAndObjects();
  563. } catch (IOException e) {
  564. fail("IOException serializing data : " + e.getMessage());
  565. } catch (ClassNotFoundException e) {
  566. fail("ClassNotFoundException reading Object type : "
  567. + e.getMessage());
  568. } catch (Error err) {
  569. System.out.println("Error when dumping mixed types");
  570. // err.printStackTrace();
  571. throw err;
  572. }
  573. }
  574. public void test_18_13_writeObject() {
  575. // Test for method void
  576. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  577. Object objToSave = null;
  578. Object objLoaded;
  579. try {
  580. SerializationTestSubclass1 st = new SerializationTestSubclass1();
  581. // Just change the default ivar values
  582. st.anInt = Integer.MAX_VALUE;
  583. st.aString = FOO;
  584. objToSave = st;
  585. if (DEBUG)
  586. System.out.println("Obj = " + objToSave);
  587. objLoaded = dumpAndReload(objToSave);
  588. // non-serializable inst var has to be initialized from top
  589. // constructor
  590. assertTrue(
  591. MSG_TEST_FAILED + objToSave,
  592. ((SerializationTestSubclass1) objLoaded).anInt == Integer.MAX_VALUE);
  593. // but serialized var has to be restored as it was in the object
  594. // when dumped
  595. assertTrue(MSG_TEST_FAILED + objToSave,
  596. ((SerializationTestSubclass1) objLoaded).aString
  597. .equals(FOO));
  598. } catch (IOException e) {
  599. fail("Exception serializing " + objToSave + "\t->"
  600. + e.toString());
  601. } catch (ClassNotFoundException e) {
  602. fail("ClassNotFoundException reading Object type : "
  603. + e.getMessage());
  604. } catch (Error err) {
  605. System.out.println("Error when obj = " + objToSave);
  606. err.printStackTrace();
  607. throw err;
  608. }
  609. }
  610. public void test_18_14_writeObject() {
  611. // Test for method void
  612. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  613. Object objToSave = null;
  614. Object objLoaded;
  615. try {
  616. SpecTest specTest = new SpecTest();
  617. // Just change the default ivar values
  618. specTest.instVar = FOO;
  619. specTest.instVar1 = specTest.instVar;
  620. objToSave = specTest;
  621. if (DEBUG)
  622. System.out.println("Obj = " + objToSave);
  623. objLoaded = dumpAndReload(objToSave);
  624. // non-serializable inst var has to be initialized from top
  625. // constructor
  626. assertNull(MSG_TEST_FAILED + objToSave,
  627. ((SpecTest) objLoaded).instVar);
  628. // instVar from non-serialized class, cant be saved/restored
  629. // by serialization but serialized ivar has to be restored as it
  630. // was in the object when dumped
  631. assertTrue(MSG_TEST_FAILED + objToSave,
  632. ((SpecTest) objLoaded).instVar1.equals(FOO));
  633. } catch (IOException e) {
  634. fail("Exception serializing " + objToSave + "\t->"
  635. + e.toString());
  636. } catch (ClassNotFoundException e) {
  637. fail("ClassNotFoundException reading Object type : "
  638. + e.getMessage());
  639. } catch (Error err) {
  640. System.out.println("Error when obj = " + objToSave);
  641. // err.printStackTrace();
  642. throw err;
  643. }
  644. }
  645. public void test_18_15_writeObject() {
  646. // Test for method void
  647. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  648. Object objToSave = null;
  649. Object objLoaded;
  650. try {
  651. SpecTestSubclass specTestSubclass = new SpecTestSubclass();
  652. // Just change the default ivar values
  653. specTestSubclass.transientInstVar = FOO;
  654. objToSave = specTestSubclass;
  655. if (DEBUG)
  656. System.out.println("Obj = " + objToSave);
  657. objLoaded = dumpAndReload(objToSave);
  658. // non-serializable inst var cant be saved, and it is not init'ed
  659. // from top constructor in this case
  660. assertNull(MSG_TEST_FAILED + objToSave,
  661. ((SpecTestSubclass) objLoaded).transientInstVar);
  662. // transient slot, cant be saved/restored by serialization
  663. } catch (IOException e) {
  664. fail("Exception serializing " + objToSave + "\t->"
  665. + e.toString());
  666. } catch (ClassNotFoundException e) {
  667. fail("ClassNotFoundException reading Object type : "
  668. + e.getMessage());
  669. } catch (Error err) {
  670. System.out.println("Error when obj = " + objToSave);
  671. // err.printStackTrace();
  672. throw err;
  673. }
  674. }
  675. public void test_18_16_writeObject() {
  676. // Test for method void
  677. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  678. Object objToSave = null;
  679. Object objLoaded;
  680. try {
  681. String[] strings = new String[2];
  682. strings[0] = FOO;
  683. strings[1] = (" " + FOO + " ").trim(); // Safe way to get a copy
  684. // that is not ==
  685. objToSave = strings;
  686. if (DEBUG)
  687. System.out.println("Obj = " + objToSave);
  688. objLoaded = dumpAndReload(objToSave);
  689. String[] stringsLoaded = (String[]) objLoaded;
  690. // Serialization has to use identity-based table for assigning IDs
  691. assertTrue(MSG_TEST_FAILED + objToSave,
  692. !(stringsLoaded[0] == stringsLoaded[1]));
  693. } catch (IOException e) {
  694. fail("Exception serializing " + objToSave + "\t->"
  695. + e.toString());
  696. } catch (ClassNotFoundException e) {
  697. fail("ClassNotFoundException reading Object type : "
  698. + e.getMessage());
  699. } catch (Error err) {
  700. System.out.println("Error when obj = " + objToSave);
  701. // err.printStackTrace();
  702. throw err;
  703. }
  704. }
  705. public void test_18_17_writeObject() {
  706. // Test for method void
  707. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  708. Object objToSave = null;
  709. Object objLoaded;
  710. try {
  711. ReadWriteObject readWrite = new ReadWriteObject();
  712. objToSave = readWrite;
  713. if (DEBUG)
  714. System.out.println("Obj = " + objToSave);
  715. objLoaded = dumpAndReload(objToSave);
  716. // has to have called the writeObject on the instance to dump
  717. assertTrue(MSG_TEST_FAILED + objToSave, readWrite.calledWriteObject);
  718. // has to have called the readObject on the instance loaded
  719. assertTrue(MSG_TEST_FAILED + objToSave,
  720. ((ReadWriteObject) objLoaded).calledReadObject);
  721. } catch (IOException e) {
  722. fail("Exception serializing " + objToSave + "\t->"
  723. + e.toString());
  724. } catch (ClassNotFoundException e) {
  725. fail("ClassNotFoundException reading Object type : "
  726. + e.getMessage());
  727. } catch (Error err) {
  728. System.out.println("Error when obj = " + objToSave);
  729. // err.printStackTrace();
  730. throw err;
  731. }
  732. }
  733. public void test_18_18_writeObject() {
  734. // Test for method void
  735. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  736. Object objToSave = null;
  737. Object objLoaded;
  738. try {
  739. PublicReadWriteObject publicReadWrite = new PublicReadWriteObject();
  740. objToSave = publicReadWrite;
  741. if (DEBUG)
  742. System.out.println("Obj = " + objToSave);
  743. objLoaded = dumpAndReload(objToSave);
  744. // Can't have called the writeObject on the instance to dump
  745. assertTrue(MSG_TEST_FAILED + objToSave,
  746. !publicReadWrite.calledWriteObject);
  747. // Can't have called the readObject on the instance loaded
  748. assertTrue(MSG_TEST_FAILED + objToSave,
  749. !((PublicReadWriteObject) objLoaded).calledReadObject);
  750. } catch (IOException e) {
  751. fail("Exception serializing " + objToSave + "\t->"
  752. + e.toString());
  753. } catch (ClassNotFoundException e) {
  754. fail("ClassNotFoundException reading Object type : "
  755. + e.getMessage());
  756. } catch (Error err) {
  757. System.out.println("Error when obj = " + objToSave);
  758. // err.printStackTrace();
  759. throw err;
  760. }
  761. }
  762. public void test_18_19_writeObject() {
  763. // Test for method void
  764. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  765. Object objToSave = null;
  766. Object objLoaded;
  767. try {
  768. FieldOrder fieldOrder = new FieldOrder();
  769. objToSave = fieldOrder;
  770. if (DEBUG)
  771. System.out.println("Obj = " + objToSave);
  772. objLoaded = dumpAndReload(objToSave);
  773. // This test is only useful for X-loading, so if it managed to
  774. // dump&load, we passed the test
  775. assertTrue(MSG_TEST_FAILED + objToSave, true);
  776. } catch (IOException e) {
  777. fail("IOException serializing " + objToSave + " : "
  778. + e.getMessage());
  779. } catch (ClassNotFoundException e) {
  780. fail("ClassNotFoundException reading Object type : "
  781. + e.getMessage());
  782. } catch (Error err) {
  783. System.out.println("Error when obj = " + objToSave);
  784. // err.printStackTrace();
  785. throw err;
  786. }
  787. }
  788. public void test_18_20_writeObject() {
  789. // Test for method void
  790. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  791. Object objToSave = null;
  792. Object objLoaded;
  793. try {
  794. objToSave = Class.forName("java.lang.Integer");
  795. if (DEBUG)
  796. System.out.println("Obj = " + objToSave);
  797. objLoaded = dumpAndReload(objToSave);
  798. // Classes with the same name are unique, so test for ==
  799. assertTrue(MSG_TEST_FAILED + objToSave, objLoaded == objToSave);
  800. } catch (IOException e) {
  801. fail("IOException serializing " + objToSave + " : "
  802. + e.getMessage());
  803. } catch (ClassNotFoundException e) {
  804. fail("ClassNotFoundException reading Object type : "
  805. + e.getMessage());
  806. } catch (Error err) {
  807. System.out.println("Error when obj = " + objToSave);
  808. // err.printStackTrace();
  809. throw err;
  810. }
  811. }
  812. public void test_18_21_writeObject() {
  813. // Test for method void
  814. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  815. Object objToSave = null;
  816. Object objLoaded;
  817. try {
  818. // Even though instances of java.lang.Object are not Serializable,
  819. // instances of java.lang.Class are. So, the object
  820. // java.lang.Object.class
  821. // should be serializable
  822. objToSave = Class.forName("java.lang.Object");
  823. if (DEBUG)
  824. System.out.println("Obj = " + objToSave);
  825. objLoaded = dumpAndReload(objToSave);
  826. // Classes with the same name are unique, so test for ==
  827. assertTrue(MSG_TEST_FAILED + objToSave, objLoaded == objToSave);
  828. } catch (IOException e) {
  829. fail("IOException serializing " + objToSave + " : "
  830. + e.getMessage());
  831. } catch (ClassNotFoundException e) {
  832. fail("ClassNotFoundException reading Object type : "
  833. + e.getMessage());
  834. } catch (Error err) {
  835. System.out.println("Error when obj = " + objToSave);
  836. // err.printStackTrace();
  837. throw err;
  838. }
  839. }
  840. public void test_18_22_writeObject() {
  841. // Test for method void
  842. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  843. Object objToSave = null;
  844. Object objLoaded;
  845. try {
  846. java.net.URL url = new java.net.URL("http://localhost/a.txt");
  847. objToSave = url;
  848. if (DEBUG)
  849. System.out.println("Obj = " + objToSave);
  850. objLoaded = dumpAndReload(objToSave);
  851. assertTrue("URLs are not the same: " + url + "\t,\t" + objLoaded,
  852. url.equals(objLoaded));
  853. } catch (IOException e) {
  854. fail("IOException serializing " + objToSave + " : "
  855. + e.getMessage());
  856. } catch (ClassNotFoundException e) {
  857. fail("ClassNotFoundException reading Object type : "
  858. + e.getMessage());
  859. } catch (Error err) {
  860. System.out.println("Error when obj = " + objToSave);
  861. // err.printStackTrace();
  862. throw err;
  863. }
  864. }
  865. public void test_18_23_writeObject() {
  866. // Test for method void
  867. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  868. Object objToSave = null;
  869. Object objLoaded;
  870. try {
  871. JustReadObject justReadObject = new JustReadObject();
  872. objToSave = justReadObject;
  873. if (DEBUG)
  874. System.out.println("Obj = " + objToSave);
  875. objLoaded = dumpAndReload(objToSave);
  876. // Only calls readObject on the instance loaded if writeObject was
  877. // also defined
  878. assertTrue("Called readObject on an object without a writeObject",
  879. !((JustReadObject) objLoaded).calledReadObject);
  880. } catch (IOException e) {
  881. fail("IOException serializing " + objToSave + " : "
  882. + e.getMessage());
  883. } catch (ClassNotFoundException e) {
  884. fail("ClassNotFoundException reading Object type : "
  885. + e.getMessage());
  886. } catch (Error err) {
  887. System.out.println("Error when obj = " + objToSave);
  888. // err.printStackTrace();
  889. throw err;
  890. }
  891. }
  892. public void test_18_24_writeObject() {
  893. // Test for method void
  894. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  895. Object objToSave = null;
  896. Object objLoaded;
  897. try {
  898. JustWriteObject justWriteObject = new JustWriteObject();
  899. objToSave = justWriteObject;
  900. if (DEBUG)
  901. System.out.println("Obj = " + objToSave);
  902. objLoaded = dumpAndReload(objToSave);
  903. // Call writeObject on the instance even if it does not define
  904. // readObject
  905. assertTrue(MSG_TEST_FAILED + objToSave,
  906. justWriteObject.calledWriteObject);
  907. } catch (IOException e) {
  908. fail("IOException serializing " + objToSave + " : "
  909. + e.getMessage());
  910. } catch (ClassNotFoundException e) {
  911. fail("ClassNotFoundException reading Object type: "
  912. + e.getMessage());
  913. } catch (Error err) {
  914. System.out.println("Error when obj = " + objToSave);
  915. // err.printStackTrace();
  916. throw err;
  917. }
  918. }
  919. public void test_18_25_writeObject() {
  920. // Test for method void
  921. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  922. Object objToSave = null;
  923. Object objLoaded;
  924. try {
  925. Vector<String> vector = new Vector<String>(1);
  926. vector.add(FOO);
  927. objToSave = vector;
  928. if (DEBUG)
  929. System.out.println("Obj = " + objToSave);
  930. objLoaded = dumpAndReload(objToSave);
  931. // Has to have the string there
  932. assertTrue(MSG_TEST_FAILED + objToSave, FOO
  933. .equals(((java.util.Vector) objLoaded).elementAt(0)));
  934. } catch (IOException e) {
  935. fail("IOException serializing " + objToSave + " : "
  936. + e.getMessage());
  937. } catch (ClassNotFoundException e) {
  938. fail("ClassNotFoundException reading Object type : "
  939. + e.getMessage());
  940. } catch (Error err) {
  941. System.out.println("Error when obj = " + objToSave);
  942. throw err;
  943. }
  944. }
  945. public void test_18_26_writeObject() {
  946. // Test for method void
  947. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  948. Object objToSave = null;
  949. Object objLoaded;
  950. try {
  951. Hashtable<String, String> hashTable = new Hashtable<String, String>(
  952. 5);
  953. hashTable.put(FOO, FOO);
  954. objToSave = hashTable;
  955. if (DEBUG)
  956. System.out.println("Obj = " + objToSave);
  957. objLoaded = dumpAndReload(objToSave);
  958. java.util.Hashtable loadedHashTable = (java.util.Hashtable) objLoaded;
  959. // Has to have the key/value there (FOO -> FOO)
  960. assertTrue(MSG_TEST_FAILED + objToSave, FOO.equals(loadedHashTable
  961. .get(FOO)));
  962. } catch (IOException e) {
  963. fail("IOException serializing " + objToSave + " : "
  964. + e.getMessage());
  965. } catch (ClassNotFoundException e) {
  966. fail("ClassNotFoundException reading Object type : "
  967. + e.getMessage());
  968. } catch (Error err) {
  969. System.out.println("Error when obj = " + objToSave);
  970. throw err;
  971. }
  972. }
  973. public void test_18_27_writeObject() {
  974. // Test for method void
  975. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  976. Object objToSave = null;
  977. Object objLoaded;
  978. try {
  979. ClassBasedReplacementWhenDumping classBasedReplacementWhenDumping = new ClassBasedReplacementWhenDumping();
  980. objToSave = classBasedReplacementWhenDumping;
  981. if (DEBUG)
  982. System.out.println("Obj = " + objToSave);
  983. objLoaded = dumpAndReload(objToSave);
  984. // Has to have run the replacement method
  985. assertTrue("Did not run writeReplace",
  986. classBasedReplacementWhenDumping.calledReplacement);
  987. // Has to have loaded a String (replacement object)
  988. assertTrue("Did not replace properly", FOO.equals(objLoaded));
  989. } catch (IOException e) {
  990. fail("IOException serializing " + objToSave + " : "
  991. + e.getMessage());
  992. } catch (ClassNotFoundException e) {
  993. fail("ClassNotFoundException reading Object type : "
  994. + e.getMessage());
  995. } catch (Error err) {
  996. System.out.println("Error when obj = " + objToSave);
  997. // err.printStackTrace();
  998. throw err;
  999. }
  1000. }
  1001. public void test_18_28_writeObject() {
  1002. // Test for method void
  1003. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1004. Object objToSave = null;
  1005. Object objLoaded;
  1006. try {
  1007. MultipleClassBasedReplacementWhenDumping multipleClassBasedReplacementWhenDumping = new MultipleClassBasedReplacementWhenDumping();
  1008. objToSave = multipleClassBasedReplacementWhenDumping;
  1009. if (DEBUG)
  1010. System.out.println("Obj = " + objToSave);
  1011. objLoaded = dumpAndReload(objToSave);
  1012. // Has to have loaded a String (replacement object)
  1013. assertTrue(
  1014. "Executed multiple levels of replacement (see PR 1F9RNT1), loaded= "
  1015. + objLoaded,
  1016. objLoaded instanceof MultipleClassBasedReplacementWhenDumping.C1);
  1017. } catch (IOException e) {
  1018. fail("IOException serializing " + objToSave + " : "
  1019. + e.getMessage());
  1020. } catch (ClassNotFoundException e) {
  1021. fail("ClassNotFoundException reading Object type : "
  1022. + e.toString());
  1023. } catch (Error err) {
  1024. System.out.println("Error when obj = " + objToSave);
  1025. // err.printStackTrace();
  1026. throw err;
  1027. }
  1028. }
  1029. public void test_18_29_writeObject() {
  1030. // Test for method void
  1031. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1032. Object objToSave = null;
  1033. Object objLoaded;
  1034. try {
  1035. ClassBasedReplacementWhenLoading classBasedReplacementWhenLoading = new ClassBasedReplacementWhenLoading();
  1036. objToSave = classBasedReplacementWhenLoading;
  1037. if (DEBUG)
  1038. System.out.println("Obj = " + objToSave);
  1039. objLoaded = dumpAndReload(objToSave);
  1040. // Has to have loaded a String (replacement object)
  1041. assertTrue("Did not run readResolve", FOO.equals(objLoaded));
  1042. } catch (IOException e) {
  1043. fail("IOException serializing " + objToSave + " : "
  1044. + e.getMessage());
  1045. } catch (ClassNotFoundException e) {
  1046. fail("ClassNotFoundException reading Object type : "
  1047. + e.getMessage());
  1048. } catch (Error err) {
  1049. System.out.println("Error when obj = " + objToSave);
  1050. // err.printStackTrace();
  1051. throw err;
  1052. }
  1053. }
  1054. public void test_18_30_writeObject() {
  1055. // Test for method void
  1056. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1057. Object objToSave = null;
  1058. Object objLoaded;
  1059. try {
  1060. ClassBasedReplacementWhenLoadingViolatesFieldType classBasedReplacementWhenLoadingViolatesFieldType = new ClassBasedReplacementWhenLoadingViolatesFieldType();
  1061. objToSave = classBasedReplacementWhenLoadingViolatesFieldType;
  1062. if (DEBUG)
  1063. System.out.println("Obj = " + objToSave);
  1064. objLoaded = dumpAndReload(objToSave);
  1065. // We cannot gere here, the load replacement must have caused a
  1066. // field type violation
  1067. fail(
  1068. "Loading replacements can cause field type violation in this implementation");
  1069. } catch (IOException e) {
  1070. fail("IOException serializing " + objToSave + " : "
  1071. + e.getMessage());
  1072. } catch (ClassNotFoundException e) {
  1073. fail("ClassNotFoundException reading Object type : "
  1074. + e.getMessage());
  1075. } catch (ClassCastException e) {
  1076. assertTrue(
  1077. "Loading replacements can NOT cause field type violation in this implementation",
  1078. true);
  1079. } catch (Error err) {
  1080. System.out.println("Error when obj = " + objToSave);
  1081. // err.printStackTrace();
  1082. throw err;
  1083. }
  1084. }
  1085. public void test_18_31_writeObject() {
  1086. // Test for method void
  1087. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1088. Object objToSave = null;
  1089. Object objLoaded;
  1090. try {
  1091. MyExceptionWhenDumping1 exceptionWhenDumping = new MyExceptionWhenDumping1();
  1092. objToSave = exceptionWhenDumping;
  1093. if (DEBUG)
  1094. System.out.println("Obj = " + objToSave);
  1095. boolean causedException = false;
  1096. try {
  1097. dump(objToSave);
  1098. } catch (MyExceptionWhenDumping1.MyException e) {
  1099. causedException = true;
  1100. }
  1101. ;
  1102. assertTrue("Should have caused an exception when dumping",
  1103. causedException);
  1104. causedException = false;
  1105. try {
  1106. objLoaded = reload();
  1107. // Although the spec says we should get a WriteAbortedException,
  1108. // the serialization format handle an Exception when reading
  1109. // primitive data so we get ClassCastException instead
  1110. } catch (ClassCastException e) {
  1111. causedException = true;
  1112. }
  1113. ;
  1114. assertTrue("Should have caused a ClassCastException when loading",
  1115. causedException);
  1116. } catch (IOException e) {
  1117. fail("IOException serializing " + objToSave + " : "
  1118. + e.getMessage());
  1119. } catch (ClassNotFoundException e) {
  1120. fail("ClassNotFoundException reading Object type : "
  1121. + e.getMessage());
  1122. } catch (Error err) {
  1123. System.out.println("Error when obj = " + objToSave);
  1124. // err.printStackTrace();
  1125. throw err;
  1126. }
  1127. }
  1128. public void test_18_32_writeObject() {
  1129. // Test for method void
  1130. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1131. Object objToSave = null;
  1132. Object objLoaded;
  1133. try {
  1134. MyExceptionWhenDumping2 exceptionWhenDumping = new MyExceptionWhenDumping2();
  1135. objToSave = exceptionWhenDumping;
  1136. if (DEBUG)
  1137. System.out.println("Obj = " + objToSave);
  1138. boolean causedException = false;
  1139. try {
  1140. dump(objToSave);
  1141. } catch (MyExceptionWhenDumping2.MyException e) {
  1142. causedException = true;
  1143. }
  1144. ;
  1145. assertTrue("Should have caused an exception when dumping",
  1146. causedException);
  1147. causedException = false;
  1148. try {
  1149. objLoaded = reload();
  1150. } catch (java.io.WriteAbortedException e) {
  1151. causedException = true;
  1152. }
  1153. ;
  1154. assertTrue(
  1155. "Should have caused a java.io.WriteAbortedException when loading",
  1156. causedException);
  1157. } catch (IOException e) {
  1158. fail("IOException serializing " + objToSave + " : "
  1159. + e.getMessage());
  1160. } catch (ClassNotFoundException e) {
  1161. fail("ClassNotFoundException reading Object type : "
  1162. + e.getMessage());
  1163. } catch (ClassCastException e) {
  1164. fail("ClassCastException : " + e.getMessage());
  1165. } catch (Error err) {
  1166. System.out.println("Error when obj = " + objToSave);
  1167. throw err;
  1168. }
  1169. }
  1170. public void test_NonSerializableExceptionWhenDumping() {
  1171. // Test for method void
  1172. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1173. Object objToSave = null;
  1174. Object objLoaded;
  1175. try {
  1176. NonSerializableExceptionWhenDumping nonSerializableExceptionWhenDumping = new NonSerializableExceptionWhenDumping();
  1177. objToSave = nonSerializableExceptionWhenDumping;
  1178. if (DEBUG)
  1179. System.out.println("Obj = " + objToSave);
  1180. boolean causedException = false;
  1181. try {
  1182. dump(objToSave);
  1183. } catch (java.io.NotSerializableException e) {
  1184. causedException = true;
  1185. }
  1186. ;
  1187. assertTrue("Should have caused an exception when dumping",
  1188. causedException);
  1189. causedException = false;
  1190. try {
  1191. objLoaded = reload();
  1192. } catch (java.io.WriteAbortedException e) {
  1193. causedException = true;
  1194. }
  1195. ;
  1196. assertTrue(
  1197. "Should have caused a java.io.WriteAbortedException when loading",
  1198. causedException);
  1199. } catch (IOException e) {
  1200. fail("IOException serializing " + objToSave + " : "
  1201. + e.getMessage());
  1202. } catch (ClassNotFoundException e) {
  1203. fail("ClassNotFoundException reading Object type : "
  1204. + e.getMessage());
  1205. } catch (Error err) {
  1206. System.out.println("Error when obj = " + objToSave);
  1207. // err.printStackTrace();
  1208. throw err;
  1209. }
  1210. }
  1211. public void test_18_33_writeObject() {
  1212. // Test for method void
  1213. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1214. Object objToSave = null;
  1215. Object objLoaded;
  1216. try {
  1217. MyUnserializableExceptionWhenDumping exceptionWhenDumping = new MyUnserializableExceptionWhenDumping();
  1218. objToSave = exceptionWhenDumping;
  1219. if (DEBUG)
  1220. System.out.println("Obj = " + objToSave);
  1221. boolean causedException = false;
  1222. try {
  1223. dump(objToSave);
  1224. } catch (java.io.StreamCorruptedException e) {
  1225. causedException = true;
  1226. }
  1227. ;
  1228. assertTrue("Should have caused an exception when dumping",
  1229. causedException);
  1230. // As the stream is corrupted, reading the stream will have
  1231. // undefined results
  1232. } catch (IOException e) {
  1233. fail("IOException serializing " + objToSave + " : "
  1234. + e.getMessage());
  1235. } catch (ClassNotFoundException e) {
  1236. fail("ClassNotFoundException reading Object type : "
  1237. + e.getMessage());
  1238. } catch (Error err) {
  1239. System.out.println("Error when obj = " + objToSave);
  1240. // err.printStackTrace();
  1241. throw err;
  1242. }
  1243. }
  1244. public void test_18_34_writeObject() {
  1245. // Test for method void
  1246. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1247. Object objToSave = null;
  1248. Object objLoaded;
  1249. try {
  1250. java.io.IOException ioe = new java.io.IOException();
  1251. objToSave = ioe;
  1252. if (DEBUG)
  1253. System.out.println("Obj = " + objToSave);
  1254. objLoaded = dumpAndReload(objToSave);
  1255. // Has to be able to save/load an exception
  1256. assertTrue(MSG_TEST_FAILED + objToSave, true);
  1257. } catch (IOException e) {
  1258. fail("IOException serializing " + objToSave + " : "
  1259. + e.getMessage());
  1260. } catch (ClassNotFoundException e) {
  1261. fail("ClassNotFoundException reading Object type : "
  1262. + e.getMessage());
  1263. } catch (Error err) {
  1264. System.out.println("Error when obj = " + objToSave);
  1265. // err.printStackTrace();
  1266. throw err;
  1267. }
  1268. }
  1269. public void test_18_35_writeObject() {
  1270. // Test for method void
  1271. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1272. Object objToSave = null;
  1273. Object objLoaded;
  1274. try {
  1275. objToSave = Class.forName("java.util.Hashtable");
  1276. if (DEBUG)
  1277. System.out.println("Obj = " + objToSave);
  1278. objLoaded = dumpAndReload(objToSave);
  1279. // Classes with the same name are unique, so test for ==
  1280. assertTrue(MSG_TEST_FAILED + objToSave, objLoaded == objToSave);
  1281. } catch (IOException e) {
  1282. fail("IOException serializing " + objToSave + " : "
  1283. + e.getMessage());
  1284. } catch (ClassNotFoundException e) {
  1285. fail("ClassNotFoundException reading Object type : "
  1286. + e.getMessage());
  1287. } catch (Error err) {
  1288. System.out.println("Error when obj = " + objToSave);
  1289. // err.printStackTrace();
  1290. throw err;
  1291. }
  1292. }
  1293. public void test_18_36_writeObject() {
  1294. // Test for method void
  1295. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1296. Object objToSave = null;
  1297. Object objLoaded;
  1298. try {
  1299. java.io.IOException ex = new java.io.InvalidClassException(FOO);
  1300. objToSave = ex;
  1301. if (DEBUG)
  1302. System.out.println("Obj = " + objToSave);
  1303. objLoaded = dumpAndReload(objToSave);
  1304. // Has to be able to save/load an exception
  1305. assertTrue(MSG_TEST_FAILED + objToSave, true);
  1306. } catch (IOException e) {
  1307. fail("IOException serializing " + objToSave + " : "
  1308. + e.getMessage());
  1309. } catch (ClassNotFoundException e) {
  1310. fail("ClassNotFoundException reading Object type : "
  1311. + e.getMessage());
  1312. } catch (Error err) {
  1313. System.out.println("Error when obj = " + objToSave);
  1314. // err.printStackTrace();
  1315. throw err;
  1316. }
  1317. }
  1318. public void test_18_37_writeObject() {
  1319. // Test for method void
  1320. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1321. Object objToSave = null;
  1322. Object objLoaded;
  1323. try {
  1324. java.io.IOException ex = new java.io.InvalidObjectException(FOO);
  1325. objToSave = ex;
  1326. if (DEBUG)
  1327. System.out.println("Obj = " + objToSave);
  1328. objLoaded = dumpAndReload(objToSave);
  1329. // Has to be able to save/load an exception
  1330. assertTrue(MSG_TEST_FAILED + objToSave, true);
  1331. } catch (IOException e) {
  1332. fail("IOException serializing " + objToSave + " : "
  1333. + e.getMessage());
  1334. } catch (ClassNotFoundException e) {
  1335. fail("ClassNotFoundException reading Object type : "
  1336. + e.getMessage());
  1337. } catch (Error err) {
  1338. System.out.println("Error when obj = " + objToSave);
  1339. // err.printStackTrace();
  1340. throw err;
  1341. }
  1342. }
  1343. public void test_18_38_writeObject() {
  1344. // Test for method void
  1345. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1346. Object objToSave = null;
  1347. Object objLoaded;
  1348. try {
  1349. java.io.IOException ex = new java.io.NotActiveException(FOO);
  1350. objToSave = ex;
  1351. if (DEBUG)
  1352. System.out.println("Obj = " + objToSave);
  1353. objLoaded = dumpAndReload(objToSave);
  1354. // Has to be able to save/load an exception
  1355. assertTrue(MSG_TEST_FAILED + objToSave, true);
  1356. } catch (IOException e) {
  1357. fail("IOException serializing " + objToSave + " : "
  1358. + e.getMessage());
  1359. } catch (ClassNotFoundException e) {
  1360. fail("ClassNotFoundException reading Object type : "
  1361. + e.getMessage());
  1362. } catch (Error err) {
  1363. System.out.println("Error when obj = " + objToSave);
  1364. // err.printStackTrace();
  1365. throw err;
  1366. }
  1367. }
  1368. public void test_18_39_writeObject() {
  1369. // Test for method void
  1370. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1371. Object objToSave = null;
  1372. Object objLoaded;
  1373. try {
  1374. java.io.IOException ex = new java.io.NotSerializableException(FOO);
  1375. objToSave = ex;
  1376. if (DEBUG)
  1377. System.out.println("Obj = " + objToSave);
  1378. objLoaded = dumpAndReload(objToSave);
  1379. // Has to be able to save/load an exception
  1380. assertTrue(MSG_TEST_FAILED + objToSave, true);
  1381. } catch (IOException e) {
  1382. fail("IOException serializing " + objToSave + " : "
  1383. + e.getMessage());
  1384. } catch (ClassNotFoundException e) {
  1385. fail("ClassNotFoundException reading Object type : "
  1386. + e.getMessage());
  1387. } catch (Error err) {
  1388. System.out.println("Error when obj = " + objToSave);
  1389. // err.printStackTrace();
  1390. throw err;
  1391. }
  1392. }
  1393. public void test_18_40_writeObject() {
  1394. // Test for method void
  1395. // java.io.ObjectOutputStream.writeObject(java.lang.Object)
  1396. Object objToSave = null;
  1397. Object objLoaded;
  1398. try {
  1399. java.io.IOException ex = new java.io.StreamCorruptedException(FOO);
  1400. objToSave = ex;
  1401. if (DEBUG)
  1402. System.out.println("Obj = " + objToSave);
  1403. objLoaded = dumpAndReload(objToSave);
  1404. // Has to be able to save/load an exception
  1405. assertTrue(MSG_TEST_FAILED + objToSave, true);
  1406. } catch (IOException e) {
  1407. fail("IOException serializing " + objToSave + " : "
  1408. + e.getMessage());
  1409. } catch (ClassNotFoundException e) {
  1410. fail("ClassNotFoundException reading Object type : "
  1411. + e.getMessage());
  1412. } catch (Error err) {
  1413. System.out.println("Error when obj = " + objToSave);
  1414. // err.printStackTrace();
  1415. throw err;
  1416. }
  1417. }
  1418. }