jjg@46: /* jjg@46: * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. jjg@46: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@46: * jjg@46: * This code is free software; you can redistribute it and/or modify it jjg@46: * under the terms of the GNU General Public License version 2 only, as jjg@46: * published by the Free Software Foundation. jjg@46: * jjg@46: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@46: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@46: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@46: * version 2 for more details (a copy is included in the LICENSE file that jjg@46: * accompanied this code). jjg@46: * jjg@46: * You should have received a copy of the GNU General Public License version jjg@46: * 2 along with this work; if not, write to the Free Software Foundation, jjg@46: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@46: * jjg@46: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@46: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@46: * have any questions. jjg@46: */ jjg@46: jjg@46: import java.io.*; jjg@46: import java.util.*; jjg@46: jjg@46: /* jjg@46: * @test jjg@46: * @bug 6439940 jjg@46: * @summary Cleanup javap implementation jjg@46: * @run main/othervm OptionTest jjg@46: */ jjg@46: public class OptionTest { jjg@46: public static void main(String[] args) throws Exception { jjg@46: new OptionTest().run(); jjg@46: } jjg@46: jjg@46: OptionTest() { jjg@46: String v = System.getProperty("view.cmd"); jjg@46: if (v != null) { jjg@46: viewResults = true; jjg@46: viewCmd = Arrays.asList(v.split(" +")); jjg@46: } jjg@46: } jjg@46: jjg@46: jjg@46: void run() throws Exception { jjg@46: int count = 0; jjg@46: int pass = 0; jjg@46: // try combinations of options and compare old javap against new javap jjg@46: for (int i = 0; i < (1<<8); i++) { jjg@46: List options = new ArrayList(); jjg@46: if ((i & 0x01) != 0) jjg@46: options.add("-c"); jjg@46: if ((i & 0x02) != 0) jjg@46: options.add("-l"); jjg@46: if ((i & 0x04) != 0) jjg@46: options.add("-public"); jjg@46: if ((i & 0x08) != 0) jjg@46: options.add("-protected"); jjg@46: if ((i & 0x10) != 0) jjg@46: options.add("-package"); jjg@46: if ((i & 0x20) != 0) jjg@46: options.add("-private"); jjg@46: if ((i & 0x40) != 0) jjg@46: options.add("-s"); jjg@46: if ((i & 0x80) != 0) jjg@46: options.add("-verbose"); jjg@46: count++; jjg@46: if (test(options)) jjg@46: pass++; jjg@46: } jjg@46: jjg@46: if (pass < count) jjg@46: throw new Error(pass + "/" + count + " test cases passed"); jjg@46: } jjg@46: jjg@46: boolean test(List options) throws Exception { jjg@46: String[] args = new String[options.size() + 1]; jjg@46: options.toArray(args); jjg@46: args[args.length - 1] = testClassName; jjg@46: String oldOut = runOldJavap(args); jjg@46: String newOut = runNewJavap(args); jjg@46: boolean ok = oldOut.equals(newOut); jjg@46: System.err.println((ok ? "pass" : "FAIL") + ": " + options); jjg@46: if (!ok && viewResults) jjg@46: view(oldOut, newOut); jjg@46: return ok; jjg@46: } jjg@46: jjg@46: String runOldJavap(String[] args) { jjg@46: //System.err.println("OLD: " + Arrays.asList(args)); jjg@46: PrintStream oldOut = System.out; jjg@46: ByteArrayOutputStream out = new ByteArrayOutputStream(); jjg@46: System.setOut(new PrintStream(out)); jjg@46: try { jjg@46: sun.tools.javap.Main.entry(args); jjg@46: } finally { jjg@46: System.setOut(oldOut); jjg@46: } jjg@46: return out.toString(); jjg@46: } jjg@46: jjg@46: String runNewJavap(String[] args) { jjg@46: String[] nArgs = new String[args.length + 2]; jjg@46: nArgs[0] = "-XDcompat"; jjg@46: nArgs[1] = "-XDignore.symbol.file"; jjg@46: System.arraycopy(args, 0, nArgs, 2, args.length); jjg@46: //System.err.println("NEW: " + Arrays.asList(nArgs)); jjg@46: StringWriter out = new StringWriter(); jjg@46: com.sun.tools.javap.Main.run(nArgs, new PrintWriter(out, true)); jjg@46: return out.toString(); jjg@46: } jjg@46: jjg@46: File write(String text, String suffix) throws IOException { jjg@46: File f = File.createTempFile("OptionTest", suffix); jjg@46: FileWriter out = new FileWriter(f); jjg@46: out.write(text); jjg@46: out.close(); jjg@46: return f; jjg@46: } jjg@46: jjg@46: void view(String oldOut, String newOut) throws Exception { jjg@46: File oldFile = write(oldOut, "old"); jjg@46: File newFile = write(newOut, "new"); jjg@46: List cmd = new ArrayList(); jjg@46: cmd.addAll(viewCmd); jjg@46: cmd.add(oldFile.getPath()); jjg@46: cmd.add(newFile.getPath()); jjg@46: Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start(); jjg@46: p.getOutputStream().close(); jjg@46: String line; jjg@46: BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); jjg@46: while ((line = in.readLine()) != null) jjg@46: System.err.println(line); jjg@46: in.close(); jjg@46: p.waitFor(); jjg@46: } jjg@46: jjg@46: String testClassName = "java.lang.SecurityManager"; jjg@46: boolean viewResults; jjg@46: List viewCmd; jjg@46: }