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--class-path=" + System.getProperty("test.classes"),
 101                            "-J--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED",
 102                            "-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
 103                            "-XDrawDiagnostics",
 104                            "-release",
 105                            platformSpec,
 106                            System.getProperty("test.src") + "/PlatformProviderTestSource.java")
 107                   .run();
 108 
 109         List<String> expectedOutput =
 110                 Arrays.asList("getSupportedPlatformNames",
 111                               "getPlatform(name, " + expectedParameter + ")",
 112                               "getSourceVersion",
 113                               "getTargetVersion",
 114                               "getPlatformPath",
 115                               "testPlugin: [testPluginKey=testPluginValue]",
 116                               "process: {testAPKey=testAPValue}",
 117                               "process: {testAPKey=testAPValue}",
 118                               "PlatformProviderTestSource.java:4:49: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>",
 119                               "compiler.misc.count.warn",
 120                               "close");
 121         List<String> actualOutput = result.getOutputLines(Task.OutputKind.STDERR);
 122         result.writeAll();
 123         if (!expectedOutput.equals(actualOutput)) {
 124             throw new AssertionError(  "Expected output: " + expectedOutput +
 125                                      "; actual output: " + actualOutput);
 126         }
 127         result.writeAll();
 128     }
 129 
 130     void doTestFailure() {
 131         ToolBox tb = new ToolBox();
 132         Task.Result result =
 133                 new JavacTask(tb, Task.Mode.EXEC)
 134                   .outdir(".")
 135                   .options("-J--class-path=" + System.getProperty("test.classes"),
 136                            "-J--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED",
 137                            "-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
 138                            "-release",
 139                            "fail",
 140                            System.getProperty("test.src") + "/PlatformProviderTestSource.java")
 141                   .run(Task.Expect.FAIL);
 142 
 143         List<String> expectedOutput =
 144                 Arrays.asList("getSupportedPlatformNames",
 145                               "getPlatform(fail, )",
 146                               "javac: javac.err.unsupported.release.version",
 147                               "javac.msg.usage");
 148         List<String> actualOutput = result.getOutputLines(Task.OutputKind.STDERR);
 149         result.writeAll();
 150         if (!expectedOutput.equals(actualOutput)) {
 151             throw new AssertionError(  "Expected output: " + expectedOutput +
 152                                      "; actual output: " + actualOutput);
 153         }
 154         result.writeAll();
 155     }
 156 
 157     @Override
 158     public Iterable<String> getSupportedPlatformNames() {
 159         System.err.println("getSupportedPlatformNames");
 160         return Arrays.asList("name", "fail");
 161     }
 162 
 163     @Override
 164     public PlatformDescription getPlatform(String platformName, String options) throws PlatformNotSupported {
 165         System.err.println("getPlatform(" + platformName + ", " + options + ")");
 166 
 167         if ("fail".equals(platformName)) {
 168             throw new PlatformNotSupported();
 169         }
 170 
 171         return new DescriptionImpl();
 172     }
 173 
 174     static {
 175         try {
 176             Field useRawMessages = Log.class.getDeclaredField("useRawMessages");
 177 
 178             useRawMessages.setAccessible(true);
 179             useRawMessages.set(null, true);
 180         } catch (Exception ex) {
 181             throw new IllegalStateException(ex);
 182         }
 183     }
 184 
 185     class DescriptionImpl implements PlatformDescription {
 186 
 187         private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 188         private final StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
 189 
 190 
 191         @Override
 192         public Collection<Path> getPlatformPath() {
 193             System.err.println("getPlatformPath");
 194             List<Path> result = new ArrayList<>();
 195             fm.getLocationAsPaths(StandardLocation.PLATFORM_CLASS_PATH).forEach(p -> { result.add(p); });
 196             return result;
 197         }
 198 
 199         @Override
 200         public String getSourceVersion() {
 201             System.err.println("getSourceVersion");
 202             return "8";
 203         }
 204 
 205         @Override
 206         public String getTargetVersion() {
 207             System.err.println("getTargetVersion");
 208             return "8";
 209         }
 210 
 211         @Override
 212         public List<PluginInfo<Processor>> getAnnotationProcessors() {
 213             return Arrays.asList(new PluginInfo<Processor>() {
 214                 @Override
 215                 public String getName() {
 216                     return "test";
 217                 }
 218                 @Override
 219                 public Map<String, String> getOptions() {
 220                     return Collections.singletonMap("testAPKey", "testAPValue");
 221                 }
 222                 @Override
 223                 public Processor getPlugin() {
 224                     return new ProcessorImpl();
 225                 }
 226             });
 227         }
 228 
 229         @Override
 230         public List<PluginInfo<Plugin>> getPlugins() {
 231             return Arrays.asList(new PluginInfo<Plugin>() {
 232                 @Override
 233                 public String getName() {
 234                     return "testPlugin";
 235                 }
 236                 @Override
 237                 public Map<String, String> getOptions() {
 238                     return Collections.singletonMap("testPluginKey", "testPluginValue");
 239                 }
 240                 @Override
 241                 public Plugin getPlugin() {
 242                     return new PluginImpl();
 243                 }
 244             });
 245         }
 246 
 247         @Override
 248         public List<String> getAdditionalOptions() {
 249             return Arrays.asList("-Xlint:rawtypes", "-XDrawDiagnostics");
 250         }
 251 
 252         @Override
 253         public void close() throws IOException {
 254             System.err.println("close");
 255             fm.close();
 256         }
 257 
 258     }
 259 
 260     @SupportedAnnotationTypes("*")
 261     @SupportedOptions("testAPKey")
 262     class ProcessorImpl extends AbstractProcessor {
 263 
 264         @Override
 265         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
 266             System.err.println("process: " + processingEnv.getOptions());
 267             return true;
 268         }
 269 
 270         @Override
 271         public SourceVersion getSupportedSourceVersion() {
 272             return SourceVersion.latest();
 273         }
 274 
 275     }
 276 
 277     class PluginImpl implements Plugin {
 278 
 279         @Override
 280         public String getName() {
 281             return "testPluginName";
 282         }
 283 
 284         @Override
 285         public void init(com.sun.source.util.JavacTask task, String... args) {
 286             System.err.println("testPlugin: " + Arrays.toString(args));
 287         }
 288 
 289     }
 290 }