001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.input;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.OutputStream;
021: import java.io.IOException;
022:
023: import junit.framework.TestCase;
024: import org.apache.commons.io.IOUtils;
025: import org.apache.commons.io.output.NullOutputStream;
026:
027: /**
028: * Tests the CountingInputStream.
029: *
030: * @author Marcelo Liberato
031: * @author Stephen Colebourne
032: * @version $Id: CountingInputStreamTest.java 471628 2006-11-06 04:06:45Z bayard $
033: */
034: public class CountingInputStreamTest extends TestCase {
035:
036: public CountingInputStreamTest(String name) {
037: super (name);
038: }
039:
040: public void testCounting() throws Exception {
041: String text = "A piece of text";
042: byte[] bytes = text.getBytes();
043: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
044: CountingInputStream cis = new CountingInputStream(bais);
045:
046: // have to declare this larger as we're going to read
047: // off the end of the stream and input stream seems
048: // to do bounds checking
049: byte[] result = new byte[21];
050:
051: byte[] ba = new byte[5];
052: int found = cis.read(ba);
053: System.arraycopy(ba, 0, result, 0, 5);
054: assertEquals(found, cis.getCount());
055:
056: int value = cis.read();
057: found++;
058: result[5] = (byte) value;
059: assertEquals(found, cis.getCount());
060:
061: found += cis.read(result, 6, 5);
062: assertEquals(found, cis.getCount());
063:
064: found += cis.read(result, 11, 10); // off the end
065: assertEquals(found, cis.getCount());
066:
067: // trim to get rid of the 6 empty values
068: String textResult = new String(result).trim();
069: assertEquals(textResult, text);
070: }
071:
072: /**
073: * Test for files > 2GB in size - see issue IO-84
074: */
075: public void testLargeFiles_IO84() throws Exception {
076: long size = (long) Integer.MAX_VALUE + (long) 1;
077: NullInputStream mock = new NullInputStream(size);
078: CountingInputStream cis = new CountingInputStream(mock);
079: OutputStream out = new NullOutputStream();
080:
081: // Test integer methods
082: IOUtils.copyLarge(cis, out);
083: try {
084: cis.getCount();
085: fail("Expected getCount() to throw an ArithmeticException");
086: } catch (ArithmeticException ae) {
087: // expected result
088: }
089: try {
090: cis.resetCount();
091: fail("Expected resetCount() to throw an ArithmeticException");
092: } catch (ArithmeticException ae) {
093: // expected result
094: }
095:
096: mock.close();
097:
098: // Test long methods
099: IOUtils.copyLarge(cis, out);
100: assertEquals("getByteCount()", size, cis.getByteCount());
101: assertEquals("resetByteCount()", size, cis.resetByteCount());
102: }
103:
104: public void testResetting() throws Exception {
105: String text = "A piece of text";
106: byte[] bytes = text.getBytes();
107: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
108: CountingInputStream cis = new CountingInputStream(bais);
109:
110: byte[] result = new byte[bytes.length];
111:
112: int found = cis.read(result, 0, 5);
113: assertEquals(found, cis.getCount());
114:
115: int count = cis.resetCount();
116: found = cis.read(result, 6, 5);
117: assertEquals(found, count);
118: }
119:
120: public void testZeroLength1() throws Exception {
121: ByteArrayInputStream bais = new ByteArrayInputStream(
122: 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(
132: new byte[0]);
133: CountingInputStream cis = new CountingInputStream(bais);
134:
135: byte[] result = new byte[10];
136:
137: int found = cis.read(result);
138: assertEquals(-1, found);
139: assertEquals(0, cis.getCount());
140: }
141:
142: public void testZeroLength3() throws Exception {
143: ByteArrayInputStream bais = new ByteArrayInputStream(
144: new byte[0]);
145: CountingInputStream cis = new CountingInputStream(bais);
146:
147: byte[] result = new byte[10];
148:
149: int found = cis.read(result, 0, 5);
150: assertEquals(-1, found);
151: assertEquals(0, cis.getCount());
152: }
153:
154: public void testEOF1() throws Exception {
155: ByteArrayInputStream bais = new ByteArrayInputStream(
156: new byte[2]);
157: CountingInputStream cis = new CountingInputStream(bais);
158:
159: int found = cis.read();
160: assertEquals(0, found);
161: assertEquals(1, cis.getCount());
162: found = cis.read();
163: assertEquals(0, found);
164: assertEquals(2, cis.getCount());
165: found = cis.read();
166: assertEquals(-1, found);
167: assertEquals(2, cis.getCount());
168: }
169:
170: public void testEOF2() throws Exception {
171: ByteArrayInputStream bais = new ByteArrayInputStream(
172: new byte[2]);
173: CountingInputStream cis = new CountingInputStream(bais);
174:
175: byte[] result = new byte[10];
176:
177: int found = cis.read(result);
178: assertEquals(2, found);
179: assertEquals(2, cis.getCount());
180: }
181:
182: public void testEOF3() throws Exception {
183: ByteArrayInputStream bais = new ByteArrayInputStream(
184: new byte[2]);
185: CountingInputStream cis = new CountingInputStream(bais);
186:
187: byte[] result = new byte[10];
188:
189: int found = cis.read(result, 0, 5);
190: assertEquals(2, found);
191: assertEquals(2, cis.getCount());
192: }
193:
194: public void testSkipping() throws IOException {
195: String text = "Hello World!";
196: byte[] bytes = text.getBytes();
197: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
198: CountingInputStream cis = new CountingInputStream(bais);
199:
200: assertEquals(6, cis.skip(6));
201: assertEquals(6, cis.getCount());
202: final byte[] result = new byte[6];
203: cis.read(result);
204:
205: assertEquals("World!", new String(result));
206: assertEquals(12, cis.getCount());
207: }
208:
209: }
|