01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.ByteArrayOutputStream;
20: import java.io.IOException;
21:
22: import junit.framework.TestCase;
23:
24: /**
25: * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
26: */
27:
28: public class TeeOutputStreamTest extends TestCase {
29:
30: public TeeOutputStreamTest(String name) {
31: super (name);
32: }
33:
34: public void testTee() throws IOException {
35: ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
36: ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
37: TeeOutputStream tos = new TeeOutputStream(baos1, baos2);
38: for (int i = 0; i < 20; i++) {
39: tos.write(i);
40: }
41: assertByteArrayEquals("TeeOutputStream.write(int)", baos1
42: .toByteArray(), baos2.toByteArray());
43:
44: byte[] array = new byte[10];
45: for (int i = 20; i < 30; i++) {
46: array[i - 20] = (byte) i;
47: }
48: tos.write(array);
49: assertByteArrayEquals("TeeOutputStream.write(byte[])", baos1
50: .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(
57: "TeeOutputStream.write(byte[], int, int)", baos1
58: .toByteArray(), baos2.toByteArray());
59: }
60:
61: private void assertByteArrayEquals(String msg, byte[] array1,
62: byte[] array2) {
63: assertEquals(msg + ": array size mismatch", array1.length,
64: array2.length);
65: for (int i = 0; i < array1.length; i++) {
66: assertEquals(msg + ": array[ " + i + "] mismatch",
67: array1[i], array2[i]);
68: }
69: }
70:
71: }
|