PlatformProviderTest.java revision 3294:9adfb22ff08f
1109998Smarkm/* 2296341Sdelphij * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3296341Sdelphij * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4296341Sdelphij * 5109998Smarkm * This code is free software; you can redistribute it and/or modify it 6109998Smarkm * under the terms of the GNU General Public License version 2 only, as 7109998Smarkm * published by the Free Software Foundation. 8109998Smarkm * 9109998Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT 10109998Smarkm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11109998Smarkm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12109998Smarkm * version 2 for more details (a copy is included in the LICENSE file that 13109998Smarkm * accompanied this code). 14296341Sdelphij * 15109998Smarkm * You should have received a copy of the GNU General Public License version 16109998Smarkm * 2 along with this work; if not, write to the Free Software Foundation, 17109998Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18109998Smarkm * 19109998Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20109998Smarkm * or visit www.oracle.com if you need additional information or have any 21109998Smarkm * questions. 22109998Smarkm */ 23109998Smarkm 24109998Smarkm/** 25109998Smarkm * @test 26109998Smarkm * @bug 8072480 27109998Smarkm * @summary Ensure all methods of PlatformProvider are called correctly, and their result is used 28109998Smarkm * correctly. 29109998Smarkm * @library /tools/lib 30109998Smarkm * @build ToolBox PlatformProviderTest 31109998Smarkm * @modules jdk.compiler/com.sun.tools.javac.api 32109998Smarkm * jdk.compiler/com.sun.tools.javac.main 33109998Smarkm * jdk.compiler/com.sun.tools.javac.platform 34109998Smarkm * jdk.compiler/com.sun.tools.javac.util 35109998Smarkm * jdk.jdeps/com.sun.tools.javap 36109998Smarkm * @run main/othervm PlatformProviderTest 37109998Smarkm */ 38109998Smarkm 39109998Smarkmimport java.io.IOException; 40109998Smarkmimport java.io.Writer; 41109998Smarkmimport java.lang.reflect.Field; 42109998Smarkmimport java.nio.file.Files; 43109998Smarkmimport java.nio.file.Path; 44109998Smarkmimport java.nio.file.Paths; 45109998Smarkmimport java.util.ArrayList; 46109998Smarkmimport java.util.Arrays; 47109998Smarkmimport java.util.Collection; 48109998Smarkmimport java.util.Collections; 49109998Smarkmimport java.util.List; 50109998Smarkmimport java.util.Map; 51109998Smarkmimport java.util.Set; 52109998Smarkm 53109998Smarkmimport javax.annotation.processing.AbstractProcessor; 54109998Smarkmimport javax.annotation.processing.Processor; 55109998Smarkmimport javax.annotation.processing.RoundEnvironment; 56109998Smarkmimport javax.annotation.processing.SupportedAnnotationTypes; 57109998Smarkmimport javax.annotation.processing.SupportedOptions; 58109998Smarkmimport javax.lang.model.SourceVersion; 59109998Smarkmimport javax.lang.model.element.TypeElement; 60109998Smarkmimport javax.tools.JavaCompiler; 61160814Ssimonimport javax.tools.StandardJavaFileManager; 62109998Smarkmimport javax.tools.StandardLocation; 63109998Smarkmimport javax.tools.ToolProvider; 64109998Smarkm 65109998Smarkmimport com.sun.source.util.JavacTask; 66296341Sdelphijimport com.sun.source.util.Plugin; 67296341Sdelphijimport com.sun.tools.javac.platform.PlatformDescription; 68109998Smarkmimport com.sun.tools.javac.platform.PlatformProvider; 69296341Sdelphijimport com.sun.tools.javac.util.Log; 70296341Sdelphij 71296341Sdelphijpublic class PlatformProviderTest implements PlatformProvider { 72296341Sdelphij 73296341Sdelphij public static void main(String... args) throws IOException { 74296341Sdelphij new PlatformProviderTest().run(); 75296341Sdelphij } 76296341Sdelphij 77296341Sdelphij void run() throws IOException { 78296341Sdelphij Path registration = Paths.get(System.getProperty("test.classes"), 79296341Sdelphij "META-INF", 80109998Smarkm "services", 81296341Sdelphij "com.sun.tools.javac.platform.PlatformProvider"); 82296341Sdelphij Files.createDirectories(registration.getParent()); 83109998Smarkm try (Writer metaInf = Files.newBufferedWriter(registration)) { 84296341Sdelphij metaInf.write(PlatformProviderTest.class.getName()); 85296341Sdelphij } 86109998Smarkm 87296341Sdelphij doTest("name", ""); 88296341Sdelphij doTest("name:param", "param"); 89296341Sdelphij doTestFailure(); 90296341Sdelphij } 91296341Sdelphij 92296341Sdelphij void doTest(String platformSpec, String expectedParameter) { 93296341Sdelphij ToolBox tb = new ToolBox(); 94296341Sdelphij ToolBox.Result result = 95296341Sdelphij tb.new JavacTask(ToolBox.Mode.EXEC) 96296341Sdelphij .outdir(".") 97296341Sdelphij .options("-J-classpath", 98296341Sdelphij "-J" + System.getProperty("test.classes"), 99296341Sdelphij "-J-XaddExports:jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED," + 100296341Sdelphij "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", 101296341Sdelphij "-XDrawDiagnostics", 102296341Sdelphij "-release", 103296341Sdelphij platformSpec, 104296341Sdelphij System.getProperty("test.src") + "/PlatformProviderTestSource.java") 105296341Sdelphij .run(); 106109998Smarkm 107109998Smarkm List<String> expectedOutput = 108296341Sdelphij Arrays.asList("getSupportedPlatformNames", 109296341Sdelphij "getPlatform(name, " + expectedParameter + ")", 110109998Smarkm "getSourceVersion", 111296341Sdelphij "getTargetVersion", 112296341Sdelphij "getPlatformPath", 113296341Sdelphij "testPlugin: [testPluginKey=testPluginValue]", 114296341Sdelphij "process: {testAPKey=testAPValue}", 115296341Sdelphij "process: {testAPKey=testAPValue}", 116296341Sdelphij "PlatformProviderTestSource.java:4:49: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>", 117296341Sdelphij "compiler.misc.count.warn", 118296341Sdelphij "close"); 119296341Sdelphij List<String> actualOutput = result.getOutputLines(ToolBox.OutputKind.STDERR); 120296341Sdelphij result.writeAll(); 121296341Sdelphij if (!expectedOutput.equals(actualOutput)) { 122109998Smarkm throw new AssertionError( "Expected output: " + expectedOutput + 123296341Sdelphij "; actual output: " + actualOutput); 124296341Sdelphij } 125296341Sdelphij result.writeAll(); 126296341Sdelphij } 127109998Smarkm 128296341Sdelphij void doTestFailure() { 129296341Sdelphij ToolBox tb = new ToolBox(); 130296341Sdelphij ToolBox.Result result = 131296341Sdelphij tb.new JavacTask(ToolBox.Mode.EXEC) 132296341Sdelphij .outdir(".") 133296341Sdelphij .options("-J-classpath", 134296341Sdelphij "-J" + System.getProperty("test.classes"), 135296341Sdelphij "-J-XaddExports:jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED," + 136296341Sdelphij "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", 137296341Sdelphij "-release", 138296341Sdelphij "fail", 139296341Sdelphij System.getProperty("test.src") + "/PlatformProviderTestSource.java") 140296341Sdelphij .run(ToolBox.Expect.FAIL); 141109998Smarkm 142109998Smarkm List<String> expectedOutput = 143296341Sdelphij Arrays.asList("getSupportedPlatformNames", 144296341Sdelphij "getPlatform(fail, )", 145296341Sdelphij "javac: javac.err.unsupported.release.version", 146109998Smarkm "javac.msg.usage"); 147109998Smarkm List<String> actualOutput = result.getOutputLines(ToolBox.OutputKind.STDERR); 148109998Smarkm result.writeAll(); 149296341Sdelphij if (!expectedOutput.equals(actualOutput)) { 150296341Sdelphij throw new AssertionError( "Expected output: " + expectedOutput + 151296341Sdelphij "; actual output: " + actualOutput); 152296341Sdelphij } 153296341Sdelphij result.writeAll(); 154296341Sdelphij } 155109998Smarkm 156109998Smarkm @Override 157296341Sdelphij public Iterable<String> getSupportedPlatformNames() { 158296341Sdelphij System.err.println("getSupportedPlatformNames"); 159296341Sdelphij return Arrays.asList("name", "fail"); 160296341Sdelphij } 161296341Sdelphij 162296341Sdelphij @Override 163296341Sdelphij public PlatformDescription getPlatform(String platformName, String options) throws PlatformNotSupported { 164296341Sdelphij System.err.println("getPlatform(" + platformName + ", " + options + ")"); 165296341Sdelphij 166109998Smarkm if ("fail".equals(platformName)) { 167296341Sdelphij throw new PlatformNotSupported(); 168296341Sdelphij } 169296341Sdelphij 170296341Sdelphij return new DescriptionImpl(); 171296341Sdelphij } 172296341Sdelphij 173296341Sdelphij static { 174296341Sdelphij try { 175109998Smarkm Field useRawMessages = Log.class.getDeclaredField("useRawMessages"); 176296341Sdelphij 177296341Sdelphij useRawMessages.setAccessible(true); 178296341Sdelphij useRawMessages.set(null, true); 179296341Sdelphij } catch (Exception ex) { 180296341Sdelphij throw new IllegalStateException(ex); 181296341Sdelphij } 182296341Sdelphij } 183296341Sdelphij 184296341Sdelphij class DescriptionImpl implements PlatformDescription { 185109998Smarkm 186296341Sdelphij private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 187296341Sdelphij private final StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); 188296341Sdelphij 189296341Sdelphij 190296341Sdelphij @Override 191296341Sdelphij public Collection<Path> getPlatformPath() { 192296341Sdelphij System.err.println("getPlatformPath"); 193296341Sdelphij List<Path> result = new ArrayList<>(); 194296341Sdelphij fm.getLocationAsPaths(StandardLocation.PLATFORM_CLASS_PATH).forEach(p -> { result.add(p); }); 195109998Smarkm return result; 196109998Smarkm } 197296341Sdelphij 198296341Sdelphij @Override 199296341Sdelphij public String getSourceVersion() { 200296341Sdelphij System.err.println("getSourceVersion"); 201296341Sdelphij return "8"; 202109998Smarkm } 203296341Sdelphij 204296341Sdelphij @Override 205296341Sdelphij public String getTargetVersion() { 206296341Sdelphij System.err.println("getTargetVersion"); 207296341Sdelphij return "8"; 208296341Sdelphij } 209296341Sdelphij 210296341Sdelphij @Override 211296341Sdelphij public List<PluginInfo<Processor>> getAnnotationProcessors() { 212296341Sdelphij return Arrays.asList(new PluginInfo<Processor>() { 213296341Sdelphij @Override 214296341Sdelphij public String getName() { 215109998Smarkm return "test"; 216109998Smarkm } 217109998Smarkm @Override 218109998Smarkm public Map<String, String> getOptions() { 219296341Sdelphij return Collections.singletonMap("testAPKey", "testAPValue"); 220296341Sdelphij } 221296341Sdelphij @Override 222296341Sdelphij public Processor getPlugin() { 223296341Sdelphij return new ProcessorImpl(); 224296341Sdelphij } 225109998Smarkm }); 226109998Smarkm } 227296341Sdelphij 228296341Sdelphij @Override 229296341Sdelphij public List<PluginInfo<Plugin>> getPlugins() { 230109998Smarkm return Arrays.asList(new PluginInfo<Plugin>() { 231109998Smarkm @Override 232296341Sdelphij public String getName() { 233296341Sdelphij return "testPlugin"; 234296341Sdelphij } 235109998Smarkm @Override 236296341Sdelphij public Map<String, String> getOptions() { 237296341Sdelphij return Collections.singletonMap("testPluginKey", "testPluginValue"); 238296341Sdelphij } 239296341Sdelphij @Override 240109998Smarkm public Plugin getPlugin() { 241109998Smarkm return new PluginImpl(); 242296341Sdelphij } 243296341Sdelphij }); 244296341Sdelphij } 245296341Sdelphij 246296341Sdelphij @Override 247296341Sdelphij public List<String> getAdditionalOptions() { 248296341Sdelphij return Arrays.asList("-Xlint:rawtypes", "-XDrawDiagnostics"); 249296341Sdelphij } 250109998Smarkm 251109998Smarkm @Override 252296341Sdelphij public void close() throws IOException { 253296341Sdelphij System.err.println("close"); 254296341Sdelphij fm.close(); 255296341Sdelphij } 256296341Sdelphij 257296341Sdelphij } 258296341Sdelphij 259296341Sdelphij @SupportedAnnotationTypes("*") 260109998Smarkm @SupportedOptions("testAPKey") 261109998Smarkm class ProcessorImpl extends AbstractProcessor { 262296341Sdelphij 263296341Sdelphij @Override 264296341Sdelphij public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 265296341Sdelphij System.err.println("process: " + processingEnv.getOptions()); 266109998Smarkm return true; 267109998Smarkm } 268296341Sdelphij 269296341Sdelphij @Override 270296341Sdelphij public SourceVersion getSupportedSourceVersion() { 271296341Sdelphij return SourceVersion.latest(); 272109998Smarkm } 273109998Smarkm 274296341Sdelphij } 275296341Sdelphij 276296341Sdelphij class PluginImpl implements Plugin { 277296341Sdelphij 278109998Smarkm @Override 279109998Smarkm public String getName() { 280296341Sdelphij return "testPluginName"; 281296341Sdelphij } 282296341Sdelphij 283296341Sdelphij @Override 284109998Smarkm public void init(JavacTask task, String... args) { 285109998Smarkm System.err.println("testPlugin: " + Arrays.toString(args)); 286296341Sdelphij } 287296341Sdelphij 288296341Sdelphij } 289296341Sdelphij} 290109998Smarkm