KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > input > CountingInputStreamTest


1 /*
2  * Copyright 2003,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 package org.apache.commons.io.input;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Tests the CountingInputStream.
25  *
26  * @author <a HREF="mailto:bayard@apache.org">Henri Yandell</a>
27  */

28 public class CountingInputStreamTest extends TestCase {
29
30     public CountingInputStreamTest(String JavaDoc name) {
31         super(name);
32     }
33
34     public void testCounting() throws Exception JavaDoc {
35         String JavaDoc text = "A piece of text";
36         byte[] bytes = text.getBytes();
37         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
38         CountingInputStream cis = new CountingInputStream(bais);
39
40         // have to declare this larger as we're going to read
41
// off the end of the stream and input stream seems
42
// to do bounds checking
43
byte[] result = new byte[21];
44
45         byte[] ba = new byte[5];
46         int found = cis.read(ba);
47         System.arraycopy(ba, 0, result, 0, 5);
48         assertEquals( found, cis.getCount() );
49
50         int value = cis.read();
51         found++;
52         result[5] = (byte)value;
53         assertEquals( found, cis.getCount() );
54
55         found += cis.read(result, 6, 5);
56         assertEquals( found, cis.getCount() );
57
58         found += cis.read(result, 11, 10); // off the end
59
assertEquals( found, cis.getCount() );
60
61         // trim to get rid of the 6 empty values
62
String JavaDoc textResult = new String JavaDoc(result).trim();
63         assertEquals(textResult, text);
64     }
65 }
66
67
Popular Tags