|
26 | 26 | public class StringFilenameValidationUnitTest { |
27 | 27 |
|
28 | 28 | private static final String CORRECT_FILENAME_PATTERN = "baeldung.txt"; |
| 29 | + private static final String FILENAME_WITH_COLON = "bael:dung.txt"; |
| 30 | + private static final String FILENAME_WITH_FORWARD_SLASH = "bael/dung.txt"; |
| 31 | + private static final String FILENAME_WITH_BACKWARD_SLASH = "bael\\dung.txt"; |
29 | 32 |
|
30 | 33 | @ParameterizedTest |
31 | 34 | @MethodSource("correctAlphanumericFilenamesProvider") |
@@ -92,6 +95,26 @@ public void givenFilenameStringWithInvalidWindowsCharAndIsWindows_whenValidateUs |
92 | 95 | assertThat(validateStringFilenameUsingContains(filename)).isFalse(); |
93 | 96 | } |
94 | 97 |
|
| 98 | + @Test |
| 99 | + @EnabledOnOs(OS.WINDOWS) |
| 100 | + public void givenFilenameStringWithInvalidColonWindowsCharAndIsWindows_thenNIO2FailsIOSucceed() |
| 101 | + throws IOException { |
| 102 | + assertThat(validateStringFilenameUsingIO(FILENAME_WITH_COLON)).isTrue(); |
| 103 | + assertThatThrownBy(() -> validateStringFilenameUsingNIO2(FILENAME_WITH_COLON)) |
| 104 | + .isInstanceOf(InvalidPathException.class).hasMessageContaining("Illegal char"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @EnabledOnOs(OS.WINDOWS) |
| 109 | + public void givenFilenameStringWithInvalidSlashWindowsCharAndIsWindows_thenIOFailsNIO2Succeed() { |
| 110 | + assertThatThrownBy(() -> validateStringFilenameUsingIO(FILENAME_WITH_FORWARD_SLASH)) |
| 111 | + .isInstanceOf(IOException.class).hasMessageContaining("The system cannot find the path specified"); |
| 112 | + assertThatThrownBy(() -> validateStringFilenameUsingIO(FILENAME_WITH_BACKWARD_SLASH)) |
| 113 | + .isInstanceOf(IOException.class).hasMessageContaining("The system cannot find the path specified"); |
| 114 | + assertThat(validateStringFilenameUsingNIO2(FILENAME_WITH_FORWARD_SLASH)).isTrue(); |
| 115 | + assertThat(validateStringFilenameUsingNIO2(FILENAME_WITH_BACKWARD_SLASH)).isTrue(); |
| 116 | + } |
| 117 | + |
95 | 118 | @ParameterizedTest |
96 | 119 | @EnabledOnOs({OS.LINUX, OS.MAC}) |
97 | 120 | @MethodSource("filenamesWithInvalidUnixChars") |
|
0 commit comments