|
23 | 23 | import java.io.InputStream; |
24 | 24 | import java.lang.ref.Cleaner.Cleanable; |
25 | 25 | import java.nio.charset.Charset; |
| 26 | +import java.util.ArrayList; |
26 | 27 | import java.util.Enumeration; |
| 28 | +import java.util.List; |
| 29 | +import java.util.UUID; |
27 | 30 | import java.util.jar.JarEntry; |
28 | 31 | import java.util.jar.JarFile; |
| 32 | +import java.util.jar.JarOutputStream; |
29 | 33 | import java.util.jar.Manifest; |
| 34 | +import java.util.zip.ZipEntry; |
30 | 35 | import java.util.zip.ZipFile; |
31 | 36 |
|
32 | 37 | import org.assertj.core.extractor.Extractors; |
@@ -386,4 +391,42 @@ void versionedStreamStreamsEntries() throws IOException { |
386 | 391 | } |
387 | 392 | } |
388 | 393 |
|
| 394 | + @Test // gh-39166 |
| 395 | + void getCommentAlignsWithJdkJar() throws Exception { |
| 396 | + File file = new File(this.tempDir, "testcomments.jar"); |
| 397 | + try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(file))) { |
| 398 | + jar.putNextEntry(new ZipEntry("BOOT-INF/")); |
| 399 | + jar.closeEntry(); |
| 400 | + jar.putNextEntry(new ZipEntry("BOOT-INF/classes/")); |
| 401 | + jar.closeEntry(); |
| 402 | + for (int i = 0; i < 5; i++) { |
| 403 | + ZipEntry entry = new ZipEntry("BOOT-INF/classes/T" + i + ".class"); |
| 404 | + entry.setComment("T" + i); |
| 405 | + jar.putNextEntry(entry); |
| 406 | + jar.write(UUID.randomUUID().toString().getBytes()); |
| 407 | + jar.closeEntry(); |
| 408 | + } |
| 409 | + } |
| 410 | + List<String> jdk = collectComments(new JarFile(file)); |
| 411 | + List<String> nested = collectComments(new NestedJarFile(file, "BOOT-INF/classes/")); |
| 412 | + assertThat(nested).isEqualTo(jdk); |
| 413 | + } |
| 414 | + |
| 415 | + private List<String> collectComments(JarFile jarFile) throws IOException { |
| 416 | + try { |
| 417 | + List<String> comments = new ArrayList<>(); |
| 418 | + Enumeration<JarEntry> entries = jarFile.entries(); |
| 419 | + while (entries.hasMoreElements()) { |
| 420 | + String comment = entries.nextElement().getComment(); |
| 421 | + if (comment != null) { |
| 422 | + comments.add(comment); |
| 423 | + } |
| 424 | + } |
| 425 | + return comments; |
| 426 | + } |
| 427 | + finally { |
| 428 | + jarFile.close(); |
| 429 | + } |
| 430 | + } |
| 431 | + |
389 | 432 | } |
0 commit comments