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