SetLocationForModule.java revision 4018:a42d01aa6c9e
1/* 2 * Copyright (c) 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 8173914 27 * @summary JavaFileManager.setLocationForModule 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.main 30 * @library /tools/lib 31 * @build toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox SetLocationForModule 32 * @run main SetLocationForModule 33 */ 34 35 36import java.io.FileNotFoundException; 37import java.io.IOException; 38import java.nio.file.Files; 39import java.nio.file.Path; 40import java.nio.file.Paths; 41import java.util.ArrayList; 42import java.util.Collections; 43import java.util.List; 44import java.util.Objects; 45 46import javax.tools.JavaCompiler; 47import javax.tools.JavaFileManager.Location; 48import javax.tools.StandardJavaFileManager; 49import javax.tools.StandardLocation; 50import javax.tools.ToolProvider; 51 52import toolbox.JavacTask; 53import toolbox.TestRunner; 54import toolbox.TestRunner.Test; 55import toolbox.ToolBox; 56 57public class SetLocationForModule extends TestRunner { 58 59 public static void main(String... args) throws Exception { 60 new SetLocationForModule().runTests(m -> new Object[] { Paths.get(m.getName()) }); 61 } 62 63 public SetLocationForModule() { 64 super(System.err); 65 } 66 67 private final JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 68 private final ToolBox tb = new ToolBox(); 69 70 @Test 71 public void testBasic(Path base) throws IOException { 72 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 73 Location[] locns = { 74 StandardLocation.SOURCE_PATH, 75 StandardLocation.CLASS_PATH, 76 StandardLocation.PLATFORM_CLASS_PATH, 77 }; 78 // set a value 79 Path out = Files.createDirectories(base.resolve("out")); 80 for (Location locn : locns) { 81 checkException("unsupported for location", 82 IllegalArgumentException.class, 83 "location is not an output location or a module-oriented location: " + locn, 84 () -> fm.setLocationForModule(locn, "m", List.of(out))); 85 } 86 } 87 } 88 89 @Test 90 public void testModulePath(Path base) throws IOException { 91 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 92 Path src = base.resolve("src"); 93 Path src_m = src.resolve("m"); 94 tb.writeJavaFiles(src_m, "module m { }"); 95 96 Location locn = StandardLocation.MODULE_PATH; 97 98 Path modules1 = Files.createDirectories(base.resolve("modules1")); 99 new JavacTask(tb) 100 .outdir(modules1) 101 .options("--module-source-path", src.toString()) 102 .files(tb.findJavaFiles(src)) 103 .run(); 104 fm.setLocationFromPaths(locn, List.of(modules1)); 105 106 Location m = fm.getLocationForModule(locn, "m"); 107 checkEqual("default setting", 108 fm.getLocationAsPaths(m), modules1.resolve("m")); 109 110 Path override1 = Files.createDirectories(base.resolve("override1")); 111 fm.setLocationForModule(locn, "m", List.of(override1)); 112 checkEqual("override setting 1", 113 fm.getLocationAsPaths(m), override1); 114 115 Path override2 = Files.createDirectories(base.resolve("override2")); 116 fm.setLocationFromPaths(m, List.of(override2)); 117 checkEqual("override setting 2", 118 fm.getLocationAsPaths(m), override2); 119 120 Path modules2 = Files.createDirectories(base.resolve("modules2")); 121 fm.setLocationFromPaths(locn, List.of(modules2)); 122 123 checkEqual("updated setting", 124 fm.getLocationAsPaths(m), modules2.resolve("m")); 125 } 126 } 127 128 @Test 129 public void testModuleSourcePath(Path base) throws IOException { 130 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 131 132 Location locn = StandardLocation.MODULE_SOURCE_PATH; 133 134 Path src1 = Files.createDirectories(base.resolve("src1")); 135 Path src1_m = src1.resolve("m"); 136 tb.writeJavaFiles(src1_m, "module m { }"); 137// fm.setLocationFromPaths(locn, List.of(src1)); 138 fm.handleOption("--module-source-path", List.of(src1.toString()).iterator()); 139 140 Location m = fm.getLocationForModule(locn, "m"); 141 checkEqual("default setting", 142 fm.getLocationAsPaths(m), src1.resolve("m")); 143 144 Path override1 = Files.createDirectories(base.resolve("override1")); 145 tb.writeJavaFiles(override1, "module m { }"); 146 fm.setLocationForModule(locn, "m", List.of(override1)); 147 checkEqual("override setting 1", 148 fm.getLocationAsPaths(m), override1); 149 150 Path override2 = Files.createDirectories(base.resolve("override2")); 151 tb.writeJavaFiles(override2, "module m { }"); 152 fm.setLocationFromPaths(m, List.of(override2)); 153 checkEqual("override setting 2", 154 fm.getLocationAsPaths(m), override2); 155 156 Path src2 = Files.createDirectories(base.resolve("src2")); 157 Path src2_m = src2.resolve("m"); 158 tb.writeJavaFiles(src2_m, "module m { }"); 159// fm.setLocationFromPaths(locn, List.of(src2)); 160 fm.handleOption("--module-source-path", List.of(src2.toString()).iterator()); 161 162 checkEqual("updated setting", 163 fm.getLocationAsPaths(m), src2.resolve("m")); 164 } 165 } 166 167 @Test 168 public void testOutput(Path base) throws IOException { 169 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 170 Location locn = StandardLocation.CLASS_OUTPUT; 171 172 Path out1 = Files.createDirectories(base.resolve("out1")); 173 fm.setLocationFromPaths(locn, List.of(out1)); 174 175 Location m = fm.getLocationForModule(locn, "m"); 176 checkEqual("default setting", 177 fm.getLocationAsPaths(m), out1.resolve("m")); 178 179 Path override1 = Files.createDirectories(base.resolve("override1")); 180 fm.setLocationForModule(locn, "m", List.of(override1)); 181 checkEqual("override setting 1", 182 fm.getLocationAsPaths(m), override1); 183 184 Path override2 = Files.createDirectories(base.resolve("override2")); 185 fm.setLocationFromPaths(m, List.of(override2)); 186 checkEqual("override setting 2", 187 fm.getLocationAsPaths(m), override2); 188 189 Path out2 = Files.createDirectories(base.resolve("out2")); 190 fm.setLocationFromPaths(locn, List.of(out2)); 191 192 checkEqual("updated setting", 193 fm.getLocationAsPaths(m), out2.resolve("m")); 194 } 195 } 196 197 @Test 198 public void testOutput_invalid(Path base) throws IOException { 199 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 200 Location locn = StandardLocation.CLASS_OUTPUT; 201 // set a top default 202 Path out1 = Files.createDirectories(base.resolve("out1")); 203 fm.setLocationFromPaths(locn, List.of(out1)); 204 // getLocnForModule 205 Location m = fm.getLocationForModule(locn, "m"); 206 checkEqual("default setting", 207 fm.getLocationAsPaths(m), out1.resolve("m")); 208 209 checkException("empty arg list", 210 IllegalArgumentException.class, "empty path for directory", 211 () -> fm.setLocationFromPaths(m, Collections.emptyList())); 212 213 Path out2 = Files.createDirectories(base.resolve("out2")); 214 checkException("empty arg list", 215 IllegalArgumentException.class, "path too long for directory", 216 () -> fm.setLocationFromPaths(m, List.of(out2, out2))); 217 218 Path notExist = base.resolve("notExist"); 219 checkException("not exist", 220 FileNotFoundException.class, notExist + ": does not exist", 221 () -> fm.setLocationFromPaths(m, List.of(notExist))); 222 223 Path file = Files.createFile(base.resolve("file.txt")); 224 checkException("not exist", 225 IOException.class, file + ": not a directory", 226 () -> fm.setLocationFromPaths(m, List.of(file))); 227 } 228 } 229 230 @Test 231 public void testOutput_nested(Path base) throws IOException { 232 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 233 Location locn = StandardLocation.CLASS_OUTPUT; 234 235 Path out1 = Files.createDirectories(base.resolve("out1")); 236 fm.setLocationForModule(locn, "m", List.of(out1)); 237 238 Location m = fm.getLocationForModule(locn, "m"); 239 checkEqual("initial setting", 240 fm.getLocationAsPaths(m), out1); 241 242 Path out2 = Files.createDirectories(base.resolve("out2")); 243 checkException("create nested module", 244 UnsupportedOperationException.class, "not supported for CLASS_OUTPUT[m]", 245 () -> fm.setLocationForModule(m, "x", List.of(out2))); 246 } 247 } 248 249 @Test 250 public void testSystemModules(Path base) throws IOException { 251 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 252 Location locn = StandardLocation.SYSTEM_MODULES; 253 254 Location javaCompiler = fm.getLocationForModule(locn, "java.compiler"); 255 // cannot easily verify default setting: could be jrt: or exploded image 256 257 Path override1 = Files.createDirectories(base.resolve("override1")); 258 fm.setLocationForModule(locn, "java.compiler", List.of(override1)); 259 checkEqual("override setting 1", 260 fm.getLocationAsPaths(javaCompiler), override1); 261 262 Path override2 = Files.createDirectories(base.resolve("override2")); 263 fm.setLocationFromPaths(javaCompiler, List.of(override2)); 264 checkEqual("override setting 2", 265 fm.getLocationAsPaths(javaCompiler), override2); 266 } 267 } 268 269 @Test 270 public void testTemplate(Path base) { 271 // set a top default 272 // getLocnForModule 273 // set a value 274 // getLocnForModule 275 // reset 276 // getLocationForModule 277 } 278 279 interface RunnableWithException { 280 public void run() throws Exception; 281 } 282 283 void checkException(String message, 284 Class<? extends Throwable> expectedException, String expectedMessage, 285 RunnableWithException r) { 286 try { 287 r.run(); 288 error(message + ": expected exception not thrown: " + expectedException); 289 } catch (Exception | Error t) { 290 if (expectedException.isAssignableFrom(t.getClass())) { 291 checkEqual("exception message", 292 t.getMessage(), expectedMessage); 293 294 } else { 295 error(message + ": unexpected exception\n" 296 + "expect: " + expectedException + "\n" 297 + " found: " + t); 298 } 299 } 300 } 301 302 void checkEqual(String message, Iterable<? extends Path> found, Path... expect) { 303 List<Path> fList = asList(found); 304 List<Path> eList = List.of(expect); 305 if (!Objects.equals(fList, fList)) { 306 error(message + ": lists not equal\n" 307 + "expect: " + eList + "\n" 308 + " found: " + fList); 309 } 310 } 311 312 void checkEqual(String message, String found, String expect) { 313 if (!Objects.equals(found, expect)) { 314 error(message + ": strings not equal\n" 315 + "expect: " + expect + "\n" 316 + " found: " + found); 317 } 318 } 319 320 List<Path> asList(Iterable<? extends Path> a) { 321 List<Path> list = new ArrayList<>(); 322 for (Path p : a) { 323 list.add(p); 324 } 325 return list; 326 } 327} 328 329