chkpnt commented on code in PR #486:
URL: https://github.com/apache/commons-io/pull/486#discussion_r1340194975
##########
src/test/java/org/apache/commons/io/FileUtilsTest.java:
##########
@@ -2463,18 +2487,56 @@ public void testReadFileToStringWithEncoding() throws
Exception {
assertEquals("Hello /u1234", data);
}
+ @Test
+ public void testReadFileToString_Errors() {
+ assertThrows(NullPointerException.class, () ->
FileUtils.readFileToString(null));
+ assertThrows(NoSuchFileException.class, () ->
FileUtils.readFileToString(new File("non-exsistent")));
+
+ Exception exception = assertThrows(IOException.class, () ->
FileUtils.readFileToString(tempDirFile));
+ assertEquals("Is a directory", exception.getMessage());
+
+ assertThrows(UnsupportedCharsetException.class, () ->
FileUtils.readFileToString(tempDirFile, "unsupported-charset"));
+ }
+
+ @Test
+ @EnabledIf("isPosixFilePermissionsSupported")
+ public void testReadFileToString_AccessDeniedExceptionOnPosixFileSystem()
throws Exception {
+ final File file = TestUtils.newFile(tempDirFile, "cant-read.txt");
+ TestUtils.createFile(file, 100);
+ Files.setPosixFilePermissions(file.toPath(),
PosixFilePermissions.fromString("---------"));
+
+ assertThrows(AccessDeniedException.class, () ->
FileUtils.readFileToString(file));
+ }
+
@Test
public void testReadLines() throws Exception {
final File file = TestUtils.newFile(tempDirFile, "lines.txt");
- try {
- final String[] data = {"hello", "/u1234", "", "this is", "some
text"};
- TestUtils.createLineBasedFile(file, data);
+ final String[] data = {"hello", "/u1234", "", "this is", "some text"};
+ TestUtils.createLineBasedFile(file, data);
- final List<String> lines = FileUtils.readLines(file, UTF_8);
- assertEquals(Arrays.asList(data), lines);
- } finally {
- TestUtils.deleteFile(file);
- }
+ final List<String> lines = FileUtils.readLines(file, UTF_8);
+ assertEquals(Arrays.asList(data), lines);
+ }
+
+ @Test
+ public void testReadLines_Errors() {
+ assertThrows(NullPointerException.class, () ->
FileUtils.readLines(null));
+ assertThrows(NoSuchFileException.class, () -> FileUtils.readLines(new
File("non-exsistent")));
+
+ Exception exception = assertThrows(IOException.class, () ->
FileUtils.readLines(tempDirFile));
+ assertEquals("Is a directory", exception.getMessage());
Review Comment:
The message isn't coming from common-io, that's the reason why I added this
assert. There is no more concrete exception class in this case, and the message
would be the feature to detect unwanted changes. But yes, it's going beyond the
Javadoc-specification, so I'm removing it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]