Committing Test Cases for Colon and Slash Characters
@@ -26,6 +26,9 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class StringFilenameValidationUnitTest {
private static final String CORRECT_FILENAME_PATTERN = "baeldung.txt";
+ private static final String FILENAME_WITH_COLON = "bael:dung.txt";
+ private static final String FILENAME_WITH_FORWARD_SLASH = "bael/dung.txt";
+ private static final String FILENAME_WITH_BACKWARD_SLASH = "bael\\dung.txt";
@ParameterizedTest
@MethodSource("correctAlphanumericFilenamesProvider")
@@ -92,6 +95,26 @@ public class StringFilenameValidationUnitTest {
assertThat(validateStringFilenameUsingContains(filename)).isFalse();
}
+ @Test
+ @EnabledOnOs(OS.WINDOWS)
+ public void givenFilenameStringWithInvalidColonWindowsCharAndIsWindows_thenNIO2FailsIOSucceed()
+ throws IOException {
+ assertThat(validateStringFilenameUsingIO(FILENAME_WITH_COLON)).isTrue();
+ assertThatThrownBy(() -> validateStringFilenameUsingNIO2(FILENAME_WITH_COLON))
+ .isInstanceOf(InvalidPathException.class).hasMessageContaining("Illegal char");
+ }
+
+ @Test
+ @EnabledOnOs(OS.WINDOWS)
+ public void givenFilenameStringWithInvalidSlashWindowsCharAndIsWindows_thenIOFailsNIO2Succeed() {
+ assertThatThrownBy(() -> validateStringFilenameUsingIO(FILENAME_WITH_FORWARD_SLASH))
+ .isInstanceOf(IOException.class).hasMessageContaining("The system cannot find the path specified");
+ assertThatThrownBy(() -> validateStringFilenameUsingIO(FILENAME_WITH_BACKWARD_SLASH))
+ .isInstanceOf(IOException.class).hasMessageContaining("The system cannot find the path specified");
+ assertThat(validateStringFilenameUsingNIO2(FILENAME_WITH_FORWARD_SLASH)).isTrue();
+ assertThat(validateStringFilenameUsingNIO2(FILENAME_WITH_BACKWARD_SLASH)).isTrue();
+ }
+
@ParameterizedTest
@EnabledOnOs({OS.LINUX, OS.MAC})
@MethodSource("filenamesWithInvalidUnixChars")
