Thu, 19 Dec 2013 11:38:45 -0500
8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
Summary: Fix NoStringToLower test to only enforce ban on String.toLowerCase on langtools classes
Reviewed-by: vromero, jfranck
Contributed-by: paul.govereau@oracle.com
1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /**
25 * @test
26 * @bug 8001098 8004961 8004082
27 * @summary Provide a simple light-weight "plug-in" mechanism for javac
28 */
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.io.StringWriter;
35 import java.nio.charset.Charset;
36 import java.nio.file.Files;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.Objects;
41 import javax.tools.JavaCompiler;
42 import javax.tools.JavaFileManager;
43 import javax.tools.JavaFileObject;
44 import javax.tools.StandardJavaFileManager;
45 import javax.tools.StandardLocation;
46 import javax.tools.ToolProvider;
48 public class Test {
49 public static void main(String... args) throws Exception {
50 new Test().run();
51 }
53 final File testSrc;
54 final File pluginSrc;
55 final File pluginClasses ;
56 final File pluginJar;
57 final List<String> ref1;
58 final List<String> ref2;
59 final JavaCompiler compiler;
60 final StandardJavaFileManager fm;
62 Test() throws Exception {
63 testSrc = new File(System.getProperty("test.src"));
64 pluginSrc = new File(testSrc, "ShowTypePlugin.java");
65 pluginClasses = new File("plugin");
66 pluginJar = new File("plugin.jar");
67 ref1 = readFile(testSrc, "Identifiers.out");
68 ref2 = readFile(testSrc, "Identifiers_PI.out");
69 compiler = ToolProvider.getSystemJavaCompiler();
70 fm = compiler.getStandardFileManager(null, null, null);
71 }
73 void run() throws Exception {
74 // compile the plugin explicitly, to a non-standard directory
75 // so that we don't find it on the wrong path by accident
76 pluginClasses.mkdirs();
77 compile("-d", pluginClasses.getPath(), pluginSrc.getPath());
78 writeFile(new File(pluginClasses, "META-INF/services/com.sun.source.util.Plugin"),
79 "ShowTypePlugin\n");
80 jar("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), ".");
82 testCommandLine("-Xplugin:showtype", ref1);
83 testCommandLine("-Xplugin:showtype PI", ref2);
84 testAPI("-Xplugin:showtype", ref1);
85 testAPI("-Xplugin:showtype PI", ref2);
87 if (errors > 0)
88 throw new Exception(errors + " errors occurred");
89 }
91 void testAPI(String opt, List<String> ref) throws Exception {
92 File identifiers = new File(testSrc, "Identifiers.java");
93 fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, Arrays.asList(pluginJar));
94 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
95 List<String> options = Arrays.asList(opt);
96 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers);
98 System.err.println("test api: " + options + " " + files);
100 StringWriter sw = new StringWriter();
101 PrintWriter pw = new PrintWriter(sw);
102 boolean ok = compiler.getTask(pw, fm, null, options, null, files).call();
103 String out = sw.toString();
104 System.err.println(out);
105 if (!ok)
106 error("testCommandLine: compilation failed");
107 checkOutput(out, ref);
108 }
110 void testCommandLine(String opt, List<String> ref) {
111 File identifiers = new File(testSrc, "Identifiers.java");
112 String[] args = {
113 "-d", ".",
114 "-processorpath", pluginJar.getPath(),
115 opt,
116 identifiers.getPath() };
118 System.err.println("test command line: " + Arrays.asList(args));
120 StringWriter sw = new StringWriter();
121 PrintWriter pw = new PrintWriter(sw);
122 int rc = com.sun.tools.javac.Main.compile(args, pw);
123 String out = sw.toString();
124 System.err.println(out);
125 if (rc != 0)
126 error("testCommandLine: compilation failed");
127 checkOutput(out, ref);
128 }
130 private void checkOutput(String out, List<String> ref) {
131 List<String> lines = Arrays.asList(out
132 .replaceAll(".*?([A-Za-z.]+:[0-9]+: .*)", "$1") // remove file directory
133 .split("[\r\n]+")); // allow for newline formats
134 if (!lines.equals(ref)) {
135 error("unexpected output");
136 }
137 }
139 private void compile(String... args) throws Exception {
140 System.err.println("compile: " + Arrays.asList(args));
141 int rc = com.sun.tools.javac.Main.compile(args);
142 if (rc != 0)
143 throw new Exception("compiled failed, rc=" + rc);
144 }
146 private void jar(String... args) throws Exception {
147 System.err.println("jar: " + Arrays.asList(args));
148 boolean ok = new sun.tools.jar.Main(System.out, System.err, "jar").run(args);
149 if (!ok)
150 throw new Exception("jar failed");
151 }
153 private List<String> readFile(File dir, String name) throws IOException {
154 return Files.readAllLines(new File(dir, name).toPath(), Charset.defaultCharset());
155 }
157 private void writeFile(File f, String body) throws IOException {
158 f.getParentFile().mkdirs();
159 try (FileWriter out = new FileWriter(f)) {
160 out.write(body);
161 }
162 }
164 private void error(String msg) {
165 System.err.println(msg);
166 errors++;
167 }
169 int errors;
170 }