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; }
|
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 testExtract() throws IOException {
139 Path cp = EXPLODED_DIR.resolve("foo").resolve("classes");
140 jmod("create",
141 "--class-path", cp.toString(),
142 MODS_DIR.resolve("fooExtract.jmod").toString())
143 .assertSuccess();
144
145 jmod("extract",
146 MODS_DIR.resolve("fooExtract.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 testMainClass() throws IOException {
162 Path jmod = MODS_DIR.resolve("fooMainClass.jmod");
163 FileUtils.deleteFileIfExistsWithRetry(jmod);
164 String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
165
166 jmod("create",
167 "--class-path", cp,
168 "--main-class", "jdk.test.foo.Foo",
169 jmod.toString())
170 .assertSuccess()
171 .resultChecker(r -> {
172 Optional<String> omc = getModuleDescriptor(jmod).mainClass();
173 assertTrue(omc.isPresent());
174 assertEquals(omc.get(), "jdk.test.foo.Foo");
175 });
176 }
177
178 @Test
179 public void testModuleVersion() throws IOException {
180 Path jmod = MODS_DIR.resolve("fooVersion.jmod");
535 notFound.forEach(s -> sb.append("\t" + s + "\n"));
536 assertTrue(false, "Jmod content check failed.\n" + sb.toString());
537 }
538 }
539
540 static void assertJmodDoesNotContain(Path jmod, Set<String> unexpectedNames) {
541 Set<String> actual = getJmodContent(jmod);
542 Set<String> unexpected = new HashSet<>();
543 for (String name : unexpectedNames) {
544 if (actual.contains(name))
545 unexpected.add(name);
546 }
547 if (!unexpected.isEmpty()) {
548 StringBuilder sb = new StringBuilder();
549 for (String s : unexpected)
550 sb.append("Unexpected but found: " + s + "\n");
551 sb.append("In :");
552 for (String s : actual)
553 sb.append("\t" + s + "\n");
554 assertTrue(false, "Jmod content check failed.\n" + sb.toString());
555 }
556 }
557
558 static void assertSameContent(Path p1, Path p2) {
559 try {
560 byte[] ba1 = Files.readAllBytes(p1);
561 byte[] ba2 = Files.readAllBytes(p2);
562 assertEquals(ba1, ba2);
563 } catch (IOException x) {
564 throw new UncheckedIOException(x);
565 }
566 }
567
568 static JmodResult jmod(String... args) {
569 ByteArrayOutputStream baos = new ByteArrayOutputStream();
570 PrintStream ps = new PrintStream(baos);
571 System.out.println("jmod " + Arrays.asList(args));
572 int ec = JMOD_TOOL.run(ps, ps, args);
573 return new JmodResult(ec, new String(baos.toByteArray(), UTF_8));
574 }
575
576 static class JmodResult {
577 final int exitCode;
578 final String output;
579
580 JmodResult(int exitValue, String output) {
581 this.exitCode = exitValue;
582 this.output = output;
583 }
584 JmodResult assertSuccess() { assertTrue(exitCode == 0, output); return this; }
|