From 08a2c27339c1554b180fab75a4df3cb20a8a5467 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Tue, 21 Jun 2022 22:25:12 +0530 Subject: [PATCH] JAVA-12359 Committing Test Cases for Colon and Slash Characters --- .../StringFilenameValidationUnitTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/stringfilenamevalidaiton/StringFilenameValidationUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/stringfilenamevalidaiton/StringFilenameValidationUnitTest.java index 3e79458a7c..62690a96ef 100644 --- a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/stringfilenamevalidaiton/StringFilenameValidationUnitTest.java +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/stringfilenamevalidaiton/StringFilenameValidationUnitTest.java @@ -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")