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