1 /* 2 * Copyright (c) 2015, 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 import java.io.*; 25 import java.lang.reflect.Method; 26 import java.nio.file.FileVisitResult; 27 import java.nio.file.Files; 28 import java.nio.file.Path; 29 import java.nio.file.Paths; 30 import java.nio.file.SimpleFileVisitor; 31 import java.nio.file.attribute.BasicFileAttributes; 32 import java.util.*; 33 import java.util.function.Consumer; 34 import java.util.jar.JarEntry; 35 import java.util.jar.JarInputStream; 36 import java.util.jar.Manifest; 37 import java.util.regex.Pattern; 38 import java.util.stream.Stream; 39 40 import jdk.testlibrary.FileUtils; 41 import jdk.testlibrary.JDKToolFinder; 42 import org.testng.annotations.BeforeTest; 43 import org.testng.annotations.Test; 44 45 import static java.lang.String.format; 46 import static java.lang.System.out; 47 48 /* 49 * @test 50 * @library /lib/testlibrary 51 * @modules jdk.compiler 52 * jdk.jartool 53 * @build jdk.testlibrary.FileUtils jdk.testlibrary.JDKToolFinder 54 * @compile Basic.java 55 * @run testng Basic 56 * @summary Tests for plain Modular jars & Multi-Release Modular jars 57 */ 58 59 public class Basic { 60 static final Path TEST_SRC = Paths.get(System.getProperty("test.src", ".")); 61 static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", ".")); 62 static final Path MODULE_CLASSES = TEST_CLASSES.resolve("build"); 63 static final Path MRJAR_DIR = MODULE_CLASSES.resolve("mrjar"); 64 65 static final String VM_OPTIONS = System.getProperty("test.vm.opts", ""); 66 static final String TOOL_VM_OPTIONS = System.getProperty("test.tool.vm.opts", ""); 67 static final String JAVA_OPTIONS = System.getProperty("test.java.opts", ""); 68 69 // Details based on the checked in module source 70 static TestModuleData FOO = new TestModuleData("foo", 71 "1.123", 72 "jdk.test.foo.Foo", 73 "Hello World!!!", 74 null, // no hashes 75 Set.of("java.base"), 76 Set.of("jdk.test.foo"), 77 null, // no uses 78 null, // no provides 79 Set.of("jdk.test.foo.internal")); 80 static TestModuleData BAR = new TestModuleData("bar", 81 "4.5.6.7", 82 "jdk.test.bar.Bar", 83 "Hello from Bar!", 84 null, // no hashes 85 Set.of("java.base", "foo"), 86 null, // no exports 87 null, // no uses 88 null, // no provides 89 Set.of("jdk.test.bar", 90 "jdk.test.bar.internal")); 91 92 static class TestModuleData { 93 final String moduleName; 94 final Set<String> requires; 95 final Set<String> exports; 96 final Set<String> uses; 97 final Set<String> provides; 98 final String mainClass; 99 final String version; 100 final String message; 101 final String hashes; 102 final Set<String> conceals; 103 104 TestModuleData(String mn, String v, String mc, String m, String h, 105 Set<String> requires, Set<String> exports, Set<String> uses, 106 Set<String> provides, Set<String> conceals) { 107 moduleName = mn; mainClass = mc; version = v; message = m; hashes = h; 108 this.requires = requires; 109 this.exports = exports; 110 this.uses = uses; 111 this.provides = provides; 112 this.conceals = conceals; 113 } 114 static TestModuleData from(String s) { 115 try { 116 BufferedReader reader = new BufferedReader(new StringReader(s)); 117 String line; 118 String message = null; 119 String name = null, version = null, mainClass = null; 120 String hashes = null; 121 Set<String> requires, exports, uses, provides, conceals; 122 requires = exports = uses = provides = conceals = null; 123 while ((line = reader.readLine()) != null) { 124 if (line.startsWith("message:")) { 125 message = line.substring("message:".length()); 126 } else if (line.startsWith("nameAndVersion:")) { 127 line = line.substring("nameAndVersion:".length()); 128 int i = line.indexOf('@'); 129 if (i != -1) { 130 name = line.substring(0, i); 131 version = line.substring(i + 1, line.length()); 132 } else { 133 name = line; 134 } 135 } else if (line.startsWith("mainClass:")) { 136 mainClass = line.substring("mainClass:".length()); 137 } else if (line.startsWith("requires:")) { 138 line = line.substring("requires:".length()); 139 requires = stringToSet(line); 140 } else if (line.startsWith("exports:")) { 141 line = line.substring("exports:".length()); 142 exports = stringToSet(line); 143 } else if (line.startsWith("uses:")) { 144 line = line.substring("uses:".length()); 145 uses = stringToSet(line); 146 } else if (line.startsWith("provides:")) { 147 line = line.substring("provides:".length()); 148 provides = stringToSet(line); 149 } else if (line.startsWith("hashes:")) { 150 hashes = line.substring("hashes:".length()); 151 } else if (line.startsWith("conceals:")) { 152 line = line.substring("conceals:".length()); 153 conceals = stringToSet(line); 154 } else { 155 throw new AssertionError("Unknown value " + line); 156 } 157 } 158 159 return new TestModuleData(name, version, mainClass, message, 160 hashes, requires, exports, uses, 161 provides, conceals); 162 } catch (IOException x) { 163 throw new UncheckedIOException(x); 164 } 165 } 166 static Set<String> stringToSet(String commaList) { 167 Set<String> s = new HashSet<>(); 168 int i = commaList.indexOf(','); 169 if (i != -1) { 170 String[] p = commaList.split(","); 171 Stream.of(p).forEach(s::add); 172 } else { 173 s.add(commaList); 174 } 175 return s; 176 } 177 } 178 179 static void assertModuleData(Result r, TestModuleData expected) { 180 //out.printf("%s%n", r.output); 181 TestModuleData received = TestModuleData.from(r.output); 182 if (expected.message != null) 183 assertTrue(expected.message.equals(received.message), 184 "Expected message:", expected.message, ", got:", received.message); 185 assertTrue(expected.moduleName.equals(received.moduleName), 186 "Expected moduleName: ", expected.moduleName, ", got:", received.moduleName); 187 assertTrue(expected.version.equals(received.version), 188 "Expected version: ", expected.version, ", got:", received.version); 189 assertTrue(expected.mainClass.equals(received.mainClass), 190 "Expected mainClass: ", expected.mainClass, ", got:", received.mainClass); 191 assertSetsEqual(expected.requires, received.requires); 192 assertSetsEqual(expected.exports, received.exports); 193 assertSetsEqual(expected.uses, received.uses); 194 assertSetsEqual(expected.provides, received.provides); 195 assertSetsEqual(expected.conceals, received.conceals); 196 } 197 198 static void assertSetsEqual(Set<String> s1, Set<String> s2) { 199 if (s1 == null && s2 == null) // none expected, or received 200 return; 201 assertTrue(s1.size() == s2.size(), 202 "Unexpected set size difference: ", s1.size(), ", ", s2.size()); 203 s1.forEach(p -> assertTrue(s2.contains(p), "Expected ", p, ", in ", s2)); 204 } 205 206 @BeforeTest 207 public void compileModules() throws Exception { 208 compileModule(FOO.moduleName); 209 compileModule(BAR.moduleName, MODULE_CLASSES); 210 compileModule("baz"); // for service provider consistency checking 211 212 setupMRJARModuleInfo(FOO.moduleName); 213 setupMRJARModuleInfo(BAR.moduleName); 214 setupMRJARModuleInfo("baz"); 215 } 216 217 @Test 218 public void createFoo() throws IOException { 219 Path mp = Paths.get("createFoo"); 220 createTestDir(mp); 221 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 222 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 223 224 jar("--create", 225 "--file=" + modularJar.toString(), 226 "--main-class=" + FOO.mainClass, 227 "--module-version=" + FOO.version, 228 "--no-manifest", 229 "-C", modClasses.toString(), ".") 230 .assertSuccess(); 231 java(mp, FOO.moduleName + "/" + FOO.mainClass) 232 .assertSuccess() 233 .resultChecker(r -> assertModuleData(r, FOO)); 234 try (InputStream fis = Files.newInputStream(modularJar); 235 JarInputStream jis = new JarInputStream(fis)) { 236 assertTrue(!jarContains(jis, "./"), 237 "Unexpected ./ found in ", modularJar.toString()); 238 } 239 } 240 241 /** Similar to createFoo, but with a Multi-Release Modular jar. */ 242 @Test 243 public void createMRMJarFoo() throws IOException { 244 Path mp = Paths.get("createMRMJarFoo"); 245 createTestDir(mp); 246 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 247 Path mrjarDir = MRJAR_DIR.resolve(FOO.moduleName); 248 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 249 250 // Positive test, create 251 jar("--create", 252 "--file=" + modularJar.toString(), 253 "--main-class=" + FOO.mainClass, 254 "--module-version=" + FOO.version, 255 "-m", mrjarDir.resolve("META-INF/MANIFEST.MF").toRealPath().toString(), 256 "-C", mrjarDir.toString(), "META-INF/versions/9/module-info.class", 257 "-C", modClasses.toString(), ".") 258 .assertSuccess(); 259 java(mp, FOO.moduleName + "/" + FOO.mainClass) 260 .assertSuccess() 261 .resultChecker(r -> assertModuleData(r, FOO)); 262 } 263 264 265 @Test 266 public void updateFoo() throws IOException { 267 Path mp = Paths.get("updateFoo"); 268 createTestDir(mp); 269 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 270 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 271 272 jar("--create", 273 "--file=" + modularJar.toString(), 274 "--no-manifest", 275 "-C", modClasses.toString(), "jdk") 276 .assertSuccess(); 277 jar("--update", 278 "--file=" + modularJar.toString(), 279 "--main-class=" + FOO.mainClass, 280 "--module-version=" + FOO.version, 281 "--no-manifest", 282 "-C", modClasses.toString(), "module-info.class") 283 .assertSuccess(); 284 java(mp, FOO.moduleName + "/" + FOO.mainClass) 285 .assertSuccess() 286 .resultChecker(r -> assertModuleData(r, FOO)); 287 } 288 289 @Test 290 public void updateMRMJarFoo() throws IOException { 291 Path mp = Paths.get("updateMRMJarFoo"); 292 createTestDir(mp); 293 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 294 Path mrjarDir = MRJAR_DIR.resolve(FOO.moduleName); 295 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 296 297 jar("--create", 298 "--file=" + modularJar.toString(), 299 "--no-manifest", 300 "-C", modClasses.toString(), "jdk") 301 .assertSuccess(); 302 jar("--update", 303 "--file=" + modularJar.toString(), 304 "--main-class=" + FOO.mainClass, 305 "--module-version=" + FOO.version, 306 "-m", mrjarDir.resolve("META-INF/MANIFEST.MF").toRealPath().toString(), 307 "-C", mrjarDir.toString(), "META-INF/versions/9/module-info.class", 308 "-C", modClasses.toString(), "module-info.class") 309 .assertSuccess(); 310 java(mp, FOO.moduleName + "/" + FOO.mainClass) 311 .assertSuccess() 312 .resultChecker(r -> assertModuleData(r, FOO)); 313 } 314 315 @Test 316 public void partialUpdateFooMainClass() throws IOException { 317 Path mp = Paths.get("partialUpdateFooMainClass"); 318 createTestDir(mp); 319 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 320 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 321 322 // A "bad" main class in first create ( and no version ) 323 jar("--create", 324 "--file=" + modularJar.toString(), 325 "--main-class=" + "IAmNotTheEntryPoint", 326 "--no-manifest", 327 "-C", modClasses.toString(), ".") // includes module-info.class 328 .assertSuccess(); 329 jar("--update", 330 "--file=" + modularJar.toString(), 331 "--main-class=" + FOO.mainClass, 332 "--module-version=" + FOO.version, 333 "--no-manifest") 334 .assertSuccess(); 335 java(mp, FOO.moduleName + "/" + FOO.mainClass) 336 .assertSuccess() 337 .resultChecker(r -> assertModuleData(r, FOO)); 338 } 339 340 @Test 341 public void partialUpdateFooVersion() throws IOException { 342 Path mp = Paths.get("partialUpdateFooVersion"); 343 createTestDir(mp); 344 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 345 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 346 347 // A "bad" version in first create ( and no main class ) 348 jar("--create", 349 "--file=" + modularJar.toString(), 350 "--module-version=" + "100000000", 351 "--no-manifest", 352 "-C", modClasses.toString(), ".") // includes module-info.class 353 .assertSuccess(); 354 jar("--update", 355 "--file=" + modularJar.toString(), 356 "--main-class=" + FOO.mainClass, 357 "--module-version=" + FOO.version, 358 "--no-manifest") 359 .assertSuccess(); 360 java(mp, FOO.moduleName + "/" + FOO.mainClass) 361 .assertSuccess() 362 .resultChecker(r -> assertModuleData(r, FOO)); 363 } 364 365 @Test 366 public void partialUpdateFooNotAllFiles() throws IOException { 367 Path mp = Paths.get("partialUpdateFooNotAllFiles"); 368 createTestDir(mp); 369 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 370 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 371 372 // Not all files, and none from non-exported packages, 373 // i.e. no concealed list in first create 374 jar("--create", 375 "--file=" + modularJar.toString(), 376 "--no-manifest", 377 "-C", modClasses.toString(), "module-info.class", 378 "-C", modClasses.toString(), "jdk/test/foo/Foo.class") 379 .assertSuccess(); 380 jar("--update", 381 "--file=" + modularJar.toString(), 382 "--main-class=" + FOO.mainClass, 383 "--module-version=" + FOO.version, 384 "--no-manifest", 385 "-C", modClasses.toString(), "jdk/test/foo/internal/Message.class") 386 .assertSuccess(); 387 java(mp, FOO.moduleName + "/" + FOO.mainClass) 388 .assertSuccess() 389 .resultChecker(r -> assertModuleData(r, FOO)); 390 } 391 392 @Test 393 public void partialUpdateMRMJarFooNotAllFiles() throws IOException { 394 Path mp = Paths.get("partialUpdateMRMJarFooNotAllFiles"); 395 createTestDir(mp); 396 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 397 Path mrjarDir = MRJAR_DIR.resolve(FOO.moduleName); 398 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 399 400 jar("--create", 401 "--file=" + modularJar.toString(), 402 "--module-version=" + FOO.version, 403 "-C", modClasses.toString(), ".") 404 .assertSuccess(); 405 jar("--update", 406 "--file=" + modularJar.toString(), 407 "--main-class=" + FOO.mainClass, 408 "-m", mrjarDir.resolve("META-INF/MANIFEST.MF").toRealPath().toString(), 409 "-C", mrjarDir.toString(), "META-INF/versions/9/module-info.class") 410 .assertSuccess(); 411 java(mp, FOO.moduleName + "/" + FOO.mainClass) 412 .assertSuccess() 413 .resultChecker(r -> assertModuleData(r, FOO)); 414 } 415 416 @Test 417 public void partialUpdateFooAllFilesAndAttributes() throws IOException { 418 Path mp = Paths.get("partialUpdateFooAllFilesAndAttributes"); 419 createTestDir(mp); 420 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 421 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 422 423 // all attributes and files 424 jar("--create", 425 "--file=" + modularJar.toString(), 426 "--main-class=" + FOO.mainClass, 427 "--module-version=" + FOO.version, 428 "--no-manifest", 429 "-C", modClasses.toString(), ".") 430 .assertSuccess(); 431 jar("--update", 432 "--file=" + modularJar.toString(), 433 "--main-class=" + FOO.mainClass, 434 "--module-version=" + FOO.version, 435 "--no-manifest", 436 "-C", modClasses.toString(), ".") 437 .assertSuccess(); 438 java(mp, FOO.moduleName + "/" + FOO.mainClass) 439 .assertSuccess() 440 .resultChecker(r -> assertModuleData(r, FOO)); 441 } 442 443 @Test 444 public void partialUpdateFooModuleInfo() throws IOException { 445 Path mp = Paths.get("partialUpdateFooModuleInfo"); 446 createTestDir(mp); 447 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 448 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 449 Path barModInfo = MODULE_CLASSES.resolve(BAR.moduleName); 450 451 jar("--create", 452 "--file=" + modularJar.toString(), 453 "--main-class=" + FOO.mainClass, 454 "--module-version=" + FOO.version, 455 "--no-manifest", 456 "-C", modClasses.toString(), ".") 457 .assertSuccess(); 458 jar("--update", 459 "--file=" + modularJar.toString(), 460 "--no-manifest", 461 "-C", barModInfo.toString(), "module-info.class") // stuff in bar's info 462 .assertSuccess(); 463 jar("-d", 464 "--file=" + modularJar.toString()) 465 .assertSuccess() 466 .resultChecker(r -> { 467 // Expect similar output: "bar, requires mandated foo, ... 468 // conceals jdk.test.foo, conceals jdk.test.foo.internal" 469 Pattern p = Pattern.compile("\\s+bar\\s+requires\\s++foo"); 470 assertTrue(p.matcher(r.output).find(), 471 "Expecting to find \"bar, requires foo,...\"", 472 "in output, but did not: [" + r.output + "]"); 473 p = Pattern.compile( 474 "conceals\\s+jdk.test.foo\\s+conceals\\s+jdk.test.foo.internal"); 475 assertTrue(p.matcher(r.output).find(), 476 "Expecting to find \"conceals jdk.test.foo,...\"", 477 "in output, but did not: [" + r.output + "]"); 478 }); 479 } 480 481 @Test 482 public void hashBarInFooModule() throws IOException { 483 Path mp = Paths.get("dependencesFooBar"); 484 createTestDir(mp); 485 486 Path modClasses = MODULE_CLASSES.resolve(BAR.moduleName); 487 Path modularJar = mp.resolve(BAR.moduleName + ".jar"); 488 jar("--create", 489 "--file=" + modularJar.toString(), 490 "--main-class=" + BAR.mainClass, 491 "--module-version=" + BAR.version, 492 "--no-manifest", 493 "-C", modClasses.toString(), ".") 494 .assertSuccess(); 495 496 modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 497 modularJar = mp.resolve(FOO.moduleName + ".jar"); 498 jar("--create", 499 "--file=" + modularJar.toString(), 500 "--main-class=" + FOO.mainClass, 501 "--module-version=" + FOO.version, 502 "--module-path=" + mp.toString(), 503 "--hash-modules=" + "bar", 504 "--no-manifest", 505 "-C", modClasses.toString(), ".") 506 .assertSuccess(); 507 508 java(mp, BAR.moduleName + "/" + BAR.mainClass, 509 "--add-exports", "java.base/jdk.internal.module=bar") 510 .assertSuccess() 511 .resultChecker(r -> { 512 assertModuleData(r, BAR); 513 TestModuleData received = TestModuleData.from(r.output); 514 assertTrue(received.hashes != null, "Expected non-null hashes value."); 515 }); 516 } 517 518 @Test 519 public void invalidHashInFooModule() throws IOException { 520 Path mp = Paths.get("badDependencyFooBar"); 521 createTestDir(mp); 522 523 Path barClasses = MODULE_CLASSES.resolve(BAR.moduleName); 524 Path barJar = mp.resolve(BAR.moduleName + ".jar"); 525 jar("--create", 526 "--file=" + barJar.toString(), 527 "--main-class=" + BAR.mainClass, 528 "--module-version=" + BAR.version, 529 "--no-manifest", 530 "-C", barClasses.toString(), ".").assertSuccess(); 531 532 Path fooClasses = MODULE_CLASSES.resolve(FOO.moduleName); 533 Path fooJar = mp.resolve(FOO.moduleName + ".jar"); 534 jar("--create", 535 "--file=" + fooJar.toString(), 536 "--main-class=" + FOO.mainClass, 537 "--module-version=" + FOO.version, 538 "-p", mp.toString(), // test short-form 539 "--hash-modules=" + "bar", 540 "--no-manifest", 541 "-C", fooClasses.toString(), ".").assertSuccess(); 542 543 // Rebuild bar.jar with a change that will cause its hash to be different 544 FileUtils.deleteFileWithRetry(barJar); 545 jar("--create", 546 "--file=" + barJar.toString(), 547 "--main-class=" + BAR.mainClass, 548 "--module-version=" + BAR.version + ".1", // a newer version 549 "--no-manifest", 550 "-C", barClasses.toString(), ".").assertSuccess(); 551 552 java(mp, BAR.moduleName + "/" + BAR.mainClass, 553 "--add-exports", "java.base/jdk.internal.module=bar") 554 .assertFailure() 555 .resultChecker(r -> { 556 // Expect similar output: "java.lang.module.ResolutionException: Hash 557 // of bar (WdktSIQSkd4+CEacpOZoeDrCosMATNrIuNub9b5yBeo=) differs to 558 // expected hash (iepvdv8xTeVrFgMtUhcFnmetSub6qQHCHc92lSaSEg0=)" 559 Pattern p = Pattern.compile(".*Hash of bar.*differs to expected hash.*"); 560 assertTrue(p.matcher(r.output).find(), 561 "Expecting error message containing \"Hash of bar ... differs to" 562 + " expected hash...\" but got: [", r.output + "]"); 563 }); 564 } 565 566 @Test 567 public void badOptionsFoo() throws IOException { 568 Path mp = Paths.get("badOptionsFoo"); 569 createTestDir(mp); 570 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 571 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 572 573 jar("--create", 574 "--file=" + modularJar.toString(), 575 "--module-version=" + 1.1, // no module-info.class 576 "-C", modClasses.toString(), "jdk") 577 .assertFailure(); // TODO: expected failure message 578 579 jar("--create", 580 "--file=" + modularJar.toString(), 581 "--hash-modules=" + ".*", // no module-info.class 582 "-C", modClasses.toString(), "jdk") 583 .assertFailure(); // TODO: expected failure message 584 } 585 586 @Test 587 public void servicesCreateWithoutFailure() throws IOException { 588 Path mp = Paths.get("servicesCreateWithoutFailure"); 589 createTestDir(mp); 590 Path modClasses = MODULE_CLASSES.resolve("baz"); 591 Path modularJar = mp.resolve("baz" + ".jar"); 592 593 // Positive test, create 594 jar("--create", 595 "--file=" + modularJar.toString(), 596 "-C", modClasses.toString(), "module-info.class", 597 "-C", modClasses.toString(), "jdk/test/baz/BazService.class", 598 "-C", modClasses.toString(), "jdk/test/baz/internal/BazServiceImpl.class") 599 .assertSuccess(); 600 } 601 602 @Test 603 public void servicesCreateWithoutServiceImpl() throws IOException { 604 Path mp = Paths.get("servicesWithoutServiceImpl"); 605 createTestDir(mp); 606 Path modClasses = MODULE_CLASSES.resolve("baz"); 607 Path modularJar = mp.resolve("baz" + ".jar"); 608 609 // Omit service impl 610 jar("--create", 611 "--file=" + modularJar.toString(), 612 "-C", modClasses.toString(), "module-info.class", 613 "-C", modClasses.toString(), "jdk/test/baz/BazService.class") 614 .assertFailure(); 615 } 616 617 @Test 618 public void servicesUpdateWithoutFailure() throws IOException { 619 Path mp = Paths.get("servicesUpdateWithoutFailure"); 620 createTestDir(mp); 621 Path modClasses = MODULE_CLASSES.resolve("baz"); 622 Path modularJar = mp.resolve("baz" + ".jar"); 623 624 // Positive test, update 625 jar("--create", 626 "--file=" + modularJar.toString(), 627 "-C", modClasses.toString(), "jdk/test/baz/BazService.class", 628 "-C", modClasses.toString(), "jdk/test/baz/internal/BazServiceImpl.class") 629 .assertSuccess(); 630 jar("--update", 631 "--file=" + modularJar.toString(), 632 "-C", modClasses.toString(), "module-info.class") 633 .assertSuccess(); 634 } 635 636 @Test 637 public void servicesUpdateWithoutServiceImpl() throws IOException { 638 Path mp = Paths.get("servicesUpdateWithoutServiceImpl"); 639 createTestDir(mp); 640 Path modClasses = MODULE_CLASSES.resolve("baz"); 641 Path modularJar = mp.resolve("baz" + ".jar"); 642 643 // Omit service impl 644 jar("--create", 645 "--file=" + modularJar.toString(), 646 "-C", modClasses.toString(), "jdk/test/baz/BazService.class") 647 .assertSuccess(); 648 jar("--update", 649 "--file=" + modularJar.toString(), 650 "-C", modClasses.toString(), "module-info.class") 651 .assertFailure(); 652 } 653 654 @Test 655 public void servicesCreateWithoutFailureMRMJAR() throws IOException { 656 Path mp = Paths.get("servicesCreateWithoutFailureMRMJAR"); 657 createTestDir(mp); 658 Path modClasses = MODULE_CLASSES.resolve("baz"); 659 Path mrjarDir = MRJAR_DIR.resolve("baz"); 660 Path modularJar = mp.resolve("baz" + ".jar"); 661 662 jar("--create", 663 "--file=" + modularJar.toString(), 664 "-m", mrjarDir.resolve("META-INF/MANIFEST.MF").toRealPath().toString(), 665 "-C", modClasses.toString(), "module-info.class", 666 "-C", mrjarDir.toString(), "META-INF/versions/9/module-info.class", 667 "-C", modClasses.toString(), "jdk/test/baz/BazService.class", 668 "-C", modClasses.toString(), "jdk/test/baz/internal/BazServiceImpl.class") 669 .assertSuccess(); 670 } 671 672 @Test 673 public void printModuleDescriptorFoo() throws IOException { 674 Path mp = Paths.get("printModuleDescriptorFoo"); 675 createTestDir(mp); 676 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 677 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 678 679 jar("--create", 680 "--file=" + modularJar.toString(), 681 "--main-class=" + FOO.mainClass, 682 "--module-version=" + FOO.version, 683 "--no-manifest", 684 "-C", modClasses.toString(), ".") 685 .assertSuccess(); 686 687 for (String option : new String[] {"--print-module-descriptor", "-d" }) { 688 jar(option, 689 "--file=" + modularJar.toString()) 690 .assertSuccess() 691 .resultChecker(r -> 692 assertTrue(r.output.contains(FOO.moduleName + "@" + FOO.version), 693 "Expected to find ", FOO.moduleName + "@" + FOO.version, 694 " in [", r.output, "]") 695 ); 696 } 697 } 698 699 @Test 700 public void printModuleDescriptorFooFromStdin() throws IOException { 701 Path mp = Paths.get("printModuleDescriptorFooFromStdin"); 702 createTestDir(mp); 703 Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); 704 Path modularJar = mp.resolve(FOO.moduleName + ".jar"); 705 706 jar("--create", 707 "--file=" + modularJar.toString(), 708 "--main-class=" + FOO.mainClass, 709 "--module-version=" + FOO.version, 710 "--no-manifest", 711 "-C", modClasses.toString(), ".") 712 .assertSuccess(); 713 714 for (String option : new String[] {"--print-module-descriptor", "-d" }) { 715 jarWithStdin(modularJar.toFile(), 716 option) 717 .assertSuccess() 718 .resultChecker(r -> 719 assertTrue(r.output.contains(FOO.moduleName + "@" + FOO.version), 720 "Expected to find ", FOO.moduleName + "@" + FOO.version, 721 " in [", r.output, "]") 722 ); 723 } 724 } 725 726 // -- Infrastructure 727 728 static Result jarWithStdin(File stdinSource, String... args) { 729 String jar = getJDKTool("jar"); 730 List<String> commands = new ArrayList<>(); 731 commands.add(jar); 732 if (!TOOL_VM_OPTIONS.isEmpty()) { 733 commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1))); 734 } 735 Stream.of(args).forEach(commands::add); 736 ProcessBuilder p = new ProcessBuilder(commands); 737 if (stdinSource != null) 738 p.redirectInput(stdinSource); 739 return run(p); 740 } 741 742 static Result jar(String... args) { 743 return jarWithStdin(null, args); 744 } 745 746 static Path compileModule(String mn) throws IOException { 747 return compileModule(mn, null); 748 } 749 750 static Path compileModule(String mn, Path mp) 751 throws IOException 752 { 753 Path fooSourcePath = TEST_SRC.resolve("src").resolve(mn); 754 Path build = Files.createDirectories(MODULE_CLASSES.resolve(mn)); 755 javac(build, mp, fileList(fooSourcePath)); 756 return build; 757 } 758 759 static void setupMRJARModuleInfo(String moduleName) throws IOException { 760 Path modClasses = MODULE_CLASSES.resolve(moduleName); 761 Path metaInfDir = MRJAR_DIR.resolve(moduleName).resolve("META-INF"); 762 Path versionSection = metaInfDir.resolve("versions").resolve("9"); 763 createTestDir(versionSection); 764 765 Path versionModuleInfo = versionSection.resolve("module-info.class"); 766 System.out.println("copying " + modClasses.resolve("module-info.class") + " to " + versionModuleInfo); 767 Files.copy(modClasses.resolve("module-info.class"), versionModuleInfo); 768 769 Manifest manifest = new Manifest(); 770 manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); 771 manifest.getMainAttributes().putValue("Multi-Release", "true"); 772 try (OutputStream os = Files.newOutputStream(metaInfDir.resolve("MANIFEST.MF"))) { 773 manifest.write(os); 774 } 775 } 776 777 // Re-enable when there is support in javax.tools for module path 778 // static void javac(Path dest, Path... sourceFiles) throws IOException { 779 // out.printf("Compiling %d source files %s%n", sourceFiles.length, 780 // Arrays.asList(sourceFiles)); 781 // JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 782 // try (StandardJavaFileManager fileManager = 783 // compiler.getStandardFileManager(null, null, null)) { 784 // 785 // List<File> files = Stream.of(sourceFiles) 786 // .map(p -> p.toFile()) 787 // .collect(Collectors.toList()); 788 // List<File> dests = Stream.of(dest) 789 // .map(p -> p.toFile()) 790 // .collect(Collectors.toList()); 791 // Iterable<? extends JavaFileObject> compilationUnits = 792 // fileManager.getJavaFileObjectsFromFiles(files); 793 // fileManager.setLocation(StandardLocation.CLASS_OUTPUT, dests); 794 // JavaCompiler.CompilationTask task = 795 // compiler.getTask(null, fileManager, null, null, null, compilationUnits); 796 // boolean passed = task.call(); 797 // if (!passed) 798 // throw new RuntimeException("Error compiling " + files); 799 // } 800 // } 801 802 static void javac(Path dest, Path... sourceFiles) throws IOException { 803 javac(dest, null, sourceFiles); 804 } 805 806 static void javac(Path dest, Path modulePath, Path... sourceFiles) 807 throws IOException 808 { 809 String javac = getJDKTool("javac"); 810 811 List<String> commands = new ArrayList<>(); 812 commands.add(javac); 813 if (!TOOL_VM_OPTIONS.isEmpty()) { 814 commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1))); 815 } 816 commands.add("-d"); 817 commands.add(dest.toString()); 818 if (dest.toString().contains("bar")) { 819 commands.add("--add-exports"); 820 commands.add("java.base/jdk.internal.module=bar"); 821 } 822 if (modulePath != null) { 823 commands.add("--module-path"); 824 commands.add(modulePath.toString()); 825 } 826 Stream.of(sourceFiles).map(Object::toString).forEach(x -> commands.add(x)); 827 828 quickFail(run(new ProcessBuilder(commands))); 829 } 830 831 static Result java(Path modulePath, String entryPoint, String... args) { 832 String java = getJDKTool("java"); 833 834 List<String> commands = new ArrayList<>(); 835 commands.add(java); 836 if (!VM_OPTIONS.isEmpty()) { 837 commands.addAll(Arrays.asList(VM_OPTIONS.split("\\s+", -1))); 838 } 839 if (!JAVA_OPTIONS.isEmpty()) { 840 commands.addAll(Arrays.asList(JAVA_OPTIONS.split("\\s+", -1))); 841 } 842 Stream.of(args).forEach(x -> commands.add(x)); 843 commands.add("--module-path"); 844 commands.add(modulePath.toString()); 845 commands.add("-m"); 846 commands.add(entryPoint); 847 848 return run(new ProcessBuilder(commands)); 849 } 850 851 static Path[] fileList(Path directory) throws IOException { 852 final List<Path> filePaths = new ArrayList<>(); 853 Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { 854 @Override 855 public FileVisitResult visitFile(Path file, 856 BasicFileAttributes attrs) { 857 filePaths.add(file); 858 return FileVisitResult.CONTINUE; 859 } 860 }); 861 return filePaths.toArray(new Path[filePaths.size()]); 862 } 863 864 static void createTestDir(Path p) throws IOException{ 865 if (Files.exists(p)) 866 FileUtils.deleteFileTreeWithRetry(p); 867 Files.createDirectories(p); 868 } 869 870 static boolean jarContains(JarInputStream jis, String entryName) 871 throws IOException 872 { 873 JarEntry e; 874 while((e = jis.getNextJarEntry()) != null) { 875 if (e.getName().equals(entryName)) 876 return true; 877 } 878 return false; 879 } 880 881 static void quickFail(Result r) { 882 if (r.ec != 0) 883 throw new RuntimeException(r.output); 884 } 885 886 static Result run(ProcessBuilder pb) { 887 Process p; 888 out.printf("Running: %s%n", pb.command()); 889 try { 890 p = pb.start(); 891 } catch (IOException e) { 892 throw new RuntimeException( 893 format("Couldn't start process '%s'", pb.command()), e); 894 } 895 896 String output; 897 try { 898 output = toString(p.getInputStream(), p.getErrorStream()); 899 } catch (IOException e) { 900 throw new RuntimeException( 901 format("Couldn't read process output '%s'", pb.command()), e); 902 } 903 904 try { 905 p.waitFor(); 906 } catch (InterruptedException e) { 907 throw new RuntimeException( 908 format("Process hasn't finished '%s'", pb.command()), e); 909 } 910 return new Result(p.exitValue(), output); 911 } 912 913 static final String DEFAULT_IMAGE_BIN = System.getProperty("java.home") 914 + File.separator + "bin" + File.separator; 915 916 static String getJDKTool(String name) { 917 try { 918 return JDKToolFinder.getJDKTool(name); 919 } catch (Exception x) { 920 return DEFAULT_IMAGE_BIN + name; 921 } 922 } 923 924 static String toString(InputStream in1, InputStream in2) throws IOException { 925 try (ByteArrayOutputStream dst = new ByteArrayOutputStream(); 926 InputStream concatenated = new SequenceInputStream(in1, in2)) { 927 concatenated.transferTo(dst); 928 return new String(dst.toByteArray(), "UTF-8"); 929 } 930 } 931 932 static class Result { 933 final int ec; 934 final String output; 935 936 private Result(int ec, String output) { 937 this.ec = ec; 938 this.output = output; 939 } 940 Result assertSuccess() { 941 assertTrue(ec == 0, "Expected ec 0, got: ", ec, " , output [", output, "]"); 942 return this; 943 } 944 Result assertFailure() { 945 assertTrue(ec != 0, "Expected ec != 0, got:", ec, " , output [", output, "]"); 946 return this; 947 } 948 Result resultChecker(Consumer<Result> r) { r.accept(this); return this; } 949 } 950 951 static void assertTrue(boolean cond, Object ... failedArgs) { 952 if (cond) 953 return; 954 StringBuilder sb = new StringBuilder(); 955 for (Object o : failedArgs) 956 sb.append(o); 957 org.testng.Assert.assertTrue(false, sb.toString()); 958 } 959 960 // Standalone entry point. 961 public static void main(String[] args) throws Throwable { 962 Basic test = new Basic(); 963 test.compileModules(); 964 for (Method m : Basic.class.getDeclaredMethods()) { 965 if (m.getAnnotation(Test.class) != null) { 966 System.out.println("Invoking " + m.getName()); 967 m.invoke(test); 968 } 969 } 970 } 971 }