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 tests.api.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;
26
27import tests.support.Support_OutputStream;
28import dalvik.annotation.TestLevel;
29import dalvik.annotation.TestTargetClass;
30import dalvik.annotation.TestTargetNew;
31
32@TestTargetClass(
33        value = DataOutputStream.class,
34        untestedMethods = {
35            @TestTargetNew(
36                    level = TestLevel.NOT_NECESSARY,
37                    notes = "Implicitely tested in setUp().",
38                    method = "DataOutputStream",
39                    args = {java.io.OutputStream.class}
40                )
41        }
42)
43public class DataOutputStreamTest extends junit.framework.TestCase {
44
45    private DataOutputStream os;
46
47    private DataInputStream dis;
48
49    private ByteArrayOutputStream bos;
50
51    private Support_OutputStream sos;
52
53    String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
54
55    private static final String testString = "Lorem ipsum dolor sit amet,\n" +
56    "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" +
57    "labore et dolore magna aliqua.\n";
58
59    private static final int testLength = testString.length();
60
61    /**
62     * @tests java.io.DataOutputStream#flush()
63     */
64    @TestTargetNew(
65        level = TestLevel.COMPLETE,
66        method = "flush",
67        args = {}
68    )
69    public void test_flush() throws IOException {
70        BufferedOutputStream buf = new BufferedOutputStream(bos);
71
72        os = new DataOutputStream(buf);
73        os.writeInt(9087589);
74        assertTrue("Test 1: Written data should not be available.",
75                bos.toByteArray().length == 0);
76        os.flush();
77        assertTrue("Test 2: Written data should be available.",
78                bos.toByteArray().length > 0);
79        os.close();
80
81        openDataInputStream();
82        int c = dis.readInt();
83        assertEquals("Test 3: Failed to flush correctly;", 9087589, c);
84        dis.close();
85
86        os = new DataOutputStream(sos);
87        try {
88            os.flush();
89            fail("Test 4: IOException expected.");
90        } catch (IOException e) {
91            // Expected.
92        }
93    }
94
95    /**
96     * @tests java.io.DataOutputStream#size()
97     */
98    @TestTargetNew(
99        level = TestLevel.COMPLETE,
100        notes = "Verifies size() method.",
101        method = "size",
102        args = {}
103    )
104    public void test_size() {
105        // Test for method int java.io.DataOutputStream.size()
106
107        try {
108            os.write(testString.getBytes(), 0, testLength / 2);
109            os.close();
110            assertEquals("Incorrect size returned", testLength / 2, os.size());
111            openDataInputStream();
112            byte[] rbuf = new byte[testLength / 2];
113            dis.read(rbuf, 0, testLength / 2);
114            dis.close();
115        } catch (IOException e) {
116            fail("Exception during write test : " + e.getMessage());
117        }
118    }
119
120    /**
121     * @tests java.io.DataOutputStream#write(byte[], int, int)
122     */
123    @TestTargetNew(
124        level = TestLevel.PARTIAL_COMPLETE,
125        notes = "IOException checking missed.",
126        method = "write",
127        args = {byte[].class, int.class, int.class}
128    )
129    public void test_write$BII() throws IOException {
130        int r;
131        os.write(testString.getBytes(), 5, testLength - 7);
132        os.close();
133        openDataInputStream();
134        byte[] rbuf = new byte[testLength];
135        r = dis.read(rbuf, 0, testLength);
136        assertEquals("Test 1: Incorrect number of bytes read;",
137                testLength - 7, r);
138        dis.close();
139        assertTrue("Test 2: Incorrect bytes written or read.",
140                new String(rbuf, 0, r).equals(
141                        testString.substring(5, testLength - 2)));
142    }
143
144    /**
145     * @tests java.io.DataOutputStream#write(byte[], int, int)
146     */
147    @TestTargetNew(
148        level = TestLevel.PARTIAL_COMPLETE,
149        notes = "Illegal argument checks.",
150        method = "write",
151        args = {byte[].class, int.class, int.class}
152    )
153    public void test_write$BII_Exception() throws IOException {
154        byte[] nullByteArray = null;
155        byte[] byteArray = new byte[10];
156
157        try {
158            os.write(nullByteArray, 0, 1);
159            fail("Test 1: NullPointerException expected.");
160        } catch (NullPointerException e) {
161            // Expected.
162        }
163
164        try {
165            os.write(byteArray, -1, 1);
166            fail("Test 2: IndexOutOfBoundsException expected.");
167        } catch (IndexOutOfBoundsException e) {
168            // Expected.
169        }
170
171        try {
172            os.write(byteArray, 0, -1);
173            fail("Test 3: IndexOutOfBoundsException expected.");
174        } catch (IndexOutOfBoundsException e) {
175            // Expected.
176        }
177
178        try {
179            os.write(byteArray, 1, 10);
180            fail("Test 4: IndexOutOfBoundsException expected.");
181        } catch (IndexOutOfBoundsException e) {
182            // Expected.
183        }
184    }
185
186    /**
187     * @tests java.io.DataOutputStream#write(int)
188     */
189    @TestTargetNew(
190        level = TestLevel.COMPLETE,
191        method = "write",
192        args = {int.class}
193    )
194    public void test_writeI() throws IOException {
195        os.write(42);
196        os.close();
197
198        openDataInputStream();
199        assertEquals("Test 1: Incorrect int written or read;",
200                42, dis.read());
201        dis.close();
202
203        os = new DataOutputStream(sos);
204        try {
205            os.write(42);
206            fail("Test 2: IOException expected.");
207        } catch (IOException e) {
208            // Expected.
209        }
210    }
211
212    /**
213     * @tests java.io.DataOutputStream#writeBytes(java.lang.String)
214     */
215    @TestTargetNew(
216        level = TestLevel.COMPLETE,
217        method = "writeBytes",
218        args = {java.lang.String.class}
219    )
220    public void test_writeBytesLjava_lang_String() throws IOException {
221        os.writeBytes(testString);
222        os.close();
223
224        openDataInputStream();
225        byte[] rbuf = new byte[testLength];
226        dis.read(rbuf, 0, testLength);
227        dis.close();
228        assertTrue("Test 1: Incorrect bytes written or read.",
229                new String(rbuf, 0, testLength).equals(testString));
230
231        os = new DataOutputStream(sos);
232        try {
233            os.writeBytes(testString);
234            fail("Test 2: IOException expected.");
235        } catch (IOException e) {
236            // Expected.
237        }
238    }
239
240    /**
241     * @tests java.io.DataOutputStream#writeChars(java.lang.String)
242     */
243    @TestTargetNew(
244        level = TestLevel.COMPLETE,
245        method = "writeChars",
246        args = {java.lang.String.class}
247    )
248    public void test_writeCharsLjava_lang_String() throws IOException {
249        os.writeChars(unihw);
250        os.close();
251        openDataInputStream();
252        char[] chars = new char[unihw.length()];
253        int i, a = dis.available() / 2;
254        for (i = 0; i < a; i++) chars[i] = dis.readChar();
255        assertEquals("Test 1: Incorrect chars written or read;",
256                unihw, new String(chars, 0, i)
257        );
258        dis.close();
259
260        os = new DataOutputStream(sos);
261        try {
262            os.writeChars(unihw);
263            fail("Test 2: IOException expected.");
264        } catch (IOException e) {
265            // Expected.
266        }
267    }
268
269    private void openDataInputStream() throws IOException {
270        dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
271    }
272
273    /**
274     * Sets up the fixture, for example, open a network connection. This method
275     * is called before a test is executed.
276     */
277    protected void setUp() {
278        sos = new Support_OutputStream(true);
279        bos = new ByteArrayOutputStream();
280        os = new DataOutputStream(bos);
281    }
282
283    /**
284     * Tears down the fixture, for example, close a network connection. This
285     * method is called after a test is executed.
286     */
287    protected void tearDown() {
288        sos.setThrowsException(false);
289        try {
290            if (os != null)
291                os.close();
292            if (dis != null)
293                dis.close();
294        } catch (IOException e) {
295        }
296    }
297}
298