171 throw new Exception("expected output not found"); 172 } 173 174 /** 175 * Verify that a multi-module loop is detected. 176 */ 177 @Test 178 public void testRequiresLoop(Path base) throws Exception { 179 Path src = base.resolve("src"); 180 Path src_m1 = src.resolve("m1"); 181 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { requires m2; }"); 182 Path src_m2 = src.resolve("m2"); 183 tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires m3; }"); 184 Path src_m3 = src.resolve("m3"); 185 tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }"); 186 187 Path classes = base.resolve("classes"); 188 Files.createDirectories(classes); 189 190 String log = new JavacTask(tb) 191 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) 192 .outdir(classes) 193 .files(findJavaFiles(src)) 194 .run(Task.Expect.FAIL) 195 .writeAll() 196 .getOutput(Task.OutputKind.DIRECT); 197 198 if (!log.contains("module-info.java:1:22: compiler.err.cyclic.requires: m3")) 199 throw new Exception("expected output not found"); 200 } 201 202 /** 203 * Verify that a multi-module loop is detected. 204 */ 205 @Test 206 public void testRequiresPublicLoop(Path base) throws Exception { 207 Path src = base.resolve("src"); 208 Path src_m1 = src.resolve("m1"); 209 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { requires m2; }"); 210 Path src_m2 = src.resolve("m2"); 211 tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires public m3; }"); 212 Path src_m3 = src.resolve("m3"); 213 tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }"); 214 215 Path classes = base.resolve("classes"); 216 Files.createDirectories(classes); 217 218 String log = new JavacTask(tb) 219 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) 220 .outdir(classes) 221 .files(findJavaFiles(src)) 222 .run(Task.Expect.FAIL) 223 .writeAll() 224 .getOutput(Task.OutputKind.DIRECT); 225 226 if (!log.contains("module-info.java:1:29: compiler.err.cyclic.requires: m3")) 227 throw new Exception("expected output not found"); 228 } 229 230 /** 231 * Verify that duplicate requires are detected. 232 */ 233 @Test 234 public void testDuplicateRequires(Path base) throws Exception { 235 Path src = base.resolve("src"); 236 Path src_m1 = src.resolve("m1"); 237 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { }"); 238 Path src_m2 = src.resolve("m2"); 239 tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires m1; requires m1; }"); 240 241 Path classes = base.resolve("classes"); 242 Files.createDirectories(classes); 243 244 String log = new JavacTask(tb) 245 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) 246 .outdir(classes) 247 .files(findJavaFiles(src)) 248 .run(Task.Expect.FAIL) 249 .writeAll() 250 .getOutput(Task.OutputKind.DIRECT); 251 252 if (!log.contains("module-info.java:1:35: compiler.err.duplicate.requires: m1")) 253 throw new Exception("expected output not found"); 254 } 255 256 /** 257 * Verify that duplicate exported packages are detected. 258 */ 259 @Test 260 public void testDuplicateExports_packages(Path base) throws Exception { 261 Path src = base.resolve("src"); 262 tb.writeJavaFiles(src, "module m1 { exports p; exports p; }"); 263 264 Path classes = base.resolve("classes"); 265 Files.createDirectories(classes); 272 .writeAll() 273 .getOutput(Task.OutputKind.DIRECT); 274 275 if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p")) 276 throw new Exception("expected output not found"); 277 } 278 279 /** 280 * Verify that duplicate exported packages are detected. 281 */ 282 @Test 283 public void testDuplicateExports_packages2(Path base) throws Exception { 284 Path src = base.resolve("src"); 285 tb.writeJavaFiles(src.resolve("m1"), "module m1 { exports p; exports p to m2; }"); 286 tb.writeJavaFiles(src.resolve("m2"), "module m2 { }"); 287 288 Path classes = base.resolve("classes"); 289 Files.createDirectories(classes); 290 291 String log = new JavacTask(tb) 292 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) 293 .outdir(classes) 294 .files(findJavaFiles(src)) 295 .run(Task.Expect.FAIL) 296 .writeAll() 297 .getOutput(Task.OutputKind.DIRECT); 298 299 if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p")) 300 throw new Exception("expected output not found"); 301 } 302 303 /** 304 * Verify that duplicate exported packages are detected. 305 */ 306 @Test 307 public void testDuplicateExports_modules(Path base) throws Exception { 308 Path src = base.resolve("src"); 309 Path src_m1 = src.resolve("m1"); 310 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { }"); 311 Path src_m2 = src.resolve("m2"); 312 tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { exports p to m1, m1; }"); 313 314 Path classes = base.resolve("classes"); 315 Files.createDirectories(classes); 316 317 String log = new JavacTask(tb) 318 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) 319 .outdir(classes) 320 .files(findJavaFiles(src)) 321 .run(Task.Expect.FAIL) 322 .writeAll() 323 .getOutput(Task.OutputKind.DIRECT); 324 325 if (!log.contains("module-info.java:1:30: compiler.err.duplicate.exports: m1")) 326 throw new Exception("expected output not found"); 327 } 328 329 /** 330 * Verify that annotations are not permitted at 331 * any of the module names or the package names. 332 */ 333 @Test 334 public void testAnnotations(Path base) throws Exception { 335 Path src = base.resolve("src"); 336 Path src_m1 = src.resolve("m1.sub"); 337 Path classes = base.resolve("classes"); 338 Files.createDirectories(classes); 339 340 String code = "module @m1.@sub { " + 341 "requires @p1.@p2; " + 342 "exports @p1.@p2; " + 343 "exports @p1.@p2 to @m2.@sub; " + 344 "exports @p1.@p2 to @m2.@sub, @m3.@sub; " + 345 "uses @p1.@Interface; " + 346 "provides @p1.@Interface with @p2.@Concrete; " + 347 "}"; 348 String[] splittedCode = code.split("@"); 349 int length = splittedCode.length; 350 String anno = "@Anno "; 351 352 for (int i = 1; i < length; i++) { 353 String preAnno = String.join("", Arrays.copyOfRange(splittedCode, 0, i)); 354 String postAnno = String.join("", Arrays.copyOfRange(splittedCode, i, length)); 355 String moduleInfo = preAnno + anno + postAnno; 356 tb.writeFile(src_m1.resolve("module-info.java"), moduleInfo); 357 358 String log = new JavacTask(tb) 359 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) 360 .outdir(classes) 361 .files(findJavaFiles(src)) 362 .run(Task.Expect.FAIL) 363 .writeAll() 364 .getOutput(Task.OutputKind.DIRECT); 365 366 if (!log.matches("(?s)^module\\-info\\.java:\\d+:\\d+: compiler\\.err\\.expected: token\\.identifier.*")) 367 throw new Exception("expected output not found"); 368 } 369 } 370 } | 171 throw new Exception("expected output not found"); 172 } 173 174 /** 175 * Verify that a multi-module loop is detected. 176 */ 177 @Test 178 public void testRequiresLoop(Path base) throws Exception { 179 Path src = base.resolve("src"); 180 Path src_m1 = src.resolve("m1"); 181 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { requires m2; }"); 182 Path src_m2 = src.resolve("m2"); 183 tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires m3; }"); 184 Path src_m3 = src.resolve("m3"); 185 tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }"); 186 187 Path classes = base.resolve("classes"); 188 Files.createDirectories(classes); 189 190 String log = new JavacTask(tb) 191 .options("-XDrawDiagnostics", "--module-source-path", src.toString()) 192 .outdir(classes) 193 .files(findJavaFiles(src)) 194 .run(Task.Expect.FAIL) 195 .writeAll() 196 .getOutput(Task.OutputKind.DIRECT); 197 198 if (!log.contains("module-info.java:1:22: compiler.err.cyclic.requires: m3")) 199 throw new Exception("expected output not found"); 200 } 201 202 /** 203 * Verify that a multi-module loop is detected. 204 */ 205 @Test 206 public void testRequiresPublicLoop(Path base) throws Exception { 207 Path src = base.resolve("src"); 208 Path src_m1 = src.resolve("m1"); 209 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { requires m2; }"); 210 Path src_m2 = src.resolve("m2"); 211 tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires public m3; }"); 212 Path src_m3 = src.resolve("m3"); 213 tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }"); 214 215 Path classes = base.resolve("classes"); 216 Files.createDirectories(classes); 217 218 String log = new JavacTask(tb) 219 .options("-XDrawDiagnostics", "--module-source-path", src.toString()) 220 .outdir(classes) 221 .files(findJavaFiles(src)) 222 .run(Task.Expect.FAIL) 223 .writeAll() 224 .getOutput(Task.OutputKind.DIRECT); 225 226 if (!log.contains("module-info.java:1:29: compiler.err.cyclic.requires: m3")) 227 throw new Exception("expected output not found"); 228 } 229 230 /** 231 * Verify that duplicate requires are detected. 232 */ 233 @Test 234 public void testDuplicateRequires(Path base) throws Exception { 235 Path src = base.resolve("src"); 236 Path src_m1 = src.resolve("m1"); 237 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { }"); 238 Path src_m2 = src.resolve("m2"); 239 tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires m1; requires m1; }"); 240 241 Path classes = base.resolve("classes"); 242 Files.createDirectories(classes); 243 244 String log = new JavacTask(tb) 245 .options("-XDrawDiagnostics", "--module-source-path", src.toString()) 246 .outdir(classes) 247 .files(findJavaFiles(src)) 248 .run(Task.Expect.FAIL) 249 .writeAll() 250 .getOutput(Task.OutputKind.DIRECT); 251 252 if (!log.contains("module-info.java:1:35: compiler.err.duplicate.requires: m1")) 253 throw new Exception("expected output not found"); 254 } 255 256 /** 257 * Verify that duplicate exported packages are detected. 258 */ 259 @Test 260 public void testDuplicateExports_packages(Path base) throws Exception { 261 Path src = base.resolve("src"); 262 tb.writeJavaFiles(src, "module m1 { exports p; exports p; }"); 263 264 Path classes = base.resolve("classes"); 265 Files.createDirectories(classes); 272 .writeAll() 273 .getOutput(Task.OutputKind.DIRECT); 274 275 if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p")) 276 throw new Exception("expected output not found"); 277 } 278 279 /** 280 * Verify that duplicate exported packages are detected. 281 */ 282 @Test 283 public void testDuplicateExports_packages2(Path base) throws Exception { 284 Path src = base.resolve("src"); 285 tb.writeJavaFiles(src.resolve("m1"), "module m1 { exports p; exports p to m2; }"); 286 tb.writeJavaFiles(src.resolve("m2"), "module m2 { }"); 287 288 Path classes = base.resolve("classes"); 289 Files.createDirectories(classes); 290 291 String log = new JavacTask(tb) 292 .options("-XDrawDiagnostics", "--module-source-path", src.toString()) 293 .outdir(classes) 294 .files(findJavaFiles(src)) 295 .run(Task.Expect.FAIL) 296 .writeAll() 297 .getOutput(Task.OutputKind.DIRECT); 298 299 if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p")) 300 throw new Exception("expected output not found"); 301 } 302 303 /** 304 * Verify that duplicate exported packages are detected. 305 */ 306 @Test 307 public void testDuplicateExports_modules(Path base) throws Exception { 308 Path src = base.resolve("src"); 309 Path src_m1 = src.resolve("m1"); 310 tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { }"); 311 Path src_m2 = src.resolve("m2"); 312 tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { exports p to m1, m1; }"); 313 314 Path classes = base.resolve("classes"); 315 Files.createDirectories(classes); 316 317 String log = new JavacTask(tb) 318 .options("-XDrawDiagnostics", "--module-source-path", src.toString()) 319 .outdir(classes) 320 .files(findJavaFiles(src)) 321 .run(Task.Expect.FAIL) 322 .writeAll() 323 .getOutput(Task.OutputKind.DIRECT); 324 325 if (!log.contains("module-info.java:1:30: compiler.err.duplicate.exports: m1")) 326 throw new Exception("expected output not found"); 327 } 328 329 /** 330 * Verify that annotations are not permitted at 331 * any of the module names or the package names. 332 */ 333 @Test 334 public void testAnnotations(Path base) throws Exception { 335 Path src = base.resolve("src"); 336 Path src_m1 = src.resolve("m1.sub"); 337 Path classes = base.resolve("classes"); 338 Files.createDirectories(classes); 339 340 String code = "module @m1.@sub { " + 341 "requires @p1.@p2; " + 342 "exports @p1.@p2; " + 343 "exports @p1.@p2 to @m2.@sub; " + 344 "exports @p1.@p2 to @m2.@sub, @m3.@sub; " + 345 "uses @p1.@Interface; " + 346 "provides @p1.@Interface with @p2.@Concrete; " + 347 "}"; 348 String[] splittedCode = code.split("@"); 349 int length = splittedCode.length; 350 String anno = "@Anno "; 351 352 for (int i = 1; i < length; i++) { 353 String preAnno = String.join("", Arrays.copyOfRange(splittedCode, 0, i)); 354 String postAnno = String.join("", Arrays.copyOfRange(splittedCode, i, length)); 355 String moduleInfo = preAnno + anno + postAnno; 356 tb.writeFile(src_m1.resolve("module-info.java"), moduleInfo); 357 358 String log = new JavacTask(tb) 359 .options("-XDrawDiagnostics", "--module-source-path", src.toString()) 360 .outdir(classes) 361 .files(findJavaFiles(src)) 362 .run(Task.Expect.FAIL) 363 .writeAll() 364 .getOutput(Task.OutputKind.DIRECT); 365 366 if (!log.matches("(?s)^module\\-info\\.java:\\d+:\\d+: compiler\\.err\\.expected: token\\.identifier.*")) 367 throw new Exception("expected output not found"); 368 } 369 } 370 } |