author | jlaskey |
Thu, 16 Jun 2016 09:09:53 -0300 | |
changeset 39042 | 52db877f18db |
parent 38320 | e24c7029e8ba |
child 39321 | c60f34e8c057 |
permissions | -rw-r--r-- |
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 sorter plugin |
|
27 |
* @author Jean-Francois Denise |
|
28 |
* @modules jdk.jlink/jdk.tools.jlink.internal |
|
29 |
* jdk.jlink/jdk.tools.jlink.internal.plugins |
|
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
30 |
* @run main OrderResourcesPluginTest |
36511 | 31 |
*/ |
32 |
||
33 |
import java.io.File; |
|
34 |
import java.nio.file.Files; |
|
35 |
import java.util.Arrays; |
|
36 |
import java.util.Collection; |
|
37 |
import java.util.HashMap; |
|
38 |
import java.util.Map; |
|
38320 | 39 |
import java.util.stream.Collectors; |
36511 | 40 |
|
38320 | 41 |
import jdk.tools.jlink.internal.ModulePoolImpl; |
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
42 |
import jdk.tools.jlink.internal.plugins.OrderResourcesPlugin; |
38320 | 43 |
import jdk.tools.jlink.plugin.ModuleEntry; |
44 |
import jdk.tools.jlink.plugin.ModulePool; |
|
36511 | 45 |
import jdk.tools.jlink.plugin.TransformerPlugin; |
46 |
||
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
47 |
public class OrderResourcesPluginTest { |
36511 | 48 |
|
49 |
public static void main(String[] args) throws Exception { |
|
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
50 |
new OrderResourcesPluginTest().test(); |
36511 | 51 |
} |
52 |
||
53 |
public void test() throws Exception { |
|
38320 | 54 |
ModuleEntry[] array = { |
55 |
ModuleEntry.create("/module1/toto1.class", new byte[0]), |
|
56 |
ModuleEntry.create("/module2/toto2.class", new byte[0]), |
|
57 |
ModuleEntry.create("/module3/toto3.class", new byte[0]), |
|
58 |
ModuleEntry.create("/module3/toto3/module-info.class", new byte[0]), |
|
59 |
ModuleEntry.create("/zazou/toto.class", new byte[0]), |
|
60 |
ModuleEntry.create("/module4/zazou.class", new byte[0]), |
|
61 |
ModuleEntry.create("/module5/toto5.class", new byte[0]), |
|
62 |
ModuleEntry.create("/module6/toto6/module-info.class", new byte[0]) |
|
36511 | 63 |
}; |
64 |
||
38320 | 65 |
ModuleEntry[] sorted = { |
66 |
ModuleEntry.create("/zazou/toto.class", new byte[0]), |
|
67 |
ModuleEntry.create("/module3/toto3/module-info.class", new byte[0]), |
|
68 |
ModuleEntry.create("/module6/toto6/module-info.class", new byte[0]), |
|
69 |
ModuleEntry.create("/module1/toto1.class", new byte[0]), |
|
70 |
ModuleEntry.create("/module2/toto2.class", new byte[0]), |
|
71 |
ModuleEntry.create("/module3/toto3.class", new byte[0]), |
|
72 |
ModuleEntry.create("/module4/zazou.class", new byte[0]), |
|
73 |
ModuleEntry.create("/module5/toto5.class", new byte[0]) |
|
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
74 |
}; |
36511 | 75 |
|
38320 | 76 |
ModuleEntry[] sorted2 = { |
77 |
ModuleEntry.create("/module5/toto5.class", new byte[0]), |
|
78 |
ModuleEntry.create("/module6/toto6/module-info.class", new byte[0]), |
|
79 |
ModuleEntry.create("/module4/zazou.class", new byte[0]), |
|
80 |
ModuleEntry.create("/module3/toto3.class", new byte[0]), |
|
81 |
ModuleEntry.create("/module3/toto3/module-info.class", new byte[0]), |
|
82 |
ModuleEntry.create("/module1/toto1.class", new byte[0]), |
|
83 |
ModuleEntry.create("/module2/toto2.class", new byte[0]), |
|
84 |
ModuleEntry.create("/zazou/toto.class", new byte[0]) |
|
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
85 |
}; |
36511 | 86 |
|
38320 | 87 |
ModulePool resources = new ModulePoolImpl(); |
88 |
for (ModuleEntry r : array) { |
|
36511 | 89 |
resources.add(r); |
90 |
} |
|
91 |
||
92 |
{ |
|
38320 | 93 |
ModulePool out = new ModulePoolImpl(); |
36511 | 94 |
Map<String, String> config = new HashMap<>(); |
39042 | 95 |
config.put(OrderResourcesPlugin.NAME, "/zazou/**,**/module-info.class"); |
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
96 |
TransformerPlugin p = new OrderResourcesPlugin(); |
36511 | 97 |
p.configure(config); |
98 |
p.visit(resources, out); |
|
38320 | 99 |
check(out.entries().collect(Collectors.toList()), sorted); |
36511 | 100 |
} |
101 |
||
102 |
{ |
|
103 |
// Order of resources in the file, then un-ordered resources. |
|
104 |
File order = new File("resources.order"); |
|
105 |
order.createNewFile(); |
|
106 |
StringBuilder builder = new StringBuilder(); |
|
107 |
// 5 first resources come from file |
|
108 |
for (int i = 0; i < 5; i++) { |
|
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
109 |
String path = sorted2[i].getPath(); |
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
110 |
int index = path.indexOf('/', 1); |
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
111 |
path = path.substring(index + 1, path.length() - ".class".length()); |
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
112 |
builder.append(path).append("\n"); |
36511 | 113 |
} |
114 |
Files.write(order.toPath(), builder.toString().getBytes()); |
|
115 |
||
38320 | 116 |
ModulePool out = new ModulePoolImpl(); |
36511 | 117 |
Map<String, String> config = new HashMap<>(); |
37893
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
118 |
config.put(OrderResourcesPlugin.NAME, "@" + order.getAbsolutePath()); |
f1cc7d17e66b
8156781: change to jlink has result in test failure
jlaskey
parents:
36511
diff
changeset
|
119 |
TransformerPlugin p = new OrderResourcesPlugin(); |
36511 | 120 |
p.configure(config); |
121 |
p.visit(resources, out); |
|
38320 | 122 |
check(out.entries().collect(Collectors.toList()), sorted2); |
36511 | 123 |
|
124 |
} |
|
125 |
} |
|
126 |
||
38320 | 127 |
private void check(Collection<ModuleEntry> outResources, |
128 |
ModuleEntry[] sorted) { |
|
36511 | 129 |
if (outResources.size() != sorted.length) { |
130 |
throw new AssertionError("Wrong number of resources:\n" |
|
131 |
+ "expected: " + Arrays.toString(sorted) + ",\n" |
|
132 |
+ " got: " + outResources); |
|
133 |
} |
|
134 |
int i = 0; |
|
38320 | 135 |
for (ModuleEntry r : outResources) { |
36511 | 136 |
System.err.println("Resource: " + r); |
137 |
if (!sorted[i].getPath().equals(r.getPath())) { |
|
138 |
throw new AssertionError("Resource not properly sorted, difference at: " + i + "\n" |
|
139 |
+ "expected: " + Arrays.toString(sorted) + ",\n" |
|
140 |
+ " got: " + outResources); |
|
141 |
} |
|
142 |
i++; |
|
143 |
} |
|
144 |
} |
|
145 |
} |