jdk/test/tools/jmod/JmodTest.java
changeset 42338 a60f280f803c
parent 41916 eb4aa81c6c5f
child 42468 7a9555a7e080
equal deleted inserted replaced
42148:7a4a59859ac0 42338:a60f280f803c
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
       
    26  * @bug 8142968
       
    27  * @summary Basic test for jmod
    26  * @library /lib/testlibrary
    28  * @library /lib/testlibrary
    27  * @modules jdk.compiler
    29  * @modules jdk.compiler
    28  *          jdk.jlink
    30  *          jdk.jlink
    29  * @build jdk.testlibrary.FileUtils CompilerUtils
    31  * @build jdk.testlibrary.FileUtils CompilerUtils
    30  * @run testng JmodTest
    32  * @run testng JmodTest
    31  * @bug 8142968
       
    32  * @summary Basic test for jmod
       
    33  */
    33  */
    34 
    34 
    35 import java.io.*;
    35 import java.io.*;
    36 import java.lang.module.ModuleDescriptor;
    36 import java.lang.module.ModuleDescriptor;
    37 import java.lang.reflect.Method;
    37 import java.lang.reflect.Method;
    74             FileUtils.deleteFileTreeWithRetry(EXPLODED_DIR);
    74             FileUtils.deleteFileTreeWithRetry(EXPLODED_DIR);
    75 
    75 
    76         for (String name : new String[] { "foo"/*, "bar", "baz"*/ } ) {
    76         for (String name : new String[] { "foo"/*, "bar", "baz"*/ } ) {
    77             Path dir = EXPLODED_DIR.resolve(name);
    77             Path dir = EXPLODED_DIR.resolve(name);
    78             assertTrue(compileModule(name, dir.resolve("classes")));
    78             assertTrue(compileModule(name, dir.resolve("classes")));
       
    79             copyResource(SRC_DIR.resolve("foo"),
       
    80                          dir.resolve("classes"),
       
    81                          "jdk/test/foo/resources/foo.properties");
    79             createCmds(dir.resolve("bin"));
    82             createCmds(dir.resolve("bin"));
    80             createLibs(dir.resolve("lib"));
    83             createLibs(dir.resolve("lib"));
    81             createConfigs(dir.resolve("conf"));
    84             createConfigs(dir.resolve("conf"));
    82         }
    85         }
    83 
    86 
   125             .resultChecker(r -> {
   128             .resultChecker(r -> {
   126                 // asserts dependent on the exact contents of foo
   129                 // asserts dependent on the exact contents of foo
   127                 assertContains(r.output, CLASSES_PREFIX + "module-info.class");
   130                 assertContains(r.output, CLASSES_PREFIX + "module-info.class");
   128                 assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/Foo.class");
   131                 assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/Foo.class");
   129                 assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/internal/Message.class");
   132                 assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/internal/Message.class");
       
   133                 assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/resources/foo.properties");
   130             });
   134             });
   131     }
   135     }
   132 
   136 
   133     @Test
   137     @Test
   134     public void testMainClass() throws IOException {
   138     public void testMainClass() throws IOException {
   277              .assertSuccess()
   281              .assertSuccess()
   278              .resultChecker(r -> {
   282              .resultChecker(r -> {
   279                  Set<String> expectedFilenames = new HashSet<>();
   283                  Set<String> expectedFilenames = new HashSet<>();
   280                  expectedFilenames.add(CLASSES_PREFIX + "module-info.class");
   284                  expectedFilenames.add(CLASSES_PREFIX + "module-info.class");
   281                  expectedFilenames.add(CLASSES_PREFIX + "jdk/test/foo/Foo.class");
   285                  expectedFilenames.add(CLASSES_PREFIX + "jdk/test/foo/Foo.class");
       
   286                  expectedFilenames.add(CLASSES_PREFIX + "jdk/test/foo/resources/foo.properties");
   282                  expectedFilenames.add(LIBS_PREFIX + "second.so");
   287                  expectedFilenames.add(LIBS_PREFIX + "second.so");
   283                  expectedFilenames.add(LIBS_PREFIX + "third/third.so");
   288                  expectedFilenames.add(LIBS_PREFIX + "third/third.so");
   284                  assertJmodContent(jmod, expectedFilenames);
   289                  assertJmodContent(jmod, expectedFilenames);
   285 
   290 
   286                  Set<String> unexpectedFilenames = new HashSet<>();
   291                  Set<String> unexpectedFilenames = new HashSet<>();
   301         jmod("describe",
   306         jmod("describe",
   302              MODS_DIR.resolve("describeFoo.jmod").toString())
   307              MODS_DIR.resolve("describeFoo.jmod").toString())
   303              .assertSuccess()
   308              .assertSuccess()
   304              .resultChecker(r -> {
   309              .resultChecker(r -> {
   305                  // Expect similar output: "foo,  requires mandated java.base
   310                  // Expect similar output: "foo,  requires mandated java.base
   306                  // exports jdk.test.foo,  conceals jdk.test.foo.internal"
   311                  // exports jdk.test.foo,  contains jdk.test.foo.internal"
   307                  Pattern p = Pattern.compile("\\s+foo\\s+requires\\s+mandated\\s+java.base");
   312                  Pattern p = Pattern.compile("\\s+foo\\s+requires\\s+mandated\\s+java.base");
   308                  assertTrue(p.matcher(r.output).find(),
   313                  assertTrue(p.matcher(r.output).find(),
   309                            "Expecting to find \"foo, requires java.base\"" +
   314                            "Expecting to find \"foo, requires java.base\"" +
   310                                 "in output, but did not: [" + r.output + "]");
   315                                 "in output, but did not: [" + r.output + "]");
   311                  p = Pattern.compile(
   316                  p = Pattern.compile(
   312                         "exports\\s+jdk.test.foo\\s+conceals\\s+jdk.test.foo.internal");
   317                         "exports\\s+jdk.test.foo\\s+contains\\s+jdk.test.foo.internal");
   313                  assertTrue(p.matcher(r.output).find(),
   318                  assertTrue(p.matcher(r.output).find(),
   314                            "Expecting to find \"exports ..., conceals ...\"" +
   319                            "Expecting to find \"exports ..., contains ...\"" +
   315                                 "in output, but did not: [" + r.output + "]");
   320                                 "in output, but did not: [" + r.output + "]");
   316              });
   321              });
   317     }
   322     }
   318 
   323 
   319     @Test
   324     @Test
   365              .assertSuccess()
   370              .assertSuccess()
   366              .resultChecker(r ->
   371              .resultChecker(r ->
   367                  assertContains(r.output, "Warning: ignoring entry")
   372                  assertContains(r.output, "Warning: ignoring entry")
   368              );
   373              );
   369     }
   374     }
       
   375 
       
   376     @Test
       
   377     public void testPackagesAttribute() throws IOException {
       
   378         Path jmod = MODS_DIR.resolve("foo.jmod");
       
   379         FileUtils.deleteFileIfExistsWithRetry(jmod);
       
   380         String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
       
   381 
       
   382         Set<String> expectedPackages = Set.of("jdk.test.foo",
       
   383                                               "jdk.test.foo.internal",
       
   384                                               "jdk.test.foo.resources");
       
   385 
       
   386         jmod("create",
       
   387              "--class-path", cp,
       
   388              jmod.toString())
       
   389              .assertSuccess()
       
   390              .resultChecker(r -> {
       
   391                  Set<String> pkgs = getModuleDescriptor(jmod).packages();
       
   392                  assertEquals(pkgs, expectedPackages);
       
   393              });
       
   394         }
   370 
   395 
   371     @Test
   396     @Test
   372     public void testVersion() {
   397     public void testVersion() {
   373         jmod("--version")
   398         jmod("--version")
   374             .assertSuccess()
   399             .assertSuccess()
   555                 os.write("blahblahblah".getBytes(UTF_8));
   580                 os.write("blahblahblah".getBytes(UTF_8));
   556             }
   581             }
   557         }
   582         }
   558     }
   583     }
   559 
   584 
       
   585     static void copyResource(Path srcDir, Path dir, String resource) throws IOException {
       
   586         Path dest = dir.resolve(resource);
       
   587         Files.deleteIfExists(dest);
       
   588 
       
   589         Files.createDirectories(dest.getParent());
       
   590         Files.copy(srcDir.resolve(resource), dest);
       
   591     }
       
   592 
   560     // Standalone entry point.
   593     // Standalone entry point.
   561     public static void main(String[] args) throws Throwable {
   594     public static void main(String[] args) throws Throwable {
   562         JmodTest test = new JmodTest();
   595         JmodTest test = new JmodTest();
   563         test.buildExplodedModules();
   596         test.buildExplodedModules();
   564         for (Method m : JmodTest.class.getDeclaredMethods()) {
   597         for (Method m : JmodTest.class.getDeclaredMethods()) {