From 71bd63c5b04b67f61134c65ddd1ad230946f1754 Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Fri, 22 Dec 2023 12:22:28 +0800 Subject: [PATCH] test: refactor content extract test --- .../gotson/nightcompress/ArchiveTest.java | 44 +++++++++++-------- .../github/gotson/nightcompress/TestData.java | 4 +- src/test/resources/rar5/rar5/FILE1.TXT | 1 - src/test/resources/rar5/rar5/FILE2.TXT | 1 - ...6\343\203\213\343\202\263\343\203\211.txt" | 1 - ...7\346\234\254\346\226\207\346\241\243.txt" | 1 - 6 files changed, 27 insertions(+), 25 deletions(-) delete mode 100644 src/test/resources/rar5/rar5/FILE1.TXT delete mode 100644 src/test/resources/rar5/rar5/FILE2.TXT delete mode 100644 "src/test/resources/rar5/unicode/\343\202\246\343\203\213\343\202\263\343\203\211.txt" delete mode 100644 "src/test/resources/rar5/unicode/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git a/src/test/java21/com/github/gotson/nightcompress/ArchiveTest.java b/src/test/java21/com/github/gotson/nightcompress/ArchiveTest.java index 3fd14fc..cbba8c9 100644 --- a/src/test/java21/com/github/gotson/nightcompress/ArchiveTest.java +++ b/src/test/java21/com/github/gotson/nightcompress/ArchiveTest.java @@ -40,22 +40,16 @@ 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 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 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); } } } @@ -63,14 +57,26 @@ void can_extract(TestData testData) throws Exception { List 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" + ) + ) ); } diff --git a/src/test/java21/com/github/gotson/nightcompress/TestData.java b/src/test/java21/com/github/gotson/nightcompress/TestData.java index 2cfa7cf..71bbb6a 100644 --- a/src/test/java21/com/github/gotson/nightcompress/TestData.java +++ b/src/test/java21/com/github/gotson/nightcompress/TestData.java @@ -5,7 +5,7 @@ public record TestData( Path archivePath, - Path contentPath, - List entries + List entries, + List entriesContent ) { } diff --git a/src/test/resources/rar5/rar5/FILE1.TXT b/src/test/resources/rar5/rar5/FILE1.TXT deleted file mode 100644 index e212970..0000000 --- a/src/test/resources/rar5/rar5/FILE1.TXT +++ /dev/null @@ -1 +0,0 @@ -file1 diff --git a/src/test/resources/rar5/rar5/FILE2.TXT b/src/test/resources/rar5/rar5/FILE2.TXT deleted file mode 100644 index 6c493ff..0000000 --- a/src/test/resources/rar5/rar5/FILE2.TXT +++ /dev/null @@ -1 +0,0 @@ -file2 diff --git "a/src/test/resources/rar5/unicode/\343\202\246\343\203\213\343\202\263\343\203\211.txt" "b/src/test/resources/rar5/unicode/\343\202\246\343\203\213\343\202\263\343\203\211.txt" deleted file mode 100644 index 4328a23..0000000 --- "a/src/test/resources/rar5/unicode/\343\202\246\343\203\213\343\202\263\343\203\211.txt" +++ /dev/null @@ -1 +0,0 @@ -このファイルにはUnicodeテキストが含まれています \ No newline at end of file diff --git "a/src/test/resources/rar5/unicode/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/src/test/resources/rar5/unicode/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" deleted file mode 100644 index 1a4ef0a..0000000 --- "a/src/test/resources/rar5/unicode/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" +++ /dev/null @@ -1 +0,0 @@ -aaaaaaaaaa \ No newline at end of file