AddModulesTest.java revision 3999:ae88ea1b7649
1/*
2 * Copyright (c) 2012, 2017, 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 8173596
27 * @summary DocumentationTool.DocumentationTask should support addModules
28 * @modules jdk.compiler/com.sun.tools.javac.api
29 *          jdk.compiler/com.sun.tools.javac.main
30 * @library /tools/lib
31 * @build APITest toolbox.JavacTask toolbox.ToolBox
32 * @run main AddModulesTest
33 */
34
35import java.io.StringWriter;
36import java.nio.file.Path;
37import java.nio.file.Paths;
38import java.util.Arrays;
39
40import javax.tools.DocumentationTool;
41import javax.tools.DocumentationTool.DocumentationTask;
42import javax.tools.DocumentationTool.Location;
43import javax.tools.JavaFileObject;
44import javax.tools.StandardJavaFileManager;
45import javax.tools.StandardLocation;
46import javax.tools.ToolProvider;
47
48import toolbox.Assert;
49import toolbox.JavacTask;
50import toolbox.ToolBox;
51
52/**
53 * Tests for DocumentationTask.addModules method.
54 */
55public class AddModulesTest extends APITest {
56    public static void main(String... args) throws Exception {
57        new AddModulesTest().run();
58    }
59
60    private final ToolBox tb = new ToolBox();
61
62    /**
63     * Verify that addModules works as expected.
64     */
65    @Test
66    public void testAddModules() throws Exception {
67        Path base = Paths.get("testAddModules");
68        Path src = base.resolve("src");
69
70        // setup some utility modules
71        Path src_m1 = src.resolve("m1x");
72        tb.writeJavaFiles(src_m1,
73                          "module m1x { exports p1; }",
74                          "package p1; public class C1 { }");
75        Path src_m2 = src.resolve("m2x");
76        tb.writeJavaFiles(src_m2,
77                          "module m2x { exports p2; }",
78                          "package p2; public class C2 { }");
79        Path modules = base.resolve("modules");
80        tb.createDirectories(modules);
81
82        new JavacTask(tb)
83                .options("--module-source-path", src.toString())
84                .outdir(modules)
85                .files(tb.findJavaFiles(src))
86                .run()
87                .writeAll();
88
89        // now test access to the modules
90        Path src2 = base.resolve("src2");
91        tb.writeJavaFiles(src2,
92                          "public class Dummy { p1.C1 c1; p2.C2 c2; }");
93        Path api = base.resolve("api");
94        tb.createDirectories(api);
95
96        DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
97        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
98            fm.setLocationFromPaths(StandardLocation.MODULE_PATH, Arrays.asList(modules));
99            fm.setLocationFromPaths(Location.DOCUMENTATION_OUTPUT, Arrays.asList(api));
100            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(tb.findJavaFiles(src2));
101
102            for (boolean useOption : new boolean[] { false, true }) {
103                System.err.println("Use --add-modules option: " + useOption);
104                StringWriter sw = new StringWriter();
105                DocumentationTask t = tool.getTask(sw, fm, null, null, null, files);
106                if (useOption) {
107                    t.addModules(Arrays.asList("m1x", "m2x"));
108                }
109                String out;
110                boolean ok;
111                try {
112                    ok = t.call();
113                } finally {
114                    out = sw.toString();
115                    System.err.println(out);
116                }
117                System.err.println("ok: " + ok);
118                boolean expectErrors = !useOption;
119                check(out, "package p1 is not visible", expectErrors);
120                check(out, "package p2 is not visible", expectErrors);
121                System.err.println();
122            }
123        }
124    }
125
126    void check(String out, String text, boolean expected) {
127        System.err.println("Checking for "
128            + (expected ? "expected" : "unexpected")
129            + " text: " + text);
130
131        if (expected) {
132            if (!out.contains(text)) {
133                error("expected text not found: " + text);
134            }
135        } else {
136            if (out.contains(text)) {
137                error("unexpected text found: " + text);
138            }
139        }
140    }
141}
142
143