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 */
17package libcore.java.io;
18
19import java.io.ByteArrayInputStream;
20import java.io.EOFException;
21import java.io.IOException;
22import java.io.ObjectInputStream;
23import java.io.ObjectOutputStream;
24import tests.support.Support_OutputStream;
25
26public class OldObjectInputOutputStreamTest extends junit.framework.TestCase {
27
28    private ObjectOutputStream os;
29
30    private ObjectInputStream is;
31
32    private Support_OutputStream sos;
33
34    String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
35
36    public void test_read_writeBoolean() throws IOException {
37        os.writeBoolean(true);
38
39        os.close();
40        openObjectInputStream();
41        assertTrue("Test 1: Incorrect boolean written or read.",
42                is.readBoolean());
43
44        try {
45            is.readBoolean();
46            fail("Test 2: EOFException expected.");
47        } catch (EOFException e) {
48            // Expected.
49        }
50
51        is.close();
52        try {
53            is.readBoolean();
54            fail("Test 3: IOException expected.");
55        } catch (IOException e) {
56            // Expected.
57        }
58    }
59
60    public void test_read_writeByte() throws IOException {
61        os.writeByte((byte) 127);
62
63        os.close();
64        openObjectInputStream();
65        assertEquals("Test 1: Incorrect byte written or read;",
66                (byte) 127, is.readByte());
67
68        try {
69            is.readByte();
70            fail("Test 2: EOFException expected.");
71        } catch (EOFException e) {
72            // Expected.
73        }
74
75        is.close();
76        try {
77            is.readByte();
78            fail("Test 3: IOException expected.");
79        } catch (IOException e) {
80            // Expected.
81        }
82    }
83
84    public void test_read_writeChar() throws IOException {
85        os.writeChar('b');
86
87        os.close();
88        openObjectInputStream();
89        assertEquals("Test 1: Incorrect char written or read;",
90                'b', is.readChar());
91
92        try {
93            is.readChar();
94            fail("Test 2: EOFException expected.");
95        } catch (EOFException e) {
96            // Expected.
97        }
98
99        is.close();
100        try {
101            is.readChar();
102            fail("Test 3: IOException expected.");
103        } catch (IOException e) {
104            // Expected.
105        }
106    }
107
108    public void test_read_writeDouble() throws IOException {
109        os.writeDouble(2345.76834720202);
110
111        os.close();
112        openObjectInputStream();
113        assertEquals("Test 1: Incorrect double written or read;",
114                2345.76834720202, is.readDouble());
115
116        try {
117            is.readDouble();
118            fail("Test 2: EOFException expected.");
119        } catch (EOFException e) {
120            // Expected.
121        }
122
123        is.close();
124        try {
125            is.readDouble();
126            fail("Test 3: IOException expected.");
127        } catch (IOException e) {
128            // Expected.
129        }
130    }
131
132    public void test_read_writeFloat() throws IOException {
133        os.writeFloat(29.08764f);
134
135        os.close();
136        openObjectInputStream();
137        assertEquals("Test 1: Incorrect float written or read;",
138                29.08764f, is.readFloat());
139
140        try {
141            is.readFloat();
142            fail("Test 2: EOFException expected.");
143        } catch (EOFException e) {
144            // Expected.
145        }
146
147        is.close();
148        try {
149            is.readFloat();
150            fail("Test 3: IOException expected.");
151        } catch (IOException e) {
152            // Expected.
153        }
154    }
155
156    public void test_read_writeInt() throws IOException {
157        os.writeInt(768347202);
158
159        os.close();
160        openObjectInputStream();
161        assertEquals("Test 1: Incorrect int written or read;",
162                768347202, is.readInt());
163
164        try {
165            is.readInt();
166            fail("Test 2: EOFException expected.");
167        } catch (EOFException e) {
168            // Expected.
169        }
170
171        is.close();
172        try {
173            is.readInt();
174            fail("Test 3: IOException expected.");
175        } catch (IOException e) {
176            // Expected.
177        }
178    }
179
180    public void test_read_writeLong() throws IOException {
181        os.writeLong(9875645283333L);
182
183        os.close();
184        openObjectInputStream();
185        assertEquals("Test 1: Incorrect long written or read;",
186                9875645283333L, is.readLong());
187
188        try {
189            is.readLong();
190            fail("Test 2: EOFException expected.");
191        } catch (EOFException e) {
192            // Expected.
193        }
194
195        is.close();
196        try {
197            is.readLong();
198            fail("Test 3: IOException expected.");
199        } catch (IOException e) {
200            // Expected.
201        }
202    }
203
204    public void test_read_writeShort() throws IOException {
205        os.writeShort(9875);
206
207        os.close();
208        openObjectInputStream();
209        assertEquals("Test 1: Incorrect short written or read;",
210                9875, is.readShort());
211
212        try {
213            is.readShort();
214            fail("Test 2: EOFException expected.");
215        } catch (EOFException e) {
216            // Expected.
217        }
218
219        is.close();
220        try {
221            is.readShort();
222            fail("Test 3: IOException expected.");
223        } catch (IOException e) {
224            // Expected.
225        }
226    }
227
228    public void test_read_writeUTF() throws IOException {
229        os.writeUTF(unihw);
230
231        os.close();
232        openObjectInputStream();
233        assertTrue("Test 1: Incorrect UTF-8 string written or read.",
234                is.readUTF().equals(unihw));
235
236        try {
237            is.readUTF();
238            fail("Test 2: EOFException expected.");
239        } catch (EOFException e) {
240            // Expected.
241        }
242
243        is.close();
244        try {
245            is.readUTF();
246            fail("Test 3: IOException expected.");
247        } catch (IOException e) {
248            // Expected.
249        }
250    }
251
252    private void openObjectInputStream() throws IOException {
253        is = new ObjectInputStream(
254                new ByteArrayInputStream(sos.toByteArray()));
255    }
256
257    protected void setUp() throws IOException {
258        sos = new Support_OutputStream(256);
259        os = new ObjectOutputStream(sos);
260    }
261
262    protected void tearDown() {
263        try {
264            os.close();
265        } catch (Exception e) {
266        }
267        try {
268            is.close();
269        } catch (Exception e) {
270        }
271    }
272}
273