HelloWorldTest.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 * @summary simple tests of javac compilation modes 27 * @library /tools/lib 28 * @modules 29 * jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.jdeps/com.sun.tools.javap 32 * @build ToolBox ModuleTestBase 33 * @run main HelloWorldTest 34 */ 35 36import java.nio.file.*; 37import javax.tools.*; 38 39public class HelloWorldTest extends ModuleTestBase { 40 public static void main(String... args) throws Exception { 41 HelloWorldTest t = new HelloWorldTest(); 42 t.runTests(); 43 } 44 45 public static final String HELLO_WORLD = 46 "class HelloWorld {\n" 47 + " public static void main(String... args) {\n" 48 + " System.out.println(\"Hello World!\");\n" 49 + " }\n" 50 + "}"; 51 52 public static final String PKG_HELLO_WORLD = 53 "package p;\n" 54 + HELLO_WORLD; 55 56 @Test 57 void testLegacyMode(Path base) throws Exception { 58 Path src = base.resolve("src"); 59 tb.writeJavaFiles(src, HELLO_WORLD); 60 61 Path classes = base.resolve("classes"); 62 Files.createDirectories(classes); 63 64 Path smallRtJar = base.resolve("small-rt.jar"); 65 try (JavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) { 66 tb.new JarTask(smallRtJar) 67 .files(fm, StandardLocation.PLATFORM_CLASS_PATH, 68 "java.lang.**", "java.io.*", "java.util.*") 69 .run(); 70 } 71 72 tb.new JavacTask() 73 .options("-source", "8", 74 "-target", "8", 75 "-bootclasspath", smallRtJar.toString()) 76 .outdir(classes) 77 .files(src.resolve("HelloWorld.java")) 78 .run(); 79 80 checkFiles(classes.resolve("HelloWorld.class")); 81 } 82 83 @Test 84 void testUnnamedModule(Path base) throws Exception { 85 Path src = base.resolve("src"); 86 tb.writeJavaFiles(src, HELLO_WORLD); 87 88 Path classes = base.resolve("classes"); 89 Files.createDirectories(classes); 90 91 tb.new JavacTask() 92 .outdir(classes) 93 .files(src.resolve("HelloWorld.java")) 94 .run(); 95 96 checkFiles(classes.resolve("HelloWorld.class")); 97 } 98 99 @Test 100 void testSingleModule(Path base) throws Exception { 101 Path src = base.resolve("src"); 102 tb.writeFile(src.resolve("module-info.java"), "module m { }"); 103 tb.writeJavaFiles(src, PKG_HELLO_WORLD); 104 105 Path classes = base.resolve("classes"); 106 Files.createDirectories(classes); 107 108 tb.new JavacTask(ToolBox.Mode.CMDLINE) 109 .outdir(classes) 110 .files(src.resolve("module-info.java"), src.resolve("p/HelloWorld.java")) 111 .run() 112 .writeAll(); 113 114 checkFiles( 115 classes.resolve("module-info.class"), 116 classes.resolve("p/HelloWorld.class")); 117 } 118 119 @Test 120 void testModuleSourcePath(Path base) throws Exception { 121 Path src = base.resolve("src"); 122 Path src_m1 = src.resolve("m1"); 123 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { }"); 124 tb.writeJavaFiles(src_m1, PKG_HELLO_WORLD); 125 126 Path classes = base.resolve("classes"); 127 Files.createDirectories(classes); 128 129 tb.new JavacTask() 130 .options("-modulesourcepath", src.toString()) 131 .outdir(classes) 132 .files(src_m1.resolve("p/HelloWorld.java")) 133 .run() 134 .writeAll(); 135 136 checkFiles( 137 classes.resolve("m1/module-info.class"), 138 classes.resolve("m1/p/HelloWorld.class")); 139 } 140 141 void checkFiles(Path... files) throws Exception { 142 for (Path f: files) { 143 if (!Files.exists(f)) 144 throw new Exception("expected file not found: " + f); 145 } 146 } 147} 148