Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jlink/JLinkReproducibleTest.java
41144 views
1
/*
2
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
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.File;
25
import java.nio.file.*;
26
import java.util.*;
27
28
import static jdk.test.lib.Asserts.*;
29
import jdk.test.lib.JDKToolFinder;
30
import jdk.test.lib.process.ProcessTools;
31
32
/*
33
* @test
34
* @bug 8214230
35
* @summary Test that jlinks generates reproducible modules files
36
* @library /test/lib
37
* @run driver JLinkReproducibleTest
38
*/
39
public class JLinkReproducibleTest {
40
private static void run(List<String> cmd) throws Exception {
41
var pb = new ProcessBuilder(cmd.toArray(new String[0]));
42
var res = ProcessTools.executeProcess(pb);
43
res.shouldHaveExitValue(0);
44
}
45
46
private static void jlink(Path image, boolean with_default_trace_file) throws Exception {
47
var cmd = new ArrayList<String>();
48
cmd.add(JDKToolFinder.getJDKTool("jlink"));
49
cmd.addAll(List.of(
50
"--module-path", JMODS_DIR.toString() + File.pathSeparator + CLASS_DIR.toString(),
51
"--add-modules", "main",
52
"--compress=2",
53
"--output", image.toString()
54
));
55
if (!with_default_trace_file) {
56
cmd.add("--generate-jli-classes=@file-not-exists");
57
}
58
run(cmd);
59
}
60
61
private static void javac(String... args) throws Exception {
62
var cmd = new ArrayList<String>();
63
cmd.add(JDKToolFinder.getJDKTool("javac"));
64
cmd.addAll(Arrays.asList(args));
65
run(cmd);
66
}
67
68
private static final List<String> MODULE_INFO = List.of(
69
"module main {",
70
" exports org.test.main;",
71
"}"
72
);
73
74
private static final List<String> MAIN_CLASS = List.of(
75
"package org.test.main;",
76
"public class Main {",
77
" public static void main(String[] args) {",
78
" System.out.println(\"Hello, world\");",
79
" }",
80
"}"
81
);
82
83
private static final Path CLASS_DIR = Path.of("classes");
84
private static final Path JMODS_DIR = Path.of(System.getProperty("java.home"), "jmods");
85
86
public static void main(String[] args) throws Exception {
87
// Write the source code
88
var srcDir = Path.of("main", "org", "test", "main");
89
Files.createDirectories(srcDir.toAbsolutePath());
90
91
var srcFile = srcDir.resolve("Main.java");
92
Files.write(srcFile, MAIN_CLASS);
93
94
var moduleFile = Path.of("main").resolve("module-info.java");
95
Files.write(moduleFile, MODULE_INFO);
96
97
// Compile the source code to class files
98
javac("--module-source-path", ".",
99
"--module", "main",
100
"-d", CLASS_DIR.toString());
101
102
// Link the first image
103
var firstImage = Path.of("image-first");
104
jlink(firstImage, true);
105
var firstModulesFile = firstImage.resolve("lib")
106
.resolve("modules");
107
108
// Link the second image
109
var secondImage = Path.of("image-second");
110
jlink(secondImage, true);
111
var secondModulesFile = secondImage.resolve("lib")
112
.resolve("modules");
113
114
// Ensure module files are identical
115
assertEquals(-1L, Files.mismatch(firstModulesFile, secondModulesFile));
116
117
// Link the third image
118
var thirdImage = Path.of("image-third");
119
jlink(thirdImage, false);
120
var thirdModulesFile = thirdImage.resolve("lib")
121
.resolve("modules");
122
// Link the fourth image
123
var fourthImage = Path.of("image-fourth");
124
jlink(fourthImage, false);
125
var fourthModulesFile = fourthImage.resolve("lib")
126
.resolve("modules");
127
128
// Ensure module files are identical
129
assertEquals(-1L, Files.mismatch(thirdModulesFile, fourthModulesFile));
130
}
131
}
132
133