< prev index next >

test/tools/jmod/JmodTest.java

Print this page




 563             .assertSuccess()
 564             .resultChecker(r -> {
 565                 assertTrue(r.output.startsWith("Usage: jmod"), "Help not printed");
 566                 assertFalse(r.output.contains("--do-not-resolve-by-default"));
 567                 assertFalse(r.output.contains("--warn-if-resolved"));
 568             });
 569     }
 570 
 571     @Test
 572     public void testHelpExtra() {
 573         jmod("--help-extra")
 574             .assertSuccess()
 575             .resultChecker(r -> {
 576                 assertTrue(r.output.startsWith("Usage: jmod"), "Extra help not printed");
 577                 assertContains(r.output, "--do-not-resolve-by-default");
 578                 assertContains(r.output, "--warn-if-resolved");
 579             });
 580     }
 581 
 582     @Test
 583     public void testTmpFileAlreadyExists() throws IOException {
 584         // Implementation detail: jmod tool creates <jmod-file>.tmp
 585         // Ensure that there are no problems if existing
 586 
 587         Path jmod = MODS_DIR.resolve("testTmpFileAlreadyExists.jmod");
 588         Path tmp = MODS_DIR.resolve("testTmpFileAlreadyExists.jmod.tmp");
 589         FileUtils.deleteFileIfExistsWithRetry(jmod);
 590         FileUtils.deleteFileIfExistsWithRetry(tmp);
 591         Files.createFile(tmp);
 592         String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
 593 
 594         jmod("create",
 595              "--class-path", cp,
 596              jmod.toString())
 597             .assertSuccess()
 598             .resultChecker(r ->
 599                 assertTrue(Files.notExists(tmp), "Unexpected tmp file:" + tmp)
 600             );
 601     }
 602 
 603     @Test
 604     public void testTmpFileRemoved() throws IOException {
 605         // Implementation detail: jmod tool creates <jmod-file>.tmp
 606         // Ensure that it is removed in the event of a failure.
 607         // The failure in this case is a class in the unnamed package.
 608 
 609         Path jmod = MODS_DIR.resolve("testTmpFileRemoved.jmod");
 610         Path tmp = MODS_DIR.resolve("testTmpFileRemoved.jmod.tmp");


 611         FileUtils.deleteFileIfExistsWithRetry(jmod);


 612         FileUtils.deleteFileIfExistsWithRetry(tmp);





 613         String cp = EXPLODED_DIR.resolve("foo").resolve("classes") + File.pathSeparator +
 614                     EXPLODED_DIR.resolve("foo").resolve("classes")
 615                                 .resolve("jdk").resolve("test").resolve("foo").toString();
 616 
 617         jmod("create",
 618              "--class-path", cp,
 619              jmod.toString())
 620              .assertFailure()
 621              .resultChecker(r -> {
 622                  assertContains(r.output, "unnamed package");
 623                  assertTrue(Files.notExists(tmp), "Unexpected tmp file:" + tmp);

 624              });
 625     }
 626 











 627     // ---
 628 
 629     static boolean compileModule(String name, Path dest) throws IOException {
 630         return CompilerUtils.compile(SRC_DIR.resolve(name), dest);
 631     }
 632 
 633     static void assertContains(String output, String subString) {
 634         if (output.contains(subString))
 635             assertTrue(true);
 636         else
 637             assertTrue(false,"Expected to find [" + subString + "], in output ["
 638                            + output + "]" + "\n");
 639     }
 640 
 641     static ModuleDescriptor getModuleDescriptor(Path jmod) {
 642         ClassLoader cl = ClassLoader.getSystemClassLoader();
 643         try (FileSystem fs = FileSystems.newFileSystem(jmod, cl)) {
 644             String p = "/classes/module-info.class";
 645             try (InputStream is = Files.newInputStream(fs.getPath(p))) {
 646                 return ModuleDescriptor.read(is);




 563             .assertSuccess()
 564             .resultChecker(r -> {
 565                 assertTrue(r.output.startsWith("Usage: jmod"), "Help not printed");
 566                 assertFalse(r.output.contains("--do-not-resolve-by-default"));
 567                 assertFalse(r.output.contains("--warn-if-resolved"));
 568             });
 569     }
 570 
 571     @Test
 572     public void testHelpExtra() {
 573         jmod("--help-extra")
 574             .assertSuccess()
 575             .resultChecker(r -> {
 576                 assertTrue(r.output.startsWith("Usage: jmod"), "Extra help not printed");
 577                 assertContains(r.output, "--do-not-resolve-by-default");
 578                 assertContains(r.output, "--warn-if-resolved");
 579             });
 580     }
 581 
 582     @Test





















 583     public void testTmpFileRemoved() throws IOException {
 584         // Implementation detail: jmod tool creates <jmod-file>.tmp
 585         // Ensure that it is removed in the event of a failure.
 586         // The failure in this case is a class in the unnamed package.
 587 
 588         String filename = "testTmpFileRemoved.jmod";
 589         Path jmod = MODS_DIR.resolve(filename);
 590 
 591         // clean up files
 592         FileUtils.deleteFileIfExistsWithRetry(jmod);
 593         findTmpFiles(filename).forEach(tmp -> {
 594             try {
 595                 FileUtils.deleteFileIfExistsWithRetry(tmp);
 596             } catch (IOException e) {
 597                 throw new UncheckedIOException(e);
 598             }
 599         });
 600 
 601         String cp = EXPLODED_DIR.resolve("foo").resolve("classes") + File.pathSeparator +
 602                     EXPLODED_DIR.resolve("foo").resolve("classes")
 603                                 .resolve("jdk").resolve("test").resolve("foo").toString();
 604 
 605         jmod("create",
 606              "--class-path", cp,
 607              jmod.toString())
 608              .assertFailure()
 609              .resultChecker(r -> {
 610                  assertContains(r.output, "unnamed package");
 611                  Set<Path> tmpfiles = findTmpFiles(filename).collect(toSet());
 612                  assertTrue(tmpfiles.isEmpty(), "Unexpected tmp file:" + tmpfiles);
 613              });
 614     }
 615 
 616     private Stream<Path> findTmpFiles(String prefix) {
 617         try {
 618             Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir"));
 619             return Files.find(tmpdir, 1, (p, attrs) ->
 620                 p.getFileName().toString().startsWith(prefix)
 621                     && p.getFileName().toString().endsWith(".tmp"));
 622         } catch (IOException e) {
 623             throw new UncheckedIOException(e);
 624         }
 625     }
 626 
 627     // ---
 628 
 629     static boolean compileModule(String name, Path dest) throws IOException {
 630         return CompilerUtils.compile(SRC_DIR.resolve(name), dest);
 631     }
 632 
 633     static void assertContains(String output, String subString) {
 634         if (output.contains(subString))
 635             assertTrue(true);
 636         else
 637             assertTrue(false,"Expected to find [" + subString + "], in output ["
 638                            + output + "]" + "\n");
 639     }
 640 
 641     static ModuleDescriptor getModuleDescriptor(Path jmod) {
 642         ClassLoader cl = ClassLoader.getSystemClassLoader();
 643         try (FileSystem fs = FileSystems.newFileSystem(jmod, cl)) {
 644             String p = "/classes/module-info.class";
 645             try (InputStream is = Files.newInputStream(fs.getPath(p))) {
 646                 return ModuleDescriptor.read(is);


< prev index next >