From d81428bab7e9c65d48b23cc863300485f0cd6859 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Fri, 17 Jun 2022 22:26:31 +0530 Subject: [PATCH] JAVA-12359 Potential issue in Validate String as Filename in Java article --- .../StringFilenameValidationUtils.java | 2 +- .../StringFilenameValidationUnitTest.java | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/stringfilenamevalidaiton/StringFilenameValidationUtils.java b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/stringfilenamevalidaiton/StringFilenameValidationUtils.java index 1a86edd45a..c1af75eca1 100644 --- a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/stringfilenamevalidaiton/StringFilenameValidationUtils.java +++ b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/stringfilenamevalidaiton/StringFilenameValidationUtils.java @@ -7,7 +7,7 @@ import java.util.Arrays; public class StringFilenameValidationUtils { - public static final Character[] INVALID_WINDOWS_SPECIFIC_CHARS = {'"', '*', ':', '<', '>', '?', '\\', '|', 0x7F}; + public static final Character[] INVALID_WINDOWS_SPECIFIC_CHARS = {'"', '*', ':', '<', '>', '?', '\\', '|', '/'}; public static final Character[] INVALID_UNIX_SPECIFIC_CHARS = {'\000'}; public static final String REGEX_PATTERN = "^[A-za-z0-9.]{1,255}$"; 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 3e787f08be..04f6b83183 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 @@ -2,6 +2,7 @@ package com.baeldung.stringfilenamevalidaiton; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; @@ -79,17 +80,21 @@ public class StringFilenameValidationUnitTest { @ParameterizedTest @EnabledOnOs(OS.WINDOWS) @MethodSource("filenamesWithInvalidWindowsChars") - public void givenFilenameStringWithInvalidWindowsCharAndIsWindows_whenValidateUsingIO_thenRaiseException(String filename) { - assertThatThrownBy(() -> validateStringFilenameUsingIO(filename)) - .isInstanceOf(IOException.class) - .hasMessageContaining("Invalid file path"); + public void givenFilenameStringWithInvalidWindowsCharAndIsWindows_whenValidateUsingIO_thenRaiseException( + String filename) { + if (!filename.contains(":")) { + assertThatThrownBy(() -> validateStringFilenameUsingIO(filename)).isInstanceOf(IOException.class) + .extracting(Throwable::getMessage, InstanceOfAssertFactories.STRING) + .containsAnyOf("The system cannot find the path specifie", + "The filename, directory name, or volume label syntax is incorrect"); + if (!filename.contains("/") && !filename.contains("\\")) { + assertThatThrownBy(() -> validateStringFilenameUsingNIO2(filename)) + .isInstanceOf(InvalidPathException.class).hasMessageContaining("Illegal char"); + } + } - assertThatThrownBy(() -> validateStringFilenameUsingNIO2(filename)) - .isInstanceOf(InvalidPathException.class) - .hasMessage("character not allowed"); - - assertThat(validateStringFilenameUsingContains(filename)).isFalse(); - } + assertThat(validateStringFilenameUsingContains(filename)).isFalse(); + } @ParameterizedTest @EnabledOnOs({OS.LINUX, OS.MAC})