1 /*
2 * Copyright (c) 2024, Red Hat, Inc.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
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 import java.io.IOException;
25 import java.nio.file.FileVisitResult;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.SimpleFileVisitor;
29 import java.nio.file.attribute.BasicFileAttributes;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34
35 import tests.Helper;
36 import tests.JImageGenerator;
37
38
39 /*
40 * @test
41 * @summary Compare packaged-modules jlink with a run-time image based jlink to
42 * produce the same result
43 * @requires (jlink.packagedModules & vm.compMode != "Xcomp" & os.maxMemory >= 2g)
44 * @library ../../lib /test/lib
45 * @enablePreview
46 * @modules java.base/jdk.internal.jimage
47 * jdk.jlink/jdk.tools.jlink.internal
48 * jdk.jlink/jdk.tools.jlink.plugin
49 * jdk.jlink/jdk.tools.jimage
50 * @build tests.* jdk.test.lib.process.OutputAnalyzer
51 * jdk.test.lib.process.ProcessTools
52 * @run main/othervm/timeout=1200 -Xmx1g PackagedModulesVsRuntimeImageLinkTest
53 */
54 public class PackagedModulesVsRuntimeImageLinkTest extends AbstractLinkableRuntimeTest {
55
56 public static void main(String[] args) throws Exception {
57 PackagedModulesVsRuntimeImageLinkTest test = new PackagedModulesVsRuntimeImageLinkTest();
58 test.run();
59 }
60
61 @Override
62 void runTest(Helper helper, boolean isLinkableRuntime) throws Exception {
63 // create a java.se using jmod-less approach
64 BaseJlinkSpecBuilder builder = new BaseJlinkSpecBuilder()
65 .helper(helper)
66 .name("java-se-jmodless")
67 .addModule("java.se")
68 .validatingModule("java.se");
69 if (isLinkableRuntime) {
70 builder.setLinkableRuntime();
71 }
72 Path javaSEruntimeLink = createJavaImageRuntimeLink(builder.build());
73
74 // create a java.se using packaged modules (jmod-full)
75 Path javaSEJmodFull = JImageGenerator.getJLinkTask()
76 .output(helper.createNewImageDir("java-se-jmodfull"))
77 .addMods("java.se").call().assertSuccess();
78
79 compareRecursively(javaSEruntimeLink, javaSEJmodFull);
80 }
81
82 // Visit all files in the given directories checking that they're byte-by-byte identical
83 private static void compareRecursively(Path javaSEJmodLess,
84 Path javaSEJmodFull) throws IOException, AssertionError {
85 FilesCapturingVisitor jmodFullVisitor = new FilesCapturingVisitor(javaSEJmodFull);
86 FilesCapturingVisitor jmodLessVisitor = new FilesCapturingVisitor(javaSEJmodLess);
87 Files.walkFileTree(javaSEJmodFull, jmodFullVisitor);
88 Files.walkFileTree(javaSEJmodLess, jmodLessVisitor);
89 List<String> jmodFullFiles = jmodFullVisitor.filesVisited();
90 List<String> jmodLessFiles = jmodLessVisitor.filesVisited();
91 Collections.sort(jmodFullFiles);
92 Collections.sort(jmodLessFiles);
93
103 throw new AssertionError(String.format("jmod-full path (%s) != jmod-less path (%s)", jmodFullPath, jmodLessPath));
104 }
105 if (jmodFullPath.equals(jimageFile)) {
106 continue;
107 }
108 Path a = javaSEJmodFull.resolve(Path.of(jmodFullPath));
109 Path b = javaSEJmodLess.resolve(Path.of(jmodLessPath));
110 if (Files.mismatch(a, b) != -1L) {
111 handleFileMismatch(a, b);
112 }
113 }
114 // Compare jimage contents by iterating its entries and comparing their
115 // paths and content bytes
116 //
117 // Note: The files aren't byte-by-byte comparable (probably due to string hashing
118 // and offset differences in container bytes)
119 Path jimageJmodLess = javaSEJmodLess.resolve(Path.of("lib")).resolve(Path.of("modules"));
120 Path jimageJmodFull = javaSEJmodFull.resolve(Path.of("lib")).resolve(Path.of("modules"));
121 List<String> jimageContentJmodLess = JImageHelper.listContents(jimageJmodLess);
122 List<String> jimageContentJmodFull = JImageHelper.listContents(jimageJmodFull);
123 if (jimageContentJmodLess.size() != jimageContentJmodFull.size()) {
124 throw new AssertionError(String.format("Size of jimage content differs for jmod-less (%d) v. jmod-full (%d)", jimageContentJmodLess.size(), jimageContentJmodFull.size()));
125 }
126 for (int i = 0; i < jimageContentJmodFull.size(); i++) {
127 if (!jimageContentJmodFull.get(i).equals(jimageContentJmodLess.get(i))) {
128 throw new AssertionError(String.format("Jimage content differs at index %d: jmod-full was: '%s' jmod-less was: '%s'",
129 i,
130 jimageContentJmodFull.get(i),
131 jimageContentJmodLess.get(i)
132 ));
133 }
134 String loc = jimageContentJmodFull.get(i);
135 if (isTreeInfoResource(loc)) {
136 // Skip container bytes as those are offsets to the content
137 // of the container which might be different between jlink runs.
138 continue;
139 }
140 byte[] resBytesFull = JImageHelper.getLocationBytes(loc, jimageJmodFull);
141 byte[] resBytesLess = JImageHelper.getLocationBytes(loc, jimageJmodLess);
142 if (resBytesFull.length != resBytesLess.length || Arrays.mismatch(resBytesFull, resBytesLess) != -1) {
143 throw new AssertionError("Content bytes mismatch for " + loc);
144 }
145 }
146 }
147
148 private static boolean isTreeInfoResource(String path) {
149 return path.startsWith("/packages") || path.startsWith("/modules");
150 }
151
152 private static void handleFileMismatch(Path a, Path b) {
153 throw new AssertionError("Files mismatch: " + a + " vs. " + b);
154 }
155
156 static class FilesCapturingVisitor extends SimpleFileVisitor<Path> {
157 private final Path basePath;
158 private final List<String> filePaths = new ArrayList<>();
159 public FilesCapturingVisitor(Path basePath) {
160 this.basePath = basePath;
161 }
162
163 @Override
164 public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
165 Path relative = basePath.relativize(path);
166 filePaths.add(relative.toString());
167 return FileVisitResult.CONTINUE;
168 }
169
|
1 /*
2 * Copyright (c) 2024, Red Hat, Inc.
3 * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 import java.io.IOException;
26 import java.nio.file.FileVisitResult;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.SimpleFileVisitor;
30 import java.nio.file.attribute.BasicFileAttributes;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Random;
36 import java.util.Set;
37 import java.util.function.Predicate;
38 import java.util.stream.Collectors;
39
40 import tests.Helper;
41 import tests.JImageGenerator;
42
43
44 /*
45 * @test
46 * @summary Compare packaged-modules jlink with a run-time image based jlink to
47 * produce the same result
48 * @requires (jlink.packagedModules & vm.compMode != "Xcomp" & os.maxMemory >= 2g)
49 * @library ../../lib /test/lib
50 * @enablePreview
51 * @modules java.base/jdk.internal.jimage
52 * jdk.jlink/jdk.tools.jlink.internal
53 * jdk.jlink/jdk.tools.jlink.plugin
54 * jdk.jlink/jdk.tools.jimage
55 * jdk.jlink
56 * @build tests.* jdk.test.lib.process.OutputAnalyzer
57 * jdk.test.lib.process.ProcessTools
58 * @run main/othervm/timeout=1200 -ea -esa -DDISABLE_PREVIEW_PATCHING=false -Xmx1g PackagedModulesVsRuntimeImageLinkTest
59 */
60 public class PackagedModulesVsRuntimeImageLinkTest extends AbstractLinkableRuntimeTest {
61
62 public static void main(String[] args) throws Exception {
63 PackagedModulesVsRuntimeImageLinkTest test = new PackagedModulesVsRuntimeImageLinkTest();
64 test.run();
65 }
66
67 @Override
68 void runTest(Helper helper, boolean isLinkableRuntime) throws Exception {
69 // create a java.se using jmod-less approach
70 BaseJlinkSpecBuilder builder = new BaseJlinkSpecBuilder()
71 .helper(helper)
72 .name("java-se-jmodless")
73 .addModule("java.se")
74 .validatingModule("java.se");
75 if (isLinkableRuntime) {
76 builder.setLinkableRuntime();
77 }
78 Path javaSEruntimeLink = createJavaImageRuntimeLink(builder.build());
79 // create a java.se using packaged modules (jmod-full)
80 Path javaSEJmodFull = JImageGenerator.getJLinkTask()
81 .output(helper.createNewImageDir("java-se-jmodfull"))
82 .addMods("java.se").call().assertSuccess();
83
84 compareRecursively(javaSEruntimeLink, javaSEJmodFull);
85 }
86
87 // Visit all files in the given directories checking that they're byte-by-byte identical
88 private static void compareRecursively(Path javaSEJmodLess,
89 Path javaSEJmodFull) throws IOException, AssertionError {
90 FilesCapturingVisitor jmodFullVisitor = new FilesCapturingVisitor(javaSEJmodFull);
91 FilesCapturingVisitor jmodLessVisitor = new FilesCapturingVisitor(javaSEJmodLess);
92 Files.walkFileTree(javaSEJmodFull, jmodFullVisitor);
93 Files.walkFileTree(javaSEJmodLess, jmodLessVisitor);
94 List<String> jmodFullFiles = jmodFullVisitor.filesVisited();
95 List<String> jmodLessFiles = jmodLessVisitor.filesVisited();
96 Collections.sort(jmodFullFiles);
97 Collections.sort(jmodLessFiles);
98
108 throw new AssertionError(String.format("jmod-full path (%s) != jmod-less path (%s)", jmodFullPath, jmodLessPath));
109 }
110 if (jmodFullPath.equals(jimageFile)) {
111 continue;
112 }
113 Path a = javaSEJmodFull.resolve(Path.of(jmodFullPath));
114 Path b = javaSEJmodLess.resolve(Path.of(jmodLessPath));
115 if (Files.mismatch(a, b) != -1L) {
116 handleFileMismatch(a, b);
117 }
118 }
119 // Compare jimage contents by iterating its entries and comparing their
120 // paths and content bytes
121 //
122 // Note: The files aren't byte-by-byte comparable (probably due to string hashing
123 // and offset differences in container bytes)
124 Path jimageJmodLess = javaSEJmodLess.resolve(Path.of("lib")).resolve(Path.of("modules"));
125 Path jimageJmodFull = javaSEJmodFull.resolve(Path.of("lib")).resolve(Path.of("modules"));
126 List<String> jimageContentJmodLess = JImageHelper.listContents(jimageJmodLess);
127 List<String> jimageContentJmodFull = JImageHelper.listContents(jimageJmodFull);
128 assertSameContent("jmod-less", jimageContentJmodLess, "jmod-full", jimageContentJmodFull);
129 // Both lists are same size, with same names, so enumerate either.
130 for (int i = 0; i < jimageContentJmodFull.size(); i++) {
131 if (!jimageContentJmodFull.get(i).equals(jimageContentJmodLess.get(i))) {
132 throw new AssertionError(String.format("Jimage content differs at index %d: jmod-full was: '%s' jmod-less was: '%s'",
133 i,
134 jimageContentJmodFull.get(i),
135 jimageContentJmodLess.get(i)
136 ));
137 }
138 String loc = jimageContentJmodFull.get(i);
139 if (isTreeInfoResource(loc)) {
140 // Skip container bytes as those are offsets to the content
141 // of the container which might be different between jlink runs.
142 continue;
143 }
144 byte[] resBytesFull = JImageHelper.getLocationBytes(loc, jimageJmodFull);
145 byte[] resBytesLess = JImageHelper.getLocationBytes(loc, jimageJmodLess);
146 if (resBytesFull.length != resBytesLess.length || Arrays.mismatch(resBytesFull, resBytesLess) != -1) {
147 throw new AssertionError("Content bytes mismatch for " + loc);
148 }
149 }
150 }
151
152 // Helper to assert the content of two jimage files are the same and provide
153 // useful debug information otherwise.
154 private static void assertSameContent(
155 String lhsLabel, List<String> lhsNames, String rhsLabel, List<String> rhsNames) {
156
157 List<String> lhsOnly =
158 lhsNames.stream().filter(Predicate.not(Set.copyOf(rhsNames)::contains)).toList();
159 List<String> rhsOnly =
160 rhsNames.stream().filter(Predicate.not(Set.copyOf(lhsNames)::contains)).toList();
161 if (!lhsOnly.isEmpty() || !rhsOnly.isEmpty()) {
162 String message = String.format(
163 "jimage content differs for %s (%d) v. %s (%d)",
164 lhsLabel, lhsNames.size(), rhsLabel, rhsNames.size());
165 if (!lhsOnly.isEmpty()) {
166 message += "\nOnly in " + lhsLabel + ":\n\t" + String.join("\n\t", lhsOnly);
167 }
168 if (!rhsOnly.isEmpty()) {
169 message += "\nOnly in " + rhsLabel + ":\n\t" + String.join("\n\t", rhsOnly);
170 }
171 throw new AssertionError(message);
172 }
173 }
174
175 private static boolean isTreeInfoResource(String path) {
176 return pathStartsWith(path, "/packages") || pathStartsWith(path, "/modules");
177 }
178
179 // Handle both "<prefix>" and "<prefix>/...".
180 private static boolean pathStartsWith(String path, String prefix) {
181 int plen = prefix.length();
182 return path.startsWith(prefix) && (path.length() == plen || path.charAt(plen) == '/');
183 }
184
185 private static void handleFileMismatch(Path a, Path b) {
186 throw new AssertionError("Files mismatch: " + a + " vs. " + b);
187 }
188
189 static class FilesCapturingVisitor extends SimpleFileVisitor<Path> {
190 private final Path basePath;
191 private final List<String> filePaths = new ArrayList<>();
192 public FilesCapturingVisitor(Path basePath) {
193 this.basePath = basePath;
194 }
195
196 @Override
197 public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
198 Path relative = basePath.relativize(path);
199 filePaths.add(relative.toString());
200 return FileVisitResult.CONTINUE;
201 }
202
|