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 org.apache.harmony.tests.java.io;
18
19import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.DataInput;
22import java.io.DataInputStream;
23import java.io.DataOutputStream;
24import java.io.EOFException;
25import java.io.IOException;
26
27public class DataInputStreamTest extends junit.framework.TestCase {
28
29    private DataOutputStream os;
30
31    private DataInputStream dis;
32
33    private ByteArrayOutputStream bos;
34
35    String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
36
37    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_DataInputStream\n";
38
39    /**
40     * java.io.DataInputStream#DataInputStream(java.io.InputStream)
41     */
42    public void test_ConstructorLjava_io_InputStream() throws IOException {
43        try {
44            os.writeChar('t');
45            os.close();
46            openDataInputStream();
47        } finally {
48            dis.close();
49        }
50    }
51
52    /**
53     * java.io.DataInputStream#read(byte[])
54     */
55    public void test_read$B() throws IOException {
56        os.write(fileString.getBytes());
57        os.close();
58        openDataInputStream();
59        byte rbytes[] = new byte[fileString.length()];
60        dis.read(rbytes);
61        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
62                .length()).equals(fileString));
63    }
64
65    /**
66     * java.io.DataInputStream#read(byte[], int, int)
67     */
68    public void test_read$BII() throws IOException {
69        os.write(fileString.getBytes());
70        os.close();
71        openDataInputStream();
72        byte rbytes[] = new byte[fileString.length()];
73        dis.read(rbytes, 0, rbytes.length);
74        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
75                .length()).equals(fileString));
76    }
77
78    /**
79     * java.io.DataInputStream#readBoolean()
80     */
81    public void test_readBoolean() throws IOException {
82        os.writeBoolean(true);
83        os.close();
84        openDataInputStream();
85        assertTrue("Incorrect boolean written", dis.readBoolean());
86    }
87
88    /**
89     * java.io.DataInputStream#readByte()
90     */
91    public void test_readByte() throws IOException {
92        os.writeByte((byte) 127);
93        os.close();
94        openDataInputStream();
95        assertTrue("Incorrect byte read", dis.readByte() == (byte) 127);
96    }
97
98    /**
99     * java.io.DataInputStream#readChar()
100     */
101    public void test_readChar() throws IOException {
102        os.writeChar('t');
103        os.close();
104        openDataInputStream();
105        assertEquals("Incorrect char read", 't', dis.readChar());
106    }
107
108    /**
109     * java.io.DataInputStream#readDouble()
110     */
111    public void test_readDouble() throws IOException {
112        os.writeDouble(2345.76834720202);
113        os.close();
114        openDataInputStream();
115        assertEquals("Incorrect double read", 2345.76834720202, dis
116                .readDouble());
117    }
118
119    /**
120     * java.io.DataInputStream#readFloat()
121     */
122    public void test_readFloat() throws IOException {
123        os.writeFloat(29.08764f);
124        os.close();
125        openDataInputStream();
126        assertTrue("Incorrect float read", dis.readFloat() == 29.08764f);
127    }
128
129    /**
130     * java.io.DataInputStream#readFully(byte[])
131     */
132    public void test_readFully$B() throws IOException {
133        os.write(fileString.getBytes());
134        os.close();
135        openDataInputStream();
136        byte rbytes[] = new byte[fileString.length()];
137        dis.readFully(rbytes);
138        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
139                .length()).equals(fileString));
140    }
141
142    /**
143     * java.io.DataInputStream#readFully(byte[], int, int)
144     */
145    public void test_readFully$BII() throws IOException {
146        os.write(fileString.getBytes());
147        os.close();
148        openDataInputStream();
149        byte rbytes[] = new byte[fileString.length()];
150        dis.readFully(rbytes, 0, fileString.length());
151        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
152                .length()).equals(fileString));
153    }
154
155    /**
156     * java.io.DataInputStream#readFully(byte[], int, int)
157     */
158    public void test_readFully$BII_Exception() throws IOException {
159        DataInputStream is = new DataInputStream(new ByteArrayInputStream(
160                new byte[fileString.length()]));
161
162        byte[] byteArray = new byte[fileString.length()];
163
164        try {
165            is.readFully(byteArray, -1, -1);
166            fail("should throw IndexOutOfBoundsException");
167        } catch (IndexOutOfBoundsException e) {
168            // expected
169        }
170
171        try {
172            is.readFully(byteArray, 0, -1);
173            fail("should throw IndexOutOfBoundsException");
174        } catch (IndexOutOfBoundsException e) {
175            // expected
176        }
177
178        try {
179            is.readFully(byteArray, 1, -1);
180            fail("should throw IndexOutOfBoundsException");
181        } catch (IndexOutOfBoundsException e) {
182            // expected
183        }
184
185        is.readFully(byteArray, -1, 0);
186        is.readFully(byteArray, 0, 0);
187        is.readFully(byteArray, 1, 0);
188
189        try {
190            is.readFully(byteArray, -1, 1);
191            fail("should throw IndexOutOfBoundsException");
192        } catch (IndexOutOfBoundsException e) {
193            // expected
194        }
195
196        is.readFully(byteArray, 0, 1);
197        is.readFully(byteArray, 1, 1);
198        try {
199            is.readFully(byteArray, 0, Integer.MAX_VALUE);
200            fail("should throw IndexOutOfBoundsException");
201        } catch (IndexOutOfBoundsException e) {
202            // expected
203        }
204    }
205
206    /**
207     * java.io.DataInputStream#readFully(byte[], int, int)
208     */
209    public void test_readFully$BII_NullArray() throws IOException {
210        DataInputStream is = new DataInputStream(new ByteArrayInputStream(
211                new byte[fileString.length()]));
212
213        byte[] nullByteArray = null;
214
215        try {
216            is.readFully(nullByteArray, -1, -1);
217            fail();
218        } catch (NullPointerException expected) {
219        } catch (IndexOutOfBoundsException expected) {
220        }
221
222        try {
223            is.readFully(nullByteArray, 0, -1);
224            fail();
225        } catch (NullPointerException expected) {
226        } catch (IndexOutOfBoundsException expected) {
227        }
228
229        try {
230            is.readFully(nullByteArray, 1, -1);
231            fail();
232        } catch (NullPointerException expected) {
233        } catch (IndexOutOfBoundsException expected) {
234        }
235
236        is.readFully(nullByteArray, -1, 0);
237        is.readFully(nullByteArray, 0, 0);
238        is.readFully(nullByteArray, 1, 0);
239
240        try {
241            is.readFully(nullByteArray, -1, 1);
242            fail();
243        } catch (NullPointerException expected) {
244        } catch (IndexOutOfBoundsException expected) {
245        }
246
247        try {
248            is.readFully(nullByteArray, 0, 1);
249            fail("should throw NullPointerException");
250        } catch (NullPointerException e) {
251            // expected
252        }
253
254        try {
255            is.readFully(nullByteArray, 1, 1);
256            fail("should throw NullPointerException");
257        } catch (NullPointerException e) {
258            // expected
259        }
260
261        try {
262            is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
263            fail("should throw NullPointerException");
264        } catch (NullPointerException e) {
265            // expected
266        }
267    }
268
269    /**
270     * java.io.DataInputStream#readFully(byte[], int, int)
271     */
272    public void test_readFully$BII_NullStream() throws IOException {
273        DataInputStream is = new DataInputStream(null);
274        byte[] byteArray = new byte[fileString.length()];
275
276        try {
277            is.readFully(byteArray, -1, -1);
278            fail();
279        } catch (NullPointerException expected) {
280        } catch (IndexOutOfBoundsException expected) {
281        }
282
283        try {
284            is.readFully(byteArray, 0, -1);
285            fail();
286        } catch (NullPointerException expected) {
287        } catch (IndexOutOfBoundsException expected) {
288        }
289
290        try {
291            is.readFully(byteArray, 1, -1);
292            fail();
293        } catch (NullPointerException expected) {
294        } catch (IndexOutOfBoundsException expected) {
295        }
296
297        is.readFully(byteArray, -1, 0);
298        is.readFully(byteArray, 0, 0);
299        is.readFully(byteArray, 1, 0);
300
301        try {
302            is.readFully(byteArray, -1, 1);
303            fail("should throw NullPointerException");
304        } catch (NullPointerException e) {
305            // expected
306        }
307
308        try {
309            is.readFully(byteArray, 0, 1);
310            fail("should throw NullPointerException");
311        } catch (NullPointerException e) {
312            // expected
313        }
314
315        try {
316            is.readFully(byteArray, 1, 1);
317            fail("should throw NullPointerException");
318        } catch (NullPointerException e) {
319            // expected
320        }
321
322        try {
323            is.readFully(byteArray, 0, Integer.MAX_VALUE);
324            fail("should throw NullPointerException");
325        } catch (NullPointerException e) {
326            // expected
327        }
328    }
329
330    /**
331     * java.io.DataInputStream#readFully(byte[], int, int)
332     */
333    public void test_readFully$BII_NullStream_NullArray() throws IOException {
334        DataInputStream is = new DataInputStream(null);
335        byte[] nullByteArray = null;
336
337        try {
338            is.readFully(nullByteArray, -1, -1);
339            fail();
340        } catch (NullPointerException expected) {
341        } catch (IndexOutOfBoundsException expected) {
342        }
343
344        try {
345            is.readFully(nullByteArray, 0, -1);
346            fail();
347        } catch (NullPointerException expected) {
348        } catch (IndexOutOfBoundsException expected) {
349        }
350
351        try {
352            is.readFully(nullByteArray, 1, -1);
353            fail();
354        } catch (NullPointerException expected) {
355        } catch (IndexOutOfBoundsException expected) {
356        }
357
358        is.readFully(nullByteArray, -1, 0);
359        is.readFully(nullByteArray, 0, 0);
360        is.readFully(nullByteArray, 1, 0);
361
362        try {
363            is.readFully(nullByteArray, -1, 1);
364            fail("should throw NullPointerException");
365        } catch (NullPointerException e) {
366            // expected
367        }
368
369        try {
370            is.readFully(nullByteArray, 0, 1);
371            fail("should throw NullPointerException");
372        } catch (NullPointerException e) {
373            // expected
374        }
375
376        try {
377            is.readFully(nullByteArray, 1, 1);
378            fail("should throw NullPointerException");
379        } catch (NullPointerException e) {
380            // expected
381        }
382
383        try {
384            is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
385            fail("should throw NullPointerException");
386        } catch (NullPointerException e) {
387            // expected
388        }
389    }
390
391    /**
392     * java.io.DataInputStream#readInt()
393     */
394    public void test_readInt() throws IOException {
395        os.writeInt(768347202);
396        os.close();
397        openDataInputStream();
398        assertEquals("Incorrect int read", 768347202, dis.readInt());
399    }
400
401    /**
402     * java.io.DataInputStream#readLine()
403     */
404    @SuppressWarnings("deprecation")
405    public void test_readLine() throws IOException {
406        os.writeBytes("Hello");
407        os.close();
408        openDataInputStream();
409        String line = dis.readLine();
410        assertTrue("Incorrect line read: " + line, line.equals("Hello"));
411    }
412
413    /**
414     * java.io.DataInputStream#readLong()
415     */
416    public void test_readLong() throws IOException {
417        os.writeLong(9875645283333L);
418        os.close();
419        openDataInputStream();
420        assertEquals("Incorrect long read", 9875645283333L, dis.readLong());
421    }
422
423    /**
424     * java.io.DataInputStream#readShort()
425     */
426    public void test_readShort() throws IOException {
427        os.writeShort(9875);
428        os.close();
429        openDataInputStream();
430        assertTrue("Incorrect short read", dis.readShort() == (short) 9875);
431    }
432
433    /**
434     * java.io.DataInputStream#readUnsignedByte()
435     */
436    public void test_readUnsignedByte() throws IOException {
437        os.writeByte((byte) -127);
438        os.close();
439        openDataInputStream();
440        assertEquals("Incorrect byte read", 129, dis.readUnsignedByte());
441    }
442
443    /**
444     * java.io.DataInputStream#readUnsignedShort()
445     */
446    public void test_readUnsignedShort() throws IOException {
447        os.writeShort(9875);
448        os.close();
449        openDataInputStream();
450        assertEquals("Incorrect short read", 9875, dis.readUnsignedShort());
451    }
452
453    /**
454     * java.io.DataInputStream#readUTF()
455     */
456    public void test_readUTF() throws IOException {
457        os.writeUTF(unihw);
458        os.close();
459        openDataInputStream();
460        assertTrue("Failed to write string in UTF format",
461                dis.available() == unihw.length() + 2);
462        assertTrue("Incorrect string read", dis.readUTF().equals(unihw));
463    }
464
465    static class TestDataInputStream implements DataInput {
466        public boolean readBoolean() throws IOException {
467            return false;
468        }
469
470        public byte readByte() throws IOException {
471            return (byte) 0;
472        }
473
474        public char readChar() throws IOException {
475            return (char) 0;
476        }
477
478        public double readDouble() throws IOException {
479            return 0.0;
480        }
481
482        public float readFloat() throws IOException {
483            return (float) 0.0;
484        }
485
486        public void readFully(byte[] buffer) throws IOException {
487        }
488
489        public void readFully(byte[] buffer, int offset, int count)
490                throws IOException {
491        }
492
493        public int readInt() throws IOException {
494            return 0;
495        }
496
497        public String readLine() throws IOException {
498            return null;
499        }
500
501        public long readLong() throws IOException {
502            return (long) 0;
503        }
504
505        public short readShort() throws IOException {
506            return (short) 0;
507        }
508
509        public int readUnsignedByte() throws IOException {
510            return 0;
511        }
512
513        public int readUnsignedShort() throws IOException {
514            return 0;
515        }
516
517        public String readUTF() throws IOException {
518            return DataInputStream.readUTF(this);
519        }
520
521        public int skipBytes(int count) throws IOException {
522            return 0;
523        }
524    }
525
526    /**
527     * java.io.DataInputStream#readUTF(java.io.DataInput)
528     */
529    public void test_readUTFLjava_io_DataInput() throws IOException {
530        os.writeUTF(unihw);
531        os.close();
532        openDataInputStream();
533        assertTrue("Failed to write string in UTF format",
534                dis.available() == unihw.length() + 2);
535        assertTrue("Incorrect string read", DataInputStream.readUTF(dis)
536                .equals(unihw));
537
538        // Regression test for HARMONY-5336
539        new TestDataInputStream().readUTF();
540    }
541
542    /**
543     * java.io.DataInputStream#skipBytes(int)
544     */
545    public void test_skipBytesI() throws IOException {
546        byte fileBytes[] = fileString.getBytes();
547        os.write(fileBytes);
548        os.close();
549        openDataInputStream();
550        dis.skipBytes(100);
551        byte rbytes[] = new byte[fileString.length()];
552        dis.read(rbytes, 0, 50);
553        dis.close();
554        assertTrue("Incorrect data read", new String(rbytes, 0, 50)
555                .equals(fileString.substring(100, 150)));
556
557        int skipped = 0;
558        openDataInputStream();
559        try {
560            skipped = dis.skipBytes(50000);
561        } catch (EOFException e) {
562        }
563        assertTrue("Skipped should report " + fileString.length() + " not "
564                + skipped, skipped == fileString.length());
565    }
566
567    private void openDataInputStream() throws IOException {
568        dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
569    }
570
571    /**
572     * Sets up the fixture, for example, open a network connection. This method
573     * is called before a test is executed.
574     */
575    protected void setUp() {
576        bos = new ByteArrayOutputStream();
577        os = new DataOutputStream(bos);
578    }
579
580    /**
581     * Tears down the fixture, for example, close a network connection. This
582     * method is called after a test is executed.
583     */
584    protected void tearDown() {
585        try {
586            os.close();
587        } catch (Exception e) {
588        }
589        try {
590            dis.close();
591        } catch (Exception e) {
592        }
593    }
594}
595