test/tools/javap/ListTest.java

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

mercurial