Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jlink/plugins/LegalFilePluginTest.java
41149 views
1
/*
2
* Copyright (c) 2016, 2017, 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
/*
25
* @test
26
* @bug 8169925
27
* @summary Validate the license files deduplicated in the image
28
* @library /test/lib
29
* @modules jdk.compiler
30
* jdk.jlink
31
* @build jdk.test.lib.compiler.CompilerUtils
32
* @run testng LegalFilePluginTest
33
*/
34
35
import java.io.BufferedWriter;
36
import java.io.File;
37
import java.io.IOException;
38
import java.io.PrintWriter;
39
import java.io.StringWriter;
40
import java.io.UncheckedIOException;
41
import java.nio.file.FileVisitResult;
42
import java.nio.file.Files;
43
import java.nio.file.Path;
44
import java.nio.file.Paths;
45
import java.nio.file.SimpleFileVisitor;
46
import java.nio.file.attribute.BasicFileAttributes;
47
import java.util.ArrayList;
48
import java.util.HashMap;
49
import java.util.HashSet;
50
import java.util.List;
51
import java.util.Map;
52
import java.util.Set;
53
import java.util.spi.ToolProvider;
54
import java.util.stream.Collectors;
55
import java.util.stream.Stream;
56
import jdk.test.lib.compiler.CompilerUtils;
57
58
import org.testng.annotations.BeforeTest;
59
import org.testng.annotations.DataProvider;
60
import org.testng.annotations.Test;
61
62
import static org.testng.Assert.*;
63
64
public class LegalFilePluginTest {
65
static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
66
.orElseThrow(() ->
67
new RuntimeException("jmod tool not found")
68
);
69
70
static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
71
.orElseThrow(() ->
72
new RuntimeException("jlink tool not found")
73
);
74
75
static final Path MODULE_PATH = Paths.get(System.getProperty("java.home"), "jmods");
76
static final Path SRC_DIR = Paths.get("src");
77
static final Path MODS_DIR = Paths.get("mods");
78
static final Path JMODS_DIR = Paths.get("jmods");
79
static final Path LEGAL_DIR = Paths.get("legal");
80
static final Path IMAGES_DIR = Paths.get("images");
81
82
static final Map<List<String>, Map<String,String>> LICENSES = Map.of(
83
// Key is module name and requires
84
// Value is a map of filename to the file content
85
List.of("m1"), Map.of("LICENSE", "m1 LICENSE",
86
"m1-license.txt", "m1 license",
87
"test-license", "test license v1"),
88
List.of("m2", "m1"), Map.of("m2-license", "m2 license",
89
"test-license", "test license v1"),
90
List.of("m3"), Map.of("m3-license.md", "m3 license",
91
"test-license", "test license v3"),
92
List.of("m4"), Map.of("test-license", "test license v4")
93
);
94
95
@BeforeTest
96
private void setup() throws Exception {
97
List<JmodFileBuilder> builders = new ArrayList<>();
98
for (Map.Entry<List<String>, Map<String,String>> e : LICENSES.entrySet()) {
99
List<String> names = e.getKey();
100
String mn = names.get(0);
101
JmodFileBuilder builder = new JmodFileBuilder(mn);
102
builders.add(builder);
103
104
if (names.size() > 1) {
105
names.subList(1, names.size())
106
.stream()
107
.forEach(builder::requires);
108
}
109
e.getValue().entrySet()
110
.stream()
111
.forEach(f -> builder.licenseFile(f.getKey(), f.getValue()));
112
// generate source
113
builder.writeModuleInfo();
114
}
115
116
// create jmod file
117
for (JmodFileBuilder builder: builders) {
118
builder.build();
119
}
120
121
}
122
123
private String imageDir(String dir) {
124
return IMAGES_DIR.resolve(dir).toString();
125
}
126
127
128
@DataProvider(name = "modules")
129
public Object[][] jlinkoptions() {
130
String m2TestLicenseContent =
131
symlinkContent(Paths.get("legal", "m2", "test-license"),
132
Paths.get("legal", "m1", "test-license"),
133
"test license v1");
134
// options and expected header files & man pages
135
return new Object[][] {
136
{ new String [] {
137
"test1",
138
"--add-modules=m1",
139
},
140
Map.of( "m1/LICENSE", "m1 LICENSE",
141
"m1/m1-license.txt", "m1 license",
142
"m1/test-license", "test license v1")
143
},
144
{ new String [] {
145
"test2",
146
"--add-modules=m1,m2",
147
},
148
Map.of( "m1/LICENSE", "m1 LICENSE",
149
"m1/m1-license.txt", "m1 license",
150
"m1/test-license", "test license v1",
151
"m2/m2-license", "m2 license",
152
"m2/test-license", m2TestLicenseContent),
153
},
154
{ new String [] {
155
"test3",
156
"--add-modules=m2,m3",
157
},
158
Map.of( "m1/LICENSE", "m1 LICENSE",
159
"m1/m1-license.txt", "m1 license",
160
"m1/test-license", "test license v1",
161
"m2/m2-license", "m2 license",
162
"m2/test-license", m2TestLicenseContent,
163
"m3/m3-license.md", "m3 license",
164
"m3/test-license", "test license v3"),
165
},
166
};
167
}
168
169
private static String symlinkContent(Path source, Path target, String content) {
170
String osName = System.getProperty("os.name");
171
if (!osName.startsWith("Windows") && MODULE_PATH.getFileSystem()
172
.supportedFileAttributeViews()
173
.contains("posix")) {
174
// symlink created
175
return content;
176
} else {
177
// tiny file is created
178
Path symlink = source.getParent().relativize(target);
179
return String.format("Please see %s", symlink.toString());
180
}
181
}
182
183
@Test(dataProvider = "modules")
184
public void test(String[] opts, Map<String,String> expectedFiles) throws Exception {
185
if (Files.notExists(MODULE_PATH)) {
186
// exploded image
187
return;
188
}
189
190
String dir = opts[0];
191
List<String> options = new ArrayList<>();
192
for (int i = 1; i < opts.length; i++) {
193
options.add(opts[i]);
194
}
195
196
String mpath = MODULE_PATH.toString() + File.pathSeparator +
197
JMODS_DIR.toString();
198
Stream.of("--module-path", mpath,
199
"--output", imageDir(dir))
200
.forEach(options::add);
201
202
Path image = createImage(dir, options);
203
204
Files.walk(image.resolve("legal"), Integer.MAX_VALUE)
205
.filter(p -> Files.isRegularFile(p))
206
.filter(p -> p.getParent().endsWith("m1") ||
207
p.getParent().endsWith("m2") ||
208
p.getParent().endsWith("m3") ||
209
p.getParent().endsWith("m4"))
210
.forEach(p -> {
211
String fn = image.resolve("legal").relativize(p)
212
.toString()
213
.replace(File.separatorChar, '/');
214
System.out.println(fn);
215
if (!expectedFiles.containsKey(fn)) {
216
throw new RuntimeException(fn + " should not be in the image");
217
}
218
compareFileContent(p, expectedFiles.get(fn));
219
});
220
}
221
222
@Test
223
public void errorIfNotSameContent() {
224
if (Files.notExists(MODULE_PATH)) {
225
// exploded image
226
return;
227
}
228
229
String dir = "test";
230
231
String mpath = MODULE_PATH.toString() + File.pathSeparator +
232
JMODS_DIR.toString();
233
List<String> options = Stream.of("--dedup-legal-notices",
234
"error-if-not-same-content",
235
"--module-path", mpath,
236
"--add-modules=m3,m4",
237
"--output", imageDir(dir))
238
.collect(Collectors.toList());
239
240
StringWriter writer = new StringWriter();
241
PrintWriter pw = new PrintWriter(writer);
242
System.out.println("jlink " + options.stream().collect(Collectors.joining(" ")));
243
int rc = JLINK_TOOL.run(pw, pw,
244
options.toArray(new String[0]));
245
assertTrue(rc != 0);
246
assertTrue(writer.toString().trim()
247
.matches("Error:.*/m4/legal/m4/test-license .*contain different content"));
248
}
249
250
private void compareFileContent(Path file, String content) {
251
try {
252
byte[] bytes = Files.readAllBytes(file);
253
byte[] expected = String.format("%s%n", content).getBytes();
254
assertEquals(bytes, expected, String.format("%s not matched:%nfile: %s%nexpected:%s%n",
255
file.toString(), new String(bytes), new String(expected)));
256
} catch (IOException e) {
257
throw new UncheckedIOException(e);
258
}
259
}
260
261
private Path createImage(String outputDir, List<String> options) {
262
System.out.println("jlink " + options.stream().collect(Collectors.joining(" ")));
263
int rc = JLINK_TOOL.run(System.out, System.out,
264
options.toArray(new String[0]));
265
assertTrue(rc == 0);
266
267
return IMAGES_DIR.resolve(outputDir);
268
}
269
270
private void deleteDirectory(Path dir) throws IOException {
271
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
272
@Override
273
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
274
throws IOException
275
{
276
Files.delete(file);
277
return FileVisitResult.CONTINUE;
278
}
279
280
@Override
281
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
282
throws IOException
283
{
284
Files.delete(dir);
285
return FileVisitResult.CONTINUE;
286
}
287
});
288
}
289
290
/**
291
* Builder to create JMOD file
292
*/
293
class JmodFileBuilder {
294
295
final String name;
296
final Set<String> requires = new HashSet<>();
297
final Map<String, String> licenses = new HashMap<>();
298
299
JmodFileBuilder(String name) throws IOException {
300
this.name = name;
301
302
Path msrc = SRC_DIR.resolve(name);
303
if (Files.exists(msrc)) {
304
deleteDirectory(msrc);
305
}
306
}
307
308
JmodFileBuilder writeModuleInfo()throws IOException {
309
Path msrc = SRC_DIR.resolve(name);
310
Files.createDirectories(msrc);
311
Path minfo = msrc.resolve("module-info.java");
312
try (BufferedWriter bw = Files.newBufferedWriter(minfo);
313
PrintWriter writer = new PrintWriter(bw)) {
314
writer.format("module %s {%n", name);
315
for (String req : requires) {
316
writer.format(" requires %s;%n", req);
317
}
318
writer.format("}%n");
319
}
320
return this;
321
}
322
323
JmodFileBuilder licenseFile(String filename, String content) {
324
licenses.put(filename, content);
325
return this;
326
}
327
328
JmodFileBuilder requires(String name) {
329
requires.add(name);
330
return this;
331
}
332
333
Path build() throws IOException {
334
compileModule();
335
336
Path mdir = LEGAL_DIR.resolve(name);
337
for (Map.Entry<String,String> e : licenses.entrySet()) {
338
Files.createDirectories(mdir);
339
String filename = e.getKey();
340
String content = e.getValue();
341
Path file = mdir.resolve(filename);
342
try (BufferedWriter writer = Files.newBufferedWriter(file);
343
PrintWriter pw = new PrintWriter(writer)) {
344
pw.println(content);
345
}
346
}
347
348
return createJmodFile();
349
}
350
351
352
void compileModule() throws IOException {
353
Path msrc = SRC_DIR.resolve(name);
354
assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
355
"--module-source-path",
356
SRC_DIR.toString()));
357
}
358
359
Path createJmodFile() throws IOException {
360
Path mclasses = MODS_DIR.resolve(name);
361
Files.createDirectories(JMODS_DIR);
362
Path outfile = JMODS_DIR.resolve(name + ".jmod");
363
List<String> args = new ArrayList<>();
364
args.add("create");
365
// add classes
366
args.add("--class-path");
367
args.add(mclasses.toString());
368
if (licenses.size() > 0) {
369
args.add("--legal-notices");
370
args.add(LEGAL_DIR.resolve(name).toString());
371
}
372
args.add(outfile.toString());
373
374
if (Files.exists(outfile))
375
Files.delete(outfile);
376
377
System.out.println("jmod " +
378
args.stream().collect(Collectors.joining(" ")));
379
380
int rc = JMOD_TOOL.run(System.out, System.out,
381
args.toArray(new String[args.size()]));
382
if (rc != 0) {
383
throw new AssertionError("jmod failed: rc = " + rc);
384
}
385
return outfile;
386
}
387
}
388
}
389
390