1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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.input;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.OutputStream;
21  import java.io.IOException;
22  
23  import junit.framework.TestCase;
24  import org.apache.commons.io.IOUtils;
25  import org.apache.commons.io.output.NullOutputStream;
26  
27  /**
28   * Tests the CountingInputStream.
29   *
30   * @author Marcelo Liberato
31   * @author Stephen Colebourne
32   * @version $Id: CountingInputStreamTest.java 471628 2006-11-06 04:06:45Z bayard $
33   */
34  public class CountingInputStreamTest extends TestCase {
35  
36      public CountingInputStreamTest(String name) {
37          super(name);
38      }
39  
40      public void testCounting() throws Exception {
41          String text = "A piece of text";
42          byte[] bytes = text.getBytes();
43          ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
44          CountingInputStream cis = new CountingInputStream(bais);
45  
46          // have to declare this larger as we're going to read 
47          // off the end of the stream and input stream seems 
48          // to do bounds checking
49          byte[] result = new byte[21];
50  
51          byte[] ba = new byte[5];
52          int found = cis.read(ba);
53          System.arraycopy(ba, 0, result, 0, 5);
54          assertEquals( found, cis.getCount() );
55  
56          int value = cis.read();
57          found++; 
58          result[5] = (byte)value;
59          assertEquals( found, cis.getCount() );
60  
61          found += cis.read(result, 6, 5);
62          assertEquals( found, cis.getCount() );
63  
64          found += cis.read(result, 11, 10); // off the end
65          assertEquals( found, cis.getCount() );
66  
67          // trim to get rid of the 6 empty values
68          String textResult = new String(result).trim();
69          assertEquals(textResult, text);
70      }
71  
72  
73      /**
74       * Test for files > 2GB in size - see issue IO-84
75       */
76      public void testLargeFiles_IO84() throws Exception {
77          long size = (long)Integer.MAX_VALUE + (long)1;
78          NullInputStream mock    = new NullInputStream(size);
79          CountingInputStream cis = new CountingInputStream(mock);
80          OutputStream out        = new NullOutputStream();
81  
82          // Test integer methods
83          IOUtils.copyLarge(cis, out);
84          try {
85              cis.getCount();
86              fail("Expected getCount() to throw an ArithmeticException");
87          } catch (ArithmeticException ae) {
88              // expected result
89          }
90          try {
91              cis.resetCount();
92              fail("Expected resetCount() to throw an ArithmeticException");
93          } catch (ArithmeticException ae) {
94              // expected result
95          }
96  
97          mock.close();
98  
99          // Test long methods
100         IOUtils.copyLarge(cis, out);
101         assertEquals("getByteCount()",   size, cis.getByteCount());
102         assertEquals("resetByteCount()", size, cis.resetByteCount());
103     }
104 
105     public void testResetting() throws Exception {
106         String text = "A piece of text";
107         byte[] bytes = text.getBytes();
108         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
109         CountingInputStream cis = new CountingInputStream(bais);
110 
111         byte[] result = new byte[bytes.length];
112 
113         int found = cis.read(result, 0, 5);
114         assertEquals( found, cis.getCount() );
115 
116         int count = cis.resetCount();
117         found = cis.read(result, 6, 5);
118         assertEquals( found, count );
119     }
120     
121     public void testZeroLength1() throws Exception {
122         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
123         CountingInputStream cis = new CountingInputStream(bais);
124 
125         int found = cis.read();
126         assertEquals(-1, found);
127         assertEquals(0, cis.getCount());
128     }
129 
130     public void testZeroLength2() throws Exception {
131         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
132         CountingInputStream cis = new CountingInputStream(bais);
133 
134         byte[] result = new byte[10];
135 
136         int found = cis.read(result);
137         assertEquals(-1, found);
138         assertEquals(0, cis.getCount());
139     }
140 
141     public void testZeroLength3() throws Exception {
142         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
143         CountingInputStream cis = new CountingInputStream(bais);
144 
145         byte[] result = new byte[10];
146 
147         int found = cis.read(result, 0, 5);
148         assertEquals(-1, found);
149         assertEquals(0, cis.getCount());
150     }
151 
152     public void testEOF1() throws Exception {
153         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
154         CountingInputStream cis = new CountingInputStream(bais);
155 
156         int found = cis.read();
157         assertEquals(0, found);
158         assertEquals(1, cis.getCount());
159         found = cis.read();
160         assertEquals(0, found);
161         assertEquals(2, cis.getCount());
162         found = cis.read();
163         assertEquals(-1, found);
164         assertEquals(2, cis.getCount());
165     }
166 
167     public void testEOF2() throws Exception {
168         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
169         CountingInputStream cis = new CountingInputStream(bais);
170 
171         byte[] result = new byte[10];
172 
173         int found = cis.read(result);
174         assertEquals(2, found);
175         assertEquals(2, cis.getCount());
176     }
177 
178     public void testEOF3() throws Exception {
179         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
180         CountingInputStream cis = new CountingInputStream(bais);
181 
182         byte[] result = new byte[10];
183 
184         int found = cis.read(result, 0, 5);
185         assertEquals(2, found);
186         assertEquals(2, cis.getCount());
187     }
188     
189     public void testSkipping() throws IOException {
190         String text = "Hello World!";
191         byte[] bytes = text.getBytes();
192         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
193         CountingInputStream cis = new CountingInputStream(bais);
194         
195         assertEquals(6,cis.skip(6));
196         assertEquals(6,cis.getCount());
197         final byte[] result = new byte[6];
198         cis.read(result);
199         
200         assertEquals("World!",new String(result));
201         assertEquals(12,cis.getCount());
202     }
203 
204 }