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 * @summary tests for single module mode compilation 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.JavacTask ModuleTestBase 32 * @run main SingleModuleModeTest 33 */ 34 35 import java.nio.file.Files; 36 import java.nio.file.Path; 37 import java.util.Set; 38 39 import javax.annotation.processing.AbstractProcessor; 40 import javax.annotation.processing.RoundEnvironment; 41 import javax.annotation.processing.SupportedAnnotationTypes; 42 import javax.lang.model.SourceVersion; 43 import javax.lang.model.element.TypeElement; 44 45 import toolbox.JavacTask; 46 import toolbox.Task; 47 import toolbox.ToolBox; 48 49 public class SingleModuleModeTest extends ModuleTestBase{ 50 51 public static void main(String... args) throws Exception { 52 new SingleModuleModeTest().run(); 53 } 54 55 void run() throws Exception { 56 tb = new ToolBox(); 57 58 runTests(); 59 } 60 61 @Test 62 public void testTooManyModules(Path base) throws Exception { 63 Path src = base.resolve("src"); 64 tb.writeJavaFiles(src.resolve("m1"), "module m1 { }"); 65 tb.writeJavaFiles(src.resolve("m2"), "module m2 { }"); 66 67 String log = new JavacTask(tb) 68 .options("-XDrawDiagnostics") 69 .files(findJavaFiles(src)) 70 .run(Task.Expect.FAIL) 71 .writeAll() 72 .getOutput(Task.OutputKind.DIRECT); 73 74 if (!log.contains("module-info.java:1:1: compiler.err.too.many.modules")) 75 throw new Exception("expected output not found"); 76 } 77 78 @Test 79 public void testImplicitModuleSource(Path base) throws Exception { 80 Path src = base.resolve("src"); 81 tb.writeJavaFiles(src, 82 "module m { }", 83 "class C { }"); 84 85 new JavacTask(tb) 86 .classpath(src) 87 .files(src.resolve("C.java")) 88 .run() 89 .writeAll(); 90 } 91 92 @Test 93 public void testImplicitModuleClass(Path base) throws Exception { 94 Path src = base.resolve("src"); 95 tb.writeJavaFiles(src, 96 "module m { }", 97 "class C { }"); 98 Path classes = base.resolve("classes"); 99 Files.createDirectories(classes); 100 101 new JavacTask(tb) 102 .outdir(classes) 103 .files(src.resolve("module-info.java")) 104 .run() 105 .writeAll(); 106 107 new JavacTask(tb) 108 .classpath(classes) 109 .files(src.resolve("C.java")) 110 .run() 111 .writeAll(); 112 } 113 114 @Test 115 public void testImplicitModuleClassAP(Path base) throws Exception { 116 Path src = base.resolve("src"); 117 tb.writeJavaFiles(src, 118 "module m { uses java.lang.Runnable; }", 119 "class C { }"); 120 Path classes = base.resolve("classes"); 121 Files.createDirectories(classes); 122 123 new JavacTask(tb) 124 .outdir(classes) 125 .files(src.resolve("module-info.java")) 126 .run() 127 .writeAll(); 128 129 new JavacTask(tb) 130 .options("-processor", VerifyUsesProvides.class.getName(), 131 "-processorpath", System.getProperty("test.classes")) 132 .outdir(classes) 133 .classpath(classes) 134 .files(src.resolve("C.java")) 135 .run() 136 .writeAll(); 137 } 138 139 @Test 140 public void testImplicitModuleSourceAP(Path base) throws Exception { 141 Path src = base.resolve("src"); 142 tb.writeJavaFiles(src, 143 "module m { uses java.lang.Runnable; }", 144 "class C { }"); 145 Path classes = base.resolve("classes"); 146 Files.createDirectories(classes); 147 148 new JavacTask(tb) 149 .options("-processor", VerifyUsesProvides.class.getName(), 150 "-processorpath", System.getProperty("test.classes")) 151 .outdir(classes) 152 .sourcepath(src) 153 .classpath(classes) 154 .files(src.resolve("C.java")) 155 .run() 156 .writeAll(); 157 } 158 159 @SupportedAnnotationTypes("*") 160 public static final class VerifyUsesProvides extends AbstractProcessor { 161 162 @Override 163 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 164 if (processingEnv.getElementUtils().getModuleElement("m") == null) { 165 throw new AssertionError(); 166 } 167 168 return false; 169 } 170 171 @Override 172 public SourceVersion getSupportedSourceVersion() { 173 return SourceVersion.latest(); 174 } 175 176 } 177 }