Add test for command construction

This commit is contained in:
RatzzFatzz
2025-12-16 15:24:24 +01:00
parent ca29c22f00
commit b6b15faf7d
2 changed files with 51 additions and 6 deletions

View File

@@ -148,6 +148,15 @@ public class MkvFileProcessor implements FileProcessor {
*/ */
@Override @Override
public void update(FileInfo fileInfo) throws IOException, MkvToolNixException { public void update(FileInfo fileInfo) throws IOException, MkvToolNixException {
String[] command = getUpdateCommand(fileInfo);
log.debug("Executing '{}'", String.join(" ", command));
InputStream inputstream = run(command);
String output = IOUtils.toString(new InputStreamReader(inputstream));
log.debug("Result: {}", output.replaceAll("\\n", " '"));
if (output.contains("Error")) throw new MkvToolNixException(output);
}
private String[] getUpdateCommand(FileInfo fileInfo) {
List<String> command = new ArrayList<>(); List<String> command = new ArrayList<>();
command.add(getPathFor(mkvToolNixInstallation, MkvToolNix.MKV_PROP_EDIT).getAbsolutePath()); command.add(getPathFor(mkvToolNixInstallation, MkvToolNix.MKV_PROP_EDIT).getAbsolutePath());
command.add(String.format(fileInfo.getFile().getAbsolutePath())); command.add(String.format(fileInfo.getFile().getAbsolutePath()));
@@ -157,12 +166,7 @@ public class MkvFileProcessor implements FileProcessor {
changes.getForcedTrack().forEach((key, value) -> command.addAll(format(FORCED_TRACK, key.id(), value ? 1 : 0))); changes.getForcedTrack().forEach((key, value) -> command.addAll(format(FORCED_TRACK, key.id(), value ? 1 : 0)));
changes.getCommentaryTrack().forEach((key, value) -> command.addAll(format(COMMENTARY_TRACK, key.id(), value ? 1 : 0))); changes.getCommentaryTrack().forEach((key, value) -> command.addAll(format(COMMENTARY_TRACK, key.id(), value ? 1 : 0)));
changes.getHearingImpairedTrack().forEach((key, value) -> command.addAll(format(HEARING_IMPAIRED_TRACK, key.id(), value ? 1 : 0))); changes.getHearingImpairedTrack().forEach((key, value) -> command.addAll(format(HEARING_IMPAIRED_TRACK, key.id(), value ? 1 : 0)));
return command.toArray(new String[0]);
log.debug("Executing '{}'", String.join(" ", command));
InputStream inputstream = run(command.toArray(new String[0]));
String output = IOUtils.toString(new InputStreamReader(inputstream));
log.debug("Result: {}", output.replaceAll("\\n", " '"));
if (output.contains("Error")) throw new MkvToolNixException(output);
} }
private List<String> format(String format, Object... args) { private List<String> format(String format, Object... args) {

View File

@@ -10,7 +10,10 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@@ -89,4 +92,42 @@ class MkvFileProcessorTest {
assertFalse(sub.commentary()); assertFalse(sub.commentary());
assertEquals(TrackType.SUBTITLES, sub.type()); assertEquals(TrackType.SUBTITLES, sub.type());
} }
@Test
void getUpdateCommand() throws InvocationTargetException, IllegalAccessException {
FileInfo fileInfo = new FileInfo(new File("./"));
fileInfo.getChanges().getDefaultTrack().put(t(1), true);
fileInfo.getChanges().getDefaultTrack().put(t(2), false);
fileInfo.getChanges().getForcedTrack().put(t(3), true);
fileInfo.getChanges().getForcedTrack().put(t(4), false);
fileInfo.getChanges().getCommentaryTrack().put(t(5), true);
fileInfo.getChanges().getCommentaryTrack().put(t(6), false);
fileInfo.getChanges().getHearingImpairedTrack().put(t(7), true);
fileInfo.getChanges().getHearingImpairedTrack().put(t(8), false);
String[] expectedCommand = """
--edit track:1 --set flag-default=1
--edit track:2 --set flag-default=0
--edit track:3 --set flag-forced=1
--edit track:4 --set flag-forced=0
--edit track:5 --set flag-commentary=1
--edit track:6 --set flag-commentary=0
--edit track:7 --set flag-hearing-impaired=1
--edit track:8 --set flag-hearing-impaired=0
""".split("\\n");
MkvFileProcessor mkvFileProcessor = new MkvFileProcessor(new File("mkvtoolnix"), null);
Method underTest = Arrays.stream(mkvFileProcessor.getClass().getDeclaredMethods()).filter(m -> "getUpdateCommand".equals(m.getName())).findFirst().get();
underTest.setAccessible(true);
String[] actualCommand = (String[]) underTest.invoke(mkvFileProcessor, fileInfo);
String[] trimmedActualCommand = Arrays.copyOfRange(actualCommand, 2, actualCommand.length);
String actualCommandString = String.join(" ", trimmedActualCommand);
assertTrue(expectedCommand.length * 4 == trimmedActualCommand.length, "Command length is equal");
for (String commandPart: expectedCommand) {
assertTrue(actualCommandString.contains(commandPart));
}
}
private static TrackAttributes t(int id) {
return new TrackAttributes(id, "", "", false, false, false, false, TrackType.AUDIO);
}
} }