KickJava   Java API By Example, From Geeks To Geeks.

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


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.2 $ $Date: 2004/02/23 05:02:25 $
30  */

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