mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Improve input validation
This commit is contained in:
@@ -16,20 +16,20 @@ class BooleanConfigParameterTest {
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(args("-s"), true, (Function<Config, Boolean>) Config::isSafeMode),
|
||||
Arguments.of(args("--safemode"), true, (Function<Config, Boolean>) Config::isSafeMode),
|
||||
Arguments.of(args(), false, (Function<Config, Boolean>) Config::isSafeMode),
|
||||
Arguments.of(args("-cf"), true, (Function<Config, Boolean>) Config::isForceCoherent),
|
||||
Arguments.of(args("--force-coherent"), true, (Function<Config, Boolean>) Config::isForceCoherent),
|
||||
Arguments.of(args(), false, (Function<Config, Boolean>) Config::isForceCoherent),
|
||||
Arguments.of(args("-n"), true, (Function<Config, Boolean>) Config::isOnlyNewFiles),
|
||||
Arguments.of(args(), false, (Function<Config, Boolean>) Config::isOnlyNewFiles)
|
||||
Arguments.of(args("-s"), true, (Function<InputConfig, Boolean>) InputConfig::isSafeMode),
|
||||
Arguments.of(args("--safemode"), true, (Function<InputConfig, Boolean>) InputConfig::isSafeMode),
|
||||
Arguments.of(args(), false, (Function<InputConfig, Boolean>) InputConfig::isSafeMode),
|
||||
Arguments.of(args("-cf"), true, (Function<InputConfig, Boolean>) InputConfig::isForceCoherent),
|
||||
Arguments.of(args("--force-coherent"), true, (Function<InputConfig, Boolean>) InputConfig::isForceCoherent),
|
||||
Arguments.of(args(), false, (Function<InputConfig, Boolean>) InputConfig::isForceCoherent),
|
||||
Arguments.of(args("-n"), true, (Function<InputConfig, Boolean>) InputConfig::isOnlyNewFiles),
|
||||
Arguments.of(args(), false, (Function<InputConfig, Boolean>) InputConfig::isOnlyNewFiles)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, boolean expected, Function<Config, Boolean> fieldUnderTest) {
|
||||
void validate(String[] cmdArgs, boolean expected, Function<InputConfig, Boolean> fieldUnderTest) {
|
||||
Main sut = new Main();
|
||||
CommandLine.populateCommand(sut, cmdArgs);
|
||||
assertEquals(expected, fieldUnderTest.apply(sut.getConfig()));
|
||||
|
||||
@@ -22,24 +22,24 @@ class ConfigTest {
|
||||
"--commentary-keywords", "testCommentary",
|
||||
"--preferred-subtitles", "testPreferred"
|
||||
};
|
||||
CommandLine.populateCommand(Config.getInstance(true), sut);
|
||||
CommandLine.populateCommand(InputConfig.getInstance(true), sut);
|
||||
|
||||
assertTrue(Config.getInstance().getLibraryPath().exists());
|
||||
assertTrue(InputConfig.getInstance().getLibraryPath().exists());
|
||||
assertEquals(List.of(new AttributeConfig("ger", "ger"), new AttributeConfig("eng", "eng")),
|
||||
Config.getInstance().getAttributeConfig());
|
||||
InputConfig.getInstance().getAttributeConfig());
|
||||
|
||||
assertTrue(Config.getInstance().isSafeMode());
|
||||
assertTrue(Config.getInstance().isForceCoherent());
|
||||
assertTrue(Config.getInstance().isOnlyNewFiles());
|
||||
assertNull(Config.getInstance().getFilterDate());
|
||||
assertTrue(InputConfig.getInstance().isSafeMode());
|
||||
assertTrue(InputConfig.getInstance().isForceCoherent());
|
||||
assertTrue(InputConfig.getInstance().isOnlyNewFiles());
|
||||
assertNull(InputConfig.getInstance().getFilterDate());
|
||||
|
||||
assertEquals(2, Config.getInstance().getCoherent());
|
||||
assertEquals(4, Config.getInstance().getThreads());
|
||||
assertEquals(".*[abc].*", Config.getInstance().getIncludePattern().pattern());
|
||||
assertTrue(Config.getInstance().getForcedKeywords().contains("testForced"));
|
||||
assertTrue(Config.getInstance().getCommentaryKeywords().contains("testCommentary"));
|
||||
assertTrue(Config.getInstance().getPreferredSubtitles().contains("testPreferred"));
|
||||
assertEquals(2, InputConfig.getInstance().getCoherent());
|
||||
assertEquals(4, InputConfig.getInstance().getThreads());
|
||||
assertEquals(".*[abc].*", InputConfig.getInstance().getIncludePattern().pattern());
|
||||
assertTrue(InputConfig.getInstance().getForcedKeywords().contains("testForced"));
|
||||
assertTrue(InputConfig.getInstance().getCommentaryKeywords().contains("testCommentary"));
|
||||
assertTrue(InputConfig.getInstance().getPreferredSubtitles().contains("testPreferred"));
|
||||
|
||||
assertNull(Config.getInstance().getConfigPath());
|
||||
assertNull(InputConfig.getInstance().getConfigPath());
|
||||
}
|
||||
}
|
||||
@@ -19,15 +19,15 @@ class IntegerConfigParameterTest {
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(args(), 2, (Function<Config, Integer>) Config::getThreads),
|
||||
Arguments.of(args("-t", "5"), 5, (Function<Config, Integer>) Config::getThreads),
|
||||
Arguments.of(args("--threads", "5"), 5, (Function<Config, Integer>) Config::getThreads)
|
||||
Arguments.of(args(), 2, (Function<InputConfig, Integer>) InputConfig::getThreads),
|
||||
Arguments.of(args("-t", "5"), 5, (Function<InputConfig, Integer>) InputConfig::getThreads),
|
||||
Arguments.of(args("--threads", "5"), 5, (Function<InputConfig, Integer>) InputConfig::getThreads)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, int expected, Function<Config, Integer> fieldUnderTest) {
|
||||
void validate(String[] cmdArgs, int expected, Function<InputConfig, Integer> fieldUnderTest) {
|
||||
Main sut = new Main();
|
||||
CommandLine.populateCommand(sut, cmdArgs);
|
||||
assertEquals(expected, fieldUnderTest.apply(sut.getConfig()));
|
||||
@@ -45,6 +45,6 @@ class IntegerConfigParameterTest {
|
||||
underTest = underTest.setErr(printWriter);
|
||||
underTest.execute(args("-t", "0"));
|
||||
printWriter.flush();
|
||||
assertTrue(writer.toString().contains("ERROR: threads must be greater than or equal to 1"));
|
||||
assertTrue(writer.toString().contains("threads must be greater than or equal to 1"));
|
||||
}
|
||||
}
|
||||
@@ -19,26 +19,18 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
class MkvToolNixPathConfigParameterTest {
|
||||
|
||||
private static final String TEST_INVALID_DIR = "src/test/resources/test-dir";
|
||||
private static final String TEST_MKVTOOLNIX_DIR = "src/test/resources/mkvtoolnix";
|
||||
private static final String TEST_MKVTOOLNIX_EXE_DIR = "src/test/resources/mkvtoolnix_exe";
|
||||
private static final String TEST_MKVTOOLNIX_DIR = SystemUtils.IS_OS_WINDOWS ? "src/test/resources/mkvtoolnix_exe" : "src/test/resources/mkvtoolnix";
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
if (SystemUtils.IS_OS_WINDOWS) {
|
||||
return Stream.of(
|
||||
Arguments.of(args("-m", TEST_MKVTOOLNIX_EXE_DIR), TEST_MKVTOOLNIX_EXE_DIR, (Function<Config, File>) Config::getMkvToolNix),
|
||||
Arguments.of(args("--mkvtoolnix", TEST_MKVTOOLNIX_EXE_DIR), TEST_MKVTOOLNIX_EXE_DIR, (Function<Config, File>) Config::getMkvToolNix)
|
||||
);
|
||||
}
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(args("-m", TEST_MKVTOOLNIX_DIR), TEST_MKVTOOLNIX_DIR, (Function<Config, File>) Config::getMkvToolNix),
|
||||
Arguments.of(args("--mkvtoolnix", TEST_MKVTOOLNIX_DIR), TEST_MKVTOOLNIX_DIR, (Function<Config, File>) Config::getMkvToolNix)
|
||||
Arguments.of(args("-m", TEST_MKVTOOLNIX_DIR), TEST_MKVTOOLNIX_DIR, (Function<InputConfig, File>) InputConfig::getMkvToolNix),
|
||||
Arguments.of(args("--mkvtoolnix", TEST_MKVTOOLNIX_DIR), TEST_MKVTOOLNIX_DIR, (Function<InputConfig, File>) InputConfig::getMkvToolNix)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, String expected, Function<Config, File> fieldUnderTest) {
|
||||
void validate(String[] cmdArgs, String expected, Function<InputConfig, File> fieldUnderTest) {
|
||||
Main sut = new Main();
|
||||
CommandLine.populateCommand(sut, cmdArgs);
|
||||
assertEquals(Path.of(expected).toFile().getAbsolutePath(), fieldUnderTest.apply(sut.getConfig()).getAbsolutePath());
|
||||
@@ -47,7 +39,7 @@ class MkvToolNixPathConfigParameterTest {
|
||||
@Test
|
||||
void validate() {
|
||||
Main sut = new Main();
|
||||
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("-m", TEST_INVALID_DIR)));
|
||||
// assertThrows(CommandLine.ParameterException.class, () -> new CommandLine(sut).execute(args("-m", TEST_INVALID_DIR)));
|
||||
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("-m")));
|
||||
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("")));
|
||||
}
|
||||
|
||||
@@ -21,14 +21,14 @@ class PathConfigParameterTest {
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(args("-l", TEST_DIR), Path.of(TEST_DIR).toFile(), true, (Function<Config, File>) Config::getLibraryPath),
|
||||
Arguments.of(args("-l", TEST_FILE), Path.of(TEST_FILE).toFile(), true, (Function<Config, File>) Config::getLibraryPath)
|
||||
Arguments.of(args("-l", TEST_DIR), Path.of(TEST_DIR).toFile(), true, (Function<InputConfig, File>) InputConfig::getLibraryPath),
|
||||
Arguments.of(args("-l", TEST_FILE), Path.of(TEST_FILE).toFile(), true, (Function<InputConfig, File>) InputConfig::getLibraryPath)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, File expected, boolean exists, Function<Config, File> fieldUnderTest) {
|
||||
void validate(String[] cmdArgs, File expected, boolean exists, Function<InputConfig, File> fieldUnderTest) {
|
||||
Main sut = new Main();
|
||||
CommandLine.populateCommand(sut, cmdArgs);
|
||||
assertEquals(expected.getAbsolutePath(), fieldUnderTest.apply(sut.getConfig()).getAbsolutePath());
|
||||
@@ -38,7 +38,7 @@ class PathConfigParameterTest {
|
||||
@Test
|
||||
void validate() {
|
||||
Main sut = new Main();
|
||||
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("-l", "arst")));
|
||||
// assertThrows(CommandLine.ParameterException.class, () -> new CommandLine(sut).execute(args("-l", "arst")));
|
||||
assertThrows(CommandLine.MissingParameterException.class, () -> CommandLine.populateCommand(sut, args("-l")));
|
||||
assertThrows(CommandLine.UnmatchedArgumentException.class, () -> CommandLine.populateCommand(sut, args("")));
|
||||
}
|
||||
|
||||
@@ -19,15 +19,15 @@ class PatternConfigParameterTest {
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(args("-i", "[abd]?.*"), Pattern.compile("[abd]?.*"), (Function<Config, Pattern>) Config::getIncludePattern),
|
||||
Arguments.of(args("-i", ".*"), Pattern.compile(".*"), (Function<Config, Pattern>) Config::getIncludePattern),
|
||||
Arguments.of(args(), Pattern.compile(".*"), (Function<Config, Pattern>) Config::getIncludePattern)
|
||||
Arguments.of(args("-i", "[abd]?.*"), Pattern.compile("[abd]?.*"), (Function<InputConfig, Pattern>) InputConfig::getIncludePattern),
|
||||
Arguments.of(args("-i", ".*"), Pattern.compile(".*"), (Function<InputConfig, Pattern>) InputConfig::getIncludePattern),
|
||||
Arguments.of(args(), Pattern.compile(".*"), (Function<InputConfig, Pattern>) InputConfig::getIncludePattern)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, Pattern expected, Function<Config, Pattern> fieldUnderTest) {
|
||||
void validate(String[] cmdArgs, Pattern expected, Function<InputConfig, Pattern> fieldUnderTest) {
|
||||
Main sut = new Main();
|
||||
CommandLine.populateCommand(sut, cmdArgs);
|
||||
assertEquals(expected.pattern(), fieldUnderTest.apply(sut.getConfig()).pattern());
|
||||
|
||||
@@ -19,21 +19,21 @@ class SetConfigParameterTest {
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(args("--commentary-keywords", "test"), 1, (Function<Config, Set<String>>) Config::getCommentaryKeywords),
|
||||
Arguments.of(args("--commentary-keywords", "test", "test1", "test2", "test3", "test4"), 5, (Function<Config, Set<String>>) Config::getCommentaryKeywords),
|
||||
Arguments.of(args(), 2, (Function<Config, Set<String>>) Config::getCommentaryKeywords),
|
||||
Arguments.of(args("--forced-keywords", "test"), 1, (Function<Config, Set<String>>) Config::getForcedKeywords),
|
||||
Arguments.of(args("--forced-keywords", "test", "test1", "test2", "test3", "test4"), 5, (Function<Config, Set<String>>) Config::getForcedKeywords),
|
||||
Arguments.of(args(), 3, (Function<Config, Set<String>>) Config::getForcedKeywords),
|
||||
Arguments.of(args("--preferred-subtitles", "test"), 1, (Function<Config, Set<String>>) Config::getPreferredSubtitles),
|
||||
Arguments.of(args("--preferred-subtitles", "test", "test1", "test2", "test3", "test4"), 5, (Function<Config, Set<String>>) Config::getPreferredSubtitles),
|
||||
Arguments.of(args(), 1, (Function<Config, Set<String>>) Config::getPreferredSubtitles)
|
||||
Arguments.of(args("--commentary-keywords", "test"), 1, (Function<InputConfig, Set<String>>) InputConfig::getCommentaryKeywords),
|
||||
Arguments.of(args("--commentary-keywords", "test", "test1", "test2", "test3", "test4"), 5, (Function<InputConfig, Set<String>>) InputConfig::getCommentaryKeywords),
|
||||
Arguments.of(args(), 2, (Function<InputConfig, Set<String>>) InputConfig::getCommentaryKeywords),
|
||||
Arguments.of(args("--forced-keywords", "test"), 1, (Function<InputConfig, Set<String>>) InputConfig::getForcedKeywords),
|
||||
Arguments.of(args("--forced-keywords", "test", "test1", "test2", "test3", "test4"), 5, (Function<InputConfig, Set<String>>) InputConfig::getForcedKeywords),
|
||||
Arguments.of(args(), 3, (Function<InputConfig, Set<String>>) InputConfig::getForcedKeywords),
|
||||
Arguments.of(args("--preferred-subtitles", "test"), 1, (Function<InputConfig, Set<String>>) InputConfig::getPreferredSubtitles),
|
||||
Arguments.of(args("--preferred-subtitles", "test", "test1", "test2", "test3", "test4"), 5, (Function<InputConfig, Set<String>>) InputConfig::getPreferredSubtitles),
|
||||
Arguments.of(args(), 1, (Function<InputConfig, Set<String>>) InputConfig::getPreferredSubtitles)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, int expectedSize, Function<Config, Set<String>> fieldUnderTest) {
|
||||
void validate(String[] cmdArgs, int expectedSize, Function<InputConfig, Set<String>> fieldUnderTest) {
|
||||
Main sut = new Main();
|
||||
CommandLine.populateCommand(sut, cmdArgs);
|
||||
assertEquals(expectedSize, fieldUnderTest.apply(sut.getConfig()).size());
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -57,8 +57,8 @@ class FileFilterTest {
|
||||
when(file.getName()).thenReturn(List.of(path.split("/")).get(1));
|
||||
when(file.toPath()).thenReturn(Path.of(TEST_FILE));
|
||||
|
||||
Config.getInstance(true).setIncludePattern(Pattern.compile(pattern));
|
||||
if (filterDate != null) Config.getInstance().setFilterDate(filterDate);
|
||||
InputConfig.getInstance(true).setIncludePattern(Pattern.compile(pattern));
|
||||
if (filterDate != null) InputConfig.getInstance().setFilterDate(filterDate);
|
||||
|
||||
try (MockedStatic<DateUtils> mockedFiles = Mockito.mockStatic(DateUtils.class)) {
|
||||
mockedFiles
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
|
||||
@@ -30,7 +30,7 @@ class MkvFileProcessorTest {
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void detectDesiredTracks(AttributeConfig expectedMatch, List<FileAttribute> tracks, AttributeConfig... configs) {
|
||||
Config.getInstance().setPreferredSubtitles(Set.of());
|
||||
InputConfig.getInstance().setPreferredSubtitles(Set.of());
|
||||
FileInfo info = new FileInfo(null);
|
||||
MkvFileProcessor processor = new MkvFileProcessor();
|
||||
processor.detectDesiredTracks(info, tracks, tracks, configs);
|
||||
@@ -52,8 +52,8 @@ class MkvFileProcessorTest {
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void retrieveNonForcedTracks(List<FileAttribute> attributes, List<FileAttribute> expected) {
|
||||
Config.getInstance().setPreferredSubtitles(Set.of());
|
||||
Config.getInstance().setForcedKeywords(Set.of("forced"));
|
||||
InputConfig.getInstance().setPreferredSubtitles(Set.of());
|
||||
InputConfig.getInstance().setForcedKeywords(Set.of("forced"));
|
||||
MkvFileProcessor processor = new MkvFileProcessor();
|
||||
List<FileAttribute> actual = processor.retrieveNonForcedTracks(attributes);
|
||||
|
||||
@@ -77,8 +77,8 @@ class MkvFileProcessorTest {
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void retrieveNonCommentaryTracks(List<FileAttribute> attributes, List<FileAttribute> expected) {
|
||||
Config.getInstance().setPreferredSubtitles(Set.of());
|
||||
Config.getInstance().setCommentaryKeywords(Set.of("commentary"));
|
||||
InputConfig.getInstance().setPreferredSubtitles(Set.of());
|
||||
InputConfig.getInstance().setCommentaryKeywords(Set.of("commentary"));
|
||||
MkvFileProcessor processor = new MkvFileProcessor();
|
||||
List<FileAttribute> actual = processor.retrieveNonCommentaryTracks(attributes);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user