Skip to content

Added resourceToString, resourceToByteArray, and resourceToURL methods to IOUtils#17

Closed
behrangsa wants to merge 10 commits into
apache:trunkfrom
behrangsa:trunk
Closed

Added resourceToString, resourceToByteArray, and resourceToURL methods to IOUtils#17
behrangsa wants to merge 10 commits into
apache:trunkfrom
behrangsa:trunk

Conversation

@behrangsa

@behrangsa behrangsa commented Aug 20, 2016

Copy link
Copy Markdown

Added resourceToString(String name, Charset encoding), resourceToByteArray(String name), and resourceToURL(String name) to read a classpath resource and convert it to String, byte array, and URL respectively.

Behrang Saeedzadeh added 2 commits August 21, 2016 00:06
Added resourceToString(String name, Charset encoding) to
read a classpath resource and convert it to String.
@behrangsa behrangsa changed the title Added IOUtils#resourceToString Added resourceToString, resourceToByteArray, and resourceToURL methods to IOUtils Aug 21, 2016
}

@Test(expected = Test.None.class) public void testResourceToURL_ExistingResourceAtSubPackage() throws Exception {
IOUtils.resourceToURL("/org/apache/commons/io/FileUtilsTestDataCR.dat");

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.

It is useful in positive test cases to verify things like the size or contents of the result to ensure that it isn't silently returning unuseful results. Ie, assertEquals("Wrong output size", expectedSize, result.size()) or similar as you can find above in this file.

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.

@ansell,

Done in c5af446.

@behrangsa

Copy link
Copy Markdown
Author

Hi all,

Any comments, ideas, or suggestions? The JIRA issue is also created: https://issues.apache.org/jira/browse/IO-513#

Cheers,
Behrang


@Test public void testResourceToString_ExistingResourceAtRootPackage() throws Exception {
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.

Replaced Charset.defaultCharset() with StandardCharsets.UTF_8 to make the tests non-fragile
@behrangsa

Copy link
Copy Markdown
Author

Hi all,

I think this PR is good to go ;)

Cheers,
Behrang

@behrangsa

Copy link
Copy Markdown
Author

This PR has been moved to: #28

@behrangsa behrangsa closed this Nov 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants