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.ByteArrayOutputStream;
21import java.io.IOException;
22import java.io.UnsupportedEncodingException;
23
24import junit.framework.TestCase;
25import tests.support.Support_OutputStream;
26
27/**
28 * Automated Test Suite for class java.io.ByteArrayOutputStream
29 *
30 * @see java.io.ByteArrayOutputStream
31 */
32public class OldByteArrayOutputStreamTest extends TestCase {
33
34    ByteArrayOutputStream bos = null;
35
36    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";
37
38    public void test_ConstructorI() {
39        bos = new java.io.ByteArrayOutputStream(100);
40        assertEquals("Test 1: Failed to create stream;", 0, bos.size());
41
42        try {
43            bos = new ByteArrayOutputStream(-1);
44            fail("Test 2: IllegalArgumentException expected.");
45        } catch (IllegalArgumentException e) {
46            // Expected.
47        }
48    }
49
50    public void test_toStringLjava_lang_String() throws Exception {
51        bos = new ByteArrayOutputStream();
52
53        bos.write(fileString.getBytes(), 0, fileString.length());
54        assertTrue("Test 1: Returned incorrect 8859-1 String",
55                bos.toString("8859_1").equals(fileString));
56        assertTrue("Test 2: Returned incorrect 8859-2 String",
57                bos.toString("8859_2").equals(fileString));
58
59        try {
60            bos.toString("NotAnEcoding");
61            fail("Test 3: UnsupportedEncodingException expected.");
62        } catch (UnsupportedEncodingException e) {
63            // Expected.
64        }
65    }
66
67    public void test_write$BII_Exception() {
68        byte[] target = new byte[10];
69        bos = new ByteArrayOutputStream();
70        try {
71            bos.write(target, -1, 1);
72            fail("Test 1: IndexOutOfBoundsException expected.");
73        } catch (IndexOutOfBoundsException e) {
74            // Expected
75        }
76        try {
77            bos.write(target, 0, -1);
78            fail("Test 2: IndexOutOfBoundsException expected.");
79        } catch (IndexOutOfBoundsException e) {
80            // Expected
81        }
82        try {
83            bos.write(target, 1, target.length);
84            fail("Test 3: IndexOutOfBoundsException expected.");
85        } catch (IndexOutOfBoundsException e) {
86            // Expected
87        }
88        try {
89            bos.write(null, 1, 1);
90            fail("Test 4: NullPointerException expected.");
91        } catch (NullPointerException e) {
92            // Expected.
93        }
94    }
95
96    public void test_writeToLjava_io_OutputStream() throws Exception {
97        Support_OutputStream sos = new Support_OutputStream();
98        bos = new java.io.ByteArrayOutputStream();
99        bos.write(fileString.getBytes(), 0, 10);
100        bos.writeTo(sos);
101        assertTrue("Test 1: Incorrect string written.",
102                sos.toString().equals(
103                        fileString.substring(0, 10)));
104
105        sos.setThrowsException(true);
106        try {
107            bos.writeTo(sos);
108            fail("Test 2: IOException expected.");
109        } catch (IOException e) {
110            // Expected.
111        }
112    }
113}
114