1 /*
   2  * Copyright (c) 2013, 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 4263768 4785453
  27  * @summary Verify that the compiler does not crash when java.lang is not available
  28  * @library /tools/lib
  29  * @modules jdk.compiler/com.sun.tools.javac.api
  30  *          jdk.compiler/com.sun.tools.javac.main
  31  * @build toolbox.ToolBox toolbox.JavacTask
  32  * @run main NoJavaLangTest
  33  */
  34 
  35 import java.nio.file.*;
  36 
  37 import toolbox.JavacTask;
  38 import toolbox.Task;
  39 import toolbox.ToolBox;
  40 
  41 public class NoJavaLangTest {
  42 
  43     private static final String noJavaLangSrc =
  44         "public class NoJavaLang {\n" +
  45         "    private String s;\n" +
  46         "\n" +
  47         "    public String s() {\n" +
  48         "        return s;\n" +
  49         "    }\n" +
  50         "}";
  51 
  52     private static final String compilerErrorMessage =
  53         "Fatal Error: Unable to find package java.lang in classpath or bootclasspath";
  54 
  55     public static void main(String[] args) throws Exception {
  56         new NoJavaLangTest().run();
  57     }
  58 
  59     final ToolBox tb = new ToolBox();
  60 
  61     void run() throws Exception {
  62         testStandard();
  63         testBootClassPath();
  64         testModulePath();
  65     }
  66 
  67     // sanity check, with java.lang available
  68     void testStandard() {
  69         new JavacTask(tb)
  70                 .sources(noJavaLangSrc)
  71                 .run();
  72     }
  73 
  74 
  75     // test with bootclasspath, for as long as its around
  76     void testBootClassPath() {
  77         String[] bcpOpts = { "-Xlint:-options", "-source", "8", "-bootclasspath", "." };
  78         test(bcpOpts, compilerErrorMessage);
  79     }
  80 
  81     // test with module path
  82     void testModulePath() throws Exception {
  83         // need to ensure there is an empty java.base to avoid different error message
  84         Files.createDirectories(Paths.get("modules/java.base"));
  85         new JavacTask(tb)
  86                 .sources("module java.base { }")
  87                 .outdir("modules/java.base")
  88                 .run();
  89 
  90         // ideally we'd have a better message for this case
  91         String[] mpOpts = { "-system", "none", "-modulepath", "modules" };
  92         test(mpOpts, compilerErrorMessage);
  93     }
  94 
  95     private void test(String[] options, String expect) {
  96         System.err.println("Testing " + java.util.Arrays.toString(options));
  97 
  98         String out = new JavacTask(tb)
  99                 .options(options)
 100                 .sources(noJavaLangSrc)
 101                 .run(Task.Expect.FAIL, 3)
 102                 .writeAll()
 103                 .getOutput(Task.OutputKind.DIRECT);
 104 
 105         if (!out.trim().equals(expect)) {
 106             throw new AssertionError("javac generated error output is not correct");
 107         }
 108 
 109         System.err.println("OK");
 110     }
 111 
 112 }