< prev index next >

test/tools/jmod/JmodTest.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8142968
  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 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.Stream;
  44 import jdk.testlibrary.FileUtils;
  45 import org.testng.annotations.BeforeTest;
  46 import org.testng.annotations.Test;


 118     public void testList() throws IOException {
 119         String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
 120         jmod("create",
 121              "--class-path", cp,
 122              MODS_DIR.resolve("foo.jmod").toString())
 123             .assertSuccess();
 124 
 125         jmod("list",
 126              MODS_DIR.resolve("foo.jmod").toString())
 127             .assertSuccess()
 128             .resultChecker(r -> {
 129                 // asserts dependent on the exact contents of foo
 130                 assertContains(r.output, CLASSES_PREFIX + "module-info.class");
 131                 assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/Foo.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");
 134             });
 135     }
 136 
 137     @Test



























































 138     public void testMainClass() throws IOException {
 139         Path jmod = MODS_DIR.resolve("fooMainClass.jmod");
 140         FileUtils.deleteFileIfExistsWithRetry(jmod);
 141         String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
 142 
 143         jmod("create",
 144              "--class-path", cp,
 145              "--main-class", "jdk.test.foo.Foo",
 146              jmod.toString())
 147             .assertSuccess()
 148             .resultChecker(r -> {
 149                 Optional<String> omc = getModuleDescriptor(jmod).mainClass();
 150                 assertTrue(omc.isPresent());
 151                 assertEquals(omc.get(), "jdk.test.foo.Foo");
 152             });
 153     }
 154 
 155     @Test
 156     public void testModuleVersion() throws IOException {
 157         Path jmod = MODS_DIR.resolve("fooVersion.jmod");


 512             notFound.forEach(s -> sb.append("\t" + s + "\n"));
 513             assertTrue(false, "Jmod content check failed.\n" + sb.toString());
 514         }
 515     }
 516 
 517     static void assertJmodDoesNotContain(Path jmod, Set<String> unexpectedNames) {
 518         Set<String> actual = getJmodContent(jmod);
 519         Set<String> unexpected = new HashSet<>();
 520         for (String name : unexpectedNames) {
 521             if (actual.contains(name))
 522                 unexpected.add(name);
 523         }
 524         if (!unexpected.isEmpty()) {
 525             StringBuilder sb = new StringBuilder();
 526             for (String s : unexpected)
 527                 sb.append("Unexpected but found: " + s + "\n");
 528             sb.append("In :");
 529             for (String s : actual)
 530                 sb.append("\t" + s + "\n");
 531             assertTrue(false, "Jmod content check failed.\n" + sb.toString());










 532         }
 533     }
 534 
 535     static JmodResult jmod(String... args) {
 536         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 537         PrintStream ps = new PrintStream(baos);
 538         System.out.println("jmod " + Arrays.asList(args));
 539         int ec = JMOD_TOOL.run(ps, ps, args);
 540         return new JmodResult(ec, new String(baos.toByteArray(), UTF_8));
 541     }
 542 
 543     static class JmodResult {
 544         final int exitCode;
 545         final String output;
 546 
 547         JmodResult(int exitValue, String output) {
 548             this.exitCode = exitValue;
 549             this.output = output;
 550         }
 551         JmodResult assertSuccess() { assertTrue(exitCode == 0, output); return this; }




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8142968 8166568
  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 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.Stream;
  44 import jdk.testlibrary.FileUtils;
  45 import org.testng.annotations.BeforeTest;
  46 import org.testng.annotations.Test;


 118     public void testList() throws IOException {
 119         String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
 120         jmod("create",
 121              "--class-path", cp,
 122              MODS_DIR.resolve("foo.jmod").toString())
 123             .assertSuccess();
 124 
 125         jmod("list",
 126              MODS_DIR.resolve("foo.jmod").toString())
 127             .assertSuccess()
 128             .resultChecker(r -> {
 129                 // asserts dependent on the exact contents of foo
 130                 assertContains(r.output, CLASSES_PREFIX + "module-info.class");
 131                 assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/Foo.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");
 134             });
 135     }
 136 
 137     @Test
 138     public void testExtractCWD() throws IOException {
 139         Path cp = EXPLODED_DIR.resolve("foo").resolve("classes");
 140         jmod("create",
 141              "--class-path", cp.toString(),
 142              MODS_DIR.resolve("fooExtractCWD.jmod").toString())
 143             .assertSuccess();
 144 
 145         jmod("extract",
 146              MODS_DIR.resolve("fooExtractCWD.jmod").toString())
 147             .assertSuccess()
 148             .resultChecker(r -> {
 149                 // module-info should exist, but jmod will have added its Packages attr.
 150                 assertTrue(Files.exists(Paths.get("classes/module-info.class")));
 151                 assertSameContent(cp.resolve("jdk/test/foo/Foo.class"),
 152                                   Paths.get("classes/jdk/test/foo/Foo.class"));
 153                 assertSameContent(cp.resolve("jdk/test/foo/internal/Message.class"),
 154                                   Paths.get("classes/jdk/test/foo/internal/Message.class"));
 155                 assertSameContent(cp.resolve("jdk/test/foo/resources/foo.properties"),
 156                                   Paths.get("classes/jdk/test/foo/resources/foo.properties"));
 157             });
 158     }
 159 
 160     @Test
 161     public void testExtractDir() throws IOException {
 162         if (Files.exists(Paths.get("extractTestDir")))
 163             FileUtils.deleteFileTreeWithRetry(Paths.get("extractTestDir"));
 164         Path cp = EXPLODED_DIR.resolve("foo").resolve("classes");
 165         Path bp = EXPLODED_DIR.resolve("foo").resolve("bin");
 166         Path lp = EXPLODED_DIR.resolve("foo").resolve("lib");
 167         Path cf = EXPLODED_DIR.resolve("foo").resolve("conf");
 168 
 169         jmod("create",
 170              "--conf", cf.toString(),
 171              "--cmds", bp.toString(),
 172              "--libs", lp.toString(),
 173              "--class-path", cp.toString(),
 174              MODS_DIR.resolve("fooExtractDir.jmod").toString())
 175             .assertSuccess();
 176 
 177         jmod("extract",
 178              "--dir", "extractTestDir",
 179              MODS_DIR.resolve("fooExtractDir.jmod").toString())
 180             .assertSuccess()
 181             .resultChecker(r -> {
 182                 // check a sample of the extracted files
 183                 Path p = Paths.get("extractTestDir");
 184                 assertTrue(Files.exists(p.resolve("classes/module-info.class")));
 185                 assertSameContent(cp.resolve("jdk/test/foo/Foo.class"),
 186                                   p.resolve("classes/jdk/test/foo/Foo.class"));
 187                 assertSameContent(bp.resolve("first"),
 188                                   p.resolve(CMDS_PREFIX).resolve("first"));
 189                 assertSameContent(lp.resolve("first.so"),
 190                                   p.resolve(LIBS_PREFIX).resolve("second.so"));
 191                 assertSameContent(cf.resolve("second.cfg"),
 192                                   p.resolve(CONFIGS_PREFIX).resolve("second.cfg"));
 193             });
 194     }
 195 
 196     @Test
 197     public void testMainClass() throws IOException {
 198         Path jmod = MODS_DIR.resolve("fooMainClass.jmod");
 199         FileUtils.deleteFileIfExistsWithRetry(jmod);
 200         String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
 201 
 202         jmod("create",
 203              "--class-path", cp,
 204              "--main-class", "jdk.test.foo.Foo",
 205              jmod.toString())
 206             .assertSuccess()
 207             .resultChecker(r -> {
 208                 Optional<String> omc = getModuleDescriptor(jmod).mainClass();
 209                 assertTrue(omc.isPresent());
 210                 assertEquals(omc.get(), "jdk.test.foo.Foo");
 211             });
 212     }
 213 
 214     @Test
 215     public void testModuleVersion() throws IOException {
 216         Path jmod = MODS_DIR.resolve("fooVersion.jmod");


 571             notFound.forEach(s -> sb.append("\t" + s + "\n"));
 572             assertTrue(false, "Jmod content check failed.\n" + sb.toString());
 573         }
 574     }
 575 
 576     static void assertJmodDoesNotContain(Path jmod, Set<String> unexpectedNames) {
 577         Set<String> actual = getJmodContent(jmod);
 578         Set<String> unexpected = new HashSet<>();
 579         for (String name : unexpectedNames) {
 580             if (actual.contains(name))
 581                 unexpected.add(name);
 582         }
 583         if (!unexpected.isEmpty()) {
 584             StringBuilder sb = new StringBuilder();
 585             for (String s : unexpected)
 586                 sb.append("Unexpected but found: " + s + "\n");
 587             sb.append("In :");
 588             for (String s : actual)
 589                 sb.append("\t" + s + "\n");
 590             assertTrue(false, "Jmod content check failed.\n" + sb.toString());
 591         }
 592     }
 593 
 594     static void assertSameContent(Path p1, Path p2) {
 595         try {
 596             byte[] ba1 = Files.readAllBytes(p1);
 597             byte[] ba2 = Files.readAllBytes(p2);
 598             assertEquals(ba1, ba2);
 599         } catch (IOException x) {
 600             throw new UncheckedIOException(x);
 601         }
 602     }
 603 
 604     static JmodResult jmod(String... args) {
 605         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 606         PrintStream ps = new PrintStream(baos);
 607         System.out.println("jmod " + Arrays.asList(args));
 608         int ec = JMOD_TOOL.run(ps, ps, args);
 609         return new JmodResult(ec, new String(baos.toByteArray(), UTF_8));
 610     }
 611 
 612     static class JmodResult {
 613         final int exitCode;
 614         final String output;
 615 
 616         JmodResult(int exitValue, String output) {
 617             this.exitCode = exitValue;
 618             this.output = output;
 619         }
 620         JmodResult assertSuccess() { assertTrue(exitCode == 0, output); return this; }


< prev index next >