Skip to content

Commit 9695283

Browse files
Dongbo HeRealFYang
authored andcommitted
8240903: Add test to check that jmod hashes are reproducible
Reviewed-by: alanb
1 parent dce7240 commit 9695283

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (c) 2022, Huawei Technologies Co., Ltd. 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+
/*
25+
* @test
26+
* @bug 8240903
27+
* @summary Test consistency of moduleHashes attribute between builds
28+
* @library /test/lib
29+
* @run testng HashesOrderTest
30+
*/
31+
32+
import java.io.IOException;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
35+
import java.util.ArrayList;
36+
import java.util.Arrays;
37+
import java.util.Collections;
38+
import java.util.List;
39+
import java.util.spi.ToolProvider;
40+
import java.util.stream.Collectors;
41+
42+
import jdk.test.lib.compiler.ModuleInfoMaker;
43+
44+
import org.testng.annotations.Test;
45+
46+
import static org.testng.Assert.assertEquals;
47+
48+
public class HashesOrderTest {
49+
private ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
50+
.orElseThrow(() ->
51+
new RuntimeException("jmod tool not found")
52+
);
53+
54+
private String DATE = "2021-01-06T14:36:00+02:00";
55+
private int NUM_MODULES = 64;
56+
private Path mods;
57+
private Path lib1;
58+
private Path lib2;
59+
private ModuleInfoMaker builder;
60+
61+
@Test
62+
public void test() throws Exception {
63+
mods = Path.of("mods");
64+
lib1 = Path.of("lib1");
65+
lib2 = Path.of("lib2");
66+
builder = new ModuleInfoMaker(Path.of("src"));
67+
68+
Files.createDirectories(mods);
69+
Files.createDirectories(lib1);
70+
Files.createDirectories(lib2);
71+
72+
makeModule("ma");
73+
String moduleName;
74+
for (int i = 0; i < NUM_MODULES; i++) {
75+
moduleName = "m" + i + "b";
76+
makeModule(moduleName, "ma");
77+
makeJmod(moduleName, lib1);
78+
makeJmod(moduleName, lib2);
79+
}
80+
makeJmod("ma", lib1, "--module-path", lib1.toString(),
81+
"--hash-modules", ".*");
82+
Path jmod1 = lib1.resolve("ma.jmod");
83+
84+
makeJmod("ma", lib2, "--module-path", lib2.toString(),
85+
"--hash-modules", ".*");
86+
Path jmod2 = lib2.resolve("ma.jmod");
87+
88+
assertEquals(Files.mismatch(jmod1, jmod2), -1);
89+
}
90+
91+
private void makeModule(String mn, String... deps)
92+
throws IOException
93+
{
94+
StringBuilder sb = new StringBuilder();
95+
sb.append("module ")
96+
.append(mn)
97+
.append(" {")
98+
.append("\n");
99+
Arrays.stream(deps)
100+
.forEach(req -> {
101+
sb.append(" requires ");
102+
sb.append(req)
103+
.append(";\n");
104+
});
105+
sb.append("}\n");
106+
builder.writeJavaFiles(mn, sb.toString());
107+
builder.compile(mn, mods);
108+
}
109+
110+
private void makeJmod(String moduleName, Path libName, String... options) {
111+
Path mclasses = mods.resolve(moduleName);
112+
Path outfile = libName.resolve(moduleName + ".jmod");
113+
List<String> args = new ArrayList<>();
114+
args.add("create");
115+
Collections.addAll(args, options);
116+
Collections.addAll(args, "--date", DATE);
117+
Collections.addAll(args, "--class-path", mclasses.toString(),
118+
outfile.toString());
119+
120+
runJmod(args);
121+
}
122+
123+
private void runJmod(List<String> args) {
124+
runJmod(args.toArray(new String[args.size()]));
125+
}
126+
127+
private void runJmod(String... args) {
128+
int rc = JMOD_TOOL.run(System.out, System.out, args);
129+
System.out.println("jmod " + Arrays.stream(args).collect(Collectors.joining(" ")));
130+
if (rc != 0) {
131+
throw new AssertionError("jmod failed: rc = " + rc);
132+
}
133+
}
134+
135+
}

0 commit comments

Comments
 (0)