File: ResourcePoolTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (256 lines) | stat: -rw-r--r-- 10,054 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @summary Test a pool containing jimage resources and classes.
 * @author Jean-Francois Denise
 * @modules jdk.jlink/jdk.tools.jlink.internal
 *          jdk.jlink/jdk.tools.jlink.plugin
 * @run build ResourcePoolTest
 * @run main ResourcePoolTest
 */

import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import jdk.tools.jlink.internal.ResourcePoolManager;
import jdk.tools.jlink.plugin.ResourcePool;
import jdk.tools.jlink.plugin.ResourcePoolModule;
import jdk.tools.jlink.plugin.ResourcePool;
import jdk.tools.jlink.plugin.ResourcePoolEntry;

public class ResourcePoolTest {

    public static void main(String[] args) throws Exception {
        new ResourcePoolTest().test();
    }

    public void test() throws Exception {
        checkResourceAdding();
        checkResourceVisitor();
        checkResourcesAfterCompression();
    }

    private static final String SUFFIX = "END";

    private void checkResourceVisitor() throws Exception {
        ResourcePoolManager input = new ResourcePoolManager();
        for (int i = 0; i < 1000; ++i) {
            String module = "/module" + (i / 10);
            String resourcePath = module + "/java/package" + i;
            byte[] bytes = resourcePath.getBytes();
            input.add(ResourcePoolEntry.create(resourcePath, bytes));
        }
        ResourcePoolManager output = new ResourcePoolManager();
        ResourceVisitor visitor = new ResourceVisitor();
        input.resourcePool().transformAndCopy(visitor, output.resourcePoolBuilder());
        if (visitor.getAmountBefore() == 0) {
            throw new AssertionError("Resources not found");
        }
        if (visitor.getAmountBefore() != input.entryCount()) {
            throw new AssertionError("Number of visited resources. Expected: " +
                    visitor.getAmountBefore() + ", got: " + input.entryCount());
        }
        if (visitor.getAmountAfter() != output.entryCount()) {
            throw new AssertionError("Number of added resources. Expected: " +
                    visitor.getAmountAfter() + ", got: " + output.entryCount());
        }
        output.entries().forEach(outResource -> {
            String path = outResource.path().replaceAll(SUFFIX + "$", "");
            if (!input.findEntry(path).isPresent()) {
                throw new AssertionError("Unknown resource: " + path);
            }
        });
    }

    private static class ResourceVisitor implements Function<ResourcePoolEntry, ResourcePoolEntry> {

        private int amountBefore;
        private int amountAfter;

        @Override
        public ResourcePoolEntry apply(ResourcePoolEntry resource) {
            int index = ++amountBefore % 3;
            switch (index) {
                case 0:
                    ++amountAfter;
                    return ResourcePoolEntry.create(resource.path() + SUFFIX,
                            resource.type(), resource.contentBytes());
                case 1:
                    ++amountAfter;
                    return resource.copyWithContent(resource.contentBytes());
            }
            return null;
        }

        public int getAmountAfter() {
            return amountAfter;
        }

        public int getAmountBefore() {
            return amountBefore;
        }
    }

    private void checkResourceAdding() {
        List<String> samples = new ArrayList<>();
        samples.add("java.base");
        samples.add("java/lang/Object");
        samples.add("java.base");
        samples.add("java/lang/String");
        samples.add("java.management");
        samples.add("javax/management/ObjectName");
        test(samples, (resources, module, path) -> {
            try {
                resources.add(ResourcePoolEntry.create(path, new byte[0]));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        test(samples, (resources, module, path) -> {
            try {
                resources.add(ResourcePoolManager.
                        newCompressedResource(ResourcePoolEntry.create(path, new byte[0]),
                                ByteBuffer.allocate(99), "bitcruncher", null,
                                ((ResourcePoolManager)resources).getStringTable(), ByteOrder.nativeOrder()));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
    }

    private void test(List<String> samples, ResourceAdder adder) {
        if (samples.isEmpty()) {
            throw new AssertionError("No sample to test");
        }
        ResourcePoolManager resources = new ResourcePoolManager();
        Set<String> modules = new HashSet<>();
        for (int i = 0; i < samples.size(); i++) {
            String module = samples.get(i);
            modules.add(module);
            i++;
            String clazz = samples.get(i);
            String path = "/" + module + "/" + clazz + ".class";
            adder.add(resources, module, path);
        }
        for (int i = 0; i < samples.size(); i++) {
            String module = samples.get(i);
            i++;
            String clazz = samples.get(i);
            String path = "/" + module + "/" + clazz + ".class";
            Optional<ResourcePoolEntry> res = resources.findEntry(path);
            if (!res.isPresent()) {
                throw new AssertionError("Resource not found " + path);
            }
            checkModule(resources.resourcePool(), res.get());
            if (resources.findEntry(clazz).isPresent()) {
                throw new AssertionError("Resource found " + clazz);
            }
        }
        if (resources.entryCount() != samples.size() / 2) {
            throw new AssertionError("Invalid number of resources");
        }
    }

    private void checkModule(ResourcePool resources, ResourcePoolEntry res) {
        Optional<ResourcePoolModule> optMod = resources.moduleView().findModule(res.moduleName());
        if (!optMod.isPresent()) {
            throw new AssertionError("No module " + res.moduleName());
        }
        ResourcePoolModule m = optMod.get();
        if (!m.name().equals(res.moduleName())) {
            throw new AssertionError("Not right module name " + res.moduleName());
        }
        if (!m.findEntry(res.path()).isPresent()) {
            throw new AssertionError("resource " + res.path()
                    + " not in module " + m.name());
        }
    }

    private void checkResourcesAfterCompression() throws Exception {
        ResourcePoolManager resources1 = new ResourcePoolManager();
        ResourcePoolEntry res1 = ResourcePoolEntry.create("/module1/toto1", new byte[0]);
        ResourcePoolEntry res2 = ResourcePoolEntry.create("/module2/toto1", new byte[0]);
        resources1.add(res1);
        resources1.add(res2);

        checkResources(resources1, res1, res2);
        ResourcePoolManager resources2 = new ResourcePoolManager();
        ResourcePoolEntry res3 = ResourcePoolEntry.create("/module2/toto1", new byte[7]);
        resources2.add(res3);
        resources2.add(ResourcePoolManager.newCompressedResource(res1,
                ByteBuffer.allocate(7), "zip", null, resources1.getStringTable(),
                ByteOrder.nativeOrder()));
        checkResources(resources2, res1, res2);
    }

    private void checkResources(ResourcePoolManager resources, ResourcePoolEntry... expected) {
        List<String> modules = new ArrayList();
        resources.modules().forEach(m -> {
            modules.add(m.name());
        });
        for (ResourcePoolEntry res : expected) {
            if (!resources.contains(res)) {
                throw new AssertionError("Resource not found: " + res);
            }

            if (!resources.findEntry(res.path()).isPresent()) {
                throw new AssertionError("Resource not found: " + res);
            }

            if (!modules.contains(res.moduleName())) {
                throw new AssertionError("Module not found: " + res.moduleName());
            }

            if (!resources.contains(res)) {
                throw new AssertionError("Resources not found: " + res);
            }

            try {
                resources.add(res);
                throw new AssertionError(res + " already present, but an exception is not thrown");
            } catch (Exception ex) {
                // Expected
            }
        }

        try {
            resources.add(ResourcePoolEntry.create("/module2/toto1", new byte[0]));
            throw new AssertionError("ResourcePool is read-only, but an exception is not thrown");
        } catch (Exception ex) {
            // Expected
        }
    }

    interface ResourceAdder {
        void add(ResourcePoolManager resources, String module, String path);
    }
}