|
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 |
|
37 import java.io.File; |
|
38 import java.io.IOException; |
|
39 import java.io.PrintWriter; |
|
40 import java.io.StringWriter; |
|
41 import java.lang.reflect.Field; |
|
42 import java.nio.file.Path; |
|
43 import java.nio.file.Paths; |
|
44 import java.util.List; |
|
45 import java.util.Map; |
|
46 import java.util.stream.Collectors; |
|
47 |
|
48 import javax.tools.JavaFileObject; |
|
49 import javax.tools.ToolProvider; |
|
50 |
|
51 import com.sun.source.util.JavacTask; |
|
52 import com.sun.tools.javac.api.JavacTool; |
|
53 import com.sun.tools.javac.file.BaseFileManager; |
|
54 import com.sun.tools.javac.file.JavacFileManager; |
|
55 import com.sun.tools.javac.file.Locations; |
|
56 |
|
57 import static java.util.Arrays.asList; |
|
58 |
|
59 |
|
60 public 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 testLastOneWins(Path base) throws Exception { |
|
95 test(asList("java.base=a", "java.compiler=b", "java.base=c"), |
|
96 "{java.base=[c], java.compiler=[b]}"); |
|
97 } |
|
98 |
|
99 void test(List<String> patches, String expect) throws Exception { |
|
100 JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler(); |
|
101 StringWriter sw = new StringWriter(); |
|
102 try (PrintWriter pw = new PrintWriter(sw)) { |
|
103 JavacFileManager fm = tool.getStandardFileManager(null, null, null); |
|
104 List<String> opts = patches.stream() |
|
105 .map(p -> "--patch-module=" + p.replace(":", PS)) |
|
106 .collect(Collectors.toList()); |
|
107 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects("C.java"); |
|
108 JavacTask task = tool.getTask(pw, fm, null, opts, null, files); |
|
109 |
|
110 Field locationsField = BaseFileManager.class.getDeclaredField("locations"); |
|
111 locationsField.setAccessible(true); |
|
112 Object locations = locationsField.get(fm); |
|
113 |
|
114 Field patchMapField = Locations.class.getDeclaredField("patchMap"); |
|
115 patchMapField.setAccessible(true); |
|
116 Map<?,?> patchMap = (Map<?,?>) patchMapField.get(locations); |
|
117 String found = patchMap.toString(); |
|
118 |
|
119 if (!found.equals(expect)) { |
|
120 tb.out.println("Expect: " + expect); |
|
121 tb.out.println("Found: " + found); |
|
122 error("output not as expected"); |
|
123 } |
|
124 } |
|
125 } |
|
126 } |
|
127 |