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.BufferedOutputStream;
21import java.io.ByteArrayInputStream;
22import java.io.ByteArrayOutputStream;
23import java.io.DataInputStream;
24import java.io.DataOutputStream;
25import java.io.IOException;
26import tests.support.Support_OutputStream;
27
28public class OldDataOutputStreamTest extends junit.framework.TestCase {
29
30    private DataOutputStream os;
31
32    private DataInputStream dis;
33
34    private ByteArrayOutputStream bos;
35
36    private Support_OutputStream sos;
37
38    String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
39
40    private static final String testString = "Lorem ipsum dolor sit amet,\n" +
41    "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" +
42    "labore et dolore magna aliqua.\n";
43
44    private static final int testLength = testString.length();
45
46    public void test_flush() throws IOException {
47        BufferedOutputStream buf = new BufferedOutputStream(bos);
48
49        os = new DataOutputStream(buf);
50        os.writeInt(9087589);
51        assertTrue("Test 1: Written data should not be available.",
52                bos.toByteArray().length == 0);
53        os.flush();
54        assertTrue("Test 2: Written data should be available.",
55                bos.toByteArray().length > 0);
56        os.close();
57
58        openDataInputStream();
59        int c = dis.readInt();
60        assertEquals("Test 3: Failed to flush correctly;", 9087589, c);
61        dis.close();
62
63        os = new DataOutputStream(sos);
64        try {
65            os.flush();
66            fail("Test 4: IOException expected.");
67        } catch (IOException e) {
68            // Expected.
69        }
70    }
71
72    public void test_write$BII() throws IOException {
73        int r;
74        os.write(testString.getBytes(), 5, testLength - 7);
75        os.close();
76        openDataInputStream();
77        byte[] rbuf = new byte[testLength];
78        r = dis.read(rbuf, 0, testLength);
79        assertEquals("Test 1: Incorrect number of bytes read;",
80                testLength - 7, r);
81        dis.close();
82        assertTrue("Test 2: Incorrect bytes written or read.",
83                new String(rbuf, 0, r).equals(
84                        testString.substring(5, testLength - 2)));
85    }
86
87    public void test_write$BII_Exception() throws IOException {
88        byte[] nullByteArray = null;
89        byte[] byteArray = new byte[10];
90
91        try {
92            os.write(nullByteArray, 0, 1);
93            fail("Test 1: NullPointerException expected.");
94        } catch (NullPointerException e) {
95            // Expected.
96        }
97
98        try {
99            os.write(byteArray, -1, 1);
100            fail("Test 2: IndexOutOfBoundsException expected.");
101        } catch (IndexOutOfBoundsException e) {
102            // Expected.
103        }
104
105        try {
106            os.write(byteArray, 0, -1);
107            fail("Test 3: IndexOutOfBoundsException expected.");
108        } catch (IndexOutOfBoundsException e) {
109            // Expected.
110        }
111
112        try {
113            os.write(byteArray, 1, 10);
114            fail("Test 4: IndexOutOfBoundsException expected.");
115        } catch (IndexOutOfBoundsException e) {
116            // Expected.
117        }
118    }
119
120    public void test_writeI() throws IOException {
121        os.write(42);
122        os.close();
123
124        openDataInputStream();
125        assertEquals("Test 1: Incorrect int written or read;",
126                42, dis.read());
127        dis.close();
128
129        os = new DataOutputStream(sos);
130        try {
131            os.write(42);
132            fail("Test 2: IOException expected.");
133        } catch (IOException e) {
134            // Expected.
135        }
136    }
137
138    public void test_writeBytesLjava_lang_String() throws IOException {
139        os.writeBytes(testString);
140        os.close();
141
142        openDataInputStream();
143        byte[] rbuf = new byte[testLength];
144        dis.read(rbuf, 0, testLength);
145        dis.close();
146        assertTrue("Test 1: Incorrect bytes written or read.",
147                new String(rbuf, 0, testLength).equals(testString));
148
149        os = new DataOutputStream(sos);
150        try {
151            os.writeBytes(testString);
152            fail("Test 2: IOException expected.");
153        } catch (IOException e) {
154            // Expected.
155        }
156    }
157
158    public void test_writeCharsLjava_lang_String() throws IOException {
159        os.writeChars(unihw);
160        os.close();
161        openDataInputStream();
162        char[] chars = new char[unihw.length()];
163        int i, a = dis.available() / 2;
164        for (i = 0; i < a; i++) chars[i] = dis.readChar();
165        assertEquals("Test 1: Incorrect chars written or read;",
166                unihw, new String(chars, 0, i)
167        );
168        dis.close();
169
170        os = new DataOutputStream(sos);
171        try {
172            os.writeChars(unihw);
173            fail("Test 2: IOException expected.");
174        } catch (IOException e) {
175            // Expected.
176        }
177    }
178
179    private void openDataInputStream() throws IOException {
180        dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
181    }
182
183    protected void setUp() {
184        sos = new Support_OutputStream(true);
185        bos = new ByteArrayOutputStream();
186        os = new DataOutputStream(bos);
187    }
188
189    protected void tearDown() {
190        sos.setThrowsException(false);
191        try {
192            if (os != null)
193                os.close();
194            if (dis != null)
195                dis.close();
196        } catch (IOException e) {
197        }
198    }
199}
200