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