Skip to content
Prev Previous commit
Next Next commit
Added assertions to verify that the size of content read via resource…
…ToXXX methods is correct
  • Loading branch information
Behrang Saeedzadeh committed Aug 22, 2016
commit c5af446ddc77b81801e451bd4e2c28cd685670ad
20 changes: 16 additions & 4 deletions src/test/java/org/apache/commons/io/IOUtilsTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1188,23 +1188,35 @@ private void testToString_URL(final String encoding) throws Exception {
}

@Test(expected = Test.None.class) public void testResourceToString_ExistingResourceAtRootPackage() throws Exception {
IOUtils.resourceToString("/test-file-utf8.bin", Charset.defaultCharset());
final long fileSize = new File(getClass().getResource("/test-file-utf8.bin").getFile()).length();
final String content = IOUtils.resourceToString("/test-file-utf8.bin", Charset.defaultCharset());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Charset.defaultCharset here and in other tests seems wrong as it is locale/environment dependent. StandardCharsets.UTF_8 may be more appropriate.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I will fix it now.

assertNotNull(content);
assertEquals(fileSize, content.getBytes().length);
}

@Test(expected = Test.None.class) public void testResourceToString_ExistingResourceAtSubPackage() throws Exception {
IOUtils.resourceToString("/org/apache/commons/io/FileUtilsTestDataCR.dat", Charset.defaultCharset());
final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length();
final String content = IOUtils.resourceToString("/org/apache/commons/io/FileUtilsTestDataCR.dat", Charset.defaultCharset());
assertNotNull(content);
assertEquals(fileSize, content.getBytes().length);
}

@Test(expected = IOException.class) public void testResourceToString_NonExistingResource() throws Exception {
IOUtils.resourceToString("/non-existing-file.bin", Charset.defaultCharset());
}

@Test(expected = Test.None.class) public void testResourceToByteArray_ExistingResourceAtRootPackage() throws Exception {
IOUtils.resourceToByteArray("/test-file-utf8.bin");
final long fileSize = new File(getClass().getResource("/test-file-utf8.bin").getFile()).length();
final byte[] bytes = IOUtils.resourceToByteArray("/test-file-utf8.bin");
assertNotNull(bytes);
assertEquals(fileSize, bytes.length);
}

@Test(expected = Test.None.class) public void testResourceToByteArray_ExistingResourceAtSubPackage() throws Exception {
IOUtils.resourceToByteArray("/org/apache/commons/io/FileUtilsTestDataCR.dat");
final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length();
final byte[] bytes = IOUtils.resourceToByteArray("/org/apache/commons/io/FileUtilsTestDataCR.dat");
assertNotNull(bytes);
assertEquals(fileSize, bytes.length);
}

@Test(expected = IOException.class) public void testResourceToByteArray_NonExistingResource() throws Exception {
Expand Down