|
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 previsitor |
|
27 * @author Andrei Eremeev |
|
28 * @modules jdk.jlink/jdk.tools.jlink.internal |
|
29 * @run main/othervm PrevisitorTest |
|
30 */ |
|
31 import java.nio.ByteOrder; |
|
32 import java.util.ArrayList; |
|
33 import java.util.Collection; |
|
34 import java.util.Collections; |
|
35 import java.util.HashMap; |
|
36 import java.util.HashSet; |
|
37 import java.util.List; |
|
38 import java.util.Map; |
|
39 import java.util.Set; |
|
40 import java.util.stream.Collectors; |
|
41 |
|
42 import jdk.tools.jlink.internal.ImagePluginConfiguration; |
|
43 import jdk.tools.jlink.internal.PluginRepository; |
|
44 import jdk.tools.jlink.internal.ImagePluginStack; |
|
45 import jdk.tools.jlink.internal.PoolImpl; |
|
46 import jdk.tools.jlink.internal.ResourcePrevisitor; |
|
47 import jdk.tools.jlink.internal.StringTable; |
|
48 import jdk.tools.jlink.Jlink; |
|
49 import jdk.tools.jlink.plugin.Plugin; |
|
50 import jdk.tools.jlink.plugin.Pool; |
|
51 import jdk.tools.jlink.plugin.Pool.ModuleData; |
|
52 import jdk.tools.jlink.plugin.TransformerPlugin; |
|
53 |
|
54 public class PrevisitorTest { |
|
55 |
|
56 public static void main(String[] args) throws Exception { |
|
57 new PrevisitorTest().test(); |
|
58 } |
|
59 |
|
60 private static Plugin createPlugin(String name) { |
|
61 return Jlink.newPlugin(name, Collections.emptyMap(), null); |
|
62 } |
|
63 |
|
64 public void test() throws Exception { |
|
65 CustomPlugin plugin = new CustomPlugin(); |
|
66 PluginRepository.registerPlugin(plugin); |
|
67 List<Plugin> plugins = new ArrayList<>(); |
|
68 plugins.add(createPlugin(CustomPlugin.NAME)); |
|
69 ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new Jlink.PluginsConfiguration(plugins, |
|
70 null, null)); |
|
71 PoolImpl inResources = new PoolImpl(ByteOrder.nativeOrder(), new CustomStringTable()); |
|
72 inResources.add(Pool.newResource("/aaa/bbb/res1.class", new byte[90])); |
|
73 inResources.add(Pool.newResource("/aaa/bbb/res2.class", new byte[90])); |
|
74 inResources.add(Pool.newResource("/aaa/bbb/res3.class", new byte[90])); |
|
75 inResources.add(Pool.newResource("/aaa/ddd/res1.class", new byte[90])); |
|
76 inResources.add(Pool.newResource("/aaa/res1.class", new byte[90])); |
|
77 Pool outResources = stack.visitResources(inResources); |
|
78 Collection<String> input = inResources.getContent().stream() |
|
79 .map(Object::toString) |
|
80 .collect(Collectors.toList()); |
|
81 Collection<String> output = outResources.getContent().stream() |
|
82 .map(Object::toString) |
|
83 .collect(Collectors.toList()); |
|
84 if (!input.equals(output)) { |
|
85 throw new AssertionError("Input and output resources differ: input: " |
|
86 + input + ", output: " + output); |
|
87 } |
|
88 } |
|
89 |
|
90 private static class CustomStringTable implements StringTable { |
|
91 |
|
92 private final List<String> strings = new ArrayList<>(); |
|
93 |
|
94 @Override |
|
95 public int addString(String str) { |
|
96 strings.add(str); |
|
97 return strings.size() - 1; |
|
98 } |
|
99 |
|
100 @Override |
|
101 public String getString(int id) { |
|
102 return strings.get(id); |
|
103 } |
|
104 |
|
105 public int size() { |
|
106 return strings.size(); |
|
107 } |
|
108 } |
|
109 |
|
110 private static class CustomPlugin implements TransformerPlugin, ResourcePrevisitor { |
|
111 |
|
112 private static String NAME = "plugin"; |
|
113 |
|
114 private boolean isPrevisitCalled = false; |
|
115 |
|
116 @Override |
|
117 public void visit(Pool inResources, Pool outResources) { |
|
118 if (!isPrevisitCalled) { |
|
119 throw new AssertionError("Previsit was not called"); |
|
120 } |
|
121 CustomStringTable table = (CustomStringTable) |
|
122 ((PoolImpl) inResources).getStringTable(); |
|
123 if (table.size() == 0) { |
|
124 throw new AssertionError("Table is empty"); |
|
125 } |
|
126 Map<String, Integer> count = new HashMap<>(); |
|
127 for (int i = 0; i < table.size(); ++i) { |
|
128 String s = table.getString(i); |
|
129 if (inResources.get(s) != null) { |
|
130 throw new AssertionError(); |
|
131 } |
|
132 count.compute(s, (k, c) -> 1 + (c == null ? 0 : c)); |
|
133 } |
|
134 count.forEach((k, v) -> { |
|
135 if (v != 1) { |
|
136 throw new AssertionError("Expected one entry in the table, got: " + v + " for " + k); |
|
137 } |
|
138 }); |
|
139 for (ModuleData r : inResources.getContent()) { |
|
140 outResources.add(r); |
|
141 } |
|
142 } |
|
143 |
|
144 @Override |
|
145 public String getName() { |
|
146 return NAME; |
|
147 } |
|
148 |
|
149 @Override |
|
150 public void previsit(Pool resources, StringTable strings) { |
|
151 isPrevisitCalled = true; |
|
152 for (ModuleData r : resources.getContent()) { |
|
153 String s = r.getPath(); |
|
154 int lastIndexOf = s.lastIndexOf('/'); |
|
155 if (lastIndexOf >= 0) { |
|
156 strings.addString(s.substring(0, lastIndexOf)); |
|
157 } |
|
158 } |
|
159 } |
|
160 |
|
161 @Override |
|
162 public Set<PluginType> getType() { |
|
163 Set<PluginType> set = new HashSet<>(); |
|
164 set.add(CATEGORY.TRANSFORMER); |
|
165 return Collections.unmodifiableSet(set); |
|
166 } |
|
167 } |
|
168 } |