mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 10:05:58 +01:00
Update config tests
This commit is contained in:
29
pom.xml
29
pom.xml
@@ -132,6 +132,35 @@
|
||||
<artifactId>picocli</artifactId>
|
||||
<version>4.7.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Hibernate Validator -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>8.0.1.Final</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Expression Language Implementation -->
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>jakarta.el</artifactId>
|
||||
<version>4.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Expression Language API -->
|
||||
<dependency>
|
||||
<groupId>jakarta.el</groupId>
|
||||
<artifactId>jakarta.el-api</artifactId>
|
||||
<version>4.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Jakarta Bean Validation -->
|
||||
<dependency>
|
||||
<groupId>jakarta.validation</groupId>
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- region logging -->
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
|
||||
@@ -7,20 +7,29 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.CoherentAttributeUp
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ProjectUtil;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
@CommandLine.Command(mixinStandardHelpOptions = true, versionProvider = ProjectUtil.class)
|
||||
public class Main implements Runnable {
|
||||
|
||||
@Getter
|
||||
@CommandLine.ArgGroup(exclusive = false)
|
||||
private Config config = Config.getInstance();
|
||||
private Config config;
|
||||
|
||||
@CommandLine.Spec
|
||||
CommandLine.Model.CommandSpec spec;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
validate();
|
||||
Config.setInstance(config);
|
||||
AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null
|
||||
? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor())
|
||||
@@ -28,6 +37,19 @@ public class Main implements Runnable {
|
||||
kernel.execute();
|
||||
}
|
||||
|
||||
private void validate() {
|
||||
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
Set<ConstraintViolation<Config>> violations = validator.validate(config);
|
||||
|
||||
if (!violations.isEmpty()) {
|
||||
StringBuilder errorMsg = new StringBuilder();
|
||||
for (ConstraintViolation<Config> violation : violations) {
|
||||
errorMsg.append("ERROR: ").append(violation.getMessage()).append("\n");
|
||||
}
|
||||
throw new CommandLine.ParameterException(spec.commandLine(), errorMsg.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new CommandLine(Main.class).execute(args);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,11 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
|
||||
import com.sun.jna.platform.win32.Netapi32Util;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -36,7 +41,8 @@ public class Config {
|
||||
@CommandLine.Option(names = {"-m", "--mkvtoolnix"}, defaultValue = "C:\\Program Files\\MKVToolNix", description = "path to mkvtoolnix installation")
|
||||
private File mkvToolNix;
|
||||
|
||||
@CommandLine.Option(names = {"-t", "--threads"}, defaultValue = "2", description = "thread count (default: 2)")
|
||||
@Min(value = 1)
|
||||
@CommandLine.Option(names = {"-t", "--threads"}, defaultValue = "2", description = "thread count (default: ${DEFAULT-VALUE})")
|
||||
private int threads;
|
||||
@CommandLine.Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern (default: \".*\")")
|
||||
private Pattern includePattern;
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.Main;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
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.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AttributeConfigTest {
|
||||
private static CommandLineParser parser;
|
||||
private static Options options;
|
||||
|
||||
@BeforeAll
|
||||
static void before() {
|
||||
parser = new DefaultParser();
|
||||
options = new Options();
|
||||
options.addOption(optionOf(ATTRIBUTE_CONFIG, ATTRIBUTE_CONFIG.abrv(), ATTRIBUTE_CONFIG.args()));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
Config.getInstance(true);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(args("-a", "jpn:ger"), attrConf("jpn", "ger")),
|
||||
Arguments.of(args("-a", "jpn:ger", "jpn:eng"), attrConf("jpn", "ger", "jpn", "eng")),
|
||||
Arguments.of(args("-a", "jpn:ger", "jpn:OFF"), attrConf("jpn", "ger", "jpn", "OFF"))
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, List<AttributeConfig> expectedConfig) {
|
||||
Main underTest = new Main();
|
||||
CommandLine.populateCommand(underTest, cmdArgs);
|
||||
assertIterableEquals(expectedConfig, underTest.getConfig().getAttributeConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate() {
|
||||
Main sut = new Main();
|
||||
assertThrows(CommandLine.MissingParameterException.class, () -> CommandLine.populateCommand(sut, new String[]{"-l", "/"}));
|
||||
assertThrows(CommandLine.MissingParameterException.class, () -> CommandLine.populateCommand(sut, new String[]{"-l", "/", "-a"}));
|
||||
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, new String[]{"-l", "/", "-a", "ger:"}));
|
||||
assertThrows(CommandLine.ParameterException.class,
|
||||
() -> CommandLine.populateCommand(sut, new String[]{"-l", "/", "-a", "ger:qwf"})); // Invalid language code
|
||||
}
|
||||
|
||||
private static String[] args(String... args) {
|
||||
String[] staticArray = new String[]{"-l", "/"};
|
||||
String[] result = new String[staticArray.length + args.length];
|
||||
System.arraycopy(staticArray, 0, result, 0, staticArray.length);
|
||||
System.arraycopy(args, 0, result, staticArray.length, args.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<AttributeConfig> attrConf(String... languages) {
|
||||
List<AttributeConfig> conf = new ArrayList<>();
|
||||
for (int i = 0; i < languages.length; i += 2) {
|
||||
conf.add(new AttributeConfig(languages[i], languages[i+1]));
|
||||
}
|
||||
return conf;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.Main;
|
||||
import org.apache.commons.cli.CommandLineParser;
|
||||
import org.apache.commons.cli.DefaultParser;
|
||||
import org.apache.commons.cli.Options;
|
||||
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 picocli.CommandLine;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.args;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
|
||||
|
||||
class BooleanConfigParameterTest {
|
||||
private static CommandLineParser parser;
|
||||
private static Options options;
|
||||
|
||||
@BeforeAll
|
||||
static void before() {
|
||||
parser = new DefaultParser();
|
||||
options = new Options();
|
||||
options.addOption(optionOf(SAFE_MODE, SAFE_MODE.abrv(), SAFE_MODE.args()));
|
||||
}
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, boolean expected, Function<Config, Boolean> fieldUnderTest) {
|
||||
Main sut = new Main();
|
||||
CommandLine.populateCommand(sut, cmdArgs);
|
||||
assertEquals(expected, fieldUnderTest.apply(sut.getConfig()));
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import java.util.List;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.PathUtils.TEST_FILE;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ConfigLoaderTest {
|
||||
class ConfigTest {
|
||||
|
||||
@Test
|
||||
void initConfig() {
|
||||
@@ -0,0 +1,59 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.Main;
|
||||
import org.apache.commons.cli.CommandLineParser;
|
||||
import org.apache.commons.cli.DefaultParser;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.THREADS;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.args;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.argumentsOf;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class IntegerConfigParameterTest {
|
||||
|
||||
private static CommandLineParser parser;
|
||||
private static Options options;
|
||||
|
||||
@BeforeAll
|
||||
static void before() {
|
||||
parser = new DefaultParser();
|
||||
options = new Options();
|
||||
options.addOption(optionOf(THREADS, "t", THREADS.args()));
|
||||
}
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String[] cmdArgs, int expected, Function<Config, Integer> fieldUnderTest) {
|
||||
Main sut = new Main();
|
||||
CommandLine.populateCommand(sut, cmdArgs);
|
||||
assertEquals(expected, fieldUnderTest.apply(sut.getConfig()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate() {
|
||||
Main sut = new Main();
|
||||
assertThrows(CommandLine.MissingParameterException.class, () -> CommandLine.populateCommand(sut, args("-t")));
|
||||
assertThrows(CommandLine.MissingParameterException.class, () -> CommandLine.populateCommand(sut, args("--threads")));
|
||||
assertThrows(CommandLine.ParameterException.class, () -> Main.main(args("--threads", "0")));
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
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.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AttributeConfigValidatorTest {
|
||||
private static CommandLineParser parser;
|
||||
private static Options options;
|
||||
|
||||
@BeforeAll
|
||||
static void before() {
|
||||
parser = new DefaultParser();
|
||||
options = new Options();
|
||||
options.addOption(optionOf(ATTRIBUTE_CONFIG, ATTRIBUTE_CONFIG.abrv(), ATTRIBUTE_CONFIG.args()));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
Config.getInstance(true);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(attrConfYaml("jpn", "ger"), new String[]{}, VALID, attrConf("jpn", "ger")),
|
||||
Arguments.of("", new String[]{"-a", "jpn:ger"}, VALID, attrConf("jpn", "ger")),
|
||||
Arguments.of(attrConfYaml("jpn", "ger", "jpn", "eng"), new String[]{}, VALID, attrConf("jpn", "ger", "jpn", "eng")),
|
||||
Arguments.of("", new String[]{"-a", "jpn:ger", "jpn:eng"}, VALID, attrConf("jpn", "ger", "jpn", "eng")),
|
||||
Arguments.of(attrConfYaml("jpn", "ger", "jpn", "OFF"), new String[]{}, VALID, attrConf("jpn", "ger", "jpn", "OFF")),
|
||||
Arguments.of("", new String[]{"-a", "jpn:ger", "jpn:OFF"}, VALID, attrConf("jpn", "ger", "jpn", "OFF")),
|
||||
Arguments.of(attrConfYaml("jpn", "invalid"), new String[]{}, INVALID, null),
|
||||
Arguments.of("", new String[]{}, MISSING, null)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(String yamlArgs, String[] cmdArgs, ValidationResult expectedResult, List<AttributeConfig> expectedConfig)
|
||||
throws ParseException, YamlInvalidContentException {
|
||||
AttributeConfigValidator underTest = new AttributeConfigValidator();
|
||||
|
||||
ValidationResult result = underTest.validate(new YAML(yamlArgs), parser.parse(options, cmdArgs));
|
||||
|
||||
assertEquals(expectedResult, result);
|
||||
assertIterableEquals(expectedConfig, Config.getInstance().getAttributeConfig());
|
||||
}
|
||||
|
||||
private static String attrConfYaml(String... languages) {
|
||||
StringBuilder yaml = new StringBuilder("attribute-config: ");
|
||||
int counter = 0;
|
||||
for (int i = 0; i < languages.length; i += 2) {
|
||||
counter++;
|
||||
yaml.append(String.format("\n %s:\n audio: %s\n subtitle: %s", counter, languages[i], languages[i+1]));
|
||||
}
|
||||
return yaml.toString();
|
||||
}
|
||||
|
||||
private static List<AttributeConfig> attrConf(String... languages) {
|
||||
List<AttributeConfig> conf = new ArrayList<>();
|
||||
for (int i = 0; i < languages.length; i += 2) {
|
||||
conf.add(new AttributeConfig(languages[i], languages[i+1]));
|
||||
}
|
||||
return conf;
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
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.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.yamlList;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class BooleanValidatorTest {
|
||||
private static CommandLineParser parser;
|
||||
private static Options options;
|
||||
|
||||
@BeforeAll
|
||||
static void before() {
|
||||
parser = new DefaultParser();
|
||||
options = new Options();
|
||||
options.addOption(optionOf(SAFE_MODE, SAFE_MODE.abrv(), SAFE_MODE.args()));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
Arguments.of(SAFE_MODE, false, "", new String[]{"-safe-mode"}, VALID),
|
||||
Arguments.of(SAFE_MODE, true, "", new String[]{"-safe-mode"}, VALID),
|
||||
Arguments.of(SAFE_MODE, false, "", new String[]{""}, NOT_PRESENT),
|
||||
Arguments.of(SAFE_MODE, true, "", new String[]{""}, MISSING),
|
||||
Arguments.of(SAFE_MODE, false, yamlList(ARGUMENTS, SAFE_MODE), new String[]{""}, VALID),
|
||||
Arguments.of(SAFE_MODE, true, yamlList(ARGUMENTS, SAFE_MODE), new String[]{""}, VALID),
|
||||
Arguments.of(SAFE_MODE, false, yamlList(ARGUMENTS, WINDOWS), new String[]{""}, NOT_PRESENT),
|
||||
Arguments.of(SAFE_MODE, true, yamlList(ARGUMENTS, WINDOWS), new String[]{""}, MISSING)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(ConfigProperty property, boolean required, String yamlArgs, String[] cmdArgs,
|
||||
ValidationResult expectedResult) throws ParseException, YamlInvalidContentException {
|
||||
BooleanValidator underTest = new BooleanValidator(property, required);
|
||||
|
||||
ValidationResult result = underTest.validate(new YAML(yamlArgs), parser.parse(options, cmdArgs));
|
||||
|
||||
assertEquals(expectedResult, result);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
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.util.stream.Stream;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.THREADS;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.argumentsOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class ThreadValidatorTest {
|
||||
|
||||
private static CommandLineParser parser;
|
||||
private static Options options;
|
||||
|
||||
@BeforeAll
|
||||
static void before() {
|
||||
parser = new DefaultParser();
|
||||
options = new Options();
|
||||
options.addOption(optionOf(THREADS, "t", THREADS.args()));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(
|
||||
argumentsOf(THREADS, false, null, "", new String[]{"-t", "10"}, VALID),
|
||||
argumentsOf(THREADS, true, null, "", new String[]{"-t", "10"}, VALID),
|
||||
argumentsOf(THREADS, false, null, "threads: 10", new String[]{}, VALID),
|
||||
argumentsOf(THREADS, true, null, "threads: 10", new String[]{}, VALID),
|
||||
argumentsOf(THREADS, false, 2, "", new String[]{}, DEFAULT),
|
||||
argumentsOf(THREADS, true, null, "", new String[]{}, MISSING),
|
||||
argumentsOf(THREADS, false, null, "", new String[]{}, NOT_PRESENT),
|
||||
argumentsOf(THREADS, true, null, "", new String[]{"-t", "-1"}, INVALID),
|
||||
argumentsOf(THREADS, true, null, "threads: 0", new String[]{}, INVALID),
|
||||
argumentsOf(THREADS, true, 2, "", new String[]{"-t", "0"}, INVALID)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void validate(ConfigProperty property, boolean required, Integer defaultValue, String yamlArgs, String[] cmdArgs,
|
||||
ValidationResult expectedResult) throws ParseException, YamlInvalidContentException {
|
||||
ThreadValidator underTest = new ThreadValidator(property, required, defaultValue);
|
||||
|
||||
ValidationResult result = underTest.validate(new YAML(yamlArgs), parser.parse(options, cmdArgs));
|
||||
|
||||
assertEquals(expectedResult, result);
|
||||
}
|
||||
}
|
||||
@@ -43,4 +43,12 @@ public class TestUtil {
|
||||
fileInfoDto.setMatchedConfig(config);
|
||||
return fileInfoDto;
|
||||
}
|
||||
|
||||
public static String[] args(String... args) {
|
||||
String[] staticArray = new String[]{"-l", "/", "-a", "jpn:ger"};
|
||||
String[] result = new String[staticArray.length + args.length];
|
||||
System.arraycopy(staticArray, 0, result, 0, staticArray.length);
|
||||
System.arraycopy(args, 0, result, staticArray.length, args.length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user