Tue, 08 Jul 2008 16:59:27 -0700
6715251: javap should be consistent with javac and return 2 if given no arguments
Reviewed-by: ksrini
jjg@46 | 1 | /* |
xdono@54 | 2 | * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. |
jjg@46 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@46 | 4 | * |
jjg@46 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@46 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@46 | 7 | * published by the Free Software Foundation. |
jjg@46 | 8 | * |
jjg@46 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@46 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@46 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@46 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@46 | 13 | * accompanied this code). |
jjg@46 | 14 | * |
jjg@46 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@46 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@46 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@46 | 18 | * |
jjg@46 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@46 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@46 | 21 | * have any questions. |
jjg@46 | 22 | */ |
jjg@46 | 23 | |
jjg@46 | 24 | import java.io.*; |
jjg@46 | 25 | import java.util.*; |
jjg@46 | 26 | |
jjg@46 | 27 | /* |
jjg@46 | 28 | * @test |
jjg@46 | 29 | * @bug 6439940 |
jjg@46 | 30 | * @summary Cleanup javap implementation |
jjg@46 | 31 | * @run main/othervm OptionTest |
jjg@46 | 32 | */ |
jjg@46 | 33 | public class OptionTest { |
jjg@46 | 34 | public static void main(String[] args) throws Exception { |
jjg@46 | 35 | new OptionTest().run(); |
jjg@46 | 36 | } |
jjg@46 | 37 | |
jjg@46 | 38 | OptionTest() { |
jjg@46 | 39 | String v = System.getProperty("view.cmd"); |
jjg@46 | 40 | if (v != null) { |
jjg@46 | 41 | viewResults = true; |
jjg@46 | 42 | viewCmd = Arrays.asList(v.split(" +")); |
jjg@46 | 43 | } |
jjg@46 | 44 | } |
jjg@46 | 45 | |
jjg@46 | 46 | |
jjg@46 | 47 | void run() throws Exception { |
jjg@46 | 48 | int count = 0; |
jjg@46 | 49 | int pass = 0; |
jjg@46 | 50 | // try combinations of options and compare old javap against new javap |
jjg@46 | 51 | for (int i = 0; i < (1<<8); i++) { |
jjg@46 | 52 | List<String> options = new ArrayList<String>(); |
jjg@46 | 53 | if ((i & 0x01) != 0) |
jjg@46 | 54 | options.add("-c"); |
jjg@46 | 55 | if ((i & 0x02) != 0) |
jjg@46 | 56 | options.add("-l"); |
jjg@46 | 57 | if ((i & 0x04) != 0) |
jjg@46 | 58 | options.add("-public"); |
jjg@46 | 59 | if ((i & 0x08) != 0) |
jjg@46 | 60 | options.add("-protected"); |
jjg@46 | 61 | if ((i & 0x10) != 0) |
jjg@46 | 62 | options.add("-package"); |
jjg@46 | 63 | if ((i & 0x20) != 0) |
jjg@46 | 64 | options.add("-private"); |
jjg@46 | 65 | if ((i & 0x40) != 0) |
jjg@46 | 66 | options.add("-s"); |
jjg@46 | 67 | if ((i & 0x80) != 0) |
jjg@46 | 68 | options.add("-verbose"); |
jjg@46 | 69 | count++; |
jjg@46 | 70 | if (test(options)) |
jjg@46 | 71 | pass++; |
jjg@46 | 72 | } |
jjg@46 | 73 | |
jjg@46 | 74 | if (pass < count) |
jjg@46 | 75 | throw new Error(pass + "/" + count + " test cases passed"); |
jjg@46 | 76 | } |
jjg@46 | 77 | |
jjg@46 | 78 | boolean test(List<String> options) throws Exception { |
jjg@46 | 79 | String[] args = new String[options.size() + 1]; |
jjg@46 | 80 | options.toArray(args); |
jjg@46 | 81 | args[args.length - 1] = testClassName; |
jjg@46 | 82 | String oldOut = runOldJavap(args); |
jjg@46 | 83 | String newOut = runNewJavap(args); |
jjg@46 | 84 | boolean ok = oldOut.equals(newOut); |
jjg@46 | 85 | System.err.println((ok ? "pass" : "FAIL") + ": " + options); |
jjg@46 | 86 | if (!ok && viewResults) |
jjg@46 | 87 | view(oldOut, newOut); |
jjg@46 | 88 | return ok; |
jjg@46 | 89 | } |
jjg@46 | 90 | |
jjg@46 | 91 | String runOldJavap(String[] args) { |
jjg@46 | 92 | //System.err.println("OLD: " + Arrays.asList(args)); |
jjg@46 | 93 | PrintStream oldOut = System.out; |
jjg@46 | 94 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
jjg@46 | 95 | System.setOut(new PrintStream(out)); |
jjg@46 | 96 | try { |
jjg@46 | 97 | sun.tools.javap.Main.entry(args); |
jjg@46 | 98 | } finally { |
jjg@46 | 99 | System.setOut(oldOut); |
jjg@46 | 100 | } |
jjg@46 | 101 | return out.toString(); |
jjg@46 | 102 | } |
jjg@46 | 103 | |
jjg@46 | 104 | String runNewJavap(String[] args) { |
jjg@46 | 105 | String[] nArgs = new String[args.length + 2]; |
jjg@46 | 106 | nArgs[0] = "-XDcompat"; |
jjg@46 | 107 | nArgs[1] = "-XDignore.symbol.file"; |
jjg@46 | 108 | System.arraycopy(args, 0, nArgs, 2, args.length); |
jjg@46 | 109 | //System.err.println("NEW: " + Arrays.asList(nArgs)); |
jjg@46 | 110 | StringWriter out = new StringWriter(); |
jjg@46 | 111 | com.sun.tools.javap.Main.run(nArgs, new PrintWriter(out, true)); |
jjg@46 | 112 | return out.toString(); |
jjg@46 | 113 | } |
jjg@46 | 114 | |
jjg@46 | 115 | File write(String text, String suffix) throws IOException { |
jjg@46 | 116 | File f = File.createTempFile("OptionTest", suffix); |
jjg@46 | 117 | FileWriter out = new FileWriter(f); |
jjg@46 | 118 | out.write(text); |
jjg@46 | 119 | out.close(); |
jjg@46 | 120 | return f; |
jjg@46 | 121 | } |
jjg@46 | 122 | |
jjg@46 | 123 | void view(String oldOut, String newOut) throws Exception { |
jjg@46 | 124 | File oldFile = write(oldOut, "old"); |
jjg@46 | 125 | File newFile = write(newOut, "new"); |
jjg@46 | 126 | List<String> cmd = new ArrayList<String>(); |
jjg@46 | 127 | cmd.addAll(viewCmd); |
jjg@46 | 128 | cmd.add(oldFile.getPath()); |
jjg@46 | 129 | cmd.add(newFile.getPath()); |
jjg@46 | 130 | Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start(); |
jjg@46 | 131 | p.getOutputStream().close(); |
jjg@46 | 132 | String line; |
jjg@46 | 133 | BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
jjg@46 | 134 | while ((line = in.readLine()) != null) |
jjg@46 | 135 | System.err.println(line); |
jjg@46 | 136 | in.close(); |
jjg@46 | 137 | p.waitFor(); |
jjg@46 | 138 | } |
jjg@46 | 139 | |
jjg@46 | 140 | String testClassName = "java.lang.SecurityManager"; |
jjg@46 | 141 | boolean viewResults; |
jjg@46 | 142 | List<String> viewCmd; |
jjg@46 | 143 | } |