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