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: package org.apache.commons.io.output;
018:
019: import java.io.IOException;
020:
021: import junit.framework.TestCase;
022:
023: /**
024: * Basic unit tests for the alternative ByteArrayOutputStream implementation.
025: *
026: * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
027: */
028: public class ByteArrayOutputStreamTestCase extends TestCase {
029:
030: private static final byte[] DATA;
031:
032: static {
033: DATA = new byte[64];
034: for (byte i = 0; i < 64; i++) {
035: DATA[i] = i;
036: }
037: }
038:
039: public ByteArrayOutputStreamTestCase(String name) {
040: super (name);
041: }
042:
043: private int writeData(ByteArrayOutputStream baout,
044: java.io.ByteArrayOutputStream ref, int count)
045: throws IOException {
046: if (count > DATA.length) {
047: throw new IllegalArgumentException(
048: "Requesting too many bytes");
049: }
050: if (count == 0) {
051: baout.write(100);
052: ref.write(100);
053: return 1;
054: } else {
055: baout.write(DATA, 0, count);
056: ref.write(DATA, 0, count);
057: return count;
058: }
059: }
060:
061: private int writeData(ByteArrayOutputStream baout,
062: java.io.ByteArrayOutputStream ref, int[] instructions)
063: throws IOException {
064: int written = 0;
065: for (int i = 0; i < instructions.length; i++) {
066: written += writeData(baout, ref, instructions[i]);
067: }
068: return written;
069: }
070:
071: private static boolean byteCmp(byte[] src, byte[] cmp) {
072: for (int i = 0; i < cmp.length; i++) {
073: if (src[i] != cmp[i]) {
074: return false;
075: }
076: }
077: return true;
078: }
079:
080: private void checkByteArrays(byte[] expected, byte[] actual) {
081: if (expected.length != actual.length) {
082: fail("Resulting byte arrays are not equally long");
083: }
084: if (!byteCmp(expected, actual)) {
085: fail("Resulting byte arrays are not equal");
086: }
087: }
088:
089: private void checkStreams(ByteArrayOutputStream actual,
090: java.io.ByteArrayOutputStream expected) {
091: assertEquals("Sizes are not equal", expected.size(), actual
092: .size());
093: byte[] buf = actual.toByteArray();
094: byte[] refbuf = expected.toByteArray();
095: checkByteArrays(buf, refbuf);
096: }
097:
098: public void testStream() throws Exception {
099: int written;
100:
101: //The ByteArrayOutputStream is initialized with 32 bytes to match
102: //the original more closely for this test.
103: ByteArrayOutputStream baout = new ByteArrayOutputStream(32);
104: java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
105:
106: //First three writes
107: written = writeData(baout, ref, new int[] { 4, 10, 22 });
108: assertEquals(36, written);
109: checkStreams(baout, ref);
110:
111: //Another two writes to see if there are any bad effects after toByteArray()
112: written = writeData(baout, ref, new int[] { 20, 12 });
113: assertEquals(32, written);
114: checkStreams(baout, ref);
115:
116: //Now reset the streams
117: baout.reset();
118: ref.reset();
119:
120: //Test again to see if reset() had any bad effects
121: written = writeData(baout, ref, new int[] { 5, 47, 33, 60, 1,
122: 0, 8 });
123: assertEquals(155, written);
124: checkStreams(baout, ref);
125:
126: //Write the commons Byte[]OutputStream to a java.io.Byte[]OutputStream
127: //and vice-versa to test the writeTo() method.
128: ByteArrayOutputStream baout1 = new ByteArrayOutputStream(32);
129: ref.writeTo(baout1);
130: java.io.ByteArrayOutputStream ref1 = new java.io.ByteArrayOutputStream();
131: baout.writeTo(ref1);
132: checkStreams(baout1, ref1);
133:
134: //Testing toString(String)
135: String baoutString = baout.toString("ASCII");
136: String refString = ref.toString("ASCII");
137: assertEquals("ASCII decoded String must be equal", refString,
138: baoutString);
139:
140: //Make sure that empty ByteArrayOutputStreams really don't create garbage
141: //on toByteArray()
142: assertSame(new ByteArrayOutputStream().toByteArray(),
143: new ByteArrayOutputStream().toByteArray());
144: }
145: }
|