JAVA-12359 Potential issue in Validate String as Filename in Java article
This commit is contained in:
parent
a5ddb7dd9c
commit
d81428bab7
|
@ -7,7 +7,7 @@ import java.util.Arrays;
|
||||||
|
|
||||||
public class StringFilenameValidationUtils {
|
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 Character[] INVALID_UNIX_SPECIFIC_CHARS = {'\000'};
|
||||||
|
|
||||||
public static final String REGEX_PATTERN = "^[A-za-z0-9.]{1,255}$";
|
public static final String REGEX_PATTERN = "^[A-za-z0-9.]{1,255}$";
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.baeldung.stringfilenamevalidaiton;
|
||||||
|
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.apache.commons.lang3.RandomUtils;
|
import org.apache.commons.lang3.RandomUtils;
|
||||||
|
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.condition.EnabledOnOs;
|
import org.junit.jupiter.api.condition.EnabledOnOs;
|
||||||
import org.junit.jupiter.api.condition.OS;
|
import org.junit.jupiter.api.condition.OS;
|
||||||
|
@ -79,17 +80,21 @@ public class StringFilenameValidationUnitTest {
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@EnabledOnOs(OS.WINDOWS)
|
@EnabledOnOs(OS.WINDOWS)
|
||||||
@MethodSource("filenamesWithInvalidWindowsChars")
|
@MethodSource("filenamesWithInvalidWindowsChars")
|
||||||
public void givenFilenameStringWithInvalidWindowsCharAndIsWindows_whenValidateUsingIO_thenRaiseException(String filename) {
|
public void givenFilenameStringWithInvalidWindowsCharAndIsWindows_whenValidateUsingIO_thenRaiseException(
|
||||||
assertThatThrownBy(() -> validateStringFilenameUsingIO(filename))
|
String filename) {
|
||||||
.isInstanceOf(IOException.class)
|
if (!filename.contains(":")) {
|
||||||
.hasMessageContaining("Invalid file path");
|
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))
|
assertThat(validateStringFilenameUsingContains(filename)).isFalse();
|
||||||
.isInstanceOf(InvalidPathException.class)
|
}
|
||||||
.hasMessage("character not allowed");
|
|
||||||
|
|
||||||
assertThat(validateStringFilenameUsingContains(filename)).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@EnabledOnOs({OS.LINUX, OS.MAC})
|
@EnabledOnOs({OS.LINUX, OS.MAC})
|
||||||
|
|
Loading…
Reference in New Issue