KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > output > TeeOutputStreamTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.io.output;
19
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import junit.framework.TestCase;
25
26
27 /**
28  * @author Henri Yandell (bayard at apache dot org)
29  * @version $Revision: 1.3 $ $Date: 2004/02/23 05:02:25 $
30  */

31
32 public class TeeOutputStreamTest extends TestCase {
33
34     public TeeOutputStreamTest(String JavaDoc name) {
35         super(name);
36     }
37
38     public void testTee() throws IOException JavaDoc {
39         ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
40         ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
41         TeeOutputStream tos = new TeeOutputStream(baos1, baos2);
42         for(int i = 0; i < 20; i++) {
43             tos.write(i);
44         }
45         assertByteArrayEquals("TeeOutputStream.write(int)", baos1.toByteArray(), baos2.toByteArray() );
46
47         byte[] array = new byte[10];
48         for(int i = 20; i < 30; i++) {
49             array[i-20] = (byte)i;
50         }
51         tos.write(array);
52         assertByteArrayEquals("TeeOutputStream.write(byte[])", baos1.toByteArray(), baos2.toByteArray() );
53
54         for(int i = 25; i < 35; i++) {
55             array[i-25] = (byte)i;
56         }
57         tos.write(array, 5, 5);
58         assertByteArrayEquals("TeeOutputStream.write(byte[], int, int)", baos1.toByteArray(), baos2.toByteArray() );
59     }
60
61     private void assertByteArrayEquals(String JavaDoc msg, byte[] array1, byte[] array2) {
62         assertEquals(msg+": array size mismatch", array1.length, array2.length);
63         for(int i=0; i<array1.length; i++) {
64             assertEquals(msg+": array[ " + i + "] mismatch", array1[i], array2[i]);
65         }
66     }
67
68 }
69
Popular Tags