You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2015/08/26 19:59:55 UTC

svn commit: r1697983 - in /commons/proper/bcel/trunk/src: changes/changes.xml test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java

Author: sebb
Date: Wed Aug 26 17:59:54 2015
New Revision: 1697983

URL: http://svn.apache.org/r1697983
Log:
BCEL-258 No tests to check the output of dump methods
Added test for generic package dump methods

Added:
    commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java   (with props)
Modified:
    commons/proper/bcel/trunk/src/changes/changes.xml

Modified: commons/proper/bcel/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/changes/changes.xml?rev=1697983&r1=1697982&r2=1697983&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/changes/changes.xml (original)
+++ commons/proper/bcel/trunk/src/changes/changes.xml Wed Aug 26 17:59:54 2015
@@ -63,6 +63,7 @@ The <action> type attribute can be add,u
 
   <body>
     <release version="6.0" date="TBA" description="Major release with Java 7 and 8 support">
+      <action issue="BCEL-258" type="fix">No tests to check the output of dump methods</action>
       <action issue="BCEL-257" type="fix">INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL need to define dump() methods</action>
       <action issue="BCEL-254" type="fix">Two more methods that would be nice to be public.</action>
       <action issue="BCEL-245" type="fix">Type class includes constants that reference subclasses</action>

Added: commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java?rev=1697983&view=auto
==============================================================================
--- commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java (added)
+++ commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java Wed Aug 26 17:59:54 2015
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.bcel6.generic;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.InputStream;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import org.apache.commons.bcel6.classfile.ClassParser;
+import org.apache.commons.bcel6.classfile.Code;
+import org.apache.commons.bcel6.classfile.JavaClass;
+import org.apache.commons.bcel6.classfile.Method;
+import org.junit.Test;
+
+/**
+ * Test that the generic dump() methods work on the JDK classes
+ * Reads each class into an instruction list and then dumps
+ * the instructions. The output bytes should be the same as the input.
+ */
+public class JDKGenericDumpTestCase {
+
+    @Test
+    public void testJDKjars() throws Exception {
+        File[] jars = listJDKjars();
+        for(File file : jars) {
+            testJar(file);
+        }
+    }
+
+    private void testJar(File file) throws Exception {
+        System.out.println(file);
+        JarFile jar = new JarFile(file);
+        Enumeration<JarEntry> en = jar.entries();
+
+        while (en.hasMoreElements()) {
+            JarEntry e = en.nextElement();
+            final String name = e.getName();
+            if (name.endsWith(".class")) {
+//                System.out.println("- " + name);
+                InputStream in = jar.getInputStream(e);
+                ClassParser parser = new ClassParser(in, name);
+                JavaClass jc = parser.parse();
+                for(Method m : jc.getMethods()) {
+                    compare(name, m);
+                }
+            }
+        }
+        jar.close();
+    }
+
+    private void compare(String name, Method m) {
+//        System.out.println("Method: " + m);
+        Code c = m.getCode();
+        if (c==null) {
+            return; // e.g. abstract method
+        }
+        byte[] src = c.getCode();
+        InstructionList il = new InstructionList(src);
+        byte[] out = il.getByteCode();
+        if (src.length == out.length) {
+            assertArrayEquals(name + ": "+m.toString(), src, out);
+        } else {
+            System.out.println(name + ": "+m.toString() +" "+ src.length+" "+out.length);
+            System.out.println(bytesToHex(src));
+            System.out.println(bytesToHex(out));
+            for(InstructionHandle ih : il) {
+                System.out.println(ih.toString(false));
+            }
+            fail("Array comparison failure");
+        }
+    }
+
+    private File[] listJDKjars() throws Exception {
+        File javaLib = new File(System.getProperty("java.home") + "/lib");
+        return javaLib.listFiles(new FileFilter() {
+            @Override
+            public boolean accept(File file) {
+                return file.getName().endsWith(".jar");
+            }
+        });
+    }
+
+    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
+    private static String bytesToHex(byte[] bytes) {
+        char[] hexChars = new char[bytes.length * 3];
+        int i=0;
+        for ( int j = 0; j < bytes.length; j++ ) {
+            int v = bytes[j] & 0xFF;
+            hexChars[i++] = hexArray[v >>> 4];
+            hexChars[i++] = hexArray[v & 0x0F];
+            hexChars[i++] = ' ';
+        }
+        return new String(hexChars);
+    }
+}

Propchange: commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native