Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jlink/plugins/OrderResourcesPluginTest.java
41149 views
1
/*
2
* Copyright (c) 2015, 2020, 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
* @summary Test sorter plugin
27
* @author Jean-Francois Denise
28
* @modules jdk.jlink/jdk.tools.jlink.internal
29
* jdk.jlink/jdk.tools.jlink.internal.plugins
30
* jdk.jlink/jdk.tools.jlink.plugin
31
* @run main OrderResourcesPluginTest
32
*/
33
34
import java.io.File;
35
import java.nio.file.Files;
36
import java.util.Arrays;
37
import java.util.Collection;
38
import java.util.HashMap;
39
import java.util.Map;
40
import java.util.stream.Collectors;
41
42
import jdk.tools.jlink.internal.ResourcePoolManager;
43
import jdk.tools.jlink.internal.plugins.OrderResourcesPlugin;
44
import jdk.tools.jlink.plugin.ResourcePoolEntry;
45
import jdk.tools.jlink.plugin.ResourcePool;
46
import jdk.tools.jlink.plugin.Plugin;
47
48
public class OrderResourcesPluginTest {
49
50
public static void main(String[] args) throws Exception {
51
new OrderResourcesPluginTest().test();
52
}
53
54
public void test() throws Exception {
55
ResourcePoolEntry[] array = {
56
ResourcePoolEntry.create("/module1/toto1.class", new byte[0]),
57
ResourcePoolEntry.create("/module2/toto2.class", new byte[0]),
58
ResourcePoolEntry.create("/module3/toto3.class", new byte[0]),
59
ResourcePoolEntry.create("/module3/toto3/module-info.class", new byte[0]),
60
ResourcePoolEntry.create("/zazou/toto.class", new byte[0]),
61
ResourcePoolEntry.create("/module4/zazou.class", new byte[0]),
62
ResourcePoolEntry.create("/module5/toto5.class", new byte[0]),
63
ResourcePoolEntry.create("/module6/toto6/module-info.class", new byte[0])
64
};
65
66
ResourcePoolEntry[] sorted = {
67
ResourcePoolEntry.create("/zazou/toto.class", new byte[0]),
68
ResourcePoolEntry.create("/module3/toto3/module-info.class", new byte[0]),
69
ResourcePoolEntry.create("/module6/toto6/module-info.class", new byte[0]),
70
ResourcePoolEntry.create("/module1/toto1.class", new byte[0]),
71
ResourcePoolEntry.create("/module2/toto2.class", new byte[0]),
72
ResourcePoolEntry.create("/module3/toto3.class", new byte[0]),
73
ResourcePoolEntry.create("/module4/zazou.class", new byte[0]),
74
ResourcePoolEntry.create("/module5/toto5.class", new byte[0])
75
};
76
77
ResourcePoolEntry[] sorted2 = {
78
ResourcePoolEntry.create("/module5/toto5.class", new byte[0]),
79
ResourcePoolEntry.create("/module6/toto6/module-info.class", new byte[0]),
80
ResourcePoolEntry.create("/module4/zazou.class", new byte[0]),
81
ResourcePoolEntry.create("/module3/toto3.class", new byte[0]),
82
ResourcePoolEntry.create("/module3/toto3/module-info.class", new byte[0]),
83
ResourcePoolEntry.create("/module1/toto1.class", new byte[0]),
84
ResourcePoolEntry.create("/module2/toto2.class", new byte[0]),
85
ResourcePoolEntry.create("/zazou/toto.class", new byte[0])
86
};
87
88
ResourcePoolManager resources = new ResourcePoolManager();
89
for (ResourcePoolEntry r : array) {
90
resources.add(r);
91
}
92
93
{
94
ResourcePoolManager out = new ResourcePoolManager();
95
Map<String, String> config = new HashMap<>();
96
config.put("order-resources", "/zazou/**,**/module-info.class");
97
Plugin p = new OrderResourcesPlugin();
98
p.configure(config);
99
ResourcePool resPool = p.transform(resources.resourcePool(), out.resourcePoolBuilder());
100
check(out.entries().collect(Collectors.toList()), sorted);
101
}
102
103
{
104
// Order of resources in the file, then un-ordered resources.
105
File order = new File("resources.order");
106
order.createNewFile();
107
StringBuilder builder = new StringBuilder();
108
// 5 first resources come from file
109
for (int i = 0; i < 5; i++) {
110
String path = sorted2[i].path();
111
int index = path.indexOf('/', 1);
112
path = path.substring(index + 1, path.length() - ".class".length());
113
builder.append(path).append("\n");
114
}
115
Files.write(order.toPath(), builder.toString().getBytes());
116
117
ResourcePoolManager out = new ResourcePoolManager();
118
Map<String, String> config = new HashMap<>();
119
config.put("order-resources", "@" + order.getAbsolutePath());
120
Plugin p = new OrderResourcesPlugin();
121
p.configure(config);
122
ResourcePool resPool = p.transform(resources.resourcePool(), out.resourcePoolBuilder());
123
check(out.entries().collect(Collectors.toList()), sorted2);
124
125
}
126
}
127
128
private void check(Collection<ResourcePoolEntry> outResources,
129
ResourcePoolEntry[] sorted) {
130
if (outResources.size() != sorted.length) {
131
throw new AssertionError("Wrong number of resources:\n"
132
+ "expected: " + Arrays.toString(sorted) + ",\n"
133
+ " got: " + outResources);
134
}
135
int i = 0;
136
for (ResourcePoolEntry r : outResources) {
137
System.err.println("Resource: " + r);
138
if (!sorted[i].path().equals(r.path())) {
139
throw new AssertionError("Resource not properly sorted, difference at: " + i + "\n"
140
+ "expected: " + Arrays.toString(sorted) + ",\n"
141
+ " got: " + outResources);
142
}
143
i++;
144
}
145
}
146
}
147
148