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  
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  
23  import junit.framework.TestCase;
24  
25  
26  /**
27   * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
28   */
29  
30  public class TeeOutputStreamTest extends TestCase {
31  
32      public TeeOutputStreamTest(String name) {
33          super(name);
34      }
35  
36      public void testTee() throws IOException {
37          ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
38          ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
39          TeeOutputStream tos = new TeeOutputStream(baos1, baos2);
40          for(int i = 0; i < 20; i++) {
41              tos.write(i);
42          }
43          assertByteArrayEquals("TeeOutputStream.write(int)", baos1.toByteArray(), baos2.toByteArray() );
44  
45          byte[] array = new byte[10];
46          for(int i = 20; i < 30; i++) {
47              array[i-20] = (byte)i;
48          }
49          tos.write(array);
50          assertByteArrayEquals("TeeOutputStream.write(byte[])", baos1.toByteArray(), baos2.toByteArray() );
51  
52          for(int i = 25; i < 35; i++) {
53              array[i-25] = (byte)i;
54          }
55          tos.write(array, 5, 5);
56          assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", baos1.toByteArray(), baos2.toByteArray() );
57      }
58  
59      private void assertByteArrayEquals(String msg, byte[] array1, byte[] array2) {
60          assertEquals(msg+": array size mismatch", array1.length, array2.length);
61          for(int i=0; i<array1.length; i++) {
62              assertEquals(msg+": array[ " + i + "] mismatch", array1[i], array2[i]);
63          }
64      }
65  
66  }