1 /* Copyright (C) 2002 Free Software Foundation, Inc. |
|
2 Written by Mark Wielaard (mark@klomp.org) |
|
3 |
|
4 This file is part of Mauve. |
|
5 |
|
6 Mauve is free software; you can redistribute it and/or modify |
|
7 it under the terms of the GNU General Public License as published by |
|
8 the Free Software Foundation; either version 2, or (at your option) |
|
9 any later version. |
|
10 |
|
11 Mauve is distributed in the hope that it will be useful, |
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 GNU General Public License for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
|
17 along with Mauve; see the file COPYING. If not, write to |
|
18 the Free Software Foundation, 59 Temple Place - Suite 330, |
|
19 Boston, MA 02111-1307, USA. |
|
20 */ |
|
21 |
|
22 // Tags: JDK1.2 |
|
23 |
|
24 package gnu.testlet.java.util.ArrayList; |
|
25 import gnu.testlet.Testlet; |
|
26 import gnu.testlet.TestHarness; |
|
27 |
|
28 import java.io.*; |
|
29 import java.util.*; |
|
30 |
|
31 /** |
|
32 * Tests serializable form. New .ser files can be generated by calling |
|
33 * the main() method. |
|
34 */ |
|
35 public class serial implements Testlet |
|
36 { |
|
37 |
|
38 public void test (TestHarness harness) |
|
39 { |
|
40 try |
|
41 { |
|
42 // Self test |
|
43 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
44 ObjectOutputStream oos = new ObjectOutputStream(baos); |
|
45 ArrayList[] array = getArrayListArray(); |
|
46 |
|
47 oos.writeObject(array); |
|
48 oos.close(); |
|
49 |
|
50 byte[] bs = baos.toByteArray(); |
|
51 ByteArrayInputStream bois = new ByteArrayInputStream(bs); |
|
52 ObjectInputStream ois = new ObjectInputStream(bois); |
|
53 Object o = ois.readObject(); |
|
54 array = (ArrayList[]) o; |
|
55 harness.check(Arrays.equals(array, getArrayListArray())); |
|
56 |
|
57 ois.close(); |
|
58 |
|
59 // Pre generated test |
|
60 String ser = "gnu#testlet#java#util#ArrayList#ArrayList.ser"; |
|
61 InputStream is = harness.getResourceStream(ser); |
|
62 ois = new ObjectInputStream(is); |
|
63 o = ois.readObject(); |
|
64 array = (ArrayList[]) o; |
|
65 harness.check(Arrays.equals(array, getArrayListArray())); |
|
66 |
|
67 ois.close(); |
|
68 is.close(); |
|
69 } |
|
70 catch (Throwable t) |
|
71 { |
|
72 harness.check(false); |
|
73 harness.debug(t); |
|
74 } |
|
75 } |
|
76 |
|
77 /** |
|
78 * Creates an array of ArrayLists. |
|
79 */ |
|
80 static ArrayList[] getArrayListArray() { |
|
81 ArrayList[] array = new ArrayList[5]; |
|
82 |
|
83 ArrayList al = new ArrayList(); |
|
84 |
|
85 ArrayList empty = (ArrayList) al.clone(); |
|
86 array[0] = empty; |
|
87 array[4] = empty; |
|
88 |
|
89 al.add(new Integer(1)); |
|
90 |
|
91 ArrayList one = (ArrayList) al.clone(); |
|
92 array[1] = one; |
|
93 |
|
94 for (int i = 2; i <= 32; i++) |
|
95 al.add(new Integer(i)); |
|
96 ArrayList list32 = (ArrayList) al.clone(); |
|
97 array[2] = list32; |
|
98 |
|
99 for (int i = 0; i < 20; i++) |
|
100 al.remove(4); |
|
101 array[3] = al; |
|
102 |
|
103 return array; |
|
104 } |
|
105 |
|
106 public static void main(String[] args) throws Exception |
|
107 { |
|
108 String filename = "ArrayListSerial.ser"; |
|
109 OutputStream os = new FileOutputStream(filename); |
|
110 ObjectOutputStream oos = new ObjectOutputStream(os); |
|
111 ArrayList[] array = getArrayListArray(); |
|
112 |
|
113 oos.writeObject(array); |
|
114 oos.close(); |
|
115 os.close(); |
|
116 |
|
117 } |
|
118 } |
|