001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.FileDescriptor;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * Automated Test Suite for class java.io.ByteArrayOutputStream
029: *
030: * @see java.io.ByteArrayOutputStream
031: */
032: public class ByteArrayOutputStreamTest extends TestCase {
033:
034: ByteArrayOutputStream bos = null;
035:
036: 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";
037:
038: /**
039: * Tears down the fixture, for example, close a network connection. This
040: * method is called after a test is executed.
041: */
042: protected void tearDown() throws Exception {
043: try {
044: bos.close();
045: } catch (Exception ignore) {
046: }
047: super .tearDown();
048: }
049:
050: /**
051: * @tests java.io.ByteArrayOutputStream#ByteArrayOutputStream(int)
052: */
053: public void test_ConstructorI() {
054: bos = new ByteArrayOutputStream(100);
055: assertEquals("Failed to create stream", 0, bos.size());
056: }
057:
058: /**
059: * @tests java.io.ByteArrayOutputStream#ByteArrayOutputStream()
060: */
061: public void test_Constructor() {
062: bos = new ByteArrayOutputStream();
063: assertEquals("Failed to create stream", 0, bos.size());
064: }
065:
066: /**
067: * @tests java.io.ByteArrayOutputStream#close()
068: */
069: public void test_close() {
070: // close() does nothing for this implementation of OutputSteam
071:
072: // The spec seems to say that a closed output stream can't be written
073: // to. We don't throw an exception if attempt is made to write.
074: // Right now our implementation doesn't do anything testable but
075: // should we decide to throw an exception if a closed stream is
076: // written to, the appropriate test is commented out below.
077:
078: /***********************************************************************
079: * java.io.ByteArrayOutputStream bos = new
080: * java.io.ByteArrayOutputStream(); bos.write (fileString.getBytes(), 0,
081: * 100); try { bos.close(); } catch (java.io.IOException e) {
082: * fail("IOException closing stream"); } try { bos.write
083: * (fileString.getBytes(), 0, 100); bos.toByteArray(); fail("Wrote to
084: * closed stream"); } catch (Exception e) { }
085: **********************************************************************/
086: }
087:
088: /**
089: * @tests java.io.ByteArrayOutputStream#reset()
090: */
091: public void test_reset() {
092: bos = new java.io.ByteArrayOutputStream();
093: bos.write(fileString.getBytes(), 0, 100);
094: bos.reset();
095: assertEquals("reset failed", 0, bos.size());
096: }
097:
098: /**
099: * @tests java.io.ByteArrayOutputStream#size()
100: */
101: public void test_size() {
102: bos = new java.io.ByteArrayOutputStream();
103: bos.write(fileString.getBytes(), 0, 100);
104: assertEquals("size test failed", 100, bos.size());
105: bos.reset();
106: assertEquals("size test failed", 0, bos.size());
107: }
108:
109: /**
110: * @tests java.io.ByteArrayOutputStream#toByteArray()
111: */
112: public void test_toByteArray() {
113: byte[] bytes;
114: byte[] sbytes = fileString.getBytes();
115: bos = new java.io.ByteArrayOutputStream();
116: bos.write(fileString.getBytes(), 0, fileString.length());
117: bytes = bos.toByteArray();
118: for (int i = 0; i < fileString.length(); i++) {
119: assertTrue("Error in byte array", bytes[i] == sbytes[i]);
120: }
121: }
122:
123: /**
124: * @tests java.io.ByteArrayOutputStream#toString(java.lang.String)
125: */
126: public void test_toStringLjava_lang_String() throws IOException {
127: ByteArrayOutputStream bos = new ByteArrayOutputStream();
128:
129: bos.write(fileString.getBytes(), 0, fileString.length());
130: assertTrue("Returned incorrect 8859-1 String", bos.toString(
131: "8859_1").equals(fileString));
132:
133: bos = new ByteArrayOutputStream();
134: bos.write(fileString.getBytes(), 0, fileString.length());
135: assertTrue("Returned incorrect 8859-2 String", bos.toString(
136: "8859_2").equals(fileString));
137: }
138:
139: /**
140: * @tests java.io.ByteArrayOutputStream#toString()
141: */
142: public void test_toString() {
143: ByteArrayOutputStream bos = new ByteArrayOutputStream();
144: bos.write(fileString.getBytes(), 0, fileString.length());
145: assertTrue("Returned incorrect String", bos.toString().equals(
146: fileString));
147: }
148:
149: /**
150: * @tests java.io.ByteArrayOutputStream#toString(int)
151: */
152: @SuppressWarnings("deprecation")
153: public void test_toStringI() {
154: ByteArrayOutputStream bos = new ByteArrayOutputStream();
155: bos.write(fileString.getBytes(), 0, fileString.length());
156: assertTrue("Returned incorrect String", bos.toString(5)
157: .length() == fileString.length());
158: }
159:
160: /**
161: * @tests java.io.ByteArrayOutputStream#write(int)
162: */
163: public void test_writeI() {
164: bos = new ByteArrayOutputStream();
165: bos.write('t');
166: byte[] result = bos.toByteArray();
167: assertEquals("Wrote incorrect bytes", "t", new String(result,
168: 0, result.length));
169: }
170:
171: /**
172: * @tests java.io.ByteArrayOutputStream#write(byte[], int, int)
173: */
174: public void test_write$BII() {
175: ByteArrayOutputStream bos = new ByteArrayOutputStream();
176: bos.write(fileString.getBytes(), 0, 100);
177: byte[] result = bos.toByteArray();
178: assertTrue("Wrote incorrect bytes", new String(result, 0,
179: result.length).equals(fileString.substring(0, 100)));
180: }
181:
182: /**
183: * @tests java.io.ByteArrayOutputStream#write(byte[], int, int)
184: */
185: public void test_write$BII_2() {
186: // Regression for HARMONY-387
187: ByteArrayOutputStream obj = new ByteArrayOutputStream();
188: try {
189: obj.write(new byte[] { (byte) 0x00 }, -1, 0);
190: fail("IndexOutOfBoundsException expected");
191: } catch (IndexOutOfBoundsException t) {
192: assertEquals(
193: "IndexOutOfBoundsException rather than a subclass expected",
194: IndexOutOfBoundsException.class, t.getClass());
195: }
196: }
197:
198: /**
199: * @tests java.io.ByteArrayOutputStream#writeTo(java.io.OutputStream)
200: */
201: public void test_writeToLjava_io_OutputStream() throws Exception {
202: ByteArrayOutputStream bos = new ByteArrayOutputStream();
203: ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
204: bos.write(fileString.getBytes(), 0, 100);
205: bos.writeTo(bos2);
206: assertTrue("Returned incorrect String", bos2.toString().equals(
207: fileString.substring(0, 100)));
208:
209: // Regression test for HARMONY-834
210: // no exception expected
211: new ByteArrayOutputStream().writeTo(new FileOutputStream(
212: new FileDescriptor()));
213: }
214: }
|