Add tests for attributeupdater

This commit is contained in:
RatzzFatzz
2025-12-16 00:22:10 +01:00
parent d3248e646b
commit 2ecea906b1
6 changed files with 111 additions and 11 deletions

View File

@@ -61,7 +61,7 @@ public class CoherentAttributeUpdater extends SingleFileAttributeUpdater {
if (config.isForceCoherent()) { if (config.isForceCoherent()) {
log.info("No coherent match found, aborting: {}", rootDir.getPath()); log.info("No coherent match found, aborting: {}", rootDir.getPath());
statistic.increaseNoSuitableConfigFoundBy(files.size()); // TODO: should matchedFiles count as already fit config? statistic.increaseNoSuitableConfigFoundBy(files.size());
return; return;
} }

View File

@@ -162,7 +162,7 @@ public class MkvFileProcessor implements FileProcessor {
log.debug("Executing '{}'", String.join(" ", command)); log.debug("Executing '{}'", String.join(" ", command));
InputStream inputstream = Runtime.getRuntime().exec(command.toArray(new String[0])).getInputStream(); InputStream inputstream = Runtime.getRuntime().exec(command.toArray(new String[0])).getInputStream();
String output = IOUtils.toString(new InputStreamReader(inputstream)); String output = IOUtils.toString(new InputStreamReader(inputstream));
log.debug("Result: {}", output); log.debug("Result: {}", output.replaceAll("\\n", " '"));
if (output.contains("Error")) throw new MkvToolNixException(output); if (output.contains("Error")) throw new MkvToolNixException(output);
} }

View File

@@ -13,6 +13,10 @@ public class AttributeConfig {
private final String audioLanguage; private final String audioLanguage;
private final String subtitleLanguage; private final String subtitleLanguage;
public static AttributeConfig of(String audioLanguage, String subtitleLanguage) {
return new AttributeConfig(audioLanguage, subtitleLanguage);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@@ -51,8 +51,9 @@ public class InputConfig implements CommandLine.IVersionProvider {
@Option(names = {"-cf", "--force-coherent"}, description = "only applies changes if a coherent match was found for the specifically entered depth") @Option(names = {"-cf", "--force-coherent"}, description = "only applies changes if a coherent match was found for the specifically entered depth")
private boolean forceCoherent; private boolean forceCoherent;
@Option(names = {"-n", "--only-new-file"}, description = "sets filter-date to last successful execution (overwrites input of filter-date)") // TODO: implement usage
private boolean onlyNewFiles; // TODO: implement usage // @Option(names = {"-n", "--only-new-file"}, description = "sets filter-date to last successful execution (overwrites input of filter-date)")
// private boolean onlyNewFiles;
@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\")") @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;
@Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern (default: \".*\")") @Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern (default: \".*\")")
@@ -92,20 +93,23 @@ public class InputConfig implements CommandLine.IVersionProvider {
public String toString() { public String toString() {
return new StringJoiner(", ", InputConfig.class.getSimpleName() + "[", "]") return new StringJoiner(", ", InputConfig.class.getSimpleName() + "[", "]")
.add("configPath=" + configPath) .add("configPath=" + configPath)
.add("spec=" + spec)
.add("attributeConfig=" + Arrays.toString(attributeConfig))
.add("libraryPath=" + libraryPath) .add("libraryPath=" + libraryPath)
.add("mkvToolNix=" + mkvToolNix) .add("mkvToolNix=" + mkvToolNix)
.add("threads=" + threads)
.add("includePattern=" + includePattern)
.add("safeMode=" + safeMode) .add("safeMode=" + safeMode)
.add("threads=" + threads)
.add("coherent=" + coherent) .add("coherent=" + coherent)
.add("forceCoherent=" + forceCoherent) .add("forceCoherent=" + forceCoherent)
.add("onlyNewFiles=" + onlyNewFiles)
.add("filterDate=" + filterDate) .add("filterDate=" + filterDate)
.add("includePattern=" + includePattern)
.add("excluded=" + excluded)
.add("overwriteForced=" + overwriteForced)
.add("forcedKeywords=" + forcedKeywords) .add("forcedKeywords=" + forcedKeywords)
.add("commentaryKeywords=" + commentaryKeywords) .add("commentaryKeywords=" + commentaryKeywords)
.add("excludedDirectories=" + excluded) .add("hearingImpaired=" + hearingImpaired)
.add("preferredSubtitles=" + preferredSubtitles) .add("preferredSubtitles=" + preferredSubtitles)
.add("attributeConfig=" + attributeConfig) .add("debug=" + debug)
.toString(); .toString();
} }

View File

@@ -71,8 +71,7 @@ class FileFilterTest {
.when(() -> DateUtils.convert(anyLong())) .when(() -> DateUtils.convert(anyLong()))
.thenReturn(new Date(currentTime)); .thenReturn(new Date(currentTime));
assertEquals(expectedHit, fileFilter.accept(file, new HashSet<>(args)), "File is accepted"); assertEquals(expectedHit, fileFilter.accept(file, new HashSet<>(extensions)), "File is accepted");
assertEquals(total, ResultStatistic.getInstance().getTotal(), "Total files");
assertEquals(total, ResultStatistic.getInstance().total(), "Total files"); assertEquals(total, ResultStatistic.getInstance().total(), "Total files");
assertEquals(excluded, ResultStatistic.getInstance().getExcluded(), "Excluded files"); assertEquals(excluded, ResultStatistic.getInstance().getExcluded(), "Excluded files");
} }

View File

@@ -0,0 +1,93 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.CommandRunner;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.TrackAttributes;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import picocli.CommandLine;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Stream;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.FileInfoTestUtil.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class CoherentAttributeUpdaterTest {
@Mock(lenient = true)
FileProcessor fileProcessor;
@Test
void process() {
}
private static Stream<Arguments> findMatch() {
return Stream.of(
Arguments.of(AttributeConfig.of("ger", "ger"),
List.of(), false, 0),
Arguments.of(AttributeConfig.of("ger", "ger"),
List.of(fileInfoMock("test.mkv", AUDIO_GER, SUB_GER)), true, 1),
Arguments.of(AttributeConfig.of("ger", "ger"),
List.of(fileInfoMock("test.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test2.mkv", AUDIO_GER, SUB_GER)), true, 2),
Arguments.of(AttributeConfig.of("ger", "ger"),
List.of(fileInfoMock("test.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test2.mkv", AUDIO_ENG, SUB_ENG)), false, 1),
Arguments.of(AttributeConfig.of("ger", "ger"),
List.of(fileInfoMock("test.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test2.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test3.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test4.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test5.mkv", AUDIO_ENG, SUB_ENG)), false, 4),
Arguments.of(AttributeConfig.of("ger", "ger"),
List.of(fileInfoMock("test.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test2.mkv", AUDIO_ENG, SUB_GER),
fileInfoMock("test3.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test4.mkv", AUDIO_GER, SUB_GER),
fileInfoMock("test5.mkv", AUDIO_GER, SUB_ENG)), false, 1)
);
}
@ParameterizedTest
@MethodSource("findMatch")
void findMatch(AttributeConfig attributeConfig, List<Pair<File, FileInfo>> fileInfoMock, boolean expectedMatch, int expectedMatchCount) throws InvocationTargetException, IllegalAccessException {
CommandRunner commandRunner = new CommandRunner();
new CommandLine(commandRunner).parseArgs("-l", "/arst", "-a", "ger:ger");
InputConfig config = commandRunner.getConfig();
CoherentAttributeUpdater updater = new CoherentAttributeUpdater(config, fileProcessor);
Set<FileInfo> matchedFiles = new HashSet<>(fileInfoMock.size() * 2);
List<File> files = new ArrayList<>();
for (Pair<File, FileInfo> pair : fileInfoMock) {
when(fileProcessor.readAttributes(pair.getKey())).thenReturn(pair.getRight());
files.add(pair.getKey());
}
Method underTest = Arrays.stream(updater.getClass().getDeclaredMethods()).filter(m -> "findMatch".equals(m.getName())).findFirst().get();
underTest.setAccessible(true);
AttributeConfig actualMatch = (AttributeConfig) underTest.invoke(updater, attributeConfig, matchedFiles, files);
assertEquals(expectedMatch ? attributeConfig : null, actualMatch, "Matched AttributeConfig");
assertEquals(expectedMatchCount, matchedFiles.size(), "Matched files count");
}
private static Pair<File, FileInfo> fileInfoMock(String path, TrackAttributes... tracks) {
File file = new File(path);
FileInfo fileInfo = new FileInfo(file);
fileInfo.addTracks(List.of(tracks));
return Pair.of(file, fileInfo);
}
}