test/tools/javap/OptionTest.java

changeset 46
7708bd6d800d
child 54
eaf608c64fec
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javap/OptionTest.java	Tue Jun 03 13:26:47 2008 -0700
     1.3 @@ -0,0 +1,143 @@
     1.4 +/*
     1.5 + * Copyright 2007 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 +
    1.30 +/*
    1.31 + * @test
    1.32 + * @bug 6439940
    1.33 + * @summary Cleanup javap implementation
    1.34 + * @run main/othervm OptionTest
    1.35 + */
    1.36 +public class OptionTest {
    1.37 +    public static void main(String[] args) throws Exception {
    1.38 +        new OptionTest().run();
    1.39 +    }
    1.40 +
    1.41 +    OptionTest() {
    1.42 +        String v = System.getProperty("view.cmd");
    1.43 +        if (v != null) {
    1.44 +            viewResults = true;
    1.45 +            viewCmd = Arrays.asList(v.split(" +"));
    1.46 +        }
    1.47 +    }
    1.48 +
    1.49 +
    1.50 +    void run() throws Exception {
    1.51 +        int count = 0;
    1.52 +        int pass = 0;
    1.53 +        // try combinations of options and compare old javap against new javap
    1.54 +        for (int i = 0; i < (1<<8); i++) {
    1.55 +            List<String> options = new ArrayList<String>();
    1.56 +            if ((i & 0x01) != 0)
    1.57 +                options.add("-c");
    1.58 +            if ((i & 0x02) != 0)
    1.59 +                options.add("-l");
    1.60 +            if ((i & 0x04) != 0)
    1.61 +                options.add("-public");
    1.62 +            if ((i & 0x08) != 0)
    1.63 +                options.add("-protected");
    1.64 +            if ((i & 0x10) != 0)
    1.65 +                options.add("-package");
    1.66 +            if ((i & 0x20) != 0)
    1.67 +                options.add("-private");
    1.68 +            if ((i & 0x40) != 0)
    1.69 +                options.add("-s");
    1.70 +            if ((i & 0x80) != 0)
    1.71 +                options.add("-verbose");
    1.72 +            count++;
    1.73 +            if (test(options))
    1.74 +                pass++;
    1.75 +        }
    1.76 +
    1.77 +        if (pass < count)
    1.78 +            throw new Error(pass + "/" + count + " test cases passed");
    1.79 +    }
    1.80 +
    1.81 +    boolean test(List<String> options) throws Exception {
    1.82 +        String[] args = new String[options.size() + 1];
    1.83 +        options.toArray(args);
    1.84 +        args[args.length - 1] = testClassName;
    1.85 +        String oldOut = runOldJavap(args);
    1.86 +        String newOut = runNewJavap(args);
    1.87 +        boolean ok = oldOut.equals(newOut);
    1.88 +        System.err.println((ok ? "pass" : "FAIL") + ": " + options);
    1.89 +        if (!ok && viewResults)
    1.90 +            view(oldOut, newOut);
    1.91 +        return ok;
    1.92 +    }
    1.93 +
    1.94 +    String runOldJavap(String[] args) {
    1.95 +        //System.err.println("OLD: " + Arrays.asList(args));
    1.96 +        PrintStream oldOut = System.out;
    1.97 +        ByteArrayOutputStream out = new ByteArrayOutputStream();
    1.98 +        System.setOut(new PrintStream(out));
    1.99 +        try {
   1.100 +            sun.tools.javap.Main.entry(args);
   1.101 +        } finally {
   1.102 +            System.setOut(oldOut);
   1.103 +        }
   1.104 +        return out.toString();
   1.105 +    }
   1.106 +
   1.107 +    String runNewJavap(String[] args) {
   1.108 +        String[] nArgs = new String[args.length + 2];
   1.109 +        nArgs[0] = "-XDcompat";
   1.110 +        nArgs[1] = "-XDignore.symbol.file";
   1.111 +        System.arraycopy(args, 0, nArgs, 2, args.length);
   1.112 +        //System.err.println("NEW: " + Arrays.asList(nArgs));
   1.113 +        StringWriter out = new StringWriter();
   1.114 +        com.sun.tools.javap.Main.run(nArgs, new PrintWriter(out, true));
   1.115 +        return out.toString();
   1.116 +    }
   1.117 +
   1.118 +    File write(String text, String suffix) throws IOException {
   1.119 +        File f = File.createTempFile("OptionTest", suffix);
   1.120 +        FileWriter out = new FileWriter(f);
   1.121 +        out.write(text);
   1.122 +        out.close();
   1.123 +        return f;
   1.124 +    }
   1.125 +
   1.126 +    void view(String oldOut, String newOut) throws Exception {
   1.127 +        File oldFile = write(oldOut, "old");
   1.128 +        File newFile = write(newOut, "new");
   1.129 +        List<String> cmd = new ArrayList<String>();
   1.130 +        cmd.addAll(viewCmd);
   1.131 +        cmd.add(oldFile.getPath());
   1.132 +        cmd.add(newFile.getPath());
   1.133 +        Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
   1.134 +        p.getOutputStream().close();
   1.135 +        String line;
   1.136 +        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   1.137 +        while ((line = in.readLine()) != null)
   1.138 +            System.err.println(line);
   1.139 +        in.close();
   1.140 +        p.waitFor();
   1.141 +    }
   1.142 +
   1.143 +    String testClassName = "java.lang.SecurityManager";
   1.144 +    boolean viewResults;
   1.145 +    List<String> viewCmd;
   1.146 +}

mercurial