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.nio.file.Files; 42 import java.nio.file.Path; 43 import java.nio.file.Paths; 44 import java.util.AbstractMap.SimpleEntry; 45 import java.util.List; 46 import java.util.Map; 47 import java.util.Map.Entry; 48 import java.util.TreeMap; 49 import java.util.stream.Collectors; 50 import java.util.stream.StreamSupport; 51 52 import javax.tools.JavaFileManager.Location; 53 import javax.tools.JavaFileObject; 54 import javax.tools.ToolProvider; 55 import javax.tools.StandardJavaFileManager; 56 import javax.tools.StandardLocation; 57 58 import com.sun.source.util.JavacTask; 59 import com.sun.tools.javac.api.JavacTool; 60 import com.sun.tools.javac.file.JavacFileManager; 61 62 import static java.util.Arrays.asList; 63 64 65 public class PatchModulesTest extends ModuleTestBase { 66 67 public static void main(String... args) throws Exception { 68 PatchModulesTest t = new PatchModulesTest(); 69 t.init(); 70 t.runTests(); 71 } 72 73 private static String PS = File.pathSeparator; 74 75 void init() throws IOException { 76 tb.createDirectories("a", "b", "c", "d", "e"); 77 tb.writeJavaFiles(Paths.get("."), "class C { }"); 78 } 79 80 @Test 81 public void testSimple(Path base) throws Exception { 82 test(asList("java.base=a"), 83 "{java.base=[a]}"); 84 } 85 86 @Test 87 public void testPair(Path base) throws Exception { 88 test(asList("java.base=a", "java.compiler=b"), 89 "{java.base=[a], java.compiler=[b]}"); 90 } 91 92 @Test 93 public void testMultiple(Path base) throws Exception { 94 test(asList("java.base=a:b"), 95 "{java.base=[a, b]}"); 96 } 97 98 @Test 99 public void testDuplicates(Path base) throws Exception { 100 test(asList("java.base=a", "java.compiler=b", "java.base=c"), 101 false, "--patch-module specified more than once for java.base"); 102 } 103 104 @Test 105 public void testEmpty(Path base) throws Exception { 106 test(asList(""), 107 false, "no value for --patch-module option"); 108 } 109 110 @Test 111 public void testInvalid(Path base) throws Exception { 112 test(asList("java.base/java.lang=."), 113 false, "bad value for --patch-module option: 'java.base/java.lang=.'"); 114 } 115 116 void test(List<String> patches, String expect) throws Exception { 117 test(patches, true, expect); 118 } 119 120 void test(List<String> patches, boolean expectOK, String expect) throws Exception { 121 JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler(); 122 StringWriter sw = new StringWriter(); 123 try (PrintWriter pw = new PrintWriter(sw); 124 JavacFileManager fm = tool.getStandardFileManager(null, null, null)) { 125 List<String> opts = patches.stream() 126 .map(p -> "--patch-module=" + p.replace(":", PS)) 127 .collect(Collectors.toList()); 128 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects("C.java"); 129 JavacTask task = tool.getTask(pw, fm, null, opts, null, files); 130 131 Map<String, List<Location>> mod2Location = 132 StreamSupport.stream(fm.listLocationsForModules(StandardLocation.PATCH_MODULE_PATH) 133 .spliterator(), 134 false) 135 .flatMap(sl -> sl.stream()) 136 .collect(Collectors.groupingBy(l -> fm.inferModuleName(l))); 137 138 Map<String, List<String>> patchMap = mod2Location.entrySet() 139 .stream() 140 .map(e -> new SimpleEntry<>(e.getKey(), e.getValue().get(0))) 141 .map(e -> new SimpleEntry<>(e.getKey(), locationPaths(fm, e.getValue()))) 142 .collect(Collectors.toMap(Entry :: getKey, 143 Entry :: getValue, 144 (v1, v2) -> {throw new IllegalStateException();}, 145 TreeMap::new)); 146 String found = patchMap.toString(); 147 148 if (!found.equals(expect)) { 149 tb.out.println("Expect: " + expect); 150 tb.out.println("Found: " + found); 151 error("output not as expected"); 152 } 153 } catch (IllegalArgumentException e) { 154 if (expectOK) { 155 error("unexpected exception: " + e); 156 throw e; 157 } 158 String found = e.getMessage(); 159 if (!found.equals(expect)) { 160 tb.out.println("Expect: " + expect); 161 tb.out.println("Found: " + found); 162 error("output not as expected"); 163 } 164 } 165 } 166 167 static List<String> locationPaths(StandardJavaFileManager fm, Location loc) { 168 return StreamSupport.stream(fm.getLocationAsPaths(loc).spliterator(), false) 169 .map(p -> p.toString()) 170 .collect(Collectors.toList()); 171 } 172 173 @Test 174 public void testPatchWithSource(Path base) throws Exception { 175 Path patch = base.resolve("patch"); 176 tb.writeJavaFiles(patch, "package javax.lang.model.element; public interface Extra { }"); 177 Path src = base.resolve("src"); 178 tb.writeJavaFiles(src, 179 "module m { requires java.compiler; }", 180 "package test; public interface Test extends javax.lang.model.element.Extra { }"); 181 Path classes = base.resolve("classes"); 182 tb.createDirectories(classes); 183 184 new toolbox.JavacTask(tb) 185 .options("--patch-module", "java.compiler=" + patch.toString()) 186 .outdir(classes) 187 .files(findJavaFiles(src)) 188 .run() 189 .writeAll(); 190 191 if (Files.exists(classes.resolve("javax"))) { 192 throw new AssertionError(); 193 } 194 } 195 } 196