Author: sebb
Date: Fri Sep 11 17:38:33 2015
New Revision: 1702521

URL: http://svn.apache.org/r1702521
Log:
Add a round-trip BCELifier test case

Modified:
    
commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java

Modified: 
commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java?rev=1702521&r1=1702520&r2=1702521&view=diff
==============================================================================
--- 
commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java
 (original)
+++ 
commons/proper/bcel/trunk/src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java
 Fri Sep 11 17:38:33 2015
@@ -2,12 +2,16 @@ package org.apache.commons.bcel6.util;
 
 import static org.junit.Assert.*;
 
+import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.io.OutputStream;
-
 import org.apache.commons.bcel6.classfile.JavaClass;
 import org.junit.Test;
 
+
 public class BCELifierTestCase {
 
     @Test
@@ -19,4 +23,55 @@ public class BCELifierTestCase {
         bcelifier.start();
     }
 
+    /*
+     * Dump a class using "javap" and compare with the same class recreated
+     * using BCELifier, "javac", "java" and dumped with "javap"
+     * TODO: detect if JDK present and skip test if not 
+     */
+    @Test
+    public void testJavapCompare() throws Exception {
+        testClassOnPath("target/test-classes/Java8Example.class");
+    }
+
+    private void testClassOnPath(String javaClass) throws Exception {
+        // Get javap of the input class
+        final String initial = exec(null, "javap", "-p", javaClass);
+
+        final File workDir = new File("target");
+        File infile = new File(javaClass);
+        JavaClass java_class = 
BCELifier.getJavaClass(infile.getName().replace(".class", ""));
+        assertNotNull(java_class);
+        File outfile = new File(workDir,infile.getName().replace(".class", 
"Creator.java"));
+        FileOutputStream fos = new FileOutputStream(outfile);
+        BCELifier bcelifier = new BCELifier(java_class, fos);
+        bcelifier.start();
+        fos.close();
+        exec(workDir, "javac", "-cp", "classes", outfile.getName());
+        exec(workDir, "java", "-cp", ".:classes", 
outfile.getName().replace(".java", ""));
+        final String output = exec(workDir, "javap", "-p", infile.getName());
+        assertEquals(initial, output);
+    }
+
+    private String exec(File workDir, String ... args) throws Exception {
+//        System.err.println(java.util.Arrays.toString(args));
+        ProcessBuilder pb = new ProcessBuilder( args );
+        pb.directory(workDir);
+        Process proc = pb.start();
+        BufferedInputStream is = new 
BufferedInputStream(proc.getInputStream());
+        InputStream es = proc.getErrorStream();
+        proc.waitFor();
+        byte []buff=new byte[2048];
+        int len;
+        while((len=es.read(buff)) != -1) {
+            System.err.print(new String(buff,0,len));
+        }
+        
+        StringBuilder sb = new StringBuilder();
+        while((len=is.read(buff)) != -1) {
+            sb.append(new String(buff,0,len));
+        }
+        is.close();
+        return sb.toString();
+    }
+
 }


Reply via email to