1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }