< prev index next >

test/tools/jar/modularJar/Basic.java

Print this page




  22  */
  23 
  24 import java.io.*;
  25 import java.lang.module.ModuleDescriptor;
  26 import java.lang.reflect.Method;
  27 import java.nio.file.*;
  28 import java.nio.file.attribute.BasicFileAttributes;
  29 import java.util.*;
  30 import java.util.function.Consumer;
  31 import java.util.jar.JarEntry;
  32 import java.util.jar.JarFile;
  33 import java.util.jar.JarInputStream;
  34 import java.util.jar.Manifest;
  35 import java.util.regex.Pattern;
  36 import java.util.stream.Collectors;
  37 import java.util.stream.Stream;
  38 
  39 import jdk.testlibrary.FileUtils;
  40 import jdk.testlibrary.JDKToolFinder;
  41 import org.testng.annotations.BeforeTest;

  42 import org.testng.annotations.Test;
  43 
  44 import static java.lang.String.format;
  45 import static java.lang.System.out;
  46 
  47 /*
  48  * @test
  49  * @bug 8167328 8171830 8165640 8174248
  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", "."));


 737     @Test
 738     public void servicesCreateWithoutFailureNonRootMRMJAR() throws IOException {
 739         // without a root module-info.class
 740         Path mp = Paths.get("servicesCreateWithoutFailureNonRootMRMJAR");
 741         createTestDir(mp);
 742         Path modClasses = MODULE_CLASSES.resolve("baz");
 743         Path mrjarDir = MRJAR_DIR.resolve("baz");
 744         Path modularJar = mp.resolve("baz.jar");
 745 
 746         jar("--create",
 747             "--file=" + modularJar.toString(),
 748             "--main-class=" + "jdk.test.baz.Baz",
 749             "-m", mrjarDir.resolve("META-INF/MANIFEST.MF").toRealPath().toString(),
 750             "-C", mrjarDir.toString(), "META-INF/versions/9/module-info.class",
 751             "-C", modClasses.toString(), "jdk/test/baz/BazService.class",
 752             "-C", modClasses.toString(), "jdk/test/baz/Baz.class",
 753             "-C", modClasses.toString(), "jdk/test/baz/internal/BazServiceImpl.class")
 754             .assertSuccess();
 755 
 756 
 757         for (String option : new String[]  {"--print-module-descriptor", "-d" }) {
 758 
 759             jar(option,
 760                 "--file=" + modularJar.toString())
 761                 .assertSuccess()
 762                 .resultChecker(r ->
 763                     assertTrue(r.output.contains("main-class jdk.test.baz.Baz"),
 764                               "Expected to find ", "main-class jdk.test.baz.Baz",
 765                                " in [", r.output, "]"));
 766 
 767             jarWithStdin(modularJar.toFile(),  option)
 768                 .assertSuccess()
 769                 .resultChecker(r ->
 770                     assertTrue(r.output.contains("main-class jdk.test.baz.Baz"),
 771                               "Expected to find ", "main-class jdk.test.baz.Baz",
 772                                " in [", r.output, "]"));
 773 
 774         }
 775         // run module maain class
 776         java(mp, "baz/jdk.test.baz.Baz")
 777             .assertSuccess()


 784     @Test
 785     public void exportCreateWithMissingPkg() throws IOException {
 786 
 787         Path foobar = TEST_SRC.resolve("src").resolve("foobar");
 788         Path dst = Files.createDirectories(MODULE_CLASSES.resolve("foobar"));
 789         javac(dst, null, sourceList(foobar));
 790 
 791         Path mp = Paths.get("exportWithMissingPkg");
 792         createTestDir(mp);
 793         Path modClasses = dst;
 794         Path modularJar = mp.resolve("foofoo.jar");
 795 
 796         jar("--create",
 797             "--file=" + modularJar.toString(),
 798             "-C", modClasses.toString(), "module-info.class",
 799             "-C", modClasses.toString(), "jdk/test/foo/Foo.class")
 800             .assertFailure();
 801     }
 802 
 803     @Test
 804     public void printModuleDescriptorFoo() throws IOException {
 805         Path mp = Paths.get("printModuleDescriptorFoo");
 806         createTestDir(mp);
 807         Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName);
 808         Path modularJar = mp.resolve(FOO.moduleName + ".jar");
 809 
 810         jar("--create",
 811             "--file=" + modularJar.toString(),
 812             "--main-class=" + FOO.mainClass,
 813             "--module-version=" + FOO.version,
 814             "--no-manifest",
 815             "-C", modClasses.toString(), ".")
 816             .assertSuccess();
 817 
 818         for (String option : new String[]  {"--print-module-descriptor", "-d" }) {
 819             jar(option,
 820                 "--file=" + modularJar.toString())
 821                 .assertSuccess()
 822                 .resultChecker(r ->
 823                     assertTrue(r.output.contains(FOO.moduleName + "@" + FOO.version),
 824                                "Expected to find ", FOO.moduleName + "@" + FOO.version,
 825                                " in [", r.output, "]")
 826                 );
 827 
 828             jar(option,
 829                 "--file=" + modularJar.toString(),
 830                 modularJar.toString())
 831             .assertFailure();
 832 
 833             jar(option, modularJar.toString())
 834             .assertFailure();
 835         }
 836     }
 837 
 838     @Test
 839     public void printModuleDescriptorFooFromStdin() throws IOException {
 840         Path mp = Paths.get("printModuleDescriptorFooFromStdin");
 841         createTestDir(mp);
 842         Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName);
 843         Path modularJar = mp.resolve(FOO.moduleName + ".jar");
 844 
 845         jar("--create",
 846             "--file=" + modularJar.toString(),
 847             "--main-class=" + FOO.mainClass,
 848             "--module-version=" + FOO.version,
 849             "--no-manifest",
 850             "-C", modClasses.toString(), ".")
 851             .assertSuccess();
 852 
 853         for (String option : new String[]  {"--print-module-descriptor", "-d" }) {
 854             jarWithStdin(modularJar.toFile(),
 855                          option)
 856                          .assertSuccess()
 857                          .resultChecker(r ->
 858                              assertTrue(r.output.contains(FOO.moduleName + "@" + FOO.version),
 859                                 "Expected to find ", FOO.moduleName + "@" + FOO.version,
 860                                 " in [", r.output, "]")
 861                 );
 862         }
 863     }
 864 











































 865     // -- Infrastructure
 866 
 867     static Result jarWithStdin(File stdinSource, String... args) {
 868         String jar = getJDKTool("jar");
 869         List<String> commands = new ArrayList<>();
 870         commands.add(jar);
 871         if (!TOOL_VM_OPTIONS.isEmpty()) {
 872             commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1)));
 873         }
 874         Stream.of(args).forEach(commands::add);
 875         ProcessBuilder p = new ProcessBuilder(commands);
 876         if (stdinSource != null)
 877             p.redirectInput(stdinSource);
 878         return run(p);
 879     }
 880 
 881     static Result jar(String... args) {
 882         return jarWithStdin(null, args);
 883     }
 884 




  22  */
  23 
  24 import java.io.*;
  25 import java.lang.module.ModuleDescriptor;
  26 import java.lang.reflect.Method;
  27 import java.nio.file.*;
  28 import java.nio.file.attribute.BasicFileAttributes;
  29 import java.util.*;
  30 import java.util.function.Consumer;
  31 import java.util.jar.JarEntry;
  32 import java.util.jar.JarFile;
  33 import java.util.jar.JarInputStream;
  34 import java.util.jar.Manifest;
  35 import java.util.regex.Pattern;
  36 import java.util.stream.Collectors;
  37 import java.util.stream.Stream;
  38 
  39 import jdk.testlibrary.FileUtils;
  40 import jdk.testlibrary.JDKToolFinder;
  41 import org.testng.annotations.BeforeTest;
  42 import org.testng.annotations.DataProvider;
  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  * @bug 8167328 8171830 8165640 8174248
  51  * @library /lib/testlibrary
  52  * @modules jdk.compiler
  53  *          jdk.jartool
  54  * @build jdk.testlibrary.FileUtils jdk.testlibrary.JDKToolFinder
  55  * @compile Basic.java
  56  * @run testng Basic
  57  * @summary Tests for plain Modular jars & Multi-Release Modular jars
  58  */
  59 
  60 public class Basic {
  61     static final Path TEST_SRC = Paths.get(System.getProperty("test.src", "."));
  62     static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", "."));


 738     @Test
 739     public void servicesCreateWithoutFailureNonRootMRMJAR() throws IOException {
 740         // without a root module-info.class
 741         Path mp = Paths.get("servicesCreateWithoutFailureNonRootMRMJAR");
 742         createTestDir(mp);
 743         Path modClasses = MODULE_CLASSES.resolve("baz");
 744         Path mrjarDir = MRJAR_DIR.resolve("baz");
 745         Path modularJar = mp.resolve("baz.jar");
 746 
 747         jar("--create",
 748             "--file=" + modularJar.toString(),
 749             "--main-class=" + "jdk.test.baz.Baz",
 750             "-m", mrjarDir.resolve("META-INF/MANIFEST.MF").toRealPath().toString(),
 751             "-C", mrjarDir.toString(), "META-INF/versions/9/module-info.class",
 752             "-C", modClasses.toString(), "jdk/test/baz/BazService.class",
 753             "-C", modClasses.toString(), "jdk/test/baz/Baz.class",
 754             "-C", modClasses.toString(), "jdk/test/baz/internal/BazServiceImpl.class")
 755             .assertSuccess();
 756 
 757 
 758         for (String option : new String[]  {"--describe-module", "-d" }) {
 759 
 760             jar(option,
 761                 "--file=" + modularJar.toString())
 762                 .assertSuccess()
 763                 .resultChecker(r ->
 764                     assertTrue(r.output.contains("main-class jdk.test.baz.Baz"),
 765                               "Expected to find ", "main-class jdk.test.baz.Baz",
 766                                " in [", r.output, "]"));
 767 
 768             jarWithStdin(modularJar.toFile(),  option)
 769                 .assertSuccess()
 770                 .resultChecker(r ->
 771                     assertTrue(r.output.contains("main-class jdk.test.baz.Baz"),
 772                               "Expected to find ", "main-class jdk.test.baz.Baz",
 773                                " in [", r.output, "]"));
 774 
 775         }
 776         // run module maain class
 777         java(mp, "baz/jdk.test.baz.Baz")
 778             .assertSuccess()


 785     @Test
 786     public void exportCreateWithMissingPkg() throws IOException {
 787 
 788         Path foobar = TEST_SRC.resolve("src").resolve("foobar");
 789         Path dst = Files.createDirectories(MODULE_CLASSES.resolve("foobar"));
 790         javac(dst, null, sourceList(foobar));
 791 
 792         Path mp = Paths.get("exportWithMissingPkg");
 793         createTestDir(mp);
 794         Path modClasses = dst;
 795         Path modularJar = mp.resolve("foofoo.jar");
 796 
 797         jar("--create",
 798             "--file=" + modularJar.toString(),
 799             "-C", modClasses.toString(), "module-info.class",
 800             "-C", modClasses.toString(), "jdk/test/foo/Foo.class")
 801             .assertFailure();
 802     }
 803 
 804     @Test
 805     public void describeModuleFoo() throws IOException {
 806         Path mp = Paths.get("describeModuleFoo");
 807         createTestDir(mp);
 808         Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName);
 809         Path modularJar = mp.resolve(FOO.moduleName + ".jar");
 810 
 811         jar("--create",
 812             "--file=" + modularJar.toString(),
 813             "--main-class=" + FOO.mainClass,
 814             "--module-version=" + FOO.version,
 815             "--no-manifest",
 816             "-C", modClasses.toString(), ".")
 817             .assertSuccess();
 818 
 819         for (String option : new String[]  {"--describe-module", "-d" }) {
 820             jar(option,
 821                 "--file=" + modularJar.toString())
 822                 .assertSuccess()
 823                 .resultChecker(r ->
 824                     assertTrue(r.output.contains(FOO.moduleName + "@" + FOO.version),
 825                                "Expected to find ", FOO.moduleName + "@" + FOO.version,
 826                                " in [", r.output, "]")
 827                 );
 828 
 829             jar(option,
 830                 "--file=" + modularJar.toString(),
 831                 modularJar.toString())
 832             .assertFailure();
 833 
 834             jar(option, modularJar.toString())
 835             .assertFailure();
 836         }
 837     }
 838 
 839     @Test
 840     public void describeModuleFooFromStdin() throws IOException {
 841         Path mp = Paths.get("describeModuleFooFromStdin");
 842         createTestDir(mp);
 843         Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName);
 844         Path modularJar = mp.resolve(FOO.moduleName + ".jar");
 845 
 846         jar("--create",
 847             "--file=" + modularJar.toString(),
 848             "--main-class=" + FOO.mainClass,
 849             "--module-version=" + FOO.version,
 850             "--no-manifest",
 851             "-C", modClasses.toString(), ".")
 852             .assertSuccess();
 853 
 854         for (String option : new String[]  {"--describe-module", "-d" }) {
 855             jarWithStdin(modularJar.toFile(),
 856                          option)
 857                          .assertSuccess()
 858                          .resultChecker(r ->
 859                              assertTrue(r.output.contains(FOO.moduleName + "@" + FOO.version),
 860                                 "Expected to find ", FOO.moduleName + "@" + FOO.version,
 861                                 " in [", r.output, "]")
 862                 );
 863         }
 864     }
 865 
 866 
 867     @DataProvider(name = "autoNames")
 868     public Object[][] autoNames() {
 869         return new Object[][] {
 870             // JAR file name                module-name[@version]
 871             { "foo.jar",                    "foo" },
 872             { "foo4j.jar",                  "foo4j", },
 873             { "foo1.2.3.jar",               "foo" },
 874             { "foo-1.2.3.4.jar",            "foo@1.2.3.4" },
 875             { "foo-bar.jar",                "foo.bar" },
 876             { "foo-1.2-SNAPSHOT.jar",       "foo@1.2-SNAPSHOT" },
 877         };
 878     }
 879 
 880     @Test(dataProvider = "autoNames")
 881     public void describeAutomaticModule(String jarName, String mid)
 882         throws IOException
 883     {
 884         Path mp = Paths.get("describeAutomaticModule");
 885         createTestDir(mp);
 886         Path regularJar = mp.resolve(jarName);
 887         Path t = Paths.get("t");
 888         if (Files.notExists(t))
 889             Files.createFile(t);
 890 
 891         jar("--create",
 892             "--file=" + regularJar.toString(),
 893             t.toString())
 894             .assertSuccess();
 895 
 896         for (String option : new String[]  {"--describe-module", "-d" }) {
 897             jar(option,
 898                 "--file=" + regularJar.toString())
 899                 .assertSuccess()
 900                 .resultChecker(r -> {
 901                     assertTrue(r.output.contains("No module descriptor found"));
 902                     assertTrue(r.output.contains("Derived automatic module: " + mid),
 903                                "Expected to find ", mid, " in [", r.output, "]");
 904                     }
 905                 );
 906         }
 907     }
 908 
 909     // -- Infrastructure
 910 
 911     static Result jarWithStdin(File stdinSource, String... args) {
 912         String jar = getJDKTool("jar");
 913         List<String> commands = new ArrayList<>();
 914         commands.add(jar);
 915         if (!TOOL_VM_OPTIONS.isEmpty()) {
 916             commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1)));
 917         }
 918         Stream.of(args).forEach(commands::add);
 919         ProcessBuilder p = new ProcessBuilder(commands);
 920         if (stdinSource != null)
 921             p.redirectInput(stdinSource);
 922         return run(p);
 923     }
 924 
 925     static Result jar(String... args) {
 926         return jarWithStdin(null, args);
 927     }
 928 


< prev index next >