1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel.classfile;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.DataInputStream;
26 import java.io.DataOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.file.Path;
30 import java.util.Enumeration;
31 import java.util.jar.JarEntry;
32 import java.util.jar.JarFile;
33
34 import org.apache.bcel.generic.JavaHome;
35 import org.apache.commons.io.function.IOStream;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.params.ParameterizedTest;
38 import org.junit.jupiter.params.provider.MethodSource;
39
40
41
42
43 class JDKClassDumpTest {
44
45 private void compare(final JavaClass jc, final InputStream inputStream, final String name) throws IOException {
46 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
47 try (DataOutputStream dos = new DataOutputStream(baos)) {
48 jc.dump(dos);
49 }
50 try (DataInputStream src = new DataInputStream(inputStream)) {
51 int i = 0;
52 for (final int out : baos.toByteArray()) {
53 final int in = src.read();
54 final int j = i;
55 assertEquals(in, out & 0xFF, () -> name + ": Mismatch at " + j);
56 i++;
57 }
58 }
59 }
60
61 private void testJar(final Path path) throws IOException {
62 try (JarFile jar = new JarFile(path.toFile())) {
63 System.out.println("Parsing " + jar.getName());
64 final Enumeration<JarEntry> en = jar.entries();
65 while (en.hasMoreElements()) {
66 final JarEntry e = en.nextElement();
67 final String name = e.getName();
68 if (name.endsWith(JavaClass.EXTENSION)) {
69
70 try (InputStream inputStream1 = jar.getInputStream(e);
71 InputStream inputStream2 = jar.getInputStream(e);) {
72 compare(new ClassParser(inputStream1, name).parse(), inputStream2, name);
73 }
74 }
75 }
76 }
77 }
78
79 @ParameterizedTest
80 @MethodSource("org.apache.bcel.generic.JavaHome#streamJarPath")
81 void testPerformance(final Path path) throws Exception {
82 testJar(path);
83 }
84
85 @Test
86 void testPerformanceJmod() throws Exception {
87 IOStream.adapt(JavaHome.streamModulePath()).forEach(this::testJar);
88 }
89 }