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.output;
018:
019: import java.io.OutputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022:
023: import junit.framework.TestCase;
024: import org.apache.commons.io.IOUtils;
025: import org.apache.commons.io.input.NullInputStream;
026:
027: /**
028: * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
029: */
030:
031: public class CountingOutputStreamTest extends TestCase {
032:
033: public CountingOutputStreamTest(String name) {
034: super (name);
035: }
036:
037: public void testCounting() throws IOException {
038: ByteArrayOutputStream baos = new ByteArrayOutputStream();
039: CountingOutputStream cos = new CountingOutputStream(baos);
040:
041: for (int i = 0; i < 20; i++) {
042: cos.write(i);
043: }
044: assertByteArrayEquals("CountingOutputStream.write(int)", baos
045: .toByteArray(), 0, 20);
046: assertEquals("CountingOutputStream.getCount()", cos.getCount(),
047: 20);
048:
049: byte[] array = new byte[10];
050: for (int i = 20; i < 30; i++) {
051: array[i - 20] = (byte) i;
052: }
053: cos.write(array);
054: assertByteArrayEquals("CountingOutputStream.write(byte[])",
055: baos.toByteArray(), 0, 30);
056: assertEquals("CountingOutputStream.getCount()", cos.getCount(),
057: 30);
058:
059: for (int i = 25; i < 35; i++) {
060: array[i - 25] = (byte) i;
061: }
062: cos.write(array, 5, 5);
063: assertByteArrayEquals(
064: "CountingOutputStream.write(byte[], int, int)", baos
065: .toByteArray(), 0, 35);
066: assertEquals("CountingOutputStream.getCount()", cos.getCount(),
067: 35);
068:
069: int count = cos.resetCount();
070: assertEquals("CountingOutputStream.resetCount()", count, 35);
071:
072: for (int i = 0; i < 10; i++) {
073: cos.write(i);
074: }
075: assertByteArrayEquals("CountingOutputStream.write(int)", baos
076: .toByteArray(), 35, 45);
077: assertEquals("CountingOutputStream.getCount()", cos.getCount(),
078: 10);
079:
080: }
081:
082: /**
083: * Test for files > 2GB in size - see issue IO-84
084: */
085: public void testLargeFiles_IO84() throws Exception {
086: long size = (long) Integer.MAX_VALUE + (long) 1;
087:
088: NullInputStream mock = new NullInputStream(size);
089: OutputStream nos = new NullOutputStream();
090: CountingOutputStream cos = new CountingOutputStream(nos);
091:
092: // Test integer methods
093: IOUtils.copyLarge(mock, cos);
094: try {
095: cos.getCount();
096: fail("Expected getCount() to throw an ArithmeticException");
097: } catch (ArithmeticException ae) {
098: // expected result
099: }
100: try {
101: cos.resetCount();
102: fail("Expected resetCount() to throw an ArithmeticException");
103: } catch (ArithmeticException ae) {
104: // expected result
105: }
106:
107: mock.close();
108:
109: // Test long methods
110: IOUtils.copyLarge(mock, cos);
111: assertEquals("getByteCount()", size, cos.getByteCount());
112: assertEquals("resetByteCount()", size, cos.resetByteCount());
113: }
114:
115: private void assertByteArrayEquals(String msg, byte[] array,
116: int start, int end) {
117: for (int i = start; i < end; i++) {
118: assertEquals(msg + ": array[" + i + "] mismatch", array[i],
119: i - start);
120: }
121: }
122:
123: }
|