test/tools/javap/ListTest.java

changeset 46
7708bd6d800d
child 164
5ebe90e0afff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javap/ListTest.java	Tue Jun 03 13:26:47 2008 -0700
     1.3 @@ -0,0 +1,147 @@
     1.4 +/*
     1.5 + * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +import java.io.*;
    1.28 +import java.util.*;
    1.29 +import javax.tools.*;
    1.30 +
    1.31 +/*
    1.32 + * @test
    1.33 + * @bug 6439940
    1.34 + * @summary Cleanup javap implementation
    1.35 + * @run main/othervm ListTest
    1.36 + */
    1.37 +public class ListTest {
    1.38 +    public static void main(String[] args) throws Exception {
    1.39 +        new ListTest().run();
    1.40 +    }
    1.41 +
    1.42 +    ListTest() {
    1.43 +        String v = System.getProperty("view.cmd");
    1.44 +        //      v = "/opt/teamware/7.7/bin/filemerge -r";
    1.45 +        if (v != null) {
    1.46 +            viewResults = true;
    1.47 +            viewCmd = Arrays.asList(v.split(" +"));
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +    void run() throws Exception {
    1.52 +        StandardLocation[] locs = new StandardLocation[] {
    1.53 +            StandardLocation.PLATFORM_CLASS_PATH,
    1.54 +            StandardLocation.CLASS_PATH,
    1.55 +        };
    1.56 +
    1.57 +        int count = 0;
    1.58 +        int pass = 0;
    1.59 +        for (StandardLocation loc: locs) {
    1.60 +            for (String testClassName: list(loc)) {
    1.61 +                count++;
    1.62 +                if (test(testClassName))
    1.63 +                    pass++;
    1.64 +            }
    1.65 +        }
    1.66 +
    1.67 +        if (pass < count)
    1.68 +            throw new Error(pass + "/" + count + " test cases passed");
    1.69 +    }
    1.70 +
    1.71 +    Iterable<String> list(StandardLocation loc) throws IOException {
    1.72 +        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    1.73 +        StandardJavaFileManager sfm = compiler.getStandardFileManager(null, null, null);
    1.74 +        Set<JavaFileObject.Kind> kinds = Collections.singleton(JavaFileObject.Kind.CLASS);
    1.75 +
    1.76 +        List<String> list = new ArrayList<String>();
    1.77 +        for (JavaFileObject fo: sfm.list(loc, testPackage, kinds, true)) {
    1.78 +            //System.err.println(com.sun.tools.javac.util.Old199.getPath(fo));
    1.79 +            list.add(sfm.inferBinaryName(loc, fo));
    1.80 +        }
    1.81 +        return list;
    1.82 +    }
    1.83 +
    1.84 +    boolean test(String testClassName) throws Exception {
    1.85 +        String[] args = new String[options.size() + 1];
    1.86 +        options.toArray(args);
    1.87 +        args[args.length - 1] = testClassName;
    1.88 +        String oldOut = runOldJavap(args);
    1.89 +        String newOut = runNewJavap(args);
    1.90 +        boolean ok = oldOut.equals(newOut);
    1.91 +        System.err.println((ok ? "pass" : "FAIL") + ": " + testClassName);
    1.92 +        if (!ok && viewResults)
    1.93 +            view(oldOut, newOut);
    1.94 +        return ok;
    1.95 +    }
    1.96 +
    1.97 +    String runOldJavap(String[] args) {
    1.98 +        //System.err.println("OLD: " + Arrays.asList(args));
    1.99 +        PrintStream oldOut = System.out;
   1.100 +        ByteArrayOutputStream out = new ByteArrayOutputStream();
   1.101 +        System.setOut(new PrintStream(out));
   1.102 +        try {
   1.103 +            sun.tools.javap.Main.entry(args);
   1.104 +        } finally {
   1.105 +            System.setOut(oldOut);
   1.106 +        }
   1.107 +        return out.toString();
   1.108 +    }
   1.109 +
   1.110 +    String runNewJavap(String[] args) {
   1.111 +        String[] nArgs = new String[args.length + 2];
   1.112 +        nArgs[0] = "-XDcompat";
   1.113 +        nArgs[1] = "-XDignore.symbol.file";
   1.114 +        System.arraycopy(args, 0, nArgs, 2, args.length);
   1.115 +        //System.err.println("NEW: " + Arrays.asList(nArgs));
   1.116 +        StringWriter out = new StringWriter();
   1.117 +        com.sun.tools.javap.Main.run(nArgs, new PrintWriter(out, true));
   1.118 +        return out.toString();
   1.119 +    }
   1.120 +
   1.121 +    File write(String text, String suffix) throws IOException {
   1.122 +        File f = File.createTempFile("ListTest", suffix);
   1.123 +        FileWriter out = new FileWriter(f);
   1.124 +        out.write(text);
   1.125 +        out.close();
   1.126 +        return f;
   1.127 +    }
   1.128 +
   1.129 +    void view(String oldOut, String newOut) throws Exception {
   1.130 +        File oldFile = write(oldOut, "old");
   1.131 +        File newFile = write(newOut, "new");
   1.132 +        List<String> cmd = new ArrayList<String>();
   1.133 +        cmd.addAll(viewCmd);
   1.134 +        cmd.add(oldFile.getPath());
   1.135 +        cmd.add(newFile.getPath());
   1.136 +        Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
   1.137 +        p.getOutputStream().close();
   1.138 +        String line;
   1.139 +        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   1.140 +        while ((line = in.readLine()) != null)
   1.141 +            System.err.println(line);
   1.142 +        in.close();
   1.143 +        p.waitFor();
   1.144 +    }
   1.145 +
   1.146 +    String testPackage = "java.lang";
   1.147 +    List<String> options = Arrays.asList("-v");
   1.148 +    boolean viewResults;
   1.149 +    List<String> viewCmd;
   1.150 +}

mercurial