Skip to content

Commit 45a0b1f

Browse files
author
Stephen Colebourne
committed
IO-96 - FileBasedTestCase - Fixed bug in compare content methods identified by GNU classpath
based on patch from Anthony Green git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@462842 13f79535-47bb-0310-9956-ffa450edef68
1 parent 464ebe8 commit 45a0b1f

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ Bug fixes from 1.2
6868
- new long based methods getByteCount()/resetByteCount() added
6969
- existing methods changed to throw an exception if the count is greater than an int
7070

71+
- FileBasedTestCase
72+
- Fixed bug in compare content methods identified by GNU classpath
73+
7174

7275
Enhancements from 1.2
7376
---------------------

src/test/org/apache/commons/io/testtools/FileBasedTestCase.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,38 +165,38 @@ private void assertEqualContent( File f0, File f1 )
165165
}
166166

167167
/** Assert that the content of a file is equal to that in a byte[]. */
168-
protected void assertEqualContent( byte[] b0, File file )
169-
throws IOException
170-
{
171-
InputStream is = new java.io.FileInputStream( file );
168+
protected void assertEqualContent(byte[] b0, File file) throws IOException {
169+
InputStream is = new java.io.FileInputStream(file);
170+
int count = 0, numRead = 0;
171+
byte[] b1 = new byte[b0.length];
172172
try {
173-
byte[] b1 = new byte[ b0.length ];
174-
int numRead = is.read( b1 );
175-
assertTrue( "Different number of bytes", numRead == b0.length && is.available() == 0 );
176-
for( int i = 0;
177-
i < numRead;
178-
assertTrue( "Byte " + i + " differs (" + b0[ i ] + " != " + b1[ i ] + ")",
179-
b0[ i ] == b1[ i ] ), i++
180-
);
173+
while (count < b0.length && numRead >= 0) {
174+
numRead = is.read(b1, count, b0.length);
175+
count += numRead;
176+
}
177+
assertEquals("Different number of bytes: ", b0.length, count);
178+
for (int i = 0; i < count; i++) {
179+
assertEquals("byte " + i + " differs", b0[i], b1[i]);
180+
}
181181
} finally {
182182
is.close();
183183
}
184184
}
185185

186186
/** Assert that the content of a file is equal to that in a char[]. */
187-
protected void assertEqualContent( char[] c0, File file )
188-
throws IOException
189-
{
190-
Reader ir = new java.io.FileReader( file );
187+
protected void assertEqualContent(char[] c0, File file) throws IOException {
188+
Reader ir = new java.io.FileReader(file);
189+
int count = 0, numRead = 0;
190+
char[] c1 = new char[c0.length];
191191
try {
192-
char[] c1 = new char[ c0.length ];
193-
int numRead = ir.read( c1 );
194-
assertTrue( "Different number of bytes", numRead == c0.length );
195-
for( int i = 0;
196-
i < numRead;
197-
assertTrue( "Byte " + i + " differs (" + c0[ i ] + " != " + c1[ i ] + ")",
198-
c0[ i ] == c1[ i ] ), i++
199-
);
192+
while (count < c0.length && numRead >= 0) {
193+
numRead = ir.read(c1, count, c0.length);
194+
count += numRead;
195+
}
196+
assertEquals("Different number of chars: ", c0.length, count);
197+
for (int i = 0; i < count; i++) {
198+
assertEquals("char " + i + " differs", c0[i], c1[i]);
199+
}
200200
} finally {
201201
ir.close();
202202
}

0 commit comments

Comments
 (0)