GetLocationForModuleTest.java revision 3822:d8766c39123a
1/*
2 * Copyright (c) 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 * @bug 8162712
27 * @summary StandardJavaFileManager.getModuleLocation() can't find a module
28 * @library /tools/lib
29 * @modules
30 *      jdk.compiler/com.sun.tools.javac.api
31 *      jdk.compiler/com.sun.tools.javac.main
32 * @build toolbox.JavacTask toolbox.ToolBox
33 * @run main GetLocationForModuleTest
34 */
35
36import java.io.IOException;
37import java.nio.file.*;
38import java.util.*;
39import javax.tools.*;
40import toolbox.JavacTask;
41import toolbox.ToolBox;
42
43public class GetLocationForModuleTest extends ModuleTestBase {
44    public static void main(String... args) throws Exception {
45        new GetLocationForModuleTest().run(Paths.get("."));
46    }
47
48    public void run(Path base) throws Exception {
49        // Set up some trivial modules
50        Path moduleSrc = base.resolve("module-src");
51        Path m1 = moduleSrc.resolve("m1x");
52        tb.writeJavaFiles(m1, "module m1x { }");
53        Path m2 = moduleSrc.resolve("m2x");
54        tb.writeJavaFiles(m2, "module m2x { }");
55
56        Path modulePath = base.resolve("module-path");
57        Files.createDirectories(modulePath);
58        new JavacTask(tb)
59                .options("--module-source-path", moduleSrc.toString())
60                .outdir(modulePath)
61                .files(findJavaFiles(moduleSrc))
62                .run()
63                .writeAll();
64
65        // Init file manager
66        StandardJavaFileManager fm =
67                ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
68        fm.setLocationFromPaths(StandardLocation.MODULE_PATH, Arrays.asList(modulePath));
69
70        // Test
71        test(fm, StandardLocation.SYSTEM_MODULES, "java.base", "java.compiler");
72        test(fm, StandardLocation.MODULE_PATH, "m1x", "m2x");
73    }
74
75    void test(JavaFileManager fm, JavaFileManager.Location locn, String... mods) throws IOException {
76        for (String mod : mods) {
77            JavaFileManager.Location modLocn = fm.getLocationForModule(locn, mod);
78            if (modLocn == null) {
79                error(locn.getName() + ": can't find " + mod);
80            } else {
81                System.err.println(locn.getName() + ": found " + mod + ": " + modLocn.getName());
82            }
83        }
84    }
85}
86
87