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 org.apache.harmony.tests.java.io;
19
20import java.io.ByteArrayOutputStream;
21import java.io.FileDescriptor;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.UnsupportedEncodingException;
25
26import junit.framework.TestCase;
27
28/**
29 * Automated Test Suite for class java.io.ByteArrayOutputStream
30 *
31 * @see java.io.ByteArrayOutputStream
32 */
33public class ByteArrayOutputStreamTest extends TestCase {
34
35    ByteArrayOutputStream bos = null;
36
37    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";
38
39    /**
40     * Tears down the fixture, for example, close a network connection. This
41     * method is called after a test is executed.
42     */
43    protected void tearDown() throws Exception {
44        try {
45            bos.close();
46        } catch (Exception ignore) {
47        }
48        super.tearDown();
49    }
50
51    /**
52     * java.io.ByteArrayOutputStream#ByteArrayOutputStream(int)
53     */
54    public void test_ConstructorI() {
55        bos = new ByteArrayOutputStream(100);
56        assertEquals("Failed to create stream", 0, bos.size());
57    }
58
59    /**
60     * java.io.ByteArrayOutputStream#ByteArrayOutputStream()
61     */
62    public void test_Constructor() {
63        bos = new ByteArrayOutputStream();
64        assertEquals("Failed to create stream", 0, bos.size());
65    }
66
67    /**
68     * java.io.ByteArrayOutputStream#close()
69     */
70    public void test_close() {
71        // close() does nothing for this implementation of OutputSteam
72
73        // The spec seems to say that a closed output stream can't be written
74        // to. We don't throw an exception if attempt is made to write.
75        // Right now our implementation doesn't do anything testable but
76        // should we decide to throw an exception if a closed stream is
77        // written to, the appropriate test is commented out below.
78
79        /***********************************************************************
80         * java.io.ByteArrayOutputStream bos = new
81         * java.io.ByteArrayOutputStream(); bos.write (fileString.getBytes(), 0,
82         * 100); try { bos.close(); } catch (java.io.IOException e) {
83         * fail("IOException closing stream"); } try { bos.write
84         * (fileString.getBytes(), 0, 100); bos.toByteArray(); fail("Wrote to
85         * closed stream"); } catch (Exception e) { }
86         **********************************************************************/
87    }
88
89    /**
90     * java.io.ByteArrayOutputStream#reset()
91     */
92    public void test_reset() {
93        bos = new java.io.ByteArrayOutputStream();
94        bos.write(fileString.getBytes(), 0, 100);
95        bos.reset();
96        assertEquals("reset failed", 0, bos.size());
97    }
98
99    /**
100     * java.io.ByteArrayOutputStream#size()
101     */
102    public void test_size() {
103        bos = new java.io.ByteArrayOutputStream();
104        bos.write(fileString.getBytes(), 0, 100);
105        assertEquals("size test failed", 100, bos.size());
106        bos.reset();
107        assertEquals("size test failed", 0, bos.size());
108    }
109
110    /**
111     * java.io.ByteArrayOutputStream#toByteArray()
112     */
113    public void test_toByteArray() {
114        byte[] bytes;
115        byte[] sbytes = fileString.getBytes();
116        bos = new java.io.ByteArrayOutputStream();
117        bos.write(fileString.getBytes(), 0, fileString.length());
118        bytes = bos.toByteArray();
119        for (int i = 0; i < fileString.length(); i++) {
120            assertTrue("Error in byte array", bytes[i] == sbytes[i]);
121        }
122    }
123
124    /**
125     * java.io.ByteArrayOutputStream#toString(java.lang.String)
126     */
127    public void test_toStringLjava_lang_String() throws IOException {
128        ByteArrayOutputStream bos = new ByteArrayOutputStream();
129
130        bos.write(fileString.getBytes("UTF-8"), 0, fileString.length());
131        assertTrue("Returned incorrect 8859-1 String", bos.toString("8859_1")
132                .equals(fileString));
133
134        bos = new ByteArrayOutputStream();
135        bos.write(fileString.getBytes("UTF-8"), 0, fileString.length());
136        assertTrue("Returned incorrect 8859-2 String", bos.toString("8859_2")
137                .equals(fileString));
138    }
139
140    /**
141     * java.io.ByteArrayOutputStream#toString()
142     */
143    public void test_toString() {
144        ByteArrayOutputStream bos = new ByteArrayOutputStream();
145        bos.write(fileString.getBytes(), 0, fileString.length());
146        assertTrue("Returned incorrect String", bos.toString().equals(
147                fileString));
148    }
149
150    /**
151     * java.io.ByteArrayOutputStream#toString(int)
152     */
153    @SuppressWarnings("deprecation")
154    public void test_toStringI() {
155        ByteArrayOutputStream bos = new ByteArrayOutputStream();
156        bos.write(fileString.getBytes(), 0, fileString.length());
157        assertTrue("Returned incorrect String",
158                bos.toString(5).length() == fileString.length());
159    }
160
161    /**
162     * java.io.ByteArrayOutputStream#write(int)
163     */
164    public void test_writeI() throws UnsupportedEncodingException {
165        bos = new ByteArrayOutputStream();
166        bos.write('t');
167        byte[] result = bos.toByteArray();
168        assertEquals("Wrote incorrect bytes", "t", new String(result, 0,
169                result.length, "UTF-8"));
170    }
171
172    /**
173     * java.io.ByteArrayOutputStream#write(byte[], int, int)
174     */
175    public void test_write$BII() {
176        ByteArrayOutputStream bos = new ByteArrayOutputStream();
177        bos.write(fileString.getBytes(), 0, 100);
178        byte[] result = bos.toByteArray();
179        assertTrue("Wrote incorrect bytes",
180                new String(result, 0, result.length).equals(fileString
181                        .substring(0, 100)));
182    }
183
184    /**
185     * java.io.ByteArrayOutputStream#write(byte[], int, int)
186     */
187    public void test_write$BII_2() {
188        // Regression for HARMONY-387
189        ByteArrayOutputStream obj = new ByteArrayOutputStream();
190        try {
191            obj.write(new byte[] { (byte) 0x00 }, -1, 0);
192            fail();
193        } catch (IndexOutOfBoundsException expected) {
194        }
195    }
196
197    /**
198     * java.io.ByteArrayOutputStream#writeTo(java.io.OutputStream)
199     */
200    public void test_writeToLjava_io_OutputStream() throws Exception {
201        ByteArrayOutputStream bos = new ByteArrayOutputStream();
202        ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
203        bos.write(fileString.getBytes(), 0, 100);
204        bos.writeTo(bos2);
205        assertTrue("Returned incorrect String", bos2.toString().equals(
206                fileString.substring(0, 100)));
207
208        // Regression test for HARMONY-834
209        // no exception expected
210        new ByteArrayOutputStream().writeTo(new FileOutputStream(
211                new FileDescriptor()));
212    }
213}
214