test/tools/javac/plugin/showtype/Test.java

Wed, 23 Jan 2013 20:57:40 +0000

author
vromero
date
Wed, 23 Jan 2013 20:57:40 +0000
changeset 1520
5c956be64b9e
parent 1457
064e372f273d
child 1728
43c2f7cb9c76
permissions
-rw-r--r--

8006694: temporarily workaround combo tests are causing time out in several platforms
Reviewed-by: jjg
Contributed-by: maurizio.cimadamore@oracle.com

jjg@1416 1 /*
jjg@1416 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1416 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1416 4 *
jjg@1416 5 * This code is free software; you can redistribute it and/or modify it
jjg@1416 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1416 7 * published by the Free Software Foundation.
jjg@1416 8 *
jjg@1416 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1416 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1416 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1416 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1416 13 * accompanied this code).
jjg@1416 14 *
jjg@1416 15 * You should have received a copy of the GNU General Public License version
jjg@1416 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1416 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1416 18 *
jjg@1416 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1416 20 * or visit www.oracle.com if you need additional information or have any
jjg@1416 21 * questions.
jjg@1416 22 */
jjg@1416 23
jjg@1416 24 /**
jjg@1416 25 * @test
jjg@1457 26 * @bug 8001098 8004961
jjg@1416 27 * @summary Provide a simple light-weight "plug-in" mechanism for javac
jjg@1416 28 */
jjg@1416 29
jjg@1457 30 import java.io.File;
jjg@1457 31 import java.io.FileWriter;
jjg@1457 32 import java.io.IOException;
jjg@1457 33 import java.io.PrintWriter;
jjg@1457 34 import java.io.StringWriter;
jjg@1457 35 import java.nio.charset.Charset;
jjg@1457 36 import java.nio.file.Files;
jjg@1457 37 import java.util.Arrays;
jjg@1457 38 import java.util.List;
jjg@1457 39 import java.util.Locale;
jjg@1457 40 import java.util.Objects;
jjg@1457 41 import javax.tools.JavaCompiler;
jjg@1457 42 import javax.tools.JavaFileManager;
jjg@1457 43 import javax.tools.JavaFileObject;
jjg@1457 44 import javax.tools.StandardJavaFileManager;
jjg@1457 45 import javax.tools.StandardLocation;
jjg@1457 46 import javax.tools.ToolProvider;
jjg@1457 47
jjg@1416 48 public class Test {
jjg@1416 49 public static void main(String... args) throws Exception {
jjg@1416 50 new Test().run();
jjg@1416 51 }
jjg@1416 52
jjg@1416 53 final File testSrc;
jjg@1416 54 final File pluginSrc;
jjg@1416 55 final File pluginClasses ;
jjg@1416 56 final File pluginJar;
jjg@1416 57 final List<String> ref1;
jjg@1416 58 final List<String> ref2;
jjg@1416 59 final JavaCompiler compiler;
jjg@1416 60 final StandardJavaFileManager fm;
jjg@1416 61
jjg@1416 62 Test() throws Exception {
jjg@1416 63 testSrc = new File(System.getProperty("test.src"));
jjg@1416 64 pluginSrc = new File(testSrc, "ShowTypePlugin.java");
jjg@1416 65 pluginClasses = new File("plugin");
jjg@1416 66 pluginJar = new File("plugin.jar");
jjg@1416 67 ref1 = readFile(testSrc, "Identifiers.out");
jjg@1416 68 ref2 = readFile(testSrc, "Identifiers_PI.out");
jjg@1416 69 compiler = ToolProvider.getSystemJavaCompiler();
jjg@1416 70 fm = compiler.getStandardFileManager(null, null, null);
jjg@1416 71 }
jjg@1416 72
jjg@1416 73 void run() throws Exception {
jjg@1416 74 // compile the plugin explicitly, to a non-standard directory
jjg@1416 75 // so that we don't find it on the wrong path by accident
jjg@1416 76 pluginClasses.mkdirs();
jjg@1416 77 compile("-d", pluginClasses.getPath(), pluginSrc.getPath());
jjg@1416 78 writeFile(new File(pluginClasses, "META-INF/services/com.sun.source.util.Plugin"),
jjg@1416 79 "ShowTypePlugin\n");
jjg@1416 80 jar("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), ".");
jjg@1416 81
jjg@1416 82 testCommandLine("-Xplugin:showtype", ref1);
jjg@1416 83 testCommandLine("-Xplugin:showtype PI", ref2);
jjg@1416 84 testAPI("-Xplugin:showtype", ref1);
jjg@1416 85 testAPI("-Xplugin:showtype PI", ref2);
jjg@1416 86
jjg@1416 87 if (errors > 0)
jjg@1416 88 throw new Exception(errors + " errors occurred");
jjg@1416 89 }
jjg@1416 90
jjg@1416 91 void testAPI(String opt, List<String> ref) throws Exception {
jjg@1416 92 File identifiers = new File(testSrc, "Identifiers.java");
jjg@1416 93 fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, Arrays.asList(pluginJar));
jjg@1416 94 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
jjg@1416 95 List<String> options = Arrays.asList(opt);
jjg@1416 96 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers);
jjg@1416 97
jjg@1416 98 System.err.println("test api: " + options + " " + files);
jjg@1416 99
jjg@1416 100 StringWriter sw = new StringWriter();
jjg@1416 101 PrintWriter pw = new PrintWriter(sw);
jjg@1416 102 boolean ok = compiler.getTask(pw, fm, null, options, null, files).call();
jjg@1416 103 String out = sw.toString();
jjg@1416 104 System.err.println(out);
jjg@1416 105 if (!ok)
jjg@1416 106 error("testCommandLine: compilation failed");
jjg@1416 107 checkOutput(out, ref);
jjg@1416 108 }
jjg@1416 109
jjg@1416 110 void testCommandLine(String opt, List<String> ref) {
jjg@1416 111 File identifiers = new File(testSrc, "Identifiers.java");
jjg@1416 112 String[] args = {
jjg@1416 113 "-d", ".",
jjg@1416 114 "-processorpath", pluginJar.getPath(),
jjg@1416 115 opt,
jjg@1416 116 identifiers.getPath() };
jjg@1416 117
jjg@1416 118 System.err.println("test command line: " + Arrays.asList(args));
jjg@1416 119
jjg@1416 120 StringWriter sw = new StringWriter();
jjg@1416 121 PrintWriter pw = new PrintWriter(sw);
jjg@1416 122 int rc = com.sun.tools.javac.Main.compile(args, pw);
jjg@1416 123 String out = sw.toString();
jjg@1416 124 System.err.println(out);
jjg@1416 125 if (rc != 0)
jjg@1416 126 error("testCommandLine: compilation failed");
jjg@1416 127 checkOutput(out, ref);
jjg@1416 128 }
jjg@1416 129
jjg@1416 130 private void checkOutput(String out, List<String> ref) {
jjg@1416 131 List<String> lines = Arrays.asList(out
jjg@1416 132 .replaceAll(".*?([A-Za-z.]+:[0-9]+: .*)", "$1") // remove file directory
jjg@1416 133 .split("[\r\n]+")); // allow for newline formats
jjg@1416 134 if (!lines.equals(ref)) {
jjg@1416 135 error("unexpected output");
jjg@1416 136 }
jjg@1416 137 }
jjg@1416 138
jjg@1416 139 private void compile(String... args) throws Exception {
jjg@1416 140 System.err.println("compile: " + Arrays.asList(args));
jjg@1416 141 int rc = com.sun.tools.javac.Main.compile(args);
jjg@1416 142 if (rc != 0)
jjg@1416 143 throw new Exception("compiled failed, rc=" + rc);
jjg@1416 144 }
jjg@1416 145
jjg@1416 146 private void jar(String... args) throws Exception {
jjg@1416 147 System.err.println("jar: " + Arrays.asList(args));
jjg@1416 148 boolean ok = new sun.tools.jar.Main(System.out, System.err, "jar").run(args);
jjg@1416 149 if (!ok)
jjg@1416 150 throw new Exception("jar failed");
jjg@1416 151 }
jjg@1416 152
jjg@1416 153 private List<String> readFile(File dir, String name) throws IOException {
jjg@1416 154 return Files.readAllLines(new File(dir, name).toPath(), Charset.defaultCharset());
jjg@1416 155 }
jjg@1416 156
jjg@1416 157 private void writeFile(File f, String body) throws IOException {
jjg@1416 158 f.getParentFile().mkdirs();
jjg@1416 159 try (FileWriter out = new FileWriter(f)) {
jjg@1416 160 out.write(body);
jjg@1416 161 }
jjg@1416 162 }
jjg@1416 163
jjg@1416 164 private void error(String msg) {
jjg@1416 165 System.err.println(msg);
jjg@1416 166 errors++;
jjg@1416 167 }
jjg@1416 168
jjg@1416 169 int errors;
jjg@1416 170 }

mercurial