View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.bcel.classfile;
19  
20  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.DataInputStream;
25  import java.io.DataOutputStream;
26  import java.io.InputStream;
27  import java.nio.file.Path;
28  import java.util.Enumeration;
29  import java.util.jar.JarEntry;
30  import java.util.jar.JarFile;
31  
32  import org.apache.bcel.generic.JavaHome;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.params.ParameterizedTest;
35  import org.junit.jupiter.params.provider.MethodSource;
36  
37  /**
38   * Test that dump() methods work on the JDK classes
39   */
40  public class JDKClassDumpTestCase {
41  
42      private void compare(final JavaClass jc, final InputStream inputStream, final String name) throws Exception {
43          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
44          try (DataOutputStream dos = new DataOutputStream(baos)) {
45              jc.dump(dos);
46          }
47          try (DataInputStream src = new DataInputStream(inputStream)) {
48              int i = 0;
49              for (final int out : baos.toByteArray()) {
50                  final int in = src.read();
51                  final int j = i;
52                  assertEquals(in, out & 0xFF, () -> name + ": Mismatch at " + j);
53                  i++;
54              }
55          }
56      }
57  
58      private void testJar(final Path path) throws Exception {
59          try (JarFile jar = new JarFile(path.toFile())) {
60              System.out.println("Parsing " + jar.getName());
61              final Enumeration<JarEntry> en = jar.entries();
62              while (en.hasMoreElements()) {
63                  final JarEntry e = en.nextElement();
64                  final String name = e.getName();
65                  if (name.endsWith(JavaClass.EXTENSION)) {
66                      // System.out.println("Parsing " + name);
67                      try (InputStream inputStream1 = jar.getInputStream(e);
68                              InputStream inputStream2 = jar.getInputStream(e);) {
69                          compare(new ClassParser(inputStream1, name).parse(), inputStream2, name);
70                      }
71                  }
72              }
73          }
74      }
75  
76      @ParameterizedTest
77      @MethodSource("org.apache.bcel.generic.JavaHome#streamJarPath")
78      public void testPerformance(final Path path) throws Exception {
79          assertDoesNotThrow(() -> testJar(path));
80      }
81  
82      @Test
83      public void testPerformanceJmod() throws Exception {
84          JavaHome.streamModulePath().forEach(path -> assertDoesNotThrow(() -> testJar(path)));
85      }
86  }