View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Test that dump() methods work on the JDK classes
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                      // System.out.println("Parsing " + name);
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  }