garydgregory commented on code in PR #486: URL: https://github.com/apache/commons-io/pull/486#discussion_r1340177856
########## 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"))); Review Comment: > My thoughts were the following: > > * the Javadoc is specifying the API to the users of the library > > * the tests can be more detailed, so that the common-io-developers know which concrete exception is thrown. > > * if the implementation changes, so that another exception is thrown, the developer get noticed and can consider about the consequences (e.g. writing a simple hint to the changelog). > > > But of course, if you see the tests as the specification, then yes. If we give ourselves more flexibility by documenting the more general exception, I do indeed feel that we should afford ourselves the same in the tests. Using tests as specification is a bit strong but I do want the flexibility for now. -- 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: issues-unsubscr...@commons.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org