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.ByteArrayOutputStream;
22import java.io.DataInputStream;
23import java.io.DataOutputStream;
24import java.io.EOFException;
25import java.io.IOException;
26import tests.support.Support_ASimpleInputStream;
27
28public class OldDataInputStreamTest extends junit.framework.TestCase {
29
30    private DataOutputStream os;
31
32    private DataInputStream dis;
33
34    private ByteArrayOutputStream bos;
35
36    String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
37
38    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";
39
40    private final int testLength = fileString.length();
41
42    public void test_ConstructorLjava_io_InputStream() {
43        try {
44            os.writeChar('t');
45            os.close();
46            openDataInputStream();
47        } catch (IOException e) {
48            fail("IOException during constructor test : " + e.getMessage());
49        } finally {
50            try {
51                dis.close();
52            } catch (IOException e) {
53                fail("IOException during constructor test : " + e.getMessage());
54            }
55        }
56    }
57
58    public void test_read$B() throws IOException {
59        byte rbytes[] = new byte[testLength - 5];
60        Support_ASimpleInputStream sis = new Support_ASimpleInputStream();
61        int r;
62
63        os.write(fileString.getBytes());
64        os.close();
65        openDataInputStream();
66
67        r = dis.read(rbytes);
68        assertEquals("Test 1: Incorrect number of bytes read;",
69                testLength - 5, r);
70        assertTrue("Test 2: Incorrect data written or read.",
71                new String(rbytes).equals(fileString.substring(0, testLength - 5)));
72
73        r = dis.read(rbytes);
74        assertEquals("Test 3: Incorrect number of bytes read;", 5, r);
75        assertTrue("Test 4: Incorrect data written or read.",
76                new String(rbytes, 0, 5).equals(fileString.substring(testLength - 5)));
77
78        dis.close();
79        sis.throwExceptionOnNextUse = true;
80        dis = new DataInputStream(sis);
81        try {
82            dis.read(rbytes);
83            fail("Test 5: IOException expected.");
84        } catch (IOException e) {
85            // Expected.
86        }
87    }
88
89    public void test_read$BII() throws IOException {
90        byte rbytes[] = new byte[testLength - 5];
91        Support_ASimpleInputStream sis = new Support_ASimpleInputStream();
92        int r;
93
94        os.write(fileString.getBytes());
95        os.close();
96        openDataInputStream();
97
98        r = dis.read(rbytes, 1, testLength - 10);
99        assertEquals("Test 1: Incorrect number of bytes read;",
100                testLength - 10, r);
101        assertEquals("Test 2: Incorrect data read.", 0, rbytes[0]);
102        assertTrue("Test 3: Incorrect data written or read.",
103                new String(rbytes, 1, r).equals(fileString.substring(0, r)));
104
105        r = dis.read(rbytes, 0, 15);
106        assertEquals("Test 3: Incorrect number of bytes read;", 10, r);
107        assertTrue("Test 4: Incorrect data written or read.",
108                new String(rbytes, 0, r).equals(fileString.substring(testLength - 10)));
109
110        dis.close();
111        sis.throwExceptionOnNextUse = true;
112        dis = new DataInputStream(sis);
113        try {
114            dis.read(rbytes, 1, 5);
115            fail("Test 5: IOException expected.");
116        } catch (IOException e) {
117            // Expected.
118        }
119    }
120
121    public void test_read$BII_Exception() throws IOException {
122        byte rbytes[] = new byte[testLength - 5];
123
124        os.write(fileString.getBytes());
125        os.close();
126        openDataInputStream();
127
128        try {
129            dis.read(rbytes, -1, 1);
130            fail("IndexOutOfBoundsException expected.");
131        } catch (IndexOutOfBoundsException e) {
132            // Expected
133        }
134
135        try {
136            dis.read(rbytes, 0, -1);
137            fail("IndexOutOfBoundsException expected.");
138        } catch (IndexOutOfBoundsException e) {
139            // Expected
140        }
141
142        try {
143            dis.read(rbytes, rbytes.length, 1);
144            fail("IndexOutOfBoundsException expected.");
145        } catch (IndexOutOfBoundsException e) {
146            // Expected
147        }
148    }
149
150    public void test_readFully$B() throws IOException {
151        byte rbytes[] = new byte[testLength];
152
153        os.write(fileString.getBytes());
154        os.close();
155        openDataInputStream();
156
157        dis.readFully(rbytes);
158        assertTrue("Test 1: Incorrect data written or read.",
159                new String(rbytes, 0, testLength).equals(fileString));
160
161        dis.close();
162        try {
163            dis.readFully(rbytes);
164            fail("Test 2: IOException expected.");
165        } catch (IOException e) {
166            // Expected.
167        }
168
169        openDataInputStream();
170        dis.readByte();
171        try {
172            dis.readFully(rbytes);
173            fail("Test 3: EOFException expected.");
174        } catch (EOFException e) {
175            // Expected.
176        }
177    }
178
179    public void test_readFully$BII() throws IOException {
180        byte rbytes[] = new byte[testLength];
181
182        os.write(fileString.getBytes());
183        os.close();
184        openDataInputStream();
185
186        dis.readFully(rbytes, 2, testLength - 4);
187        assertTrue("Test 1: Incorrect data written or read.",
188                new String(rbytes, 2, testLength - 4).equals(
189                        fileString.substring(0, testLength - 4)));
190
191        dis.close();
192        try {
193            dis.readFully(rbytes, 0, testLength);
194            fail("Test 2: IOException expected.");
195        } catch (IOException e) {
196            // Expected.
197        }
198
199        openDataInputStream();
200        dis.readByte();
201        try {
202            dis.readFully(rbytes, 0, testLength);
203            fail("Test 3: EOFException expected.");
204        } catch (EOFException e) {
205            // Expected.
206        }
207    }
208
209    public void test_readFully$BII_Exception() throws IOException {
210        DataInputStream is =  new DataInputStream(new ByteArrayInputStream(new byte[testLength]));
211
212        byte[] byteArray = new byte[testLength];
213
214        try {
215            is.readFully(byteArray, 0, -1);
216            fail("Test 1: IndexOutOfBoundsException expected.");
217        } catch (IndexOutOfBoundsException e) {
218            // Expected.
219        }
220
221        try {
222            is.readFully(byteArray, 0, byteArray.length + 1);
223            fail("Test 2: IndexOutOfBoundsException expected.");
224        } catch (IndexOutOfBoundsException e) {
225            // Expected.
226        }
227
228        try {
229            is.readFully(byteArray, 1, byteArray.length);
230            fail("Test 3: IndexOutOfBoundsException expected.");
231        } catch (IndexOutOfBoundsException e) {
232            // Expected.
233        }
234
235        try {
236            is.readFully(byteArray, -1, byteArray.length);
237            fail("Test 4: IndexOutOfBoundsException expected.");
238        } catch (IndexOutOfBoundsException e) {
239            // Expected.
240        }
241
242        try {
243            is.readFully(null, 0, 1);
244            fail("Test 5: NullPointerException expected.");
245        } catch (NullPointerException e) {
246            // Expected.
247        }
248
249        is = new DataInputStream(null);
250
251        try {
252            is.readFully(byteArray, 0, 1);
253            fail("Test 6: NullPointerException expected.");
254        } catch (NullPointerException e) {
255            // Expected.
256        }
257    }
258
259    @SuppressWarnings("deprecation")
260    public void test_readLine() throws IOException {
261        String line;
262        os.writeBytes("Lorem\nipsum\rdolor sit amet...");
263        os.close();
264        openDataInputStream();
265        line = dis.readLine();
266        assertTrue("Test 1: Incorrect line written or read: " + line,
267                line.equals("Lorem"));
268        line = dis.readLine();
269        assertTrue("Test 2: Incorrect line written or read: " + line,
270                line.equals("ipsum"));
271        line = dis.readLine();
272        assertTrue("Test 3: Incorrect line written or read: " + line,
273                line.equals("dolor sit amet..."));
274
275        dis.close();
276        try {
277            dis.readLine();
278            fail("Test 4: IOException expected.");
279        } catch (IOException e) {
280            // Expected.
281        }
282    }
283
284    public void test_readUnsignedByte() throws IOException {
285        os.writeByte((byte) -127);
286        os.close();
287        openDataInputStream();
288        assertEquals("Test 1: Incorrect byte written or read;",
289                129, dis.readUnsignedByte());
290
291        try {
292            dis.readUnsignedByte();
293            fail("Test 2: EOFException expected.");
294        } catch (EOFException e) {
295            // Expected.
296        }
297
298        dis.close();
299        try {
300            dis.readUnsignedByte();
301            fail("Test 3: IOException expected.");
302        } catch (IOException e) {
303            // Expected.
304        }
305    }
306
307    public void test_readUnsignedShort() throws IOException {
308        os.writeShort(Short.MIN_VALUE);
309        os.close();
310        openDataInputStream();
311        assertEquals("Test 1: Incorrect short written or read;",
312                (Short.MAX_VALUE + 1), dis.readUnsignedShort());
313
314        try {
315            dis.readUnsignedShort();
316            fail("Test 2: EOFException expected.");
317        } catch (EOFException e) {
318            // Expected.
319        }
320
321        dis.close();
322        try {
323            dis.readUnsignedShort();
324            fail("Test 3: IOException expected.");
325        } catch (IOException e) {
326            // Expected.
327        }
328    }
329
330    public void test_readUTFLjava_io_DataInput() throws IOException {
331        os.writeUTF(unihw);
332        os.close();
333        openDataInputStream();
334        assertTrue("Test 1: Incorrect UTF-8 string written or read.",
335                DataInputStream.readUTF(dis).equals(unihw));
336
337        try {
338            DataInputStream.readUTF(dis);
339            fail("Test 2: EOFException expected.");
340        } catch (EOFException e) {
341            // Expected.
342        }
343
344        dis.close();
345        try {
346            DataInputStream.readUTF(dis);
347            fail("Test 3: IOException expected.");
348        } catch (IOException e) {
349            // Expected.
350        }
351    }
352
353    private void openDataInputStream() throws IOException {
354        dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
355    }
356
357    /**
358     * Sets up the fixture, for example, open a network connection. This method
359     * is called before a test is executed.
360     */
361    protected void setUp() {
362        bos = new ByteArrayOutputStream();
363        os = new DataOutputStream(bos);
364    }
365
366    /**
367     * Tears down the fixture, for example, close a network connection. This
368     * method is called after a test is executed.
369     */
370    protected void tearDown() {
371        try {
372            os.close();
373        } catch (Exception e) {
374        }
375        try {
376            dis.close();
377        } catch (Exception e) {
378        }
379    }
380}
381