PatchModulesTest.java revision 3756:6afd59d40256
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 8160489
27 * @summary tests for --patch-modules
28 * @library /tools/lib
29 * @modules
30 *      jdk.compiler/com.sun.tools.javac.api
31 *      jdk.compiler/com.sun.tools.javac.file
32 *      jdk.compiler/com.sun.tools.javac.main
33 * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
34 * @run main PatchModulesTest
35 */
36
37import java.io.File;
38import java.io.IOException;
39import java.io.PrintWriter;
40import java.io.StringWriter;
41import java.lang.reflect.Field;
42import java.nio.file.Path;
43import java.nio.file.Paths;
44import java.util.List;
45import java.util.Map;
46import java.util.stream.Collectors;
47
48import javax.tools.JavaFileObject;
49import javax.tools.ToolProvider;
50
51import com.sun.source.util.JavacTask;
52import com.sun.tools.javac.api.JavacTool;
53import com.sun.tools.javac.file.BaseFileManager;
54import com.sun.tools.javac.file.JavacFileManager;
55import com.sun.tools.javac.file.Locations;
56
57import static java.util.Arrays.asList;
58
59
60public class PatchModulesTest extends ModuleTestBase {
61
62    public static void main(String... args) throws Exception {
63        PatchModulesTest t = new PatchModulesTest();
64        t.init();
65        t.runTests();
66    }
67
68    private static String PS = File.pathSeparator;
69
70    void init() throws IOException {
71        tb.createDirectories("a", "b", "c", "d", "e");
72        tb.writeJavaFiles(Paths.get("."), "class C { }");
73    }
74
75    @Test
76    public void testSimple(Path base) throws Exception {
77        test(asList("java.base=a"),
78            "{java.base=[a]}");
79    }
80
81    @Test
82    public void testPair(Path base) throws Exception {
83        test(asList("java.base=a", "java.compiler=b"),
84            "{java.base=[a], java.compiler=[b]}");
85    }
86
87    @Test
88    public void testMultiple(Path base) throws Exception {
89        test(asList("java.base=a:b"),
90            "{java.base=[a, b]}");
91    }
92
93    @Test
94    public void testDuplicates(Path base) throws Exception {
95        test(asList("java.base=a", "java.compiler=b", "java.base=c"),
96            false, "--patch-module specified more than once for java.base");
97    }
98
99    @Test
100    public void testEmpty(Path base) throws Exception {
101        test(asList(""),
102            false, "no value for --patch-module option");
103    }
104
105    @Test
106    public void testInvalid(Path base) throws Exception {
107        test(asList("java.base/java.lang=."),
108            false, "bad value for --patch-module option: 'java.base/java.lang=.'");
109    }
110
111    void test(List<String> patches, String expect) throws Exception {
112        test(patches, true, expect);
113    }
114
115    void test(List<String> patches, boolean expectOK, String expect) throws Exception {
116        JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
117        StringWriter sw = new StringWriter();
118        try (PrintWriter pw = new PrintWriter(sw)) {
119            JavacFileManager fm = tool.getStandardFileManager(null, null, null);
120            List<String> opts = patches.stream()
121                .map(p -> "--patch-module=" + p.replace(":", PS))
122                .collect(Collectors.toList());
123            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects("C.java");
124            JavacTask task = tool.getTask(pw, fm, null, opts, null, files);
125
126            Field locationsField = BaseFileManager.class.getDeclaredField("locations");
127            locationsField.setAccessible(true);
128            Object locations = locationsField.get(fm);
129
130            Field patchMapField = Locations.class.getDeclaredField("patchMap");
131            patchMapField.setAccessible(true);
132            Map<?,?> patchMap = (Map<?,?>) patchMapField.get(locations);
133            String found = patchMap.toString();
134
135            if (!found.equals(expect)) {
136                tb.out.println("Expect: " + expect);
137                tb.out.println("Found:  " + found);
138                error("output not as expected");
139            }
140        } catch (IllegalArgumentException e) {
141            if (expectOK) {
142                error("unexpected exception: " + e);
143                throw e;
144            }
145            String found = e.getMessage();
146            if (!found.equals(expect)) {
147                tb.out.println("Expect: " + expect);
148                tb.out.println("Found:  " + found);
149                error("output not as expected");
150            }
151        }
152    }
153}
154
155