test/jdk/tools/jlink/ResourcePoolTest.java
author ihse
Tue, 22 Oct 2019 09:51:52 +0200
branchihse-cflags-rewrite-branch
changeset 58736 e878a0b7cff0
parent 47216 71c04702a3d5
permissions -rw-r--r--
Add doubunder on solaris in two places.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Test a pool containing jimage resources and classes.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @modules jdk.jlink/jdk.tools.jlink.internal
43185
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 39834
diff changeset
    29
 *          jdk.jlink/jdk.tools.jlink.plugin
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @run build ResourcePoolTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @run main ResourcePoolTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.ByteArrayInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Collection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.List;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    41
import java.util.Optional;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Set;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    43
import java.util.function.Function;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    44
import jdk.tools.jlink.internal.ResourcePoolManager;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    45
import jdk.tools.jlink.plugin.ResourcePool;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    46
import jdk.tools.jlink.plugin.ResourcePoolModule;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    47
import jdk.tools.jlink.plugin.ResourcePool;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    48
import jdk.tools.jlink.plugin.ResourcePoolEntry;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
public class ResourcePoolTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        new ResourcePoolTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        checkResourceAdding();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        checkResourceVisitor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        checkResourcesAfterCompression();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    private static final String SUFFIX = "END";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    private void checkResourceVisitor() throws Exception {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    65
        ResourcePoolManager input = new ResourcePoolManager();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        for (int i = 0; i < 1000; ++i) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            String module = "/module" + (i / 10);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            String resourcePath = module + "/java/package" + i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            byte[] bytes = resourcePath.getBytes();
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    70
            input.add(ResourcePoolEntry.create(resourcePath, bytes));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    72
        ResourcePoolManager output = new ResourcePoolManager();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        ResourceVisitor visitor = new ResourceVisitor();
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    74
        input.resourcePool().transformAndCopy(visitor, output.resourcePoolBuilder());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        if (visitor.getAmountBefore() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            throw new AssertionError("Resources not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    78
        if (visitor.getAmountBefore() != input.entryCount()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            throw new AssertionError("Number of visited resources. Expected: " +
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    80
                    visitor.getAmountBefore() + ", got: " + input.entryCount());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    82
        if (visitor.getAmountAfter() != output.entryCount()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            throw new AssertionError("Number of added resources. Expected: " +
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    84
                    visitor.getAmountAfter() + ", got: " + output.entryCount());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    86
        output.entries().forEach(outResource -> {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    87
            String path = outResource.path().replaceAll(SUFFIX + "$", "");
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    88
            if (!input.findEntry(path).isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                throw new AssertionError("Unknown resource: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    91
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
    94
    private static class ResourceVisitor implements Function<ResourcePoolEntry, ResourcePoolEntry> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        private int amountBefore;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        private int amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   100
        public ResourcePoolEntry apply(ResourcePoolEntry resource) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            int index = ++amountBefore % 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            switch (index) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
                case 0:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                    ++amountAfter;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   105
                    return ResourcePoolEntry.create(resource.path() + SUFFIX,
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   106
                            resource.type(), resource.contentBytes());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                case 1:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                    ++amountAfter;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   109
                    return resource.copyWithContent(resource.contentBytes());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        public int getAmountAfter() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            return amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        public int getAmountBefore() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            return amountBefore;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    private void checkResourceAdding() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        List<String> samples = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        samples.add("java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        samples.add("java/lang/Object");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        samples.add("java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        samples.add("java/lang/String");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        samples.add("java.management");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        samples.add("javax/management/ObjectName");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        test(samples, (resources, module, path) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            try {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   133
                resources.add(ResourcePoolEntry.create(path, new byte[0]));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                throw new RuntimeException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        test(samples, (resources, module, path) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            try {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   140
                resources.add(ResourcePoolManager.
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   141
                        newCompressedResource(ResourcePoolEntry.create(path, new byte[0]),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                                ByteBuffer.allocate(99), "bitcruncher", null,
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   143
                                ((ResourcePoolManager)resources).getStringTable(), ByteOrder.nativeOrder()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                throw new RuntimeException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
    private void test(List<String> samples, ResourceAdder adder) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        if (samples.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            throw new AssertionError("No sample to test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   154
        ResourcePoolManager resources = new ResourcePoolManager();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        Set<String> modules = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        for (int i = 0; i < samples.size(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            String module = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            modules.add(module);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
            String clazz = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            String path = "/" + module + "/" + clazz + ".class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            adder.add(resources, module, path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        for (int i = 0; i < samples.size(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            String module = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
            i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
            String clazz = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            String path = "/" + module + "/" + clazz + ".class";
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   169
            Optional<ResourcePoolEntry> res = resources.findEntry(path);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   170
            if (!res.isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                throw new AssertionError("Resource not found " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   173
            checkModule(resources.resourcePool(), res.get());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   174
            if (resources.findEntry(clazz).isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                throw new AssertionError("Resource found " + clazz);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   178
        if (resources.entryCount() != samples.size() / 2) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            throw new AssertionError("Invalid number of resources");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   183
    private void checkModule(ResourcePool resources, ResourcePoolEntry res) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   184
        Optional<ResourcePoolModule> optMod = resources.moduleView().findModule(res.moduleName());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   185
        if (!optMod.isPresent()) {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   186
            throw new AssertionError("No module " + res.moduleName());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   188
        ResourcePoolModule m = optMod.get();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   189
        if (!m.name().equals(res.moduleName())) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   190
            throw new AssertionError("Not right module name " + res.moduleName());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   192
        if (!m.findEntry(res.path()).isPresent()) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   193
            throw new AssertionError("resource " + res.path()
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   194
                    + " not in module " + m.name());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    private void checkResourcesAfterCompression() throws Exception {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   199
        ResourcePoolManager resources1 = new ResourcePoolManager();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   200
        ResourcePoolEntry res1 = ResourcePoolEntry.create("/module1/toto1", new byte[0]);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   201
        ResourcePoolEntry res2 = ResourcePoolEntry.create("/module2/toto1", new byte[0]);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        resources1.add(res1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        resources1.add(res2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
        checkResources(resources1, res1, res2);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   206
        ResourcePoolManager resources2 = new ResourcePoolManager();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   207
        ResourcePoolEntry res3 = ResourcePoolEntry.create("/module2/toto1", new byte[7]);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        resources2.add(res3);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   209
        resources2.add(ResourcePoolManager.newCompressedResource(res1,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                ByteBuffer.allocate(7), "zip", null, resources1.getStringTable(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                ByteOrder.nativeOrder()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        checkResources(resources2, res1, res2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   215
    private void checkResources(ResourcePoolManager resources, ResourcePoolEntry... expected) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        List<String> modules = new ArrayList();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   217
        resources.modules().forEach(m -> {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   218
            modules.add(m.name());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   219
        });
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   220
        for (ResourcePoolEntry res : expected) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
            if (!resources.contains(res)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                throw new AssertionError("Resource not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   225
            if (!resources.findEntry(res.path()).isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                throw new AssertionError("Resource not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   229
            if (!modules.contains(res.moduleName())) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   230
                throw new AssertionError("Module not found: " + res.moduleName());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   233
            if (!resources.contains(res)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                throw new AssertionError("Resources not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                resources.add(res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
                throw new AssertionError(res + " already present, but an exception is not thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                // Expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        try {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   246
            resources.add(ResourcePoolEntry.create("/module2/toto1", new byte[0]));
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   247
            throw new AssertionError("ResourcePool is read-only, but an exception is not thrown");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            // Expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
    interface ResourceAdder {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39151
diff changeset
   254
        void add(ResourcePoolManager resources, String module, String path);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
}