Skip to content

Commit 69531f0

Browse files
committed
Reuse IOUtils in assertSameContent()
1 parent 8de852e commit 69531f0

File tree

1 file changed

+4
-30
lines changed

1 file changed

+4
-30
lines changed

commons-vfs2/src/test/java/org/apache/commons/vfs2/AbstractProviderTestCase.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,15 @@ protected void assertSameContent(final String expected, final FileObject fileObj
7171
assertTrue(fileObject.exists());
7272
assertSame(FileType.FILE, fileObject.getType());
7373
assertTrue(fileObject.isFile());
74-
7574
// Get file content as a binary stream
7675
final byte[] expectedBytes = expected.getBytes(StandardCharsets.UTF_8);
77-
7876
// Check lengths
7977
final FileContent content = fileObject.getContent();
8078
assertEquals("same content length", expectedBytes.length, content.getSize());
81-
82-
// Read content into byte array
83-
final InputStream instr = content.getInputStream();
84-
final ByteArrayOutputStream outstr;
85-
try {
86-
outstr = new ByteArrayOutputStream(expectedBytes.length);
87-
final byte[] buffer = new byte[256];
88-
int nread = 0;
89-
while (nread >= 0) {
90-
outstr.write(buffer, 0, nread);
91-
nread = instr.read(buffer);
92-
}
93-
} finally {
94-
instr.close();
79+
// Compare input streams
80+
try (InputStream in = content.getInputStream()) {
81+
assertTrue(IOUtils.contentEquals(UnsynchronizedByteArrayInputStream.builder().setByteArray(expectedBytes).get(), in));
9582
}
96-
97-
// Compare
98-
assertArrayEquals(expectedBytes, outstr.toByteArray(), "same binary content");
9983
}
10084

10185
/**
@@ -108,20 +92,10 @@ protected void assertSameURLContent(final String expected, final URLConnection u
10892
final byte[] expectedBytes = expected.getBytes(StandardCharsets.UTF_8);
10993
// Check lengths
11094
assertEquals("same content length", expectedBytes.length, urlConnection.getContentLength());
111-
// Read content into byte array
112-
// final ByteArrayOutputStream outstr;
95+
// Compare input streams
11396
try (InputStream in = urlConnection.getInputStream()) {
114-
// outstr = new ByteArrayOutputStream();
115-
// final byte[] buffer = new byte[256];
116-
// int nread = 0;
117-
// while (nread >= 0) {
118-
// outstr.write(buffer, 0, nread);
119-
// nread = instr.read(buffer);
120-
// }
12197
assertTrue(IOUtils.contentEquals(UnsynchronizedByteArrayInputStream.builder().setByteArray(expectedBytes).get(), in));
12298
}
123-
// Compare
124-
// assertArrayEquals(expectedBin, outstr.toByteArray(), "same binary content");
12599
}
126100

127101
/**

0 commit comments

Comments
 (0)