Path: blob/master/test/langtools/jdk/javadoc/tool/api/basic/AddModulesTest.java
40983 views
/*1* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 817359626* @summary DocumentationTool.DocumentationTask should support addModules27* @modules jdk.compiler/com.sun.tools.javac.api28* jdk.compiler/com.sun.tools.javac.main29* @library /tools/lib30* @build APITest toolbox.JavacTask toolbox.ToolBox31* @run main AddModulesTest32*/3334import java.io.StringWriter;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.util.Arrays;3839import javax.tools.DocumentationTool;40import javax.tools.DocumentationTool.DocumentationTask;41import javax.tools.DocumentationTool.Location;42import javax.tools.JavaFileObject;43import javax.tools.StandardJavaFileManager;44import javax.tools.StandardLocation;45import javax.tools.ToolProvider;4647import toolbox.Assert;48import toolbox.JavacTask;49import toolbox.ToolBox;5051/**52* Tests for DocumentationTask.addModules method.53*/54public class AddModulesTest extends APITest {55public static void main(String... args) throws Exception {56new AddModulesTest().run();57}5859private final ToolBox tb = new ToolBox();6061/**62* Verify that addModules works as expected.63*/64@Test65public void testAddModules() throws Exception {66Path base = Paths.get("testAddModules");67Path src = base.resolve("src");6869// setup some utility modules70Path src_m1 = src.resolve("m1x");71tb.writeJavaFiles(src_m1,72"module m1x { exports p1; }",73"package p1; public class C1 { }");74Path src_m2 = src.resolve("m2x");75tb.writeJavaFiles(src_m2,76"module m2x { exports p2; }",77"package p2; public class C2 { }");78Path modules = base.resolve("modules");79tb.createDirectories(modules);8081new JavacTask(tb)82.options("--module-source-path", src.toString())83.outdir(modules)84.files(tb.findJavaFiles(src))85.run()86.writeAll();8788// now test access to the modules89Path src2 = base.resolve("src2");90tb.writeJavaFiles(src2,91"public class Dummy { p1.C1 c1; p2.C2 c2; }");92Path api = base.resolve("api");93tb.createDirectories(api);9495DocumentationTool tool = ToolProvider.getSystemDocumentationTool();96try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {97fm.setLocationFromPaths(StandardLocation.MODULE_PATH, Arrays.asList(modules));98fm.setLocationFromPaths(Location.DOCUMENTATION_OUTPUT, Arrays.asList(api));99Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(tb.findJavaFiles(src2));100101for (boolean useOption : new boolean[] { false, true }) {102System.err.println("Use --add-modules option: " + useOption);103StringWriter sw = new StringWriter();104DocumentationTask t = tool.getTask(sw, fm, null, null, null, files);105if (useOption) {106t.addModules(Arrays.asList("m1x", "m2x"));107}108String out;109boolean ok;110try {111ok = t.call();112} finally {113out = sw.toString();114System.err.println(out);115}116System.err.println("ok: " + ok);117boolean expectErrors = !useOption;118check(out, "package p1 is not visible", expectErrors);119check(out, "package p2 is not visible", expectErrors);120System.err.println();121}122}123}124125void check(String out, String text, boolean expected) {126System.err.println("Checking for "127+ (expected ? "expected" : "unexpected")128+ " text: " + text);129130if (expected) {131if (!out.contains(text)) {132error("expected text not found: " + text);133}134} else {135if (out.contains(text)) {136error("unexpected text found: " + text);137}138}139}140}141142143144