36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 last sorter property
|
|
27 |
* @author Jean-Francois Denise
|
|
28 |
* @modules jdk.jlink/jdk.tools.jlink.internal
|
38320
|
29 |
* jdk.jlink/jdk.tools.jlink
|
36511
|
30 |
* @run main/othervm LastSorterTest
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.util.ArrayList;
|
|
34 |
import java.util.Collections;
|
|
35 |
import java.util.HashMap;
|
|
36 |
import java.util.List;
|
|
37 |
import java.util.Map;
|
|
38 |
|
|
39 |
import jdk.tools.jlink.internal.ImagePluginConfiguration;
|
|
40 |
import jdk.tools.jlink.internal.PluginRepository;
|
|
41 |
import jdk.tools.jlink.internal.ImagePluginStack;
|
38320
|
42 |
import jdk.tools.jlink.internal.ModulePoolImpl;
|
36511
|
43 |
import jdk.tools.jlink.Jlink;
|
|
44 |
import jdk.tools.jlink.Jlink.PluginsConfiguration;
|
|
45 |
import jdk.tools.jlink.plugin.Plugin;
|
38320
|
46 |
import jdk.tools.jlink.plugin.ModuleEntry;
|
|
47 |
import jdk.tools.jlink.plugin.ModulePool;
|
36511
|
48 |
import jdk.tools.jlink.plugin.TransformerPlugin;
|
|
49 |
|
|
50 |
public class LastSorterTest {
|
|
51 |
|
|
52 |
public LastSorterTest() {
|
|
53 |
for (int i = 1; i <= 6; i++) {
|
|
54 |
PluginRepository.registerPlugin(new SorterPlugin("sorterplugin" + i));
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
public static void main(String[] args) throws Exception {
|
|
59 |
new LastSorterTest().test();
|
|
60 |
}
|
|
61 |
|
|
62 |
public void test() throws Exception {
|
|
63 |
checkUnknownPlugin();
|
|
64 |
|
|
65 |
checkOrderAfterLastSorter();
|
|
66 |
|
|
67 |
checkPositiveCase();
|
|
68 |
|
|
69 |
checkTwoLastSorters();
|
|
70 |
}
|
|
71 |
|
|
72 |
private void checkTwoLastSorters() throws Exception {
|
|
73 |
List<Plugin> plugins = new ArrayList<>();
|
|
74 |
plugins.add(createPlugin("sorterplugin5", "/a"));
|
|
75 |
plugins.add(createPlugin("sorterplugin6", "/a"));
|
|
76 |
PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins,
|
|
77 |
null, "sorterplugin5");
|
|
78 |
|
|
79 |
ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config);
|
|
80 |
|
|
81 |
// check order
|
38320
|
82 |
ModulePoolImpl res = fillOutResourceModulePool();
|
36511
|
83 |
|
|
84 |
try {
|
|
85 |
stack.visitResources(res);
|
|
86 |
throw new AssertionError("Exception expected: Order of resources is already frozen." +
|
|
87 |
"Plugin sorterplugin6 is badly located");
|
|
88 |
} catch (Exception e) {
|
|
89 |
// expected
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
38320
|
93 |
private ModulePoolImpl fillOutResourceModulePool() throws Exception {
|
|
94 |
ModulePoolImpl res = new ModulePoolImpl();
|
|
95 |
res.add(ModuleEntry.create("/eee/bbb/res1.class", new byte[90]));
|
|
96 |
res.add(ModuleEntry.create("/aaaa/bbb/res2.class", new byte[90]));
|
|
97 |
res.add(ModuleEntry.create("/bbb/aa/res1.class", new byte[90]));
|
|
98 |
res.add(ModuleEntry.create("/aaaa/bbb/res3.class", new byte[90]));
|
|
99 |
res.add(ModuleEntry.create("/bbb/aa/res2.class", new byte[90]));
|
|
100 |
res.add(ModuleEntry.create("/fff/bbb/res1.class", new byte[90]));
|
|
101 |
res.add(ModuleEntry.create("/aaaa/bbb/res1.class", new byte[90]));
|
|
102 |
res.add(ModuleEntry.create("/bbb/aa/res3.class", new byte[90]));
|
|
103 |
res.add(ModuleEntry.create("/ccc/bbb/res1.class", new byte[90]));
|
|
104 |
res.add(ModuleEntry.create("/ddd/bbb/res1.class", new byte[90]));
|
36511
|
105 |
return res;
|
|
106 |
}
|
|
107 |
|
|
108 |
private static Plugin createPlugin(String name, String arg) {
|
|
109 |
Map<String, String> conf = new HashMap<>();
|
|
110 |
conf.put(name, arg);
|
|
111 |
return Jlink.newPlugin(name, conf, null);
|
|
112 |
}
|
|
113 |
|
|
114 |
private void checkPositiveCase() throws Exception {
|
|
115 |
List<Plugin> plugins = new ArrayList<>();
|
|
116 |
plugins.add(createPlugin("sorterplugin1", "/c"));
|
|
117 |
plugins.add(createPlugin("sorterplugin2", "/b"));
|
|
118 |
plugins.add(createPlugin("sorterplugin3", "/a"));
|
|
119 |
|
|
120 |
PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins,
|
|
121 |
null, "sorterplugin3");
|
|
122 |
|
|
123 |
ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config);
|
|
124 |
|
|
125 |
// check order
|
38320
|
126 |
ModulePoolImpl res = fillOutResourceModulePool();
|
36511
|
127 |
|
|
128 |
stack.visitResources(res);
|
|
129 |
}
|
|
130 |
|
|
131 |
private void checkUnknownPlugin() {
|
|
132 |
List<Plugin> plugins = new ArrayList<>();
|
|
133 |
plugins.add(createPlugin("sorterplugin1", "/1"));
|
|
134 |
plugins.add(createPlugin("sorterplugin2", "/1"));
|
|
135 |
plugins.add(createPlugin("sorterplugin3", "/1"));
|
|
136 |
plugins.add(createPlugin("sorterplugin4", "/1"));
|
|
137 |
|
|
138 |
PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins,
|
|
139 |
null, "sorterplugin5");
|
|
140 |
try {
|
|
141 |
ImagePluginConfiguration.parseConfiguration(config);
|
|
142 |
throw new AssertionError("Unknown plugin should have failed.");
|
|
143 |
} catch (Exception ex) {
|
|
144 |
// XXX OK expected
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
private void checkOrderAfterLastSorter() throws Exception {
|
|
149 |
List<Plugin> plugins = new ArrayList<>();
|
|
150 |
plugins.add(createPlugin("sorterplugin1", "/c"));
|
|
151 |
plugins.add(createPlugin("sorterplugin2", "/b"));
|
|
152 |
plugins.add(createPlugin("sorterplugin3", "/a"));
|
|
153 |
plugins.add(createPlugin("sorterplugin4", "/d"));
|
|
154 |
|
|
155 |
PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins,
|
|
156 |
null, "sorterplugin3");
|
|
157 |
|
|
158 |
ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config);
|
|
159 |
|
|
160 |
// check order
|
38320
|
161 |
ModulePoolImpl res = fillOutResourceModulePool();
|
36511
|
162 |
try {
|
|
163 |
stack.visitResources(res);
|
|
164 |
throw new AssertionError("Order was changed after the last sorter, but no exception occurred");
|
|
165 |
} catch (Exception ex) {
|
|
166 |
// XXX OK expected
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
public static class SorterPlugin implements TransformerPlugin {
|
|
171 |
|
|
172 |
private final String name;
|
|
173 |
private String starts;
|
|
174 |
|
|
175 |
private SorterPlugin(String name) {
|
|
176 |
this.name = name;
|
|
177 |
}
|
|
178 |
|
|
179 |
@Override
|
38320
|
180 |
public void visit(ModulePool resources, ModulePool output) {
|
|
181 |
List<ModuleEntry> paths = new ArrayList<>();
|
|
182 |
resources.entries().forEach(res -> {
|
36511
|
183 |
if (res.getPath().startsWith(starts)) {
|
|
184 |
paths.add(0, res);
|
|
185 |
} else {
|
|
186 |
paths.add(res);
|
|
187 |
}
|
38320
|
188 |
});
|
36511
|
189 |
|
38320
|
190 |
for (ModuleEntry r : paths) {
|
36511
|
191 |
output.add(r);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
@Override
|
|
196 |
public String getName() {
|
|
197 |
return name;
|
|
198 |
}
|
|
199 |
|
|
200 |
@Override
|
|
201 |
public void configure(Map<String, String> config) {
|
|
202 |
String arguments = config.get(name);
|
|
203 |
this.starts = arguments;
|
|
204 |
}
|
|
205 |
}
|
|
206 |
}
|