Skip to content

Commit

Permalink
test: refactor content extract test
Browse files Browse the repository at this point in the history
  • Loading branch information
gotson committed Dec 22, 2023
1 parent 5e125ee commit 71bd63c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
44 changes: 25 additions & 19 deletions src/test/java21/com/github/gotson/nightcompress/ArchiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,43 @@ void can_get_entries(TestData testData) throws LibArchiveException {
@MethodSource("getTestData")
void can_extract(TestData testData) throws Exception {
try (var archive = new Archive(testData.archivePath())) {
List<Path> entries = Files.list(testData.contentPath()).toList();
LOGGER.info("testData content path: {}", testData.contentPath());
entries.forEach(path -> LOGGER.info("disk entry: {}", path.getFileName()));

for (var entry : archive.getEntries()) {
LOGGER.info("archive entry: {}", entry.getName());
var testEntryPath = entries.stream().filter(path -> path.getFileName().toString().equals(entry.getName())).findAny().orElse(null);
assertThat(testEntryPath).isNotNull();

var testEntryBytes = Files.readAllBytes(testEntryPath);
List<ArchiveEntry> entries = archive.getEntries();
for (int i = 0; i < entries.size(); i++) {
var entry = entries.get(i);

try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (InputStream is = archive.getInputStream(entry)) {
is.transferTo(baos);
}
assertThat(baos.toByteArray()).isEqualTo(testEntryBytes);
var expectedContent = testData.entriesContent().get(i);
assertThat(baos.toString()).isEqualTo(expectedContent);
}
}
}
}

List<TestData> getTestData() throws URISyntaxException {
return List.of(
new TestData(getResourcePath("/rar5/rar5.rar"), getResourcePath("/rar5/rar5/"), asList(
new ArchiveEntry("FILE1.TXT", 7L, ZonedDateTime.of(2010, 11, 2, 23, 27, 28, 0, ZoneId.of("UTC")).toInstant()),
new ArchiveEntry("FILE2.TXT", 7L, ZonedDateTime.of(2010, 11, 2, 23, 27, 34, 0, ZoneId.of("UTC")).toInstant())
)),
new TestData(getResourcePath("/rar5/unicode.rar"), getResourcePath("/rar5/unicode/"), asList(
new ArchiveEntry("ウニコド.txt", 67L, ZonedDateTime.of(2020, 7, 28, 1, 49, 34, 0, ZoneId.of("UTC")).toInstant()),
new ArchiveEntry("新建文本文档.txt", 10L, ZonedDateTime.of(2020, 7, 28, 1, 50, 48, 0, ZoneId.of("UTC")).toInstant())
))
new TestData(getResourcePath("/rar5/rar5.rar"),
List.of(
new ArchiveEntry("FILE1.TXT", 7L, ZonedDateTime.of(2010, 11, 2, 23, 27, 28, 0, ZoneId.of("UTC")).toInstant()),
new ArchiveEntry("FILE2.TXT", 7L, ZonedDateTime.of(2010, 11, 2, 23, 27, 34, 0, ZoneId.of("UTC")).toInstant())
),
List.of(
"file1\r\n",
"file2\r\n"
)
),
new TestData(getResourcePath("/rar5/unicode.rar"),
List.of(
new ArchiveEntry("ウニコド.txt", 67L, ZonedDateTime.of(2020, 7, 28, 1, 49, 34, 0, ZoneId.of("UTC")).toInstant()),
new ArchiveEntry("新建文本文档.txt", 10L, ZonedDateTime.of(2020, 7, 28, 1, 50, 48, 0, ZoneId.of("UTC")).toInstant())
),
List.of(
"このファイルにはUnicodeテキストが含まれています",
"aaaaaaaaaa"
)
)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java21/com/github/gotson/nightcompress/TestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public record TestData(
Path archivePath,
Path contentPath,
List<ArchiveEntry> entries
List<ArchiveEntry> entries,
List<String> entriesContent
) {
}
1 change: 0 additions & 1 deletion src/test/resources/rar5/rar5/FILE1.TXT

This file was deleted.

1 change: 0 additions & 1 deletion src/test/resources/rar5/rar5/FILE2.TXT

This file was deleted.

1 change: 0 additions & 1 deletion src/test/resources/rar5/unicode/ウニコド.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/test/resources/rar5/unicode/新建文本文档.txt

This file was deleted.

0 comments on commit 71bd63c

Please sign in to comment.