Mon, 27 Oct 2008 14:25:59 -0700
6764226: ListTest fails on javap output with bad characters
Reviewed-by: darcy
jjg@46 | 1 | /* |
jjg@46 | 2 | * Copyright 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 | import javax.tools.*; |
jjg@46 | 27 | |
jjg@46 | 28 | /* |
jjg@46 | 29 | * @test |
jjg@46 | 30 | * @bug 6439940 |
jjg@46 | 31 | * @summary Cleanup javap implementation |
jjg@46 | 32 | * @run main/othervm ListTest |
jjg@46 | 33 | */ |
jjg@46 | 34 | public class ListTest { |
jjg@46 | 35 | public static void main(String[] args) throws Exception { |
jjg@46 | 36 | new ListTest().run(); |
jjg@46 | 37 | } |
jjg@46 | 38 | |
jjg@46 | 39 | ListTest() { |
jjg@46 | 40 | String v = System.getProperty("view.cmd"); |
jjg@46 | 41 | // v = "/opt/teamware/7.7/bin/filemerge -r"; |
jjg@46 | 42 | if (v != null) { |
jjg@46 | 43 | viewResults = true; |
jjg@46 | 44 | viewCmd = Arrays.asList(v.split(" +")); |
jjg@46 | 45 | } |
jjg@46 | 46 | } |
jjg@46 | 47 | |
jjg@46 | 48 | void run() throws Exception { |
jjg@46 | 49 | StandardLocation[] locs = new StandardLocation[] { |
jjg@46 | 50 | StandardLocation.PLATFORM_CLASS_PATH, |
jjg@46 | 51 | StandardLocation.CLASS_PATH, |
jjg@46 | 52 | }; |
jjg@46 | 53 | |
jjg@46 | 54 | int count = 0; |
jjg@46 | 55 | int pass = 0; |
jjg@46 | 56 | for (StandardLocation loc: locs) { |
jjg@46 | 57 | for (String testClassName: list(loc)) { |
jjg@46 | 58 | count++; |
jjg@46 | 59 | if (test(testClassName)) |
jjg@46 | 60 | pass++; |
jjg@46 | 61 | } |
jjg@46 | 62 | } |
jjg@46 | 63 | |
jjg@46 | 64 | if (pass < count) |
jjg@46 | 65 | throw new Error(pass + "/" + count + " test cases passed"); |
jjg@46 | 66 | } |
jjg@46 | 67 | |
jjg@46 | 68 | Iterable<String> list(StandardLocation loc) throws IOException { |
jjg@46 | 69 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
jjg@46 | 70 | StandardJavaFileManager sfm = compiler.getStandardFileManager(null, null, null); |
jjg@46 | 71 | Set<JavaFileObject.Kind> kinds = Collections.singleton(JavaFileObject.Kind.CLASS); |
jjg@46 | 72 | |
jjg@46 | 73 | List<String> list = new ArrayList<String>(); |
jjg@46 | 74 | for (JavaFileObject fo: sfm.list(loc, testPackage, kinds, true)) { |
jjg@46 | 75 | //System.err.println(com.sun.tools.javac.util.Old199.getPath(fo)); |
jjg@46 | 76 | list.add(sfm.inferBinaryName(loc, fo)); |
jjg@46 | 77 | } |
jjg@46 | 78 | return list; |
jjg@46 | 79 | } |
jjg@46 | 80 | |
jjg@46 | 81 | boolean test(String testClassName) throws Exception { |
jjg@46 | 82 | String[] args = new String[options.size() + 1]; |
jjg@46 | 83 | options.toArray(args); |
jjg@46 | 84 | args[args.length - 1] = testClassName; |
jjg@164 | 85 | byte[] oldOut = runOldJavap(args); |
jjg@164 | 86 | byte[] newOut = runNewJavap(args); |
jjg@164 | 87 | boolean ok = equal(oldOut, newOut); |
jjg@46 | 88 | System.err.println((ok ? "pass" : "FAIL") + ": " + testClassName); |
jjg@46 | 89 | if (!ok && viewResults) |
jjg@46 | 90 | view(oldOut, newOut); |
jjg@46 | 91 | return ok; |
jjg@46 | 92 | } |
jjg@46 | 93 | |
jjg@164 | 94 | byte[] runOldJavap(String[] args) { |
jjg@46 | 95 | //System.err.println("OLD: " + Arrays.asList(args)); |
jjg@46 | 96 | PrintStream oldOut = System.out; |
jjg@46 | 97 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
jjg@46 | 98 | System.setOut(new PrintStream(out)); |
jjg@46 | 99 | try { |
jjg@46 | 100 | sun.tools.javap.Main.entry(args); |
jjg@46 | 101 | } finally { |
jjg@46 | 102 | System.setOut(oldOut); |
jjg@46 | 103 | } |
jjg@164 | 104 | return out.toByteArray(); |
jjg@46 | 105 | } |
jjg@46 | 106 | |
jjg@164 | 107 | byte[] runNewJavap(String[] args) { |
jjg@46 | 108 | String[] nArgs = new String[args.length + 2]; |
jjg@46 | 109 | nArgs[0] = "-XDcompat"; |
jjg@46 | 110 | nArgs[1] = "-XDignore.symbol.file"; |
jjg@46 | 111 | System.arraycopy(args, 0, nArgs, 2, args.length); |
jjg@46 | 112 | //System.err.println("NEW: " + Arrays.asList(nArgs)); |
jjg@164 | 113 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
jjg@164 | 114 | com.sun.tools.javap.Main.run(nArgs, |
jjg@164 | 115 | new PrintWriter(new OutputStreamWriter(out), true)); |
jjg@164 | 116 | return out.toByteArray(); |
jjg@46 | 117 | } |
jjg@46 | 118 | |
jjg@164 | 119 | File write(byte[] text, String suffix) throws IOException { |
jjg@164 | 120 | File f = new File("ListTest." + suffix); |
jjg@164 | 121 | FileOutputStream out = new FileOutputStream(f); |
jjg@46 | 122 | out.write(text); |
jjg@46 | 123 | out.close(); |
jjg@46 | 124 | return f; |
jjg@46 | 125 | } |
jjg@46 | 126 | |
jjg@164 | 127 | boolean equal(byte[] a1, byte[] a2) { |
jjg@164 | 128 | return Arrays.equals(a1, a2); |
jjg@164 | 129 | } |
jjg@164 | 130 | |
jjg@164 | 131 | void view(byte[] oldOut, byte[] newOut) throws Exception { |
jjg@46 | 132 | File oldFile = write(oldOut, "old"); |
jjg@46 | 133 | File newFile = write(newOut, "new"); |
jjg@46 | 134 | List<String> cmd = new ArrayList<String>(); |
jjg@46 | 135 | cmd.addAll(viewCmd); |
jjg@46 | 136 | cmd.add(oldFile.getPath()); |
jjg@46 | 137 | cmd.add(newFile.getPath()); |
jjg@46 | 138 | Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start(); |
jjg@46 | 139 | p.getOutputStream().close(); |
jjg@46 | 140 | String line; |
jjg@46 | 141 | BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
jjg@46 | 142 | while ((line = in.readLine()) != null) |
jjg@46 | 143 | System.err.println(line); |
jjg@46 | 144 | in.close(); |
jjg@46 | 145 | p.waitFor(); |
jjg@46 | 146 | } |
jjg@46 | 147 | |
jjg@46 | 148 | String testPackage = "java.lang"; |
jjg@46 | 149 | List<String> options = Arrays.asList("-v"); |
jjg@46 | 150 | boolean viewResults; |
jjg@46 | 151 | List<String> viewCmd; |
jjg@46 | 152 | } |