Improve input validation

This commit is contained in:
RatzzFatzz
2025-12-04 22:09:24 +01:00
parent 0f6bc271b1
commit 9ab417f71d
21 changed files with 239 additions and 177 deletions

View File

@@ -1,23 +1,18 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger; package at.pcgamingfreaks.mkvaudiosubtitlechanger;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.CachedMkvFileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.CachedMkvFileProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.AttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.AttributeUpdaterKernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.CoherentAttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.CoherentAttributeUpdaterKernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ProjectUtil; import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ProjectUtil;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.config.Configurator;
import picocli.CommandLine; import picocli.CommandLine;
import java.util.Set;
@Slf4j @Slf4j
@CommandLine.Command( @CommandLine.Command(
name = "mkvaudiosubtitlechanger", name = "mkvaudiosubtitlechanger",
@@ -34,36 +29,7 @@ public class Main implements Runnable {
@Getter @Getter
@CommandLine.ArgGroup(exclusive = false) @CommandLine.ArgGroup(exclusive = false)
private Config config; private InputConfig config;
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;
@Override
public void run() {
if (config.isDebug()) {
Configurator.setRootLevel(Level.DEBUG);
}
validate();
Config.setInstance(config);
AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null
? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor())
: new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor());
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.getPropertyPath()).append(" ").append(violation.getMessage()).append("\n");
}
throw new CommandLine.ParameterException(spec.commandLine(), errorMsg.toString());
}
}
public static void main(String[] args) { public static void main(String[] args) {
if (args.length == 0) { if (args.length == 0) {
@@ -71,4 +37,17 @@ public class Main implements Runnable {
} }
new CommandLine(Main.class).execute(args); new CommandLine(Main.class).execute(args);
} }
@Override
public void run() {
if (config.isDebug()) {
Configurator.setRootLevel(Level.DEBUG);
}
InputConfig.setInstance(config);
AttributeUpdaterKernel kernel = InputConfig.getInstance().getCoherent() != null
? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor())
: new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor());
kernel.execute();
}
} }

View File

@@ -1,7 +1,12 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config; package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validation.ValidFile;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validation.ValidMkvToolNix;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ValidationUtil;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
@@ -12,109 +17,115 @@ import org.apache.commons.lang3.SystemUtils;
import picocli.CommandLine; import picocli.CommandLine;
import java.io.File; import java.io.File;
import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import picocli.CommandLine.Option;
@Slf4j @Slf4j
@Getter @Getter
@Setter @Setter
@NoArgsConstructor @NoArgsConstructor
public class Config implements CommandLine.IVersionProvider { @CommandLine.Command
public class InputConfig {
@Getter(AccessLevel.NONE) @Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE) @Setter(AccessLevel.NONE)
private static Config config = null; private static InputConfig config = null;
private File configPath; private File configPath;
@CommandLine.Spec @CommandLine.Spec
CommandLine.Model.CommandSpec spec; CommandLine.Model.CommandSpec spec;
@CommandLine.Option(names = {"-a", "--attribute-config"}, required = true, arity = "1..*", converter = AttributeConfigConverter.class, @Option(names = {"-a", "--attribute-config"}, required = true, arity = "1..*", converter = AttributeConfigConverter.class,
description = "List of audio:subtitle pairs used to match in order and update files accordingly (e.g. jpn:eng jpn:ger)") description = "List of audio:subtitle pairs used to match in order and update files accordingly (e.g. jpn:eng jpn:ger)")
private List<AttributeConfig> attributeConfig; private List<AttributeConfig> attributeConfig;
@Setter(AccessLevel.NONE) @ValidFile(message = "does not exist")
@Option(names = {"-l", "--library"}, required = true, description = "path to library")
private File libraryPath; private File libraryPath;
@CommandLine.Option(names = {"-s", "--safemode"}, description = "test run (no files will be changes)") @Option(names = {"-s", "--safemode"}, description = "test run (no files will be changes)")
private boolean safeMode; private boolean safeMode;
@Setter(AccessLevel.NONE) @ValidMkvToolNix(message = "does not exist")
@Option(names = {"-m", "--mkvtoolnix"}, defaultValue = "${DEFAULT_MKV_TOOL_NIX}", description = "path to mkvtoolnix installation")
private File mkvToolNix; private File mkvToolNix;
@Min(value = 1) @Min(1)
@CommandLine.Option(names = {"-t", "--threads"}, defaultValue = "2", description = "thread count (default: ${DEFAULT-VALUE})") @Option(names = {"-t", "--threads"}, defaultValue = "2", description = "thread count (default: ${DEFAULT-VALUE})")
private int threads; private int threads;
@CommandLine.Option(names = {"-c", "--coherent"}, description = "try to match all files in dir of depth with the same attribute config") @Min(0)
@Option(names = {"-c", "--coherent"}, description = "try to match all files in dir of depth with the same attribute config")
private Integer coherent; private Integer coherent;
@CommandLine.Option(names = {"-cf", "--force-coherent"}, description = "changes are only applied if it's a coherent match")
@Option(names = {"-cf", "--force-coherent"}, description = "changes are only applied if it's a coherent match")
private boolean forceCoherent; private boolean forceCoherent;
@CommandLine.Option(names = {"-n", "--only-new-file"}, description = "sets filter-date to last successful execution (overwrites input of filter-date)") @Option(names = {"-n", "--only-new-file"}, description = "sets filter-date to last successful execution (overwrites input of filter-date)")
private boolean onlyNewFiles; private boolean onlyNewFiles;
@CommandLine.Option(names = {"-d", "--filter-date"}, defaultValue = CommandLine.Option.NULL_VALUE, description = "only consider files created newer than entered date (format: \"dd.MM.yyyy-HH:mm:ss\")")
@Option(names = {"-d", "--filter-date"}, defaultValue = Option.NULL_VALUE, description = "only consider files created newer than entered date (format: \"dd.MM.yyyy-HH:mm:ss\")")
private Date filterDate; private Date filterDate;
@CommandLine.Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern (default: \".*\")")
@Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern (default: \".*\")")
private Pattern includePattern; private Pattern includePattern;
@CommandLine.Option(names = {"-e", "--excluded-directory"}, arity = "1..*",
@Option(names = {"-e", "--excluded-directory"}, arity = "1..*",
description = "Directories to be excluded, combines with config file") description = "Directories to be excluded, combines with config file")
private Set<String> excludedDirectories = new HashSet<>(); private Set<String> excludedDirectories = new HashSet<>();
@CommandLine.Option(names = {"--forced-keywords"}, arity = "1..*", defaultValue = "forced, signs, songs", split = ", ", @Option(names = {"--forced-keywords"}, arity = "1..*", defaultValue = "forced, signs, songs", split = ", ",
description = "Keywords to identify forced tracks (Defaults will be overwritten; Default: ${DEFAULT-VALUE})") description = "Keywords to identify forced tracks (Defaults will be overwritten; Default: ${DEFAULT-VALUE})")
private Set<String> forcedKeywords; private Set<String> forcedKeywords;
@CommandLine.Option(names = {"--commentary-keywords"}, arity = "1..*", defaultValue = "commentary, director", split = ", ",
@Option(names = {"--commentary-keywords"}, arity = "1..*", defaultValue = "commentary, director", split = ", ",
description = "Keywords to identify commentary tracks (Defaults will be overwritten; Default: ${DEFAULT-VALUE})") description = "Keywords to identify commentary tracks (Defaults will be overwritten; Default: ${DEFAULT-VALUE})")
private Set<String> commentaryKeywords; private Set<String> commentaryKeywords;
@CommandLine.Option(names = {"--preferred-subtitles"}, arity = "1..*", defaultValue = "unstyled", split = ", ",
@Option(names = {"--preferred-subtitles"}, arity = "1..*", defaultValue = "unstyled", split = ", ",
description = "Keywords to prefer specific subtitle tracks (Defaults will be overwritten; Default: ${DEFAULT-VALUE})") description = "Keywords to prefer specific subtitle tracks (Defaults will be overwritten; Default: ${DEFAULT-VALUE})")
private Set<String> preferredSubtitles; private Set<String> preferredSubtitles;
@CommandLine.Option(names = {"--debug"}, description = "Enable debug logging") @Option(names = {"--debug"}, description = "Enable debug logging")
private boolean debug; private boolean debug;
@CommandLine.Option(names = {"-l", "--library"}, required = true, description = "path to library")
public void setLibraryPath(File libraryPath) {
if (!libraryPath.exists()) throw new CommandLine.ParameterException(spec.commandLine(), "Path does not exist: " + libraryPath.getAbsolutePath());
this.libraryPath = libraryPath;
}
static { static {
// Set default value into system properties to picocli can read the conditional value // Set default value into system properties to picocli can read the conditional value
System.setProperty("DEFAULT_MKV_TOOL_NIX", SystemUtils.IS_OS_WINDOWS ? "C:\\Program Files\\MKVToolNix" : "/usr/bin/"); System.setProperty("DEFAULT_MKV_TOOL_NIX", SystemUtils.IS_OS_WINDOWS ? "C:\\Program Files\\MKVToolNix" : "/usr/bin/");
} }
@CommandLine.Option(names = {"-m", "--mkvtoolnix"}, defaultValue = "${DEFAULT_MKV_TOOL_NIX}", description = "path to mkvtoolnix installation") public static InputConfig getInstance() {
public void setMkvToolNix(File mkvToolNix) {
this.mkvToolNix = mkvToolNix;
if (!mkvToolNix.exists()
|| !Path.of(getPathFor(MkvToolNix.MKV_MERGE, SystemUtils.IS_OS_WINDOWS)).toFile().exists()
|| !Path.of(getPathFor(MkvToolNix.MKV_PROP_EDIT, SystemUtils.IS_OS_WINDOWS)).toFile().exists()) {
throw new CommandLine.ParameterException(spec.commandLine(),
"Invalid path to mkvtoolnix installation: " + mkvToolNix.getAbsolutePath());
}
}
public static Config getInstance() {
return getInstance(false); return getInstance(false);
} }
public static Config getInstance(boolean reset) { public static InputConfig getInstance(boolean reset) {
if (config == null || reset) { if (config == null || reset) {
config = new Config(); config = new InputConfig();
} }
return config; return config;
} }
public static void setInstance(Config c) { public static void setInstance(InputConfig c) {
config = c; config = c;
validate();
}
private static void validate() {
Validator validator = ValidationUtil.getValidator();
Set<ConstraintViolation<InputConfig>> violations = validator.validate(config);
if (!violations.isEmpty()) {
StringBuilder errors = new StringBuilder();
for (ConstraintViolation<InputConfig> violation : violations) {
errors.append(violation.getPropertyPath()).append(" ").append(violation.getMessage()).append("\n");
}
throw new CommandLine.ParameterException(config.getSpec().commandLine(), errors.toString());
}
} }
/** /**
* Get path to specific mkvtoolnix application. * Get path to specific mkvtoolnix application.
*
* @return absolute path to desired application. * @return absolute path to desired application.
*/ */
public String getPathFor(MkvToolNix application) { public String getPathFor(MkvToolNix application) {
@@ -133,7 +144,7 @@ public class Config implements CommandLine.IVersionProvider {
@Override @Override
public String toString() { public String toString() {
return new StringJoiner(", ", Config.class.getSimpleName() + "[", "]") return new StringJoiner(", ", InputConfig.class.getSimpleName() + "[", "]")
.add("configPath=" + configPath) .add("configPath=" + configPath)
.add("libraryPath=" + libraryPath) .add("libraryPath=" + libraryPath)
.add("mkvToolNix=" + mkvToolNix) .add("mkvToolNix=" + mkvToolNix)
@@ -151,10 +162,5 @@ public class Config implements CommandLine.IVersionProvider {
.add("attributeConfig=" + attributeConfig) .add("attributeConfig=" + attributeConfig)
.toString(); .toString();
} }
@Override
public String[] getVersion() throws Exception {
return new String[0];
}
} }

View File

@@ -0,0 +1,16 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validation;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = ValidFileValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidFile {
String message() default "File does not exist";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@@ -0,0 +1,17 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validation;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.io.File;
public class ValidFileValidator implements ConstraintValidator<ValidFile, File> {
@Override
public void initialize(ValidFile constraintAnnotation) {
}
@Override
public boolean isValid(File file, ConstraintValidatorContext context) {
return file != null && file.exists();
}
}

View File

@@ -0,0 +1,16 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validation;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = ValidMkvToolNixValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidMkvToolNix {
String message() default "MkvToolNix does not exist";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@@ -0,0 +1,24 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validation;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.SystemUtils;
import java.io.File;
import java.nio.file.Path;
public class ValidMkvToolNixValidator implements ConstraintValidator<ValidMkvToolNix, File> {
@Override
public void initialize(ValidMkvToolNix constraintAnnotation) {
}
@Override
public boolean isValid(File file, ConstraintValidatorContext context) {
return file != null && file.exists()
&& Path.of(InputConfig.getInstance().getPathFor(MkvToolNix.MKV_MERGE, SystemUtils.IS_OS_WINDOWS)).toFile().exists()
&& Path.of(InputConfig.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT, SystemUtils.IS_OS_WINDOWS)).toFile().exists();
}
}

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils; import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -31,11 +31,11 @@ public class FileFilter {
} }
private static boolean hasMatchingPattern(File pathName) { private static boolean hasMatchingPattern(File pathName) {
return Config.getInstance().getIncludePattern().matcher(pathName.getName()).matches(); return InputConfig.getInstance().getIncludePattern().matcher(pathName.getName()).matches();
} }
private static boolean isNewer(File pathName) { private static boolean isNewer(File pathName) {
Config config = Config.getInstance(); InputConfig config = InputConfig.getInstance();
if (config.getFilterDate() == null) return true; if (config.getFilterDate() == null) return true;
try { try {
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class); BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
@@ -47,6 +47,6 @@ public class FileFilter {
} }
private static boolean isNewer(Date creationDate) { private static boolean isNewer(Date creationDate) {
return creationDate.toInstant().isAfter(Config.getInstance().getFilterDate().toInstant()); return creationDate.toInstant().isAfter(InputConfig.getInstance().getFilterDate().toInstant());
} }
} }

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException; import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.SetUtils; import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.SetUtils;
@@ -26,7 +26,7 @@ public class MkvFileProcessor implements FileProcessor {
private final ObjectMapper mapper = new ObjectMapper(); private final ObjectMapper mapper = new ObjectMapper();
private static final SubtitleTrackComparator subtitleTrackComparator = private static final SubtitleTrackComparator subtitleTrackComparator =
new SubtitleTrackComparator(Config.getInstance().getPreferredSubtitles().toArray(new String[0])); new SubtitleTrackComparator(InputConfig.getInstance().getPreferredSubtitles().toArray(new String[0]));
private static final String DISABLE_DEFAULT_TRACK = "--edit track:%s --set flag-default=0"; private static final String DISABLE_DEFAULT_TRACK = "--edit track:%s --set flag-default=0";
private static final String ENABLE_DEFAULT_TRACK = "--edit track:%s --set flag-default=1"; private static final String ENABLE_DEFAULT_TRACK = "--edit track:%s --set flag-default=1";
@@ -40,7 +40,7 @@ public class MkvFileProcessor implements FileProcessor {
List<FileAttribute> fileAttributes = new ArrayList<>(); List<FileAttribute> fileAttributes = new ArrayList<>();
try { try {
String[] command = new String[]{ String[] command = new String[]{
Config.getInstance().getPathFor(MkvToolNix.MKV_MERGE), InputConfig.getInstance().getPathFor(MkvToolNix.MKV_MERGE),
"--identify", "--identify",
"--identification-format", "--identification-format",
"json", "json",
@@ -132,7 +132,7 @@ public class MkvFileProcessor implements FileProcessor {
public List<FileAttribute> retrieveNonForcedTracks(List<FileAttribute> attributes) { public List<FileAttribute> retrieveNonForcedTracks(List<FileAttribute> attributes) {
return attributes.stream() return attributes.stream()
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.trackName(), .filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.trackName(),
Config.getInstance().getForcedKeywords().toArray(new CharSequence[0]))) InputConfig.getInstance().getForcedKeywords().toArray(new CharSequence[0])))
.filter(elem -> !elem.forcedTrack()) .filter(elem -> !elem.forcedTrack())
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@@ -141,7 +141,7 @@ public class MkvFileProcessor implements FileProcessor {
public List<FileAttribute> retrieveNonCommentaryTracks(List<FileAttribute> attributes) { public List<FileAttribute> retrieveNonCommentaryTracks(List<FileAttribute> attributes) {
return attributes.stream() return attributes.stream()
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.trackName(), .filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.trackName(),
Config.getInstance().getCommentaryKeywords().toArray(new CharSequence[0]))) InputConfig.getInstance().getCommentaryKeywords().toArray(new CharSequence[0])))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@@ -151,7 +151,7 @@ public class MkvFileProcessor implements FileProcessor {
@Override @Override
public void update(File file, FileInfo fileInfo) throws IOException, MkvToolNixException { public void update(File file, FileInfo fileInfo) throws IOException, MkvToolNixException {
List<String> command = new ArrayList<>(); List<String> command = new ArrayList<>();
command.add(Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT)); command.add(InputConfig.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT));
command.add(String.format(file.getAbsolutePath())); command.add(String.format(file.getAbsolutePath()));
if (fileInfo.isAudioDifferent()) { if (fileInfo.isAudioDifferent()) {

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException; import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
@@ -38,7 +38,7 @@ public abstract class AttributeUpdaterKernel {
protected final FileCollector collector; protected final FileCollector collector;
protected final FileProcessor processor; protected final FileProcessor processor;
protected final ResultStatistic statistic = ResultStatistic.getInstance(); protected final ResultStatistic statistic = ResultStatistic.getInstance();
private final ExecutorService executor = Executors.newFixedThreadPool(Config.getInstance().getThreads()); private final ExecutorService executor = Executors.newFixedThreadPool(InputConfig.getInstance().getThreads());
protected ProgressBarBuilder pbBuilder() { protected ProgressBarBuilder pbBuilder() {
return new ProgressBarBuilder() return new ProgressBarBuilder()
@@ -52,7 +52,7 @@ public abstract class AttributeUpdaterKernel {
statistic.startTimer(); statistic.startTimer();
try (ProgressBar progressBar = pbBuilder().build()) { try (ProgressBar progressBar = pbBuilder().build()) {
List<File> files = loadFiles(Config.getInstance().getLibraryPath().getAbsolutePath()); List<File> files = loadFiles(InputConfig.getInstance().getLibraryPath().getAbsolutePath());
progressBar.maxHint(files.size()); progressBar.maxHint(files.size());
files.forEach(file -> executor.submit(() -> { files.forEach(file -> executor.submit(() -> {
@@ -71,7 +71,7 @@ public abstract class AttributeUpdaterKernel {
} }
protected List<File> loadExcludedFiles() { protected List<File> loadExcludedFiles() {
List<File> excludedFiles = Config.getInstance().getExcludedDirectories().stream() List<File> excludedFiles = InputConfig.getInstance().getExcludedDirectories().stream()
.map(collector::loadFiles) .map(collector::loadFiles)
.flatMap(Collection::stream) .flatMap(Collection::stream)
.collect(Collectors.toList()); .collect(Collectors.toList());
@@ -111,7 +111,7 @@ public abstract class AttributeUpdaterKernel {
processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks); processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks);
processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks, processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks,
Config.getInstance().getAttributeConfig().toArray(new AttributeConfig[]{})); InputConfig.getInstance().getAttributeConfig().toArray(new AttributeConfig[]{}));
updateFile(fileInfo); updateFile(fileInfo);
} }
@@ -142,7 +142,7 @@ public abstract class AttributeUpdaterKernel {
} }
private void commitChange(FileInfo fileInfo) { private void commitChange(FileInfo fileInfo) {
if (Config.getInstance().isSafeMode()) { if (InputConfig.getInstance().isSafeMode()) {
return; return;
} }
@@ -157,7 +157,7 @@ public abstract class AttributeUpdaterKernel {
} }
protected void endProcess() { protected void endProcess() {
if (Config.getInstance().isSafeMode()) { if (InputConfig.getInstance().isSafeMode()) {
return; return;
} }
@@ -171,7 +171,7 @@ public abstract class AttributeUpdaterKernel {
if (!lastExecutionFile.exists()) lastExecutionFile.createNewFile(); if (!lastExecutionFile.exists()) lastExecutionFile.createNewFile();
YAML yaml = new YAML(lastExecutionFile); YAML yaml = new YAML(lastExecutionFile);
yaml.set(Config.getInstance().getNormalizedLibraryPath(), DateUtils.convert(new Date())); yaml.set(InputConfig.getInstance().getNormalizedLibraryPath(), DateUtils.convert(new Date()));
yaml.save(lastExecutionFile); yaml.save(lastExecutionFile);
} catch (IOException | YamlInvalidContentException e) { } catch (IOException | YamlInvalidContentException e) {
log.error("last-execution.yml could not be created or read.", e); log.error("last-execution.yml could not be created or read.", e);

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
@@ -32,7 +32,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
*/ */
@Override @Override
List<File> loadFiles(String path) { List<File> loadFiles(String path) {
return loadFiles(path, Config.getInstance().getCoherent()); return loadFiles(path, InputConfig.getInstance().getCoherent());
} }
List<File> loadFiles(String path, int depth) { List<File> loadFiles(String path, int depth) {
@@ -53,7 +53,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
/** /**
* Update files in directory, if possible, with the same {@link AttributeConfig}. * Update files in directory, if possible, with the same {@link AttributeConfig}.
* If {@link Config#isForceCoherent()} then there will be no changes to the file if they don't match the same config. * If {@link InputConfig#isForceCoherent()} then there will be no changes to the file if they don't match the same config.
* Otherwise, the default behaviour is executed. * Otherwise, the default behaviour is executed.
* This method is called by the executor and is run in parallel. * This method is called by the executor and is run in parallel.
* *
@@ -61,7 +61,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
*/ */
@Override @Override
void process(File file) { void process(File file) {
process(file, Config.getInstance().getCoherent()); process(file, InputConfig.getInstance().getCoherent());
} }
void process(File file, int depth) { void process(File file, int depth) {
@@ -71,7 +71,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
.map(FileInfo::new) .map(FileInfo::new)
.collect(Collectors.toList()); .collect(Collectors.toList());
for (AttributeConfig config : Config.getInstance().getAttributeConfig()) { for (AttributeConfig config : InputConfig.getInstance().getAttributeConfig()) {
for (FileInfo fileInfo : fileInfos) { for (FileInfo fileInfo : fileInfos) {
List<FileAttribute> attributes = processor.loadAttributes(fileInfo.getFile()); List<FileAttribute> attributes = processor.loadAttributes(fileInfo.getFile());
@@ -99,7 +99,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
log.info("No coherent match found for {}", file.getAbsoluteFile()); log.info("No coherent match found for {}", file.getAbsoluteFile());
for (FileInfo fileInfo : fileInfos) { for (FileInfo fileInfo : fileInfos) {
if (!Config.getInstance().isForceCoherent()) { if (!InputConfig.getInstance().isForceCoherent()) {
super.process(fileInfo.getFile()); super.process(fileInfo.getFile());
} else { } else {
statistic.excluded(); statistic.excluded();

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -29,7 +29,7 @@ public class DefaultAttributeUpdaterKernel extends AttributeUpdaterKernel {
@Override @Override
List<File> loadFiles(String path) { List<File> loadFiles(String path) {
List<File> excludedFiles = loadExcludedFiles(); List<File> excludedFiles = loadExcludedFiles();
return collector.loadFiles(Config.getInstance().getLibraryPath().getAbsolutePath()).stream() return collector.loadFiles(InputConfig.getInstance().getLibraryPath().getAbsolutePath()).stream()
.filter(file -> !excludedFiles.contains(file)) .filter(file -> !excludedFiles.contains(file))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@@ -0,0 +1,12 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import lombok.Getter;
public class ValidationUtil {
private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
@Getter
private static final Validator validator = factory.getValidator();
}

View File

@@ -16,20 +16,20 @@ class BooleanConfigParameterTest {
private static Stream<Arguments> provideTestCases() { private static Stream<Arguments> provideTestCases() {
return Stream.of( return Stream.of(
Arguments.of(args("-s"), true, (Function<Config, Boolean>) Config::isSafeMode), Arguments.of(args("-s"), true, (Function<InputConfig, Boolean>) InputConfig::isSafeMode),
Arguments.of(args("--safemode"), true, (Function<Config, Boolean>) Config::isSafeMode), Arguments.of(args("--safemode"), true, (Function<InputConfig, Boolean>) InputConfig::isSafeMode),
Arguments.of(args(), false, (Function<Config, Boolean>) Config::isSafeMode), Arguments.of(args(), false, (Function<InputConfig, Boolean>) InputConfig::isSafeMode),
Arguments.of(args("-cf"), true, (Function<Config, Boolean>) Config::isForceCoherent), Arguments.of(args("-cf"), true, (Function<InputConfig, Boolean>) InputConfig::isForceCoherent),
Arguments.of(args("--force-coherent"), true, (Function<Config, Boolean>) Config::isForceCoherent), Arguments.of(args("--force-coherent"), true, (Function<InputConfig, Boolean>) InputConfig::isForceCoherent),
Arguments.of(args(), false, (Function<Config, Boolean>) Config::isForceCoherent), Arguments.of(args(), false, (Function<InputConfig, Boolean>) InputConfig::isForceCoherent),
Arguments.of(args("-n"), true, (Function<Config, Boolean>) Config::isOnlyNewFiles), Arguments.of(args("-n"), true, (Function<InputConfig, Boolean>) InputConfig::isOnlyNewFiles),
Arguments.of(args(), false, (Function<Config, Boolean>) Config::isOnlyNewFiles) Arguments.of(args(), false, (Function<InputConfig, Boolean>) InputConfig::isOnlyNewFiles)
); );
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("provideTestCases") @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(); Main sut = new Main();
CommandLine.populateCommand(sut, cmdArgs); CommandLine.populateCommand(sut, cmdArgs);
assertEquals(expected, fieldUnderTest.apply(sut.getConfig())); assertEquals(expected, fieldUnderTest.apply(sut.getConfig()));

View File

@@ -22,24 +22,24 @@ class ConfigTest {
"--commentary-keywords", "testCommentary", "--commentary-keywords", "testCommentary",
"--preferred-subtitles", "testPreferred" "--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")), assertEquals(List.of(new AttributeConfig("ger", "ger"), new AttributeConfig("eng", "eng")),
Config.getInstance().getAttributeConfig()); InputConfig.getInstance().getAttributeConfig());
assertTrue(Config.getInstance().isSafeMode()); assertTrue(InputConfig.getInstance().isSafeMode());
assertTrue(Config.getInstance().isForceCoherent()); assertTrue(InputConfig.getInstance().isForceCoherent());
assertTrue(Config.getInstance().isOnlyNewFiles()); assertTrue(InputConfig.getInstance().isOnlyNewFiles());
assertNull(Config.getInstance().getFilterDate()); assertNull(InputConfig.getInstance().getFilterDate());
assertEquals(2, Config.getInstance().getCoherent()); assertEquals(2, InputConfig.getInstance().getCoherent());
assertEquals(4, Config.getInstance().getThreads()); assertEquals(4, InputConfig.getInstance().getThreads());
assertEquals(".*[abc].*", Config.getInstance().getIncludePattern().pattern()); assertEquals(".*[abc].*", InputConfig.getInstance().getIncludePattern().pattern());
assertTrue(Config.getInstance().getForcedKeywords().contains("testForced")); assertTrue(InputConfig.getInstance().getForcedKeywords().contains("testForced"));
assertTrue(Config.getInstance().getCommentaryKeywords().contains("testCommentary")); assertTrue(InputConfig.getInstance().getCommentaryKeywords().contains("testCommentary"));
assertTrue(Config.getInstance().getPreferredSubtitles().contains("testPreferred")); assertTrue(InputConfig.getInstance().getPreferredSubtitles().contains("testPreferred"));
assertNull(Config.getInstance().getConfigPath()); assertNull(InputConfig.getInstance().getConfigPath());
} }
} }

View File

@@ -19,15 +19,15 @@ class IntegerConfigParameterTest {
private static Stream<Arguments> provideTestCases() { private static Stream<Arguments> provideTestCases() {
return Stream.of( return Stream.of(
Arguments.of(args(), 2, (Function<Config, Integer>) Config::getThreads), Arguments.of(args(), 2, (Function<InputConfig, Integer>) InputConfig::getThreads),
Arguments.of(args("-t", "5"), 5, (Function<Config, Integer>) Config::getThreads), Arguments.of(args("-t", "5"), 5, (Function<InputConfig, Integer>) InputConfig::getThreads),
Arguments.of(args("--threads", "5"), 5, (Function<Config, Integer>) Config::getThreads) Arguments.of(args("--threads", "5"), 5, (Function<InputConfig, Integer>) InputConfig::getThreads)
); );
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("provideTestCases") @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(); Main sut = new Main();
CommandLine.populateCommand(sut, cmdArgs); CommandLine.populateCommand(sut, cmdArgs);
assertEquals(expected, fieldUnderTest.apply(sut.getConfig())); assertEquals(expected, fieldUnderTest.apply(sut.getConfig()));
@@ -45,6 +45,6 @@ class IntegerConfigParameterTest {
underTest = underTest.setErr(printWriter); underTest = underTest.setErr(printWriter);
underTest.execute(args("-t", "0")); underTest.execute(args("-t", "0"));
printWriter.flush(); 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"));
} }
} }

View File

@@ -19,26 +19,18 @@ import static org.junit.jupiter.api.Assertions.*;
class MkvToolNixPathConfigParameterTest { class MkvToolNixPathConfigParameterTest {
private static final String TEST_INVALID_DIR = "src/test/resources/test-dir"; 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_DIR = SystemUtils.IS_OS_WINDOWS ? "src/test/resources/mkvtoolnix_exe" : "src/test/resources/mkvtoolnix";
private static final String TEST_MKVTOOLNIX_EXE_DIR = "src/test/resources/mkvtoolnix_exe";
private static Stream<Arguments> provideTestCases() { 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( return Stream.of(
Arguments.of(args("-m", 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<Config, File>) Config::getMkvToolNix) Arguments.of(args("--mkvtoolnix", TEST_MKVTOOLNIX_DIR), TEST_MKVTOOLNIX_DIR, (Function<InputConfig, File>) InputConfig::getMkvToolNix)
); );
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("provideTestCases") @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(); Main sut = new Main();
CommandLine.populateCommand(sut, cmdArgs); CommandLine.populateCommand(sut, cmdArgs);
assertEquals(Path.of(expected).toFile().getAbsolutePath(), fieldUnderTest.apply(sut.getConfig()).getAbsolutePath()); assertEquals(Path.of(expected).toFile().getAbsolutePath(), fieldUnderTest.apply(sut.getConfig()).getAbsolutePath());
@@ -47,7 +39,7 @@ class MkvToolNixPathConfigParameterTest {
@Test @Test
void validate() { void validate() {
Main sut = new Main(); 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("-m")));
assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args(""))); assertThrows(CommandLine.ParameterException.class, () -> CommandLine.populateCommand(sut, args("")));
} }

View File

@@ -21,14 +21,14 @@ class PathConfigParameterTest {
private static Stream<Arguments> provideTestCases() { private static Stream<Arguments> provideTestCases() {
return Stream.of( 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_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<Config, File>) Config::getLibraryPath) Arguments.of(args("-l", TEST_FILE), Path.of(TEST_FILE).toFile(), true, (Function<InputConfig, File>) InputConfig::getLibraryPath)
); );
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("provideTestCases") @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(); Main sut = new Main();
CommandLine.populateCommand(sut, cmdArgs); CommandLine.populateCommand(sut, cmdArgs);
assertEquals(expected.getAbsolutePath(), fieldUnderTest.apply(sut.getConfig()).getAbsolutePath()); assertEquals(expected.getAbsolutePath(), fieldUnderTest.apply(sut.getConfig()).getAbsolutePath());
@@ -38,7 +38,7 @@ class PathConfigParameterTest {
@Test @Test
void validate() { void validate() {
Main sut = new Main(); 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.MissingParameterException.class, () -> CommandLine.populateCommand(sut, args("-l")));
assertThrows(CommandLine.UnmatchedArgumentException.class, () -> CommandLine.populateCommand(sut, args(""))); assertThrows(CommandLine.UnmatchedArgumentException.class, () -> CommandLine.populateCommand(sut, args("")));
} }

View File

@@ -19,15 +19,15 @@ class PatternConfigParameterTest {
private static Stream<Arguments> provideTestCases() { private static Stream<Arguments> provideTestCases() {
return Stream.of( return Stream.of(
Arguments.of(args("-i", "[abd]?.*"), Pattern.compile("[abd]?.*"), (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<Config, Pattern>) Config::getIncludePattern), Arguments.of(args("-i", ".*"), Pattern.compile(".*"), (Function<InputConfig, Pattern>) InputConfig::getIncludePattern),
Arguments.of(args(), Pattern.compile(".*"), (Function<Config, Pattern>) Config::getIncludePattern) Arguments.of(args(), Pattern.compile(".*"), (Function<InputConfig, Pattern>) InputConfig::getIncludePattern)
); );
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("provideTestCases") @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(); Main sut = new Main();
CommandLine.populateCommand(sut, cmdArgs); CommandLine.populateCommand(sut, cmdArgs);
assertEquals(expected.pattern(), fieldUnderTest.apply(sut.getConfig()).pattern()); assertEquals(expected.pattern(), fieldUnderTest.apply(sut.getConfig()).pattern());

View File

@@ -19,21 +19,21 @@ class SetConfigParameterTest {
private static Stream<Arguments> provideTestCases() { private static Stream<Arguments> provideTestCases() {
return Stream.of( return Stream.of(
Arguments.of(args("--commentary-keywords", "test"), 1, (Function<Config, Set<String>>) Config::getCommentaryKeywords), 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<Config, Set<String>>) Config::getCommentaryKeywords), Arguments.of(args("--commentary-keywords", "test", "test1", "test2", "test3", "test4"), 5, (Function<InputConfig, Set<String>>) InputConfig::getCommentaryKeywords),
Arguments.of(args(), 2, (Function<Config, Set<String>>) Config::getCommentaryKeywords), Arguments.of(args(), 2, (Function<InputConfig, Set<String>>) InputConfig::getCommentaryKeywords),
Arguments.of(args("--forced-keywords", "test"), 1, (Function<Config, Set<String>>) Config::getForcedKeywords), 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<Config, Set<String>>) Config::getForcedKeywords), Arguments.of(args("--forced-keywords", "test", "test1", "test2", "test3", "test4"), 5, (Function<InputConfig, Set<String>>) InputConfig::getForcedKeywords),
Arguments.of(args(), 3, (Function<Config, Set<String>>) Config::getForcedKeywords), Arguments.of(args(), 3, (Function<InputConfig, Set<String>>) InputConfig::getForcedKeywords),
Arguments.of(args("--preferred-subtitles", "test"), 1, (Function<Config, Set<String>>) Config::getPreferredSubtitles), 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<Config, Set<String>>) Config::getPreferredSubtitles), Arguments.of(args("--preferred-subtitles", "test", "test1", "test2", "test3", "test4"), 5, (Function<InputConfig, Set<String>>) InputConfig::getPreferredSubtitles),
Arguments.of(args(), 1, (Function<Config, Set<String>>) Config::getPreferredSubtitles) Arguments.of(args(), 1, (Function<InputConfig, Set<String>>) InputConfig::getPreferredSubtitles)
); );
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("provideTestCases") @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(); Main sut = new Main();
CommandLine.populateCommand(sut, cmdArgs); CommandLine.populateCommand(sut, cmdArgs);
assertEquals(expectedSize, fieldUnderTest.apply(sut.getConfig()).size()); assertEquals(expectedSize, fieldUnderTest.apply(sut.getConfig()).size());

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils; import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith; 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.getName()).thenReturn(List.of(path.split("/")).get(1));
when(file.toPath()).thenReturn(Path.of(TEST_FILE)); when(file.toPath()).thenReturn(Path.of(TEST_FILE));
Config.getInstance(true).setIncludePattern(Pattern.compile(pattern)); InputConfig.getInstance(true).setIncludePattern(Pattern.compile(pattern));
if (filterDate != null) Config.getInstance().setFilterDate(filterDate); if (filterDate != null) InputConfig.getInstance().setFilterDate(filterDate);
try (MockedStatic<DateUtils> mockedFiles = Mockito.mockStatic(DateUtils.class)) { try (MockedStatic<DateUtils> mockedFiles = Mockito.mockStatic(DateUtils.class)) {
mockedFiles mockedFiles

View File

@@ -1,6 +1,6 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; 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.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
@@ -30,7 +30,7 @@ class MkvFileProcessorTest {
@ParameterizedTest @ParameterizedTest
@MethodSource @MethodSource
void detectDesiredTracks(AttributeConfig expectedMatch, List<FileAttribute> tracks, AttributeConfig... configs) { void detectDesiredTracks(AttributeConfig expectedMatch, List<FileAttribute> tracks, AttributeConfig... configs) {
Config.getInstance().setPreferredSubtitles(Set.of()); InputConfig.getInstance().setPreferredSubtitles(Set.of());
FileInfo info = new FileInfo(null); FileInfo info = new FileInfo(null);
MkvFileProcessor processor = new MkvFileProcessor(); MkvFileProcessor processor = new MkvFileProcessor();
processor.detectDesiredTracks(info, tracks, tracks, configs); processor.detectDesiredTracks(info, tracks, tracks, configs);
@@ -52,8 +52,8 @@ class MkvFileProcessorTest {
@ParameterizedTest @ParameterizedTest
@MethodSource @MethodSource
void retrieveNonForcedTracks(List<FileAttribute> attributes, List<FileAttribute> expected) { void retrieveNonForcedTracks(List<FileAttribute> attributes, List<FileAttribute> expected) {
Config.getInstance().setPreferredSubtitles(Set.of()); InputConfig.getInstance().setPreferredSubtitles(Set.of());
Config.getInstance().setForcedKeywords(Set.of("forced")); InputConfig.getInstance().setForcedKeywords(Set.of("forced"));
MkvFileProcessor processor = new MkvFileProcessor(); MkvFileProcessor processor = new MkvFileProcessor();
List<FileAttribute> actual = processor.retrieveNonForcedTracks(attributes); List<FileAttribute> actual = processor.retrieveNonForcedTracks(attributes);
@@ -77,8 +77,8 @@ class MkvFileProcessorTest {
@ParameterizedTest @ParameterizedTest
@MethodSource @MethodSource
void retrieveNonCommentaryTracks(List<FileAttribute> attributes, List<FileAttribute> expected) { void retrieveNonCommentaryTracks(List<FileAttribute> attributes, List<FileAttribute> expected) {
Config.getInstance().setPreferredSubtitles(Set.of()); InputConfig.getInstance().setPreferredSubtitles(Set.of());
Config.getInstance().setCommentaryKeywords(Set.of("commentary")); InputConfig.getInstance().setCommentaryKeywords(Set.of("commentary"));
MkvFileProcessor processor = new MkvFileProcessor(); MkvFileProcessor processor = new MkvFileProcessor();
List<FileAttribute> actual = processor.retrieveNonCommentaryTracks(attributes); List<FileAttribute> actual = processor.retrieveNonCommentaryTracks(attributes);