garydgregory commented on code in PR #486:
URL: https://github.com/apache/commons-io/pull/486#discussion_r1340117974
##########
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:
Let's not do this kind of asserting, we need flexibility in adjusting
messages without breaking tests.
##########
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:
This the the opposite of what the Javadoc changes are all about. This PR
changes the documentation to say we throw IOException, so let's test that.
Let's NOT test what we do NOT document.
##########
src/test/java/org/apache/commons/io/file/AbstractTempDirTest.java:
##########
@@ -19,8 +19,11 @@
import java.io.File;
import java.io.IOException;
+import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
Review Comment:
Remove unused imports.
##########
src/test/java/org/apache/commons/io/FileUtilsTest.java:
##########
@@ -2463,18 +2485,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);
Review Comment:
Please revert and let the test clean itself up as soon as it can. There is
no sense in letting temp files pile up and up and up. The super class clean up
is a back-stop. IOW, you allocate, you release.
--
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]