/*
 * 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.bcel.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.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.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 {
        final File[] jars = listJDKjars();
        for (final File file : jars) {
            testJar(file);
        }
    }

    private void testJar(final File file) throws Exception {
        System.out.println(file);
        try (JarFile jar = new JarFile(file)) {
            final Enumeration<JarEntry> en = jar.entries();
            while (en.hasMoreElements()) {
                final JarEntry e = en.nextElement();
                final String name = e.getName();
                if (name.endsWith(".class")) {
                    // System.out.println("- " + name);
                    try (InputStream in = jar.getInputStream(e)) {
                        final ClassParser parser = new ClassParser(in, name);
                        final JavaClass jc = parser.parse();
                        for (final Method m : jc.getMethods()) {
                            compare(name, m);
                        }
                    }
                }
            }
        }
    }

    private void compare(final String name, final Method m) {
//        System.out.println("Method: " + m);
        final Code c = m.getCode();
        if (c==null) {
            return; // e.g. abstract method
        }
        final byte[] src = c.getCode();
        final InstructionList il = new InstructionList(src);
        final 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 (final InstructionHandle ih : il) {
                System.out.println(ih.toString(false));
            }
            fail("Array comparison failure");
        }
    }

    private File[] listJDKjars() throws Exception {
        final File javaLib = new File(System.getProperty("java.home") + "/lib");
        return javaLib.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File file) {
                return file.getName().endsWith(".jar");
            }
        });
    }

    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
    private static String bytesToHex(final byte[] bytes) {
        final char[] hexChars = new char[bytes.length * 3];
        int i=0;
        for (final byte b : bytes) {
            final int v = b & 0xFF;
            hexChars[i++] = hexArray[v >>> 4];
            hexChars[i++] = hexArray[v & 0x0F];
            hexChars[i++] = ' ';
        }
        return new String(hexChars);
    }
}

@@ -992,16 +992,70 @@
 Stream;%0A
+import java.util.Arrays;%0Aimport java.util.Collection;%0A
 import j
@@ -1330,16 +1330,140 @@
 it.Test;
+%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.Parameterized;%0Aimport org.junit.runners.Parameterized.Parameters;
 %0A%0A/**%0A *
@@ -1659,16 +1659,46 @@
 ut.%0A */%0A
+@RunWith(Parameterized.class)%0A
 public c
@@ -1728,16 +1728,317 @@
 Case %7B%0A%0A
+    @Parameters(name = %22%7B0%7D%22)%0A    public static Collection%3CObject%5B%5D%3E data() %7B%0A        return Arrays.asList(new Object%5B%5D%5B%5D %7B %7B System.getProperty(%22java.home%22) %7D %7D);%0A    %7D%0A%0A    public JDKGenericDumpTestCase(final String javaHome) %7B%0A        this.javaHome = javaHome;%0A    %7D%0A%0A    private String javaHome;%0A%0A
     @Tes
@@ -3145,17 +3145,18 @@
 ) %7B%0A
-//
        
+ //
  Sys
@@ -3237,18 +3237,20 @@
    if (c
+ 
 ==
+ 
 null) %7B%0A
@@ -3522,33 +3522,35 @@
 uals(name + %22: %22
-+
+ + 
 m.toString(), sr
@@ -3617,17 +3617,19 @@
 e + %22: %22
-+
+ + 
 m.toStri
@@ -3634,19 +3634,21 @@
 ring() +
+ 
 %22 %22
+ 
 + src.le
@@ -3655,13 +3655,17 @@
 ngth
-+%22 %22+
+ + %22 %22 + 
 out.
@@ -4050,39 +4050,16 @@
 ile(
-System.getProperty(%22
 java
-.h
+H
 ome
-%22)
  + %22
@@ -4355,24 +4355,25 @@
 harArray();%0A
+%0A
     private 
@@ -4492,17 +4492,19 @@
    int i
-=
+ = 
 0;%0A     

Parameterize test.