• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Assert.assertEquals;
21 
22 import java.io.ByteArrayOutputStream;
23 import java.io.DataInputStream;
24 import java.io.DataOutputStream;
25 import java.io.File;
26 import java.io.FileFilter;
27 import java.io.InputStream;
28 import java.util.Enumeration;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarFile;
31 
32 import org.junit.Assert;
33 import org.junit.Test;
34 
35 /**
36  * Test that dump() methods work on the JDK classes
37  */
38 public class JDKClassDumpTestCase {
39 
40     @Test
testPerformance()41     public void testPerformance() throws Exception {
42         final File javaLib = new File(System.getProperty("java.home") + "/lib");
43         javaLib.listFiles(new FileFilter() {
44 
45             @Override
46             public boolean accept(final File file) {
47                 if (file.getName().endsWith(".jar")) {
48                     try {
49                         testJar(file);
50                     } catch (final Exception e) {
51                         Assert.fail(e.getMessage());
52                     }
53                 }
54                 return false;
55             }
56         });
57     }
58 
59 
testJar(final File file)60     private void testJar(final File file) throws Exception {
61         System.out.println("parsing " + file);
62         try (JarFile jar = new JarFile(file)) {
63             final Enumeration<JarEntry> en = jar.entries();
64             while (en.hasMoreElements()) {
65                 final JarEntry e = en.nextElement();
66                 final String name = e.getName();
67                 if (name.endsWith(".class")) {
68                     // System.out.println("parsing " + name);
69                     try (InputStream in = jar.getInputStream(e)) {
70                         final ClassParser parser = new ClassParser(in, name);
71                         final JavaClass jc = parser.parse();
72                         compare(jc, jar.getInputStream(e), name);
73                     }
74                 }
75             }
76         }
77     }
78 
compare(final JavaClass jc, final InputStream inputStream, final String name)79     private void compare(final JavaClass jc, final InputStream inputStream, final String name) throws Exception {
80         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
81         try (DataOutputStream dos = new DataOutputStream(baos)) {
82             jc.dump(dos);
83         }
84         try (DataInputStream src = new DataInputStream(inputStream)) {
85             int i = 0;
86             for (final int out : baos.toByteArray()) {
87                 final int in = src.read();
88                 assertEquals(name + ": Mismatch at " + i, in, out & 0xFF);
89                 i++;
90             }
91         }
92     }
93 
94 
95 }
96