PlatformProviderTest.java revision 2973:0e8fa3249327
1/* 2 * Copyright (c) 2015, 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 */ 23 24/** 25 * @test 26 * @bug 8072480 27 * @summary Ensure all methods of PlatformProvider are called correctly, and their result is used 28 * correctly. 29 * @library /tools/lib 30 * @build ToolBox PlatformProviderTest 31 * @run main/othervm PlatformProviderTest 32 */ 33 34import java.io.IOException; 35import java.io.Writer; 36import java.lang.reflect.Field; 37import java.nio.file.Files; 38import java.nio.file.Path; 39import java.nio.file.Paths; 40import java.util.ArrayList; 41import java.util.Arrays; 42import java.util.Collection; 43import java.util.Collections; 44import java.util.List; 45import java.util.Map; 46import java.util.Set; 47 48import javax.annotation.processing.AbstractProcessor; 49import javax.annotation.processing.Processor; 50import javax.annotation.processing.RoundEnvironment; 51import javax.annotation.processing.SupportedAnnotationTypes; 52import javax.annotation.processing.SupportedOptions; 53import javax.lang.model.SourceVersion; 54import javax.lang.model.element.TypeElement; 55import javax.tools.JavaCompiler; 56import javax.tools.StandardJavaFileManager; 57import javax.tools.StandardLocation; 58import javax.tools.ToolProvider; 59 60import com.sun.source.util.JavacTask; 61import com.sun.source.util.Plugin; 62import com.sun.tools.javac.platform.PlatformDescription; 63import com.sun.tools.javac.platform.PlatformProvider; 64import com.sun.tools.javac.util.Log; 65 66public class PlatformProviderTest implements PlatformProvider { 67 68 public static void main(String... args) throws IOException { 69 new PlatformProviderTest().run(); 70 } 71 72 void run() throws IOException { 73 Path registration = Paths.get(System.getProperty("test.classes"), 74 "META-INF", 75 "services", 76 "com.sun.tools.javac.platform.PlatformProvider"); 77 Files.createDirectories(registration.getParent()); 78 try (Writer metaInf = Files.newBufferedWriter(registration)) { 79 metaInf.write(PlatformProviderTest.class.getName()); 80 } 81 82 doTest("name", ""); 83 doTest("name:param", "param"); 84 doTestFailure(); 85 } 86 87 void doTest(String platformSpec, String expectedParameter) { 88 ToolBox tb = new ToolBox(); 89 ToolBox.Result result = 90 tb.new JavacTask(ToolBox.Mode.EXEC) 91 .options("-J-classpath", 92 "-J" + System.getProperty("test.classes"), 93 "-XDrawDiagnostics", 94 "-release", 95 platformSpec, 96 System.getProperty("test.src") + "/PlatformProviderTestSource.java") 97 .run(); 98 99 List<String> expectedOutput = 100 Arrays.asList("getSupportedPlatformNames", 101 "getPlatform(name, " + expectedParameter + ")", 102 "getSourceVersion", 103 "getTargetVersion", 104 "getPlatformPath", 105 "testPlugin: [testPluginKey=testPluginValue]", 106 "process: {testAPKey=testAPValue}", 107 "process: {testAPKey=testAPValue}", 108 "PlatformProviderTestSource.java:4:49: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>", 109 "compiler.misc.count.warn", 110 "close"); 111 List<String> actualOutput = result.getOutputLines(ToolBox.OutputKind.STDERR); 112 result.writeAll(); 113 if (!expectedOutput.equals(actualOutput)) { 114 throw new AssertionError( "Expected output: " + expectedOutput + 115 "; actual output: " + actualOutput); 116 } 117 result.writeAll(); 118 } 119 120 void doTestFailure() { 121 ToolBox tb = new ToolBox(); 122 ToolBox.Result result = 123 tb.new JavacTask(ToolBox.Mode.EXEC) 124 .options("-J-classpath", 125 "-J" + System.getProperty("test.classes"), 126 "-release", 127 "fail", 128 System.getProperty("test.src") + "/PlatformProviderTestSource.java") 129 .run(ToolBox.Expect.FAIL); 130 131 List<String> expectedOutput = 132 Arrays.asList("getSupportedPlatformNames", 133 "getPlatform(fail, )", 134 "javac: javac.err.unsupported.release.version", 135 "javac.msg.usage"); 136 List<String> actualOutput = result.getOutputLines(ToolBox.OutputKind.STDERR); 137 result.writeAll(); 138 if (!expectedOutput.equals(actualOutput)) { 139 throw new AssertionError( "Expected output: " + expectedOutput + 140 "; actual output: " + actualOutput); 141 } 142 result.writeAll(); 143 } 144 145 @Override 146 public Iterable<String> getSupportedPlatformNames() { 147 System.err.println("getSupportedPlatformNames"); 148 return Arrays.asList("name", "fail"); 149 } 150 151 @Override 152 public PlatformDescription getPlatform(String platformName, String options) throws PlatformNotSupported { 153 System.err.println("getPlatform(" + platformName + ", " + options + ")"); 154 155 if ("fail".equals(platformName)) { 156 throw new PlatformNotSupported(); 157 } 158 159 return new DescriptionImpl(); 160 } 161 162 static { 163 try { 164 Field useRawMessages = Log.class.getDeclaredField("useRawMessages"); 165 166 useRawMessages.setAccessible(true); 167 useRawMessages.set(null, true); 168 } catch (Exception ex) { 169 throw new IllegalStateException(ex); 170 } 171 } 172 173 class DescriptionImpl implements PlatformDescription { 174 175 private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 176 private final StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); 177 178 179 @Override 180 public Collection<Path> getPlatformPath() { 181 System.err.println("getPlatformPath"); 182 List<Path> result = new ArrayList<>(); 183 fm.getLocationAsPaths(StandardLocation.PLATFORM_CLASS_PATH).forEach(p -> { result.add(p); }); 184 return result; 185 } 186 187 @Override 188 public String getSourceVersion() { 189 System.err.println("getSourceVersion"); 190 return "8"; 191 } 192 193 @Override 194 public String getTargetVersion() { 195 System.err.println("getTargetVersion"); 196 return "8"; 197 } 198 199 @Override 200 public List<PluginInfo<Processor>> getAnnotationProcessors() { 201 return Arrays.asList(new PluginInfo<Processor>() { 202 @Override 203 public String getName() { 204 return "test"; 205 } 206 @Override 207 public Map<String, String> getOptions() { 208 return Collections.singletonMap("testAPKey", "testAPValue"); 209 } 210 @Override 211 public Processor getPlugin() { 212 return new ProcessorImpl(); 213 } 214 }); 215 } 216 217 @Override 218 public List<PluginInfo<Plugin>> getPlugins() { 219 return Arrays.asList(new PluginInfo<Plugin>() { 220 @Override 221 public String getName() { 222 return "testPlugin"; 223 } 224 @Override 225 public Map<String, String> getOptions() { 226 return Collections.singletonMap("testPluginKey", "testPluginValue"); 227 } 228 @Override 229 public Plugin getPlugin() { 230 return new PluginImpl(); 231 } 232 }); 233 } 234 235 @Override 236 public List<String> getAdditionalOptions() { 237 return Arrays.asList("-Xlint:rawtypes", "-XDrawDiagnostics"); 238 } 239 240 @Override 241 public void close() throws IOException { 242 System.err.println("close"); 243 fm.close(); 244 } 245 246 } 247 248 @SupportedAnnotationTypes("*") 249 @SupportedOptions("testAPKey") 250 class ProcessorImpl extends AbstractProcessor { 251 252 @Override 253 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 254 System.err.println("process: " + processingEnv.getOptions()); 255 return true; 256 } 257 258 @Override 259 public SourceVersion getSupportedSourceVersion() { 260 return SourceVersion.latest(); 261 } 262 263 } 264 265 class PluginImpl implements Plugin { 266 267 @Override 268 public String getName() { 269 return "testPluginName"; 270 } 271 272 @Override 273 public void init(JavacTask task, String... args) { 274 System.err.println("testPlugin: " + Arrays.toString(args)); 275 } 276 277 } 278} 279