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.ByteArrayOutputStream;
21import java.io.IOException;
22import java.io.UnsupportedEncodingException;
23
24import junit.framework.TestCase;
25import tests.support.Support_OutputStream;
26import dalvik.annotation.TestLevel;
27import dalvik.annotation.TestTargetClass;
28import dalvik.annotation.TestTargetNew;
29
30/**
31 * Automated Test Suite for class java.io.ByteArrayOutputStream
32 *
33 * @see java.io.ByteArrayOutputStream
34 */
35@TestTargetClass(ByteArrayOutputStream.class)
36public class ByteArrayOutputStreamTest extends TestCase {
37
38    ByteArrayOutputStream bos = null;
39
40    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
41
42    /**
43     * @tests java.io.ByteArrayOutputStream#ByteArrayOutputStream(int)
44     */
45    @TestTargetNew(
46        level = TestLevel.COMPLETE,
47        method = "ByteArrayOutputStream",
48        args = {int.class}
49    )
50    public void test_ConstructorI() {
51        bos = new java.io.ByteArrayOutputStream(100);
52        assertEquals("Test 1: Failed to create stream;", 0, bos.size());
53
54        try {
55            bos = new ByteArrayOutputStream(-1);
56            fail("Test 2: IllegalArgumentException expected.");
57        } catch (IllegalArgumentException e) {
58            // Expected.
59        }
60    }
61
62    /**
63     * @tests java.io.ByteArrayOutputStream#ByteArrayOutputStream()
64     */
65    @TestTargetNew(
66        level = TestLevel.COMPLETE,
67        method = "ByteArrayOutputStream",
68        args = {}
69    )
70    public void test_Constructor() {
71        // Test for method java.io.ByteArrayOutputStream()
72        bos = new java.io.ByteArrayOutputStream();
73        assertEquals("Failed to create stream", 0, bos.size());
74    }
75
76    /**
77     * @tests java.io.ByteArrayOutputStream#close()
78     */
79    @TestTargetNew(
80        level = TestLevel.SUFFICIENT,
81        notes = "No IOException check since it is never thrown.",
82        method = "close",
83        args = {}
84    )
85    public void test_close() {
86        // Test for method void java.io.ByteArrayOutputStream.close()
87
88        assertTrue(
89                "close() does nothing for this implementation of OutputSteam",
90                true);
91
92        // The spec seems to say that a closed output stream can't be written
93        // to. We don't throw an exception if attempt is made to write.
94        // Right now our implementation doesn't do anything testable but
95        // should we decide to throw an exception if a closed stream is
96        // written to, the appropriate test is commented out below.
97
98        /***********************************************************************
99         * java.io.ByteArrayOutputStream bos = new
100         * java.io.ByteArrayOutputStream(); bos.write (fileString.getBytes(), 0,
101         * 100); try { bos.close(); } catch (java.io.IOException e) {
102         * fail("IOException closing stream"); } try { bos.write
103         * (fileString.getBytes(), 0, 100); bos.toByteArray(); fail("Wrote
104         * to closed stream"); } catch (Exception e) { }
105         **********************************************************************/
106    }
107
108    /**
109     * @tests java.io.ByteArrayOutputStream#reset()
110     */
111    @TestTargetNew(
112        level = TestLevel.COMPLETE,
113        method = "reset",
114        args = {}
115    )
116    public void test_reset() {
117        // Test for method void java.io.ByteArrayOutputStream.reset()
118        bos = new java.io.ByteArrayOutputStream();
119        bos.write(fileString.getBytes(), 0, 100);
120        bos.reset();
121        assertEquals("Test 1: Reset failed;", 0, bos.size());
122    }
123
124    /**
125     * @tests java.io.ByteArrayOutputStream#size()
126     */
127    @TestTargetNew(
128        level = TestLevel.COMPLETE,
129        notes = "Verifies size() method.",
130        method = "size",
131        args = {}
132    )
133    public void test_size() {
134        // Test for method int java.io.ByteArrayOutputStream.size()
135        bos = new java.io.ByteArrayOutputStream();
136        bos.write(fileString.getBytes(), 0, 100);
137        assertEquals("size test failed", 100, bos.size());
138        bos.reset();
139        assertEquals("size test failed", 0, bos.size());
140    }
141
142    /**
143     * @tests java.io.ByteArrayOutputStream#toByteArray()
144     */
145    @TestTargetNew(
146        level = TestLevel.COMPLETE,
147        notes = "Verifies toByteArray() method.",
148        method = "toByteArray",
149        args = {}
150    )
151    public void test_toByteArray() {
152        // Test for method byte [] java.io.ByteArrayOutputStream.toByteArray()
153        byte[] bytes;
154        byte[] sbytes = fileString.getBytes();
155        bos = new java.io.ByteArrayOutputStream();
156        bos.write(fileString.getBytes(), 0, fileString.length());
157        bytes = bos.toByteArray();
158        for (int i = 0; i < fileString.length(); i++) {
159            assertTrue("Error in byte array", bytes[i] == sbytes[i]);
160        }
161    }
162
163    /**
164     * @tests java.io.ByteArrayOutputStream#toString(java.lang.String)
165     */
166    @TestTargetNew(
167        level = TestLevel.COMPLETE,
168        method = "toString",
169        args = {java.lang.String.class}
170    )
171    public void test_toStringLjava_lang_String() throws Exception {
172        bos = new ByteArrayOutputStream();
173
174        bos.write(fileString.getBytes(), 0, fileString.length());
175        assertTrue("Test 1: Returned incorrect 8859-1 String",
176                bos.toString("8859_1").equals(fileString));
177        assertTrue("Test 2: Returned incorrect 8859-2 String",
178                bos.toString("8859_2").equals(fileString));
179
180        try {
181            bos.toString("NotAnEcoding");
182            fail("Test 3: UnsupportedEncodingException expected.");
183        } catch (UnsupportedEncodingException e) {
184            // Expected.
185        }
186    }
187
188    /**
189     * @tests java.io.ByteArrayOutputStream#toString()
190     */
191    @TestTargetNew(
192        level = TestLevel.COMPLETE,
193        notes = "Verifies toString() method.",
194        method = "toString",
195        args = {}
196    )
197    public void test_toString() {
198        // Test for method java.lang.String
199        // java.io.ByteArrayOutputStream.toString()
200        java.io.ByteArrayOutputStream bos = null;
201        bos = new java.io.ByteArrayOutputStream();
202        bos.write(fileString.getBytes(), 0, fileString.length());
203        assertTrue("Returned incorrect String", bos.toString().equals(
204                fileString));
205    }
206
207    /**
208     * @tests java.io.ByteArrayOutputStream#toString(int)
209     */
210    @SuppressWarnings("deprecation")
211    @TestTargetNew(
212        level = TestLevel.COMPLETE,
213        notes = "Verifies toString(int hibyte) method.",
214        method = "toString",
215        args = {int.class}
216    )
217    public void test_toStringI() {
218        // Test for method java.lang.String
219        // java.io.ByteArrayOutputStream.toString(int)
220        java.io.ByteArrayOutputStream bos = null;
221        bos = new java.io.ByteArrayOutputStream();
222        bos.write(fileString.getBytes(), 0, fileString.length());
223        assertTrue("Returned incorrect String",
224                bos.toString(5).length() == fileString.length());
225    }
226
227    /**
228     * @tests java.io.ByteArrayOutputStream#write(int)
229     */
230    @TestTargetNew(
231        level = TestLevel.COMPLETE,
232        notes = "",
233        method = "write",
234        args = {int.class}
235    )
236    public void test_writeI() {
237        // Test for method void java.io.ByteArrayOutputStream.write(int)
238        bos = new java.io.ByteArrayOutputStream();
239        bos.write('t');
240        byte[] result = bos.toByteArray();
241        assertEquals("Wrote incorrect bytes",
242                "t", new String(result, 0, result.length));
243    }
244
245    /**
246     * @tests java.io.ByteArrayOutputStream#write(byte[], int, int)
247     */
248    @TestTargetNew(
249        level = TestLevel.PARTIAL_COMPLETE,
250        method = "write",
251        args = {byte[].class, int.class, int.class}
252    )
253    public void test_write$BII() {
254        // Test for method void java.io.ByteArrayOutputStream.write(byte [],
255        // int, int)
256        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
257        bos.write(fileString.getBytes(), 0, 100);
258        byte[] result = bos.toByteArray();
259        assertTrue("Wrote incorrect bytes",
260                new String(result, 0, result.length).equals(fileString
261                        .substring(0, 100)));
262    }
263
264    /**
265     * @tests java.io.ByteArrayOutputStream#write(byte[], int, int)
266     */
267    @TestTargetNew(
268        level = TestLevel.PARTIAL_COMPLETE,
269        notes = "Illegal argument checks.",
270        method = "write",
271        args = {byte[].class, int.class, int.class}
272    )
273    public void test_write$BII_Exception() {
274        byte[] target = new byte[10];
275        bos = new ByteArrayOutputStream();
276        try {
277            bos.write(target, -1, 1);
278            fail("Test 1: IndexOutOfBoundsException expected.");
279        } catch (IndexOutOfBoundsException e) {
280            // Expected
281        }
282        try {
283            bos.write(target, 0, -1);
284            fail("Test 2: IndexOutOfBoundsException expected.");
285        } catch (IndexOutOfBoundsException e) {
286            // Expected
287        }
288        try {
289            bos.write(target, 1, target.length);
290            fail("Test 3: IndexOutOfBoundsException expected.");
291        } catch (IndexOutOfBoundsException e) {
292            // Expected
293        }
294        try {
295            bos.write(null, 1, 1);
296            fail("Test 4: NullPointerException expected.");
297        } catch (NullPointerException e) {
298            // Expected.
299        }
300    }
301
302    /**
303     * @tests java.io.ByteArrayOutputStream#writeTo(java.io.OutputStream)
304     */
305    @TestTargetNew(
306        level = TestLevel.COMPLETE,
307        method = "writeTo",
308        args = {java.io.OutputStream.class}
309    )
310    public void test_writeToLjava_io_OutputStream() throws Exception {
311        Support_OutputStream sos = new Support_OutputStream();
312        bos = new java.io.ByteArrayOutputStream();
313        bos.write(fileString.getBytes(), 0, 10);
314        bos.writeTo(sos);
315        assertTrue("Test 1: Incorrect string written.",
316                sos.toString().equals(
317                        fileString.substring(0, 10)));
318
319        sos.setThrowsException(true);
320        try {
321            bos.writeTo(sos);
322            fail("Test 2: IOException expected.");
323        } catch (IOException e) {
324            // Expected.
325        }
326    }
327}
328