mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Add config validators & tests
This commit is contained in:
@@ -10,8 +10,7 @@ import lombok.extern.log4j.Log4j2;
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
ConfigLoader.initConfig(args);
|
||||
// Config.getInstance().initConfig(args);
|
||||
// AttributeUpdaterKernel kernel = new AttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor());
|
||||
// kernel.execute();
|
||||
AttributeUpdaterKernel kernel = new AttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor());
|
||||
kernel.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,64 +58,6 @@ public class Config {
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
private Pattern loadIncludePattern(CommandLine cmd, YAML config, ConfigErrors errors) {
|
||||
try {
|
||||
return Pattern.compile(cmd.hasOption(INCLUDE_PATTERN.prop())
|
||||
? cmd.getOptionValue(INCLUDE_PATTERN.prop())
|
||||
: config.getString(INCLUDE_PATTERN.prop(), ".*"));
|
||||
} catch (PatternSyntaxException e) {
|
||||
errors.add("invalid regex pattern");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private String loadMkvToolNixPath(CommandLine cmd, YAML config, ConfigErrors errors){
|
||||
if (cmd.hasOption(MKV_TOOL_NIX.prop())) return cmd.getOptionValue(MKV_TOOL_NIX.prop());
|
||||
if (config.isSet(MKV_TOOL_NIX.prop())) return config.getString(MKV_TOOL_NIX.prop());
|
||||
errors.add("path to mkv tool nix installation missing");
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean loadOperatingSystem() {
|
||||
return System.getProperty("os.name").toLowerCase().contains("windows");
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private void loadForcedKeywords(CommandLine cmd, YAML config) {
|
||||
if (cmd.hasOption(FORCED_KEYWORDS.prop())) forcedKeywords.addAll(List.of(cmd.getOptionValues(FORCED_KEYWORDS.prop())));
|
||||
if (config.isSet(FORCED_KEYWORDS.prop())) forcedKeywords.addAll(config.getStringList(FORCED_KEYWORDS.prop()));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private void loadExcludedDirectories(CommandLine cmd, YAML config) {
|
||||
if (cmd.hasOption(EXCLUDE_DIRECTORY.prop())) excludedDirectories.addAll(List.of(cmd.getOptionValues(EXCLUDE_DIRECTORY.prop())));
|
||||
if (config.isSet(EXCLUDE_DIRECTORY.prop())) excludedDirectories.addAll(config.getStringList(EXCLUDE_DIRECTORY.prop()));
|
||||
}
|
||||
|
||||
private List<AttributeConfig> loadAttributeConfig(YAML config, ConfigErrors errors) {
|
||||
Function<String, String> audio = key -> config.getString(key + ".audio", null);
|
||||
Function<String, String> subtitle = key -> config.getString(key + ".subtitle", null);
|
||||
|
||||
List<AttributeConfig> attributeConfigs = config.getKeysFiltered(".*audio.*").stream()
|
||||
.sorted()
|
||||
.map(key -> key.replace(".audio", ""))
|
||||
.map(key -> new AttributeConfig(audio.apply(key), subtitle.apply(key)))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (attributeConfigs.isEmpty()) {
|
||||
errors.add("no language configuration");
|
||||
} else {
|
||||
for (AttributeConfig attributeConfig : attributeConfigs) {
|
||||
isLanguageValid(attributeConfig.getAudioLanguage(), errors);
|
||||
isLanguageValid(attributeConfig.getSubtitleLanguage(), errors);
|
||||
}
|
||||
}
|
||||
|
||||
return attributeConfigs;
|
||||
}
|
||||
|
||||
public String getPathFor(MkvToolNix exe) {
|
||||
return mkvToolNix.getAbsolutePath().endsWith("/") ? mkvToolNix.getAbsolutePath() + exe + ".exe" :
|
||||
mkvToolNix.getAbsolutePath() + "/" + exe + ".exe";
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigErrors {
|
||||
private final List<String> errors = new ArrayList<>();
|
||||
|
||||
public void add(String errorMessage) {
|
||||
errors.add(errorMessage);
|
||||
}
|
||||
|
||||
public boolean hasErrors() {
|
||||
return !errors.isEmpty();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return StringUtils.capitalize(String.join(", ", errors));
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.apache.commons.cli.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -17,7 +18,7 @@ import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsU
|
||||
|
||||
public class ConfigLoader {
|
||||
private static final List<ConfigValidator<?>> VALIDATORS = List.of(
|
||||
new PathValidator(CONFIG_PATH, false, Path.of("./").toFile()),
|
||||
// new PathValidator(CONFIG_PATH, false, Path.of("./").toFile()), Singelton for yaml instance
|
||||
new PathValidator(LIBRARY, true, null),
|
||||
new ThreadValidator(THREADS, false, 2),
|
||||
new MkvToolNixPathValidator(MKV_TOOL_NIX, true, Path.of("C:\\Program Files\\MKVToolNix").toFile()),
|
||||
@@ -26,13 +27,13 @@ public class ConfigLoader {
|
||||
new PatternValidator(INCLUDE_PATTERN, false, Pattern.compile(".*")),
|
||||
new SetValidator(FORCED_KEYWORDS, false, true),
|
||||
new SetValidator(COMMENTARY_KEYWORDS, false, true),
|
||||
new SetValidator(EXCLUDE_DIRECTORY, false, true)
|
||||
new SetValidator(EXCLUDE_DIRECTORY, false, true),
|
||||
new AttributeConfigValidator()
|
||||
);
|
||||
|
||||
public static void initConfig(String[] args) throws InvalidConfigException {
|
||||
public static void initConfig(String[] args) {
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
ConfigErrors errors = new ConfigErrors();
|
||||
CommandLine cmd = null;
|
||||
Options options = initOptions();
|
||||
|
||||
@@ -49,17 +50,14 @@ public class ConfigLoader {
|
||||
exitIfHelp(cmd, options, formatter);
|
||||
exitIfVersion(cmd);
|
||||
|
||||
File configPath = loadConfigPath(cmd, errors);
|
||||
|
||||
try (YAML config = new YAML(configPath)) {
|
||||
List<ValidationResult> results = new ArrayList<>();
|
||||
try (YAML config = new YAML(loadConfigPath(cmd))) {
|
||||
for (ConfigValidator<?> validator : VALIDATORS) {
|
||||
if (validator.validate(config, cmd).equals(ValidationResult.INVALID)) {
|
||||
throw new InvalidConfigException(new ConfigErrors());
|
||||
}
|
||||
results.add(validator.validate(config, cmd));
|
||||
}
|
||||
} catch (IOException | YamlInvalidContentException ignored) {}
|
||||
|
||||
System.out.println(Config.getInstance());
|
||||
if (results.contains(ValidationResult.INVALID)) System.exit(1);
|
||||
}
|
||||
|
||||
private static Options initOptions() {
|
||||
@@ -93,11 +91,12 @@ public class ConfigLoader {
|
||||
}
|
||||
}
|
||||
|
||||
private static File loadConfigPath(CommandLine cmd, ConfigErrors errors) {
|
||||
private static File loadConfigPath(CommandLine cmd) {
|
||||
File configPath = new File(cmd.getOptionValue(CONFIG_PATH.prop(), "config.yaml"));
|
||||
if (configPath.isFile()) return configPath;
|
||||
|
||||
errors.add("invalid config path");
|
||||
System.out.println("invalid config path");
|
||||
System.exit(1);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
public class InvalidConfigException extends RuntimeException{
|
||||
public InvalidConfigException(ConfigErrors errors) {
|
||||
super("Errors in config: " + errors);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import lombok.Setter;
|
||||
|
||||
public enum ValidationResult {
|
||||
VALID,
|
||||
DEFAULT,
|
||||
NOT_PRESENT,
|
||||
MISSING,
|
||||
INVALID;
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
|
||||
import at.pcgamingfreaks.yaml.YAML;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.LanguageValidatorUtil.isLanguageValid;
|
||||
|
||||
public class AttributeConfigValidator extends ConfigValidator<List<AttributeConfig>> {
|
||||
|
||||
public AttributeConfigValidator() {
|
||||
super(ConfigProperty.ATTRIBUTE_CONFIG, true, null);
|
||||
}
|
||||
|
||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||
System.out.printf("%s: ", property.prop());
|
||||
List<AttributeConfig> result;
|
||||
|
||||
Function<String, String> audio = key -> yaml.getString(key + ".audio", null);
|
||||
Function<String, String> subtitle = key -> yaml.getString(key + ".subtitle", null);
|
||||
|
||||
if (yaml.getKeysFiltered(property.prop() + ".*").size() > 0) {
|
||||
result = yaml.getKeysFiltered(".*audio.*").stream()
|
||||
.sorted()
|
||||
.map(key -> key.replace(".audio", ""))
|
||||
.map(key -> new AttributeConfig(audio.apply(key), subtitle.apply(key)))
|
||||
.collect(Collectors.toList());
|
||||
} else if (required) {
|
||||
System.out.println("missing");
|
||||
return ValidationResult.MISSING;
|
||||
} else {
|
||||
System.out.println("ok");
|
||||
return ValidationResult.NOT_PRESENT;
|
||||
}
|
||||
|
||||
if (!isValid(result) || !setValue(result)) {
|
||||
System.out.println("invalid");
|
||||
return ValidationResult.INVALID;
|
||||
}
|
||||
|
||||
System.out.println("ok");
|
||||
return ValidationResult.VALID;
|
||||
}
|
||||
|
||||
@Override
|
||||
List<AttributeConfig> parse(String value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isValid(List<AttributeConfig> result) {
|
||||
if (result.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
boolean isValid = true;
|
||||
for (AttributeConfig attributeConfig : result) {
|
||||
isValid = isLanguageValid(attributeConfig.getAudioLanguage())
|
||||
&& isLanguageValid(attributeConfig.getSubtitleLanguage());
|
||||
if (!isValid) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,12 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
|
||||
import at.pcgamingfreaks.yaml.YAML;
|
||||
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.ARGUMENTS;
|
||||
|
||||
@@ -15,30 +18,23 @@ public class BooleanValidator extends ConfigValidator<Boolean> {
|
||||
super(property, required, null);
|
||||
}
|
||||
|
||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||
System.out.printf("Checking %s... ", property.prop());
|
||||
boolean result;
|
||||
protected BiFunction<YAML, ConfigProperty, Optional<Boolean>> provideDataYaml() {
|
||||
return (yaml, property) -> {
|
||||
if (yaml.isSet(ARGUMENTS.prop())
|
||||
&& yaml.getStringList(ARGUMENTS.prop(), List.of()).contains(property.prop())) {
|
||||
return Optional.of(true);
|
||||
}
|
||||
return Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
if (cmd.hasOption(property.prop())) {
|
||||
result = true;
|
||||
} else if (yaml.isSet(ARGUMENTS.prop())
|
||||
&& yaml.getStringList(ARGUMENTS.prop(), List.of()).contains(property.prop())) {
|
||||
result = true;
|
||||
} else if (required) {
|
||||
System.out.println("missing");
|
||||
return ValidationResult.MISSING;
|
||||
} else {
|
||||
System.out.println("ok");
|
||||
return ValidationResult.NOT_PRESENT;
|
||||
}
|
||||
|
||||
if (!isValid(result) || !setValue(result)) {
|
||||
System.out.println("invalid");
|
||||
return ValidationResult.INVALID;
|
||||
}
|
||||
|
||||
System.out.println("ok");
|
||||
return ValidationResult.VALID;
|
||||
protected BiFunction<CommandLine, ConfigProperty, Optional<Boolean>> provideDataCmd() {
|
||||
return (cmd, property) -> {
|
||||
if (cmd.hasOption(property.prop())) {
|
||||
return Optional.of(true);
|
||||
}
|
||||
return Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,6 +13,9 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -23,25 +26,35 @@ public abstract class ConfigValidator<FieldType> {
|
||||
protected final FieldType defaultValue;
|
||||
|
||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||
System.out.printf("Checking %s... ", property.prop());
|
||||
String resultString = null;
|
||||
System.out.printf("%s: ", property.prop());
|
||||
FieldType result;
|
||||
|
||||
if (cmd.hasOption(property.prop())) {
|
||||
resultString = cmd.getOptionValue(property.prop());
|
||||
} else if (yaml.isSet(property.prop())) {
|
||||
try {
|
||||
resultString = yaml.getString(property.prop());
|
||||
} catch (YamlKeyNotFoundException ignored) {}
|
||||
} else if (required) {
|
||||
System.out.println("missing");
|
||||
return ValidationResult.MISSING;
|
||||
Optional<FieldType> cmdResult = provideDataCmd().apply(cmd, property);
|
||||
Optional<FieldType> yamlResult = provideDataYaml().apply(yaml, property);
|
||||
|
||||
if (cmdResult.isPresent()) {
|
||||
result = cmdResult.get();
|
||||
} else if (yamlResult.isPresent()) {
|
||||
result = yamlResult.get();
|
||||
} else {
|
||||
System.out.println("ok");
|
||||
return ValidationResult.NOT_PRESENT;
|
||||
if (defaultValue != null) {
|
||||
if (setValue(defaultValue)) {
|
||||
System.out.println("default");
|
||||
return ValidationResult.DEFAULT;
|
||||
} else {
|
||||
System.out.println("invalid");
|
||||
return ValidationResult.INVALID;
|
||||
}
|
||||
}
|
||||
if (required) {
|
||||
System.out.println("missing");
|
||||
return ValidationResult.MISSING;
|
||||
} else {
|
||||
System.out.println("ok");
|
||||
return ValidationResult.NOT_PRESENT;
|
||||
}
|
||||
}
|
||||
|
||||
FieldType result = parse(resultString);
|
||||
|
||||
if (!isValid(result) || !setValue(result)) {
|
||||
System.out.println("invalid");
|
||||
return ValidationResult.INVALID;
|
||||
@@ -51,6 +64,28 @@ public abstract class ConfigValidator<FieldType> {
|
||||
return ValidationResult.VALID;
|
||||
}
|
||||
|
||||
protected BiFunction<YAML, ConfigProperty, Optional<FieldType>> provideDataYaml() {
|
||||
return (yaml, property) -> {
|
||||
if (yaml.isSet(property.prop())) {
|
||||
try {
|
||||
return Optional.of(parse(yaml.getString(property.prop())));
|
||||
} catch (YamlKeyNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
protected BiFunction<CommandLine, ConfigProperty, Optional<FieldType>> provideDataCmd() {
|
||||
return (cmd, property) -> {
|
||||
if (cmd.hasOption(property.prop())) {
|
||||
return Optional.of(parse(cmd.getOptionValue(property.prop())));
|
||||
}
|
||||
return Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
abstract FieldType parse(String value);
|
||||
|
||||
abstract boolean isValid(FieldType result);
|
||||
|
||||
@@ -14,7 +14,7 @@ public class OperatingSystemValidator extends BooleanValidator {
|
||||
|
||||
@Override
|
||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||
System.out.printf("Checking %s... ", property.prop());
|
||||
System.out.printf("%s: ", property.prop());
|
||||
Boolean result = StringUtils.containsIgnoreCase(System.getProperty("os.name"), "windows");
|
||||
|
||||
if (!isValid(result) || !setValue(result)) {
|
||||
|
||||
@@ -13,7 +13,7 @@ public class PathValidator extends ConfigValidator<File> {
|
||||
|
||||
@Override
|
||||
protected File parse(String value) {
|
||||
return value != null ? Path.of(value).toFile() : defaultValue;
|
||||
return Path.of(value).toFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,10 +9,8 @@ import org.apache.commons.cli.CommandLine;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SetValidator extends ConfigValidator<Set<String>> {
|
||||
@@ -24,7 +22,7 @@ public class SetValidator extends ConfigValidator<Set<String>> {
|
||||
}
|
||||
|
||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||
System.out.printf("Checking %s... ", property.prop());
|
||||
System.out.printf("%s: ", property.prop());
|
||||
List<String> resultList = null;
|
||||
|
||||
if (cmd.hasOption(property.prop())) {
|
||||
@@ -51,6 +49,27 @@ public class SetValidator extends ConfigValidator<Set<String>> {
|
||||
System.out.println("ok");
|
||||
return ValidationResult.VALID;
|
||||
}
|
||||
|
||||
protected BiFunction<YAML, ConfigProperty, Optional<Set<String>>> provideDataYaml() {
|
||||
return (yaml, property) -> {
|
||||
if (yaml.isSet(property.prop())) {
|
||||
try {
|
||||
return Optional.of(parse(yaml.getStringList(property.prop())));
|
||||
} catch (YamlKeyNotFoundException ignored) {
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
protected BiFunction<CommandLine, ConfigProperty, Optional<Set<String>>> provideDataCmd() {
|
||||
return (cmd, property) -> {
|
||||
if (cmd.hasOption(property.prop())) {
|
||||
return Optional.of(parse(List.of(cmd.getOptionValues(property.prop()))));
|
||||
}
|
||||
return Optional.empty();
|
||||
};
|
||||
}
|
||||
@Override
|
||||
Set<String> parse(String value) {
|
||||
throw new RuntimeException("This should not be called");
|
||||
|
||||
@@ -16,16 +16,17 @@ public enum ConfigProperty {
|
||||
EXCLUDE_DIRECTORY("exclude-directories", "Directories to be excluded, combines with config file"),
|
||||
HELP("help", "\"for help this is\" - Yoda"),
|
||||
VERSION("version", "Display version"),
|
||||
ARGUMENTS("arguments", "List of arguments");
|
||||
ARGUMENTS("arguments", "List of arguments"),
|
||||
ATTRIBUTE_CONFIG("attribute-config", "Attribute config to decide which tracks to choose when");
|
||||
|
||||
private final String property;
|
||||
private final String description;
|
||||
|
||||
public String desc() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String prop() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public String desc() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigErrors;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -28,10 +26,4 @@ public class LanguageValidatorUtil {
|
||||
public static boolean isLanguageValid(String language) {
|
||||
return ISO3_LANGUAGES.contains(language);
|
||||
}
|
||||
|
||||
public static void isLanguageValid(String language, ConfigErrors errors) {
|
||||
if (!isLanguageValid(language)) {
|
||||
errors.add(String.format("%s is not a valid language", language));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
|
||||
import at.pcgamingfreaks.yaml.YAML;
|
||||
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
|
||||
import org.apache.commons.cli.CommandLineParser;
|
||||
import org.apache.commons.cli.DefaultParser;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.apache.commons.cli.ParseException;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.MISSING;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.WINDOWS;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.yamlList;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MkvToolNixPathValidatorTest {
|
||||
private static CommandLineParser parser;
|
||||
private static Options options;
|
||||
|
||||
@BeforeAll
|
||||
static void before() {
|
||||
parser = new DefaultParser();
|
||||
options = new Options();
|
||||
options.addOption(optionOf(MKV_TOOL_NIX, "m", true));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(MKV_TOOL_NIX, false, null, "", new String[]{"-m", "\"C:\\Program Files\\MKVToolNix\""}, VALID),
|
||||
Arguments.of(MKV_TOOL_NIX, true, null, "", new String[]{"-m", "\"C:\\Program Files\\MKVToolNix\""}, VALID),
|
||||
Arguments.of(MKV_TOOL_NIX, false, null, "mkvtoolnix: C:\\Program Files\\MKVToolNix", new String[]{}, VALID),
|
||||
Arguments.of(MKV_TOOL_NIX, true, null, "mkvtoolnix: C:\\Program Files\\MKVToolNix", new String[]{}, VALID),
|
||||
Arguments.of(MKV_TOOL_NIX, false, Path.of("C:\\Program Files\\MKVToolNix").toFile(), "", new String[]{}, DEFAULT),
|
||||
Arguments.of(MKV_TOOL_NIX, false, null, "", new String[]{}, NOT_PRESENT),
|
||||
Arguments.of(MKV_TOOL_NIX, true, null, "", new String[]{}, MISSING),
|
||||
Arguments.of(MKV_TOOL_NIX, true, null, "", new String[]{"-m", "\"C:\\Program Files\\MKVTool\""}, INVALID)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(ConfigProperty property, boolean required, File defaultValue, String yamlArgs, String[] cmdArgs,
|
||||
ValidationResult expectedResult) throws ParseException, YamlInvalidContentException {
|
||||
MkvToolNixPathValidator underTest = new MkvToolNixPathValidator(property, required, defaultValue);
|
||||
|
||||
ValidationResult result = underTest.validate(new YAML(yamlArgs), parser.parse(options, cmdArgs));
|
||||
|
||||
assertEquals(expectedResult, result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user