Skip to content

Commit a9d448f

Browse files
author
Niall Pemberton
committed
IO-84 - add tests for files > 2GB to IOUtils.copy(), CountingInputStream and CountingOutputStream
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@463537 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1815c8b commit a9d448f

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

src/test/org/apache/commons/io/IOUtilsCopyTestCase.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStreamReader;
2222
import java.io.OutputStream;
2323
import java.io.OutputStreamWriter;
24+
import java.io.IOException;
2425
import java.io.Reader;
2526
import java.io.Writer;
2627
import java.util.Arrays;
@@ -29,7 +30,11 @@
2930
import junit.framework.TestSuite;
3031
import junit.textui.TestRunner;
3132

33+
import org.apache.commons.io.input.NullInputStream;
34+
import org.apache.commons.io.input.NullReader;
3235
import org.apache.commons.io.output.ByteArrayOutputStream;
36+
import org.apache.commons.io.output.NullOutputStream;
37+
import org.apache.commons.io.output.NullWriter;
3338
import org.apache.commons.io.testtools.FileBasedTestCase;
3439
import org.apache.commons.io.testtools.YellOnCloseInputStream;
3540
import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
@@ -111,6 +116,29 @@ public void testCopy_inputStreamToOutputStream_nullOut() throws Exception {
111116
} catch (NullPointerException ex) {}
112117
}
113118

119+
/**
120+
* Test Copying file > 2GB - see issue# IO-84
121+
*/
122+
public void testCopy_inputStreamToOutputStream_IO84() throws Exception {
123+
long size = (long)Integer.MAX_VALUE + (long)1;
124+
InputStream in = new NullInputStream(size);
125+
OutputStream out = new NullOutputStream();
126+
127+
// Test copy() method
128+
try {
129+
IOUtils.copy(in, out);
130+
fail("Expected copy() to throw an ArithmeticException");
131+
} catch (ArithmeticException ae) {
132+
// expected result
133+
}
134+
135+
// reset the input
136+
in.close();
137+
138+
// Test copyLarge() method
139+
assertEquals("copyLarge()", size, IOUtils.copyLarge(in, out));
140+
}
141+
114142
//-----------------------------------------------------------------------
115143
public void testCopy_inputStreamToWriter() throws Exception {
116144
InputStream in = new ByteArrayInputStream(inData);
@@ -332,4 +360,28 @@ public void testCopy_readerToWriter_nullOut() throws Exception {
332360
} catch (NullPointerException ex) {}
333361
}
334362

363+
/**
364+
* Test Copying file > 2GB - see issue# IO-84
365+
*/
366+
public void testCopy_readerToWriter_IO84() throws Exception {
367+
long size = (long)Integer.MAX_VALUE + (long)1;
368+
Reader reader = new NullReader(size);
369+
Writer writer = new NullWriter();
370+
371+
// Test copy() method
372+
try {
373+
IOUtils.copy(reader, writer);
374+
fail("Expected copy() to throw an ArithmeticException");
375+
} catch (ArithmeticException ae) {
376+
// expected result
377+
}
378+
379+
// reset the input
380+
reader.close();
381+
382+
// Test copyLarge() method
383+
assertEquals("copyLarge()", size, IOUtils.copyLarge(reader, writer));
384+
385+
}
386+
335387
}

src/test/org/apache/commons/io/input/CountingInputStreamTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
package org.apache.commons.io.input;
1818

1919
import java.io.ByteArrayInputStream;
20+
import java.io.OutputStream;
2021
import java.io.IOException;
2122

2223
import junit.framework.TestCase;
24+
import org.apache.commons.io.IOUtils;
25+
import org.apache.commons.io.output.NullOutputStream;
2326

2427
/**
2528
* Tests the CountingInputStream.
@@ -67,6 +70,39 @@ public void testCounting() throws Exception {
6770
assertEquals(textResult, text);
6871
}
6972

73+
74+
/**
75+
* Test for files > 2GB in size - see issue IO-84
76+
*/
77+
public void testLargeFiles_IO84() throws Exception {
78+
long size = (long)Integer.MAX_VALUE + (long)1;
79+
NullInputStream mock = new NullInputStream(size);
80+
CountingInputStream cis = new CountingInputStream(mock);
81+
OutputStream out = new NullOutputStream();
82+
83+
// Test integer methods
84+
IOUtils.copyLarge(cis, out);
85+
try {
86+
cis.getCount();
87+
fail("Expected getCount() to throw an ArithmeticException");
88+
} catch (ArithmeticException ae) {
89+
// expected result
90+
}
91+
try {
92+
cis.resetCount();
93+
fail("Expected resetCount() to throw an ArithmeticException");
94+
} catch (ArithmeticException ae) {
95+
// expected result
96+
}
97+
98+
mock.close();
99+
100+
// Test long methods
101+
IOUtils.copyLarge(cis, out);
102+
assertEquals("getByteCount()", size, cis.getByteCount());
103+
assertEquals("resetByteCount()", size, cis.resetByteCount());
104+
}
105+
70106
public void testResetting() throws Exception {
71107
String text = "A piece of text";
72108
byte[] bytes = text.getBytes();

src/test/org/apache/commons/io/output/CountingOutputStreamTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package org.apache.commons.io.output;
1818

1919

20+
import java.io.OutputStream;
2021
import java.io.ByteArrayOutputStream;
2122
import java.io.IOException;
2223

2324
import junit.framework.TestCase;
25+
import org.apache.commons.io.IOUtils;
26+
import org.apache.commons.io.input.NullInputStream;
2427

2528

2629
/**
@@ -70,6 +73,39 @@ public void testCounting() throws IOException {
7073

7174
}
7275

76+
/**
77+
* Test for files > 2GB in size - see issue IO-84
78+
*/
79+
public void testLargeFiles_IO84() throws Exception {
80+
long size = (long)Integer.MAX_VALUE + (long)1;
81+
82+
NullInputStream mock = new NullInputStream(size);
83+
OutputStream nos = new NullOutputStream();
84+
CountingOutputStream cos = new CountingOutputStream(nos);
85+
86+
// Test integer methods
87+
IOUtils.copyLarge(mock, cos);
88+
try {
89+
cos.getCount();
90+
fail("Expected getCount() to throw an ArithmeticException");
91+
} catch (ArithmeticException ae) {
92+
// expected result
93+
}
94+
try {
95+
cos.resetCount();
96+
fail("Expected resetCount() to throw an ArithmeticException");
97+
} catch (ArithmeticException ae) {
98+
// expected result
99+
}
100+
101+
mock.close();
102+
103+
// Test long methods
104+
IOUtils.copyLarge(mock, cos);
105+
assertEquals("getByteCount()", size, cos.getByteCount());
106+
assertEquals("resetByteCount()", size, cos.resetByteCount());
107+
}
108+
73109
private void assertByteArrayEquals(String msg, byte[] array, int start, int end) {
74110
for (int i = start; i < end; i++) {
75111
assertEquals(msg+": array[" + i + "] mismatch", array[i], i-start);

0 commit comments

Comments
 (0)