< prev index next >

jdk/test/tools/jar/modularJar/Basic.java

Print this page




 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("-p",
 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             "--modulepath=" + mp.toString(),
 503             "--hash-modules=" + "bar",
 504             "--no-manifest",
 505             "-C", modClasses.toString(), ".")
 506             .assertSuccess();
 507 
 508         java(mp, BAR.moduleName + "/" + BAR.mainClass,
 509              "-XaddExports: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             "--modulepath=" + mp.toString(),
 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              "-XaddExports: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",


 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", "-p" }) {
 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", "-p" }) {
 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         }


 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("-XaddExports:java.base/jdk.internal.module=bar");


 820         if (modulePath != null) {
 821             commands.add("-mp");
 822             commands.add(modulePath.toString());
 823         }
 824         Stream.of(sourceFiles).map(Object::toString).forEach(x -> commands.add(x));
 825 
 826         quickFail(run(new ProcessBuilder(commands)));
 827     }
 828 
 829     static Result java(Path modulePath, String entryPoint, String... args) {
 830         String java = getJDKTool("java");
 831 
 832         List<String> commands = new ArrayList<>();
 833         commands.add(java);
 834         if (!VM_OPTIONS.isEmpty()) {
 835             commands.addAll(Arrays.asList(VM_OPTIONS.split("\\s+", -1)));
 836         }
 837         if (!JAVA_OPTIONS.isEmpty()) {
 838             commands.addAll(Arrays.asList(JAVA_OPTIONS.split("\\s+", -1)));
 839         }
 840         Stream.of(args).forEach(x -> commands.add(x));
 841         commands.add("-mp");
 842         commands.add(modulePath.toString());
 843         commands.add("-m");
 844         commands.add(entryPoint);
 845 
 846         return run(new ProcessBuilder(commands));
 847     }
 848 
 849     static Path[] fileList(Path directory) throws IOException {
 850         final List<Path> filePaths = new ArrayList<>();
 851         Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
 852             @Override
 853             public FileVisitResult visitFile(Path file,
 854                                              BasicFileAttributes attrs) {
 855                 filePaths.add(file);
 856                 return FileVisitResult.CONTINUE;
 857             }
 858         });
 859         return filePaths.toArray(new Path[filePaths.size()]);
 860     }
 861 




 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",


 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         }


 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 


< prev index next >