jdk/test/tools/jlink/ResourcePoolTest.java
author sundar
Tue, 21 Jun 2016 19:05:34 +0530
changeset 39129 4b2086305b68
parent 38320 e24c7029e8ba
child 39151 34455cc82f5e
permissions -rw-r--r--
8159593: Plugin Set<Category> getType() should return a Category Reviewed-by: jlaskey
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @run build ResourcePoolTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @run main ResourcePoolTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.ByteArrayInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Collection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.List;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    40
import java.util.Optional;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Set;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    42
import java.util.function.Function;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    43
import jdk.tools.jlink.internal.ModulePoolImpl;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    44
import jdk.tools.jlink.plugin.ModulePool;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    45
import jdk.tools.jlink.plugin.LinkModule;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    46
import jdk.tools.jlink.plugin.ModuleEntry;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    47
import jdk.tools.jlink.plugin.ModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
public class ResourcePoolTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        new ResourcePoolTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        checkResourceAdding();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        checkResourceVisitor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        checkResourcesAfterCompression();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    private static final String SUFFIX = "END";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    private void checkResourceVisitor() throws Exception {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    64
        ModulePool input = new ModulePoolImpl();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        for (int i = 0; i < 1000; ++i) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            String module = "/module" + (i / 10);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            String resourcePath = module + "/java/package" + i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            byte[] bytes = resourcePath.getBytes();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    69
            input.add(ModuleEntry.create(module, resourcePath,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    70
                    ModuleEntry.Type.CLASS_OR_RESOURCE,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                    new ByteArrayInputStream(bytes), bytes.length));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    73
        ModulePool output = new ModulePoolImpl();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        ResourceVisitor visitor = new ResourceVisitor();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    75
        input.transformAndCopy(visitor, output);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        if (visitor.getAmountBefore() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            throw new AssertionError("Resources not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    79
        if (visitor.getAmountBefore() != input.getEntryCount()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            throw new AssertionError("Number of visited resources. Expected: " +
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    81
                    visitor.getAmountBefore() + ", got: " + input.getEntryCount());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    83
        if (visitor.getAmountAfter() != output.getEntryCount()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            throw new AssertionError("Number of added resources. Expected: " +
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    85
                    visitor.getAmountAfter() + ", got: " + output.getEntryCount());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    87
        output.entries().forEach(outResource -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            String path = outResource.getPath().replaceAll(SUFFIX + "$", "");
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    89
            if (!input.findEntry(path).isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                throw new AssertionError("Unknown resource: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    92
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    95
    private static class ResourceVisitor implements Function<ModuleEntry, ModuleEntry> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        private int amountBefore;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        private int amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        @Override
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   101
        public ModuleEntry apply(ModuleEntry resource) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            int index = ++amountBefore % 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            switch (index) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                case 0:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                    ++amountAfter;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   106
                    return ModuleEntry.create(resource.getModule(), resource.getPath() + SUFFIX,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                            resource.getType(), resource.stream(), resource.getLength());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                case 1:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                    ++amountAfter;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   110
                    return ModuleEntry.create(resource.getModule(), resource.getPath(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                            resource.getType(), resource.stream(), resource.getLength());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        public int getAmountAfter() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            return amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        public int getAmountBefore() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            return amountBefore;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    private void checkResourceAdding() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        List<String> samples = new ArrayList<>();
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/Object");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        samples.add("java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        samples.add("java/lang/String");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        samples.add("java.management");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        samples.add("javax/management/ObjectName");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        test(samples, (resources, module, path) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            try {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   135
                resources.add(ModuleEntry.create(module, path,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   136
                        ModuleEntry.Type.CLASS_OR_RESOURCE,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                        new ByteArrayInputStream(new byte[0]), 0));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                throw new RuntimeException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        test(samples, (resources, module, path) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            try {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   144
                resources.add(ModulePoolImpl.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   145
                        newCompressedResource(ModuleEntry.create(module, path,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   146
                                ModuleEntry.Type.CLASS_OR_RESOURCE,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                                new ByteArrayInputStream(new byte[0]), 0),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                                ByteBuffer.allocate(99), "bitcruncher", null,
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   149
                                ((ModulePoolImpl)resources).getStringTable(), ByteOrder.nativeOrder()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
                throw new RuntimeException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    private void test(List<String> samples, ResourceAdder adder) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        if (samples.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            throw new AssertionError("No sample to test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   160
        ModulePool resources = new ModulePoolImpl();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        Set<String> modules = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        for (int i = 0; i < samples.size(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
            String module = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            modules.add(module);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
            String clazz = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
            String path = "/" + module + "/" + clazz + ".class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            adder.add(resources, module, path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        for (int i = 0; i < samples.size(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
            String module = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            String clazz = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            String path = "/" + module + "/" + clazz + ".class";
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   175
            Optional<ModuleEntry> res = resources.findEntry(path);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   176
            if (!res.isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                throw new AssertionError("Resource not found " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   179
            checkModule(resources, res.get());
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   180
            if (resources.findEntry(clazz).isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                throw new AssertionError("Resource found " + clazz);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   184
        if (resources.getEntryCount() != samples.size() / 2) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
            throw new AssertionError("Invalid number of resources");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   189
    private void checkModule(ModulePool resources, ModuleEntry res) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   190
        Optional<LinkModule> optMod = resources.findModule(res.getModule());
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   191
        if (!optMod.isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
            throw new AssertionError("No module " + res.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   194
        LinkModule m = optMod.get();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        if (!m.getName().equals(res.getModule())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
            throw new AssertionError("Not right module name " + res.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   198
        if (!m.findEntry(res.getPath()).isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
            throw new AssertionError("resource " + res.getPath()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                    + " not in module " + m.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
    private void checkResourcesAfterCompression() throws Exception {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   205
        ModulePoolImpl resources1 = new ModulePoolImpl();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   206
        ModuleEntry res1 = ModuleEntry.create("module1", "/module1/toto1",
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   207
                ModuleEntry.Type.CLASS_OR_RESOURCE,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                new ByteArrayInputStream(new byte[0]), 0);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   209
        ModuleEntry res2 = ModuleEntry.create("module2", "/module2/toto1",
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   210
                ModuleEntry.Type.CLASS_OR_RESOURCE,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                new ByteArrayInputStream(new byte[0]), 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        resources1.add(res1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        resources1.add(res2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        checkResources(resources1, res1, res2);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   216
        ModulePool resources2 = new ModulePoolImpl();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   217
        ModuleEntry res3 = ModuleEntry.create("module2", "/module2/toto1",
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   218
                ModuleEntry.Type.CLASS_OR_RESOURCE,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                new ByteArrayInputStream(new byte[7]), 7);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        resources2.add(res3);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   221
        resources2.add(ModulePoolImpl.newCompressedResource(res1,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                ByteBuffer.allocate(7), "zip", null, resources1.getStringTable(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
                ByteOrder.nativeOrder()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
        checkResources(resources2, res1, res2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   227
    private void checkResources(ModulePool resources, ModuleEntry... expected) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        List<String> modules = new ArrayList();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   229
        resources.modules().forEach(m -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
            modules.add(m.getName());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   231
        });
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   232
        for (ModuleEntry res : expected) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            if (!resources.contains(res)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                throw new AssertionError("Resource not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   237
            if (!resources.findEntry(res.getPath()).isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                throw new AssertionError("Resource not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            if (!modules.contains(res.getModule())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                throw new AssertionError("Module not found: " + res.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   245
            if (!resources.contains(res)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                throw new AssertionError("Resources not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                resources.add(res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                throw new AssertionError(res + " already present, but an exception is not thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                // Expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
        if (resources.isReadOnly()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
            throw new AssertionError("ReadOnly resources");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   261
        ((ModulePoolImpl) resources).setReadOnly();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        try {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   263
            resources.add(ModuleEntry.create("module2",  "/module2/toto1",
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   264
                    ModuleEntry.Type.CLASS_OR_RESOURCE, new ByteArrayInputStream(new byte[0]), 0));
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   265
            throw new AssertionError("ModulePool is read-only, but an exception is not thrown");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
        } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
            // Expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
    interface ResourceAdder {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   272
        void add(ModulePool resources, String module, String path);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
}