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