jdk/test/tools/jlink/ResourcePoolTest.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
child 38320 e24c7029e8ba
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
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;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.tools.jlink.internal.PoolImpl;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.tools.jlink.plugin.Pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.tools.jlink.plugin.Pool.Module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.tools.jlink.plugin.Pool.ModuleData;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.tools.jlink.plugin.Pool.ModuleDataType;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import jdk.tools.jlink.plugin.Pool.Visitor;
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 {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        Pool input = new PoolImpl();
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();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            input.add(new ModuleData(module, resourcePath,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                    ModuleDataType.CLASS_OR_RESOURCE,
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
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        Pool output = new PoolImpl();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        ResourceVisitor visitor = new ResourceVisitor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        input.visit(visitor, output);
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
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        if (visitor.getAmountBefore() != input.getContent().size()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            throw new AssertionError("Number of visited resources. Expected: " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                    visitor.getAmountBefore() + ", got: " + input.getContent().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        if (visitor.getAmountAfter() != output.getContent().size()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            throw new AssertionError("Number of added resources. Expected: " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                    visitor.getAmountAfter() + ", got: " + output.getContent().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        for (ModuleData outResource : output.getContent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            String path = outResource.getPath().replaceAll(SUFFIX + "$", "");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            ModuleData inResource = input.get(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            if (inResource == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                throw new AssertionError("Unknown resource: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    private static class ResourceVisitor implements Visitor {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        private int amountBefore;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        private int amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        public ModuleData visit(ModuleData resource) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            int index = ++amountBefore % 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            switch (index) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                case 0:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                    ++amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                    return new ModuleData(resource.getModule(), resource.getPath() + SUFFIX,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                            resource.getType(), resource.stream(), resource.getLength());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                case 1:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                    ++amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                    return new ModuleData(resource.getModule(), resource.getPath(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                            resource.getType(), resource.stream(), resource.getLength());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        public int getAmountAfter() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            return amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        public int getAmountBefore() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            return amountBefore;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    private void checkResourceAdding() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        List<String> samples = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        samples.add("java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        samples.add("java/lang/Object");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        samples.add("java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        samples.add("java/lang/String");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        samples.add("java.management");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        samples.add("javax/management/ObjectName");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        test(samples, (resources, module, path) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                resources.add(new ModuleData(module, path,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                        ModuleDataType.CLASS_OR_RESOURCE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                        new ByteArrayInputStream(new byte[0]), 0));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                throw new RuntimeException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        test(samples, (resources, module, path) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                resources.add(PoolImpl.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                        newCompressedResource(new ModuleData(module, path,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                                ModuleDataType.CLASS_OR_RESOURCE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                                new ByteArrayInputStream(new byte[0]), 0),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                                ByteBuffer.allocate(99), "bitcruncher", null,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                                ((PoolImpl)resources).getStringTable(), ByteOrder.nativeOrder()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                throw new RuntimeException(ex);
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    private void test(List<String> samples, ResourceAdder adder) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        if (samples.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            throw new AssertionError("No sample to test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        Pool resources = new PoolImpl();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        Set<String> modules = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        for (int i = 0; i < samples.size(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            String module = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            modules.add(module);
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";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
            adder.add(resources, module, path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        for (int i = 0; i < samples.size(); i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            String module = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            String clazz = samples.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            String path = "/" + module + "/" + clazz + ".class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            ModuleData res = resources.get(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            checkModule(resources, res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            if (res == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                throw new AssertionError("Resource not found " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
            ModuleData res2 = resources.get(clazz);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            if (res2 != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                throw new AssertionError("Resource found " + clazz);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        if (resources.getContent().size() != samples.size() / 2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
            throw new AssertionError("Invalid number of resources");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
    private void checkModule(Pool resources, ModuleData res) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        Module m = resources.getModule(res.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        if (m == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
            throw new AssertionError("No module " + res.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
        if (!m.getName().equals(res.getModule())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
            throw new AssertionError("Not right module name " + res.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
        if (m.get(res.getPath()) == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            throw new AssertionError("resource " + res.getPath()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                    + " not in module " + m.getName());
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
    private void checkResourcesAfterCompression() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
        PoolImpl resources1 = new PoolImpl();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        ModuleData res1 = new ModuleData("module1", "/module1/toto1",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                ModuleDataType.CLASS_OR_RESOURCE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                new ByteArrayInputStream(new byte[0]), 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        ModuleData res2 = new ModuleData("module2", "/module2/toto1",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                ModuleDataType.CLASS_OR_RESOURCE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                new ByteArrayInputStream(new byte[0]), 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        resources1.add(res1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        resources1.add(res2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        checkResources(resources1, res1, res2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        Pool resources2 = new PoolImpl();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        ModuleData res3 = new ModuleData("module2", "/module2/toto1",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                ModuleDataType.CLASS_OR_RESOURCE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
                new ByteArrayInputStream(new byte[7]), 7);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        resources2.add(res3);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        resources2.add(PoolImpl.newCompressedResource(res1,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
                ByteBuffer.allocate(7), "zip", null, resources1.getStringTable(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
                ByteOrder.nativeOrder()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        checkResources(resources2, res1, res2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
    private void checkResources(Pool resources, ModuleData... expected) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        Collection<Module> ms = resources.getModules();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        List<String> modules = new ArrayList();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
        for(Module m : ms) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
            modules.add(m.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        for (ModuleData res : expected) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            if (!resources.contains(res)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                throw new AssertionError("Resource not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            if (resources.get(res.getPath()) == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                throw new AssertionError("Resource not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            if (!modules.contains(res.getModule())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
                throw new AssertionError("Module not found: " + res.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            if (!resources.getContent().contains(res)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                throw new AssertionError("Resources not found: " + res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
                resources.add(res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                throw new AssertionError(res + " already present, but an exception is not thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
            } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                // Expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
        if (resources.isReadOnly()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            throw new AssertionError("ReadOnly resources");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        ((PoolImpl) resources).setReadOnly();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
            resources.add(new ModuleData("module2",  "/module2/toto1",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                    ModuleDataType.CLASS_OR_RESOURCE, new ByteArrayInputStream(new byte[0]), 0));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
            throw new AssertionError("Pool is read-only, but an exception is not thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            // Expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
    interface ResourceAdder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
        void add(Pool resources, String module, String path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
}