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 tests.api.java.io;
18
19import java.io.ByteArrayInputStream;
20import java.io.EOFException;
21import java.io.IOException;
22import java.io.ObjectInputStream;
23import java.io.ObjectOutputStream;
24
25import tests.support.Support_OutputStream;
26import dalvik.annotation.TestLevel;
27import dalvik.annotation.TestTargetClass;
28import dalvik.annotation.TestTargetNew;
29import dalvik.annotation.TestTargets;
30
31@TestTargetClass(ObjectOutputStream.class)
32public class ObjectInputOutputStreamTest extends junit.framework.TestCase {
33
34    private ObjectOutputStream os;
35
36    private ObjectInputStream is;
37
38    private Support_OutputStream sos;
39
40    String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
41
42    /**
43     * @tests java.io.ObjectInputStream#readBoolean()
44     * @tests java.io.ObjectOutputStream#writeBoolean(boolean)
45     */
46    @TestTargets({
47        @TestTargetNew(
48            level = TestLevel.SUFFICIENT,
49            notes = "Tests against golden file missing. IOException can " +
50                    "not be checked since is never thrown (primitive data " +
51                    "is written into a self-expanding buffer).",
52            method = "writeBoolean",
53            args = {boolean.class}
54        ),
55        @TestTargetNew(
56            level = TestLevel.SUFFICIENT,
57            notes = "Tests against golden file missing.",
58            method = "readBoolean",
59            args = {},
60            clazz = ObjectInputStream.class
61        )
62    })
63    public void test_read_writeBoolean() throws IOException {
64        os.writeBoolean(true);
65
66        os.close();
67        openObjectInputStream();
68        assertTrue("Test 1: Incorrect boolean written or read.",
69                is.readBoolean());
70
71        try {
72            is.readBoolean();
73            fail("Test 2: EOFException expected.");
74        } catch (EOFException e) {
75            // Expected.
76        }
77
78        is.close();
79        try {
80            is.readBoolean();
81            fail("Test 3: IOException expected.");
82        } catch (IOException e) {
83            // Expected.
84        }
85    }
86
87    /**
88     * @tests java.io.ObjectInputStream#readByte()
89     * @tests java.io.ObjectOutputStream#writeByte(int)
90     */
91    @TestTargets({
92        @TestTargetNew(
93            level = TestLevel.SUFFICIENT,
94            notes = "Tests against golden file missing. IOException can " +
95                    "not be checked since is never thrown (primitive data " +
96                    "is written into a self-expanding buffer).",
97            method = "writeByte",
98            args = {int.class}
99        ),
100        @TestTargetNew(
101            level = TestLevel.SUFFICIENT,
102            notes = "Tests against golden file missing.",
103            method = "readByte",
104            args = {},
105            clazz = ObjectInputStream.class
106        )
107    })
108    public void test_read_writeByte() throws IOException {
109        os.writeByte((byte) 127);
110
111        os.close();
112        openObjectInputStream();
113        assertEquals("Test 1: Incorrect byte written or read;",
114                (byte) 127, is.readByte());
115
116        try {
117            is.readByte();
118            fail("Test 2: EOFException expected.");
119        } catch (EOFException e) {
120            // Expected.
121        }
122
123        is.close();
124        try {
125            is.readByte();
126            fail("Test 3: IOException expected.");
127        } catch (IOException e) {
128            // Expected.
129        }
130    }
131
132    /**
133     * @tests java.io.ObjectInputStream#readChar()
134     * @tests java.io.ObjectOutputStream#writeChar(int)
135     */
136    @TestTargets({
137        @TestTargetNew(
138            level = TestLevel.SUFFICIENT,
139            notes = "Tests against golden file missing. IOException can " +
140                    "not be checked since is never thrown (primitive data " +
141                    "is written into a self-expanding buffer).",
142            method = "writeChar",
143            args = {int.class}
144        ),
145        @TestTargetNew(
146            level = TestLevel.SUFFICIENT,
147            notes = "Tests against golden file missing.",
148            method = "readChar",
149            args = {},
150            clazz = ObjectInputStream.class
151        )
152    })
153    public void test_read_writeChar() throws IOException {
154        os.writeChar('b');
155
156        os.close();
157        openObjectInputStream();
158        assertEquals("Test 1: Incorrect char written or read;",
159                'b', is.readChar());
160
161        try {
162            is.readChar();
163            fail("Test 2: EOFException expected.");
164        } catch (EOFException e) {
165            // Expected.
166        }
167
168        is.close();
169        try {
170            is.readChar();
171            fail("Test 3: IOException expected.");
172        } catch (IOException e) {
173            // Expected.
174        }
175    }
176
177    /**
178     * @tests java.io.ObjectInputStream#readDouble()
179     * @tests java.io.ObjectOutputStream#writeDouble(double)
180     */
181    @TestTargets({
182        @TestTargetNew(
183            level = TestLevel.SUFFICIENT,
184            notes = "Tests against golden file missing. IOException can " +
185                    "not be checked since is never thrown (primitive data " +
186                    "is written into a self-expanding buffer).",
187            method = "writeDouble",
188            args = {double.class}
189        ),
190        @TestTargetNew(
191            level = TestLevel.SUFFICIENT,
192            notes = "Tests against golden file missing.",
193            method = "readDouble",
194            args = {},
195            clazz = ObjectInputStream.class
196        )
197    })
198    public void test_read_writeDouble() throws IOException {
199        os.writeDouble(2345.76834720202);
200
201        os.close();
202        openObjectInputStream();
203        assertEquals("Test 1: Incorrect double written or read;",
204                2345.76834720202, is.readDouble());
205
206        try {
207            is.readDouble();
208            fail("Test 2: EOFException expected.");
209        } catch (EOFException e) {
210            // Expected.
211        }
212
213        is.close();
214        try {
215            is.readDouble();
216            fail("Test 3: IOException expected.");
217        } catch (IOException e) {
218            // Expected.
219        }
220    }
221
222    /**
223     * @tests java.io.ObjectInputStream#readFloat()
224     * @tests java.io.ObjectOutputStream#writeFloat(float)
225     */
226    @TestTargets({
227        @TestTargetNew(
228            level = TestLevel.SUFFICIENT,
229            notes = "Tests against golden file missing. IOException can " +
230                    "not be checked since is never thrown (primitive data " +
231                    "is written into a self-expanding buffer).",
232            method = "writeFloat",
233            args = {float.class}
234        ),
235        @TestTargetNew(
236            level = TestLevel.SUFFICIENT,
237            notes = "Tests against golden file missing.",
238            method = "readFloat",
239            args = {},
240            clazz = ObjectInputStream.class
241        )
242    })
243    public void test_read_writeFloat() throws IOException {
244        os.writeFloat(29.08764f);
245
246        os.close();
247        openObjectInputStream();
248        assertEquals("Test 1: Incorrect float written or read;",
249                29.08764f, is.readFloat());
250
251        try {
252            is.readFloat();
253            fail("Test 2: EOFException expected.");
254        } catch (EOFException e) {
255            // Expected.
256        }
257
258        is.close();
259        try {
260            is.readFloat();
261            fail("Test 3: IOException expected.");
262        } catch (IOException e) {
263            // Expected.
264        }
265    }
266
267    /**
268     * @tests java.io.ObjectInputStream#readInt()
269     * @tests java.io.ObjectOutputStream#writeInt(int)
270     */
271    @TestTargets({
272        @TestTargetNew(
273            level = TestLevel.SUFFICIENT,
274            notes = "Tests against golden file missing. IOException can " +
275                    "not be checked since is never thrown (primitive data " +
276                    "is written into a self-expanding buffer).",
277            method = "writeInt",
278            args = {int.class}
279        ),
280        @TestTargetNew(
281            level = TestLevel.SUFFICIENT,
282            notes = "Tests against golden file missing.",
283            method = "readInt",
284            args = {},
285            clazz = ObjectInputStream.class
286        )
287    })
288    public void test_read_writeInt() throws IOException {
289        os.writeInt(768347202);
290
291        os.close();
292        openObjectInputStream();
293        assertEquals("Test 1: Incorrect int written or read;",
294                768347202, is.readInt());
295
296        try {
297            is.readInt();
298            fail("Test 2: EOFException expected.");
299        } catch (EOFException e) {
300            // Expected.
301        }
302
303        is.close();
304        try {
305            is.readInt();
306            fail("Test 3: IOException expected.");
307        } catch (IOException e) {
308            // Expected.
309        }
310    }
311
312    /**
313     * @tests java.io.ObjectInputStream#readLong()
314     * @tests java.io.ObjectOutputStream#writeLong(long)
315     */
316    @TestTargets({
317        @TestTargetNew(
318            level = TestLevel.SUFFICIENT,
319            notes = "Tests against golden file missing. IOException can " +
320                    "not be checked since is never thrown (primitive data " +
321                    "is written into a self-expanding buffer).",
322            method = "writeLong",
323            args = {long.class}
324        ),
325        @TestTargetNew(
326            level = TestLevel.SUFFICIENT,
327            notes = "Tests against golden file missing.",
328            method = "readLong",
329            args = {},
330            clazz = ObjectInputStream.class
331        )
332    })
333    public void test_read_writeLong() throws IOException {
334        os.writeLong(9875645283333L);
335
336        os.close();
337        openObjectInputStream();
338        assertEquals("Test 1: Incorrect long written or read;",
339                9875645283333L, is.readLong());
340
341        try {
342            is.readLong();
343            fail("Test 2: EOFException expected.");
344        } catch (EOFException e) {
345            // Expected.
346        }
347
348        is.close();
349        try {
350            is.readLong();
351            fail("Test 3: IOException expected.");
352        } catch (IOException e) {
353            // Expected.
354        }
355    }
356
357    /**
358     * @tests java.io.ObjectInputStream#readShort()
359     * @tests java.io.ObjectOutputStream#writeShort(short)
360     */
361    @TestTargets({
362        @TestTargetNew(
363            level = TestLevel.SUFFICIENT,
364            notes = "Tests against golden file missing. IOException can " +
365                    "not be checked since is never thrown (primitive data " +
366                    "is written into a self-expanding buffer).",
367            method = "writeShort",
368            args = {int.class}
369        ),
370        @TestTargetNew(
371            level = TestLevel.SUFFICIENT,
372            notes = "Tests against golden file missing.",
373            method = "readShort",
374            args = {},
375            clazz = ObjectInputStream.class
376        )
377    })
378    public void test_read_writeShort() throws IOException {
379        os.writeShort(9875);
380
381        os.close();
382        openObjectInputStream();
383        assertEquals("Test 1: Incorrect short written or read;",
384                9875, is.readShort());
385
386        try {
387            is.readShort();
388            fail("Test 2: EOFException expected.");
389        } catch (EOFException e) {
390            // Expected.
391        }
392
393        is.close();
394        try {
395            is.readShort();
396            fail("Test 3: IOException expected.");
397        } catch (IOException e) {
398            // Expected.
399        }
400    }
401
402
403    /**
404     * @tests java.io.ObjectInputStream#readUTF()
405     * @tests java.io.ObjectOutputStream#writeUTF(java.lang.String)
406     */
407    @TestTargets({
408        @TestTargetNew(
409            level = TestLevel.SUFFICIENT,
410            notes = "Tests against golden file missing. IOException can " +
411                    "not be checked since is never thrown (primitive data " +
412                    "is written into a self-expanding buffer).",
413            method = "writeUTF",
414            args = {String.class}
415        ),
416        @TestTargetNew(
417            level = TestLevel.SUFFICIENT,
418            notes = "Tests against golden file missing.",
419            method = "readUTF",
420            args = {},
421            clazz = ObjectInputStream.class
422        )
423    })
424    public void test_read_writeUTF() throws IOException {
425        os.writeUTF(unihw);
426
427        os.close();
428        openObjectInputStream();
429        assertTrue("Test 1: Incorrect UTF-8 string written or read.",
430                is.readUTF().equals(unihw));
431
432        try {
433            is.readUTF();
434            fail("Test 2: EOFException expected.");
435        } catch (EOFException e) {
436            // Expected.
437        }
438
439        is.close();
440        try {
441            is.readUTF();
442            fail("Test 3: IOException expected.");
443        } catch (IOException e) {
444            // Expected.
445        }
446    }
447
448    private void openObjectInputStream() throws IOException {
449        is = new ObjectInputStream(
450                new ByteArrayInputStream(sos.toByteArray()));
451    }
452
453    protected void setUp() throws IOException {
454        sos = new Support_OutputStream(256);
455        os = new ObjectOutputStream(sos);
456    }
457
458    protected void tearDown() {
459        try {
460            os.close();
461        } catch (Exception e) {
462        }
463        try {
464            is.close();
465        } catch (Exception e) {
466        }
467    }
468}
469