26 * @bug 8142968 8166568 8166286 8170618 8168149
27 * @summary Basic test for jmod
28 * @library /lib/testlibrary
29 * @modules jdk.compiler
30 * jdk.jlink
31 * @build jdk.testlibrary.FileUtils CompilerUtils
32 * @run testng/othervm -Djava.io.tmpdir=. JmodTest
33 */
34
35 import java.io.*;
36 import java.lang.module.ModuleDescriptor;
37 import java.lang.reflect.Method;
38 import java.nio.file.*;
39 import java.util.*;
40 import java.util.function.Consumer;
41 import java.util.regex.Pattern;
42 import java.util.spi.ToolProvider;
43 import java.util.stream.Collectors;
44 import java.util.stream.Stream;
45 import jdk.testlibrary.FileUtils;
46 import jdk.testlibrary.JDKToolFinder;
47 import org.testng.annotations.BeforeTest;
48 import org.testng.annotations.Test;
49
50 import static java.io.File.pathSeparator;
51 import static java.lang.module.ModuleDescriptor.Version;
52 import static java.nio.charset.StandardCharsets.UTF_8;
53 import static java.util.stream.Collectors.toSet;
54 import static org.testng.Assert.*;
55
56 public class JmodTest {
57
58 static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
59 .orElseThrow(() ->
60 new RuntimeException("jmod tool not found")
61 );
62
63 static final String TEST_SRC = System.getProperty("test.src", ".");
64 static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
65 static final Path EXPLODED_DIR = Paths.get("build");
66 static final Path MODS_DIR = Paths.get("jmods");
577 .resultChecker(r -> {
578 assertTrue(r.output.startsWith("Usage: jmod"), "Extra help not printed");
579 assertContains(r.output, "--do-not-resolve-by-default");
580 assertContains(r.output, "--warn-if-resolved");
581 });
582 }
583
584 @Test
585 public void testTmpFileRemoved() throws IOException {
586 // Implementation detail: jmod tool creates <jmod-file>.tmp
587 // Ensure that it is removed in the event of a failure.
588 // The failure in this case is a class in the unnamed package.
589
590 String filename = "testTmpFileRemoved.jmod";
591 Path jmod = MODS_DIR.resolve(filename);
592
593 // clean up files
594 FileUtils.deleteFileIfExistsWithRetry(jmod);
595 findTmpFiles(filename).forEach(tmp -> {
596 try {
597 FileUtils.deleteFileIfExistsWithRetry(tmp);
598 } catch (IOException e) {}
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 List<Path> tmpfiles = findTmpFiles(filename);
612 assertTrue(tmpfiles.isEmpty(), "Unexpected tmp file:" + tmpfiles);
613 });
614 }
615
616 /*
617 * Returns the list of writeable tmp files with the given prefix.
618 *
619 * Ignore the non-writeable tmp files because this test is possibly
620 * running by another user.
621 */
622 private List<Path> findTmpFiles(String prefix) {
623 Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir"));
624 try (Stream<Path> stream = Files.list(tmpdir)) {
625 return stream.filter(p -> {
626 String fn = p.getFileName().toString();
627 return Files.isWritable(p)
628 && fn.startsWith(prefix) && fn.endsWith(".tmp");
629 }).collect(Collectors.toList());
630 } catch (IOException e) {
631 throw new UncheckedIOException(e);
632 }
633 }
634
635 // ---
636
637 static boolean compileModule(String name, Path dest) throws IOException {
638 return CompilerUtils.compile(SRC_DIR.resolve(name), dest);
639 }
640
641 static void assertContains(String output, String subString) {
642 if (output.contains(subString))
643 assertTrue(true);
644 else
645 assertTrue(false,"Expected to find [" + subString + "], in output ["
646 + output + "]" + "\n");
647 }
648
|
26 * @bug 8142968 8166568 8166286 8170618 8168149
27 * @summary Basic test for jmod
28 * @library /lib/testlibrary
29 * @modules jdk.compiler
30 * jdk.jlink
31 * @build jdk.testlibrary.FileUtils CompilerUtils
32 * @run testng/othervm -Djava.io.tmpdir=. JmodTest
33 */
34
35 import java.io.*;
36 import java.lang.module.ModuleDescriptor;
37 import java.lang.reflect.Method;
38 import java.nio.file.*;
39 import java.util.*;
40 import java.util.function.Consumer;
41 import java.util.regex.Pattern;
42 import java.util.spi.ToolProvider;
43 import java.util.stream.Collectors;
44 import java.util.stream.Stream;
45 import jdk.testlibrary.FileUtils;
46 import org.testng.annotations.BeforeTest;
47 import org.testng.annotations.Test;
48
49 import static java.io.File.pathSeparator;
50 import static java.lang.module.ModuleDescriptor.Version;
51 import static java.nio.charset.StandardCharsets.UTF_8;
52 import static java.util.stream.Collectors.toSet;
53 import static org.testng.Assert.*;
54
55 public class JmodTest {
56
57 static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
58 .orElseThrow(() ->
59 new RuntimeException("jmod tool not found")
60 );
61
62 static final String TEST_SRC = System.getProperty("test.src", ".");
63 static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
64 static final Path EXPLODED_DIR = Paths.get("build");
65 static final Path MODS_DIR = Paths.get("jmods");
576 .resultChecker(r -> {
577 assertTrue(r.output.startsWith("Usage: jmod"), "Extra help not printed");
578 assertContains(r.output, "--do-not-resolve-by-default");
579 assertContains(r.output, "--warn-if-resolved");
580 });
581 }
582
583 @Test
584 public void testTmpFileRemoved() throws IOException {
585 // Implementation detail: jmod tool creates <jmod-file>.tmp
586 // Ensure that it is removed in the event of a failure.
587 // The failure in this case is a class in the unnamed package.
588
589 String filename = "testTmpFileRemoved.jmod";
590 Path jmod = MODS_DIR.resolve(filename);
591
592 // clean up files
593 FileUtils.deleteFileIfExistsWithRetry(jmod);
594 findTmpFiles(filename).forEach(tmp -> {
595 try {
596 FileUtils.deleteFileTreeWithRetry(tmp);
597 } catch (IOException e) {}
598 });
599
600 String cp = EXPLODED_DIR.resolve("foo").resolve("classes") + File.pathSeparator +
601 EXPLODED_DIR.resolve("foo").resolve("classes")
602 .resolve("jdk").resolve("test").resolve("foo").toString();
603
604 jmod("create",
605 "--class-path", cp,
606 jmod.toString())
607 .assertFailure()
608 .resultChecker(r -> {
609 assertContains(r.output, "unnamed package");
610 List<Path> tmpfiles = findTmpFiles(filename);
611 assertTrue(tmpfiles.isEmpty(), "Unexpected tmp file:" + tmpfiles);
612 });
613 }
614
615 /*
616 * Returns the list of writeable temp directory with the given prefix
617 * where the temporary jmod file is created.
618 *
619 * Ignore the non-writeable temp directory because this test is possibly
620 * running by another user.
621 */
622 private List<Path> findTmpFiles(String prefix) {
623 Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir"));
624 try (Stream<Path> stream = Files.list(tmpdir)) {
625 return stream.filter(p -> {
626 String fn = p.getFileName().toString();
627 return Files.isWritable(p)
628 && fn.startsWith(prefix) && Files.isDirectory(p);
629 }).collect(Collectors.toList());
630 } catch (IOException e) {
631 throw new UncheckedIOException(e);
632 }
633 }
634
635 // ---
636
637 static boolean compileModule(String name, Path dest) throws IOException {
638 return CompilerUtils.compile(SRC_DIR.resolve(name), dest);
639 }
640
641 static void assertContains(String output, String subString) {
642 if (output.contains(subString))
643 assertTrue(true);
644 else
645 assertTrue(false,"Expected to find [" + subString + "], in output ["
646 + output + "]" + "\n");
647 }
648
|