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  package org.apache.commons.io.output;
18  
19  import java.io.IOException;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * Basic unit tests for the alternative ByteArrayOutputStream implementation.
25   *
26   * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
27   */
28  public class ByteArrayOutputStreamTestCase extends TestCase {
29  
30      private static final byte[] DATA;
31      
32      static {
33          DATA = new byte[64];
34          for (byte i = 0; i < 64; i++) {
35              DATA[i] = i;
36          }
37      }
38  
39      public ByteArrayOutputStreamTestCase(String name) {
40          super(name);
41      }
42  
43      private int writeData(ByteArrayOutputStream baout, 
44                  java.io.ByteArrayOutputStream ref,
45                  int count) throws IOException {
46          if (count > DATA.length) {
47              throw new IllegalArgumentException("Requesting too many bytes");
48          }
49          if (count == 0) {
50              baout.write(100);
51              ref.write(100);
52              return 1;
53          } else {
54              baout.write(DATA, 0, count);
55              ref.write(DATA, 0, count);
56              return count;
57          }
58      }
59      
60      private int writeData(ByteArrayOutputStream baout, 
61                  java.io.ByteArrayOutputStream ref, 
62                  int[] instructions) throws IOException {
63          int written = 0;
64          for (int i = 0; i < instructions.length; i++) {
65              written += writeData(baout, ref, instructions[i]);
66          }
67          return written;
68      }
69  
70      private static boolean byteCmp(byte[] src, byte[] cmp) {
71          for (int i = 0; i < cmp.length; i++) {
72              if (src[i] != cmp[i]) {
73                  return false;
74              }
75          }
76          return true;
77      }
78  
79      private void checkByteArrays(byte[] expected, byte[] actual) {
80          if (expected.length != actual.length) {
81              fail("Resulting byte arrays are not equally long");
82          }
83          if (!byteCmp(expected, actual)) {
84              fail("Resulting byte arrays are not equal");
85          }
86      }
87  
88      private void checkStreams(
89              ByteArrayOutputStream actual,
90              java.io.ByteArrayOutputStream expected) {
91          assertEquals("Sizes are not equal", expected.size(), actual.size());
92          byte[] buf = actual.toByteArray();
93          byte[] refbuf = expected.toByteArray();
94          checkByteArrays(buf, refbuf);
95      }
96                
97      public void testStream() throws Exception {
98          int written;
99          
100         //The ByteArrayOutputStream is initialized with 32 bytes to match
101         //the original more closely for this test.
102         ByteArrayOutputStream baout = new ByteArrayOutputStream(32);
103         java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
104         
105         //First three writes
106         written = writeData(baout, ref, new int[] {4, 10, 22});
107         assertEquals(36, written);
108         checkStreams(baout, ref);
109 
110         //Another two writes to see if there are any bad effects after toByteArray()
111         written = writeData(baout, ref, new int[] {20, 12});
112         assertEquals(32, written);
113         checkStreams(baout, ref);
114 
115         //Now reset the streams        
116         baout.reset();
117         ref.reset();
118         
119         //Test again to see if reset() had any bad effects
120         written = writeData(baout, ref, new int[] {5, 47, 33, 60, 1, 0, 8});
121         assertEquals(155, written);
122         checkStreams(baout, ref);
123         
124         //Write the commons Byte[]OutputStream to a java.io.Byte[]OutputStream 
125         //and vice-versa to test the writeTo() method.
126         ByteArrayOutputStream baout1 = new ByteArrayOutputStream(32);
127         ref.writeTo(baout1);
128         java.io.ByteArrayOutputStream ref1 = new java.io.ByteArrayOutputStream();
129         baout.writeTo(ref1);
130         checkStreams(baout1, ref1);
131         
132         //Testing toString(String)
133         String baoutString = baout.toString("ASCII");
134         String refString = ref.toString("ASCII");
135         assertEquals("ASCII decoded String must be equal", refString, baoutString);
136         
137         //Make sure that empty ByteArrayOutputStreams really don't create garbage
138         //on toByteArray()
139         assertSame(new ByteArrayOutputStream().toByteArray(),
140             new ByteArrayOutputStream().toByteArray());
141     }
142 }
143