Add tests for reading attributes

This commit is contained in:
RatzzFatzz
2025-12-16 02:13:18 +01:00
parent 1ae5b1bef1
commit ca29c22f00
3 changed files with 103 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.List; import java.util.List;
public interface FileProcessor { public interface FileProcessor {
@@ -43,4 +44,8 @@ public interface FileProcessor {
* @throws MkvToolNixException when error occurs while sending query to mkvpropedit * @throws MkvToolNixException when error occurs while sending query to mkvpropedit
*/ */
void update(FileInfo fileInfo) throws IOException, MkvToolNixException; void update(FileInfo fileInfo) throws IOException, MkvToolNixException;
default InputStream run(String[] command) throws IOException {
return Runtime.getRuntime().exec(command).getInputStream();
}
} }

View File

@@ -1,7 +1,7 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileFilter;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException; import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileFilter;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -73,10 +73,10 @@ public class MkvFileProcessor implements FileProcessor {
/** /**
* Recursively explores directories to find items at the target depth. * Recursively explores directories to find items at the target depth.
* *
* @param currentDir The current directory being explored * @param currentDir The current directory being explored
* @param currentDepth The current depth level * @param currentDepth The current depth level
* @param targetDepth The target depth to collect files * @param targetDepth The target depth to collect files
* @param result The collection to store found files * @param result The collection to store found files
*/ */
private static void exploreDirectory(File currentDir, int currentDepth, int targetDepth, List<File> result) { private static void exploreDirectory(File currentDir, int currentDepth, int targetDepth, List<File> result) {
if (currentDepth == targetDepth) { if (currentDepth == targetDepth) {
@@ -113,8 +113,7 @@ public class MkvFileProcessor implements FileProcessor {
}; };
log.debug("Executing: {}", String.join(" ", command)); log.debug("Executing: {}", String.join(" ", command));
InputStream inputStream = Runtime.getRuntime().exec(command) InputStream inputStream = run(command);
.getInputStream();
Map<String, Object> jsonMap = mapper.readValue(inputStream, Map.class); Map<String, Object> jsonMap = mapper.readValue(inputStream, Map.class);
List<Map<String, Object>> tracks = (List<Map<String, Object>>) jsonMap.get("tracks"); List<Map<String, Object>> tracks = (List<Map<String, Object>>) jsonMap.get("tracks");
if (tracks != null) { if (tracks != null) {
@@ -160,7 +159,7 @@ public class MkvFileProcessor implements FileProcessor {
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)));
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 = run(command.toArray(new String[0]));
String output = IOUtils.toString(new InputStreamReader(inputstream)); String output = IOUtils.toString(new InputStreamReader(inputstream));
log.debug("Result: {}", output.replaceAll("\\n", " '")); log.debug("Result: {}", output.replaceAll("\\n", " '"));
if (output.contains("Error")) throw new MkvToolNixException(output); if (output.contains("Error")) throw new MkvToolNixException(output);

View File

@@ -0,0 +1,92 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.processors;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfo;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.TrackAttributes;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.TrackType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
@ExtendWith(MockitoExtension.class)
class MkvFileProcessorTest {
@Test
void readAttributes() throws IOException {
String mkvmergeResponse = """
{
"tracks": [
{
"id": 0,
"properties": {
"default_track": true,
"enabled_track": true,
"forced_track": false,
"language": "jpn",
"number": 1
},
"type": "video"
},
{
"id": 1,
"properties": {
"track_name": "testing",
"default_track": true,
"enabled_track": true,
"forced_track": false,
"language": "jpn",
"number": 2
},
"type": "audio"
},
{
"id": 2,
"properties": {
"default_track": true,
"enabled_track": true,
"forced_track": false,
"language": "eng",
"number": 3
},
"type": "subtitles"
}
]
}
""";
MkvFileProcessor underTest = spy(new MkvFileProcessor(new File("mkvtoolnix"), null));
doReturn(new ByteArrayInputStream(mkvmergeResponse.getBytes(StandardCharsets.UTF_8)))
.when(underTest).run(any(String[].class));
FileInfo result = underTest.readAttributes(new File("arst"));
TrackAttributes audio = result.getAudioTracks().get(0);
assertEquals(2, audio.id());
assertEquals("testing", audio.trackName());
assertEquals("jpn", audio.language());
assertTrue(audio.defaultt());
assertFalse(audio.forced());
assertFalse(audio.hearingImpaired());
assertFalse(audio.commentary());
assertEquals(TrackType.AUDIO, audio.type());
TrackAttributes sub = result.getSubtitleTracks().get(0);
assertEquals(3, sub.id());
assertNull(sub.trackName());
assertEquals("eng", sub.language());
assertTrue(sub.defaultt());
assertFalse(sub.forced());
assertFalse(sub.hearingImpaired());
assertFalse(sub.commentary());
assertEquals(TrackType.SUBTITLES, sub.type());
}
}