jdk/test/tools/jlink/ImageFilePoolTest.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 external files.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Andrei Eremeev
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 ImageFilePoolTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @run main ImageFilePoolTest
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;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    34
import java.util.Optional;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    35
import java.util.function.Function;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    36
import jdk.tools.jlink.internal.ModuleEntryImpl;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    37
import jdk.tools.jlink.internal.ModulePoolImpl;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    38
import jdk.tools.jlink.plugin.ModuleEntry;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    39
import jdk.tools.jlink.plugin.ModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
public class ImageFilePoolTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        new ImageFilePoolTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        checkNegative();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        checkVisitor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    private static final String SUFFIX = "END";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    private void checkVisitor() throws Exception {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    54
        ModulePool input = new ModulePoolImpl();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        for (int i = 0; i < 1000; ++i) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
            String module = "module" + (i / 100);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            input.add(new InMemoryImageFile(module, "/" + module + "/java/class" + i,
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    58
                    ModuleEntry.Type.CONFIG, "class" + i));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    60
        if (input.getEntryCount() != 1000) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
            throw new AssertionError();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    63
        ModulePool output = new ModulePoolImpl();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        ResourceVisitor visitor = new ResourceVisitor();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    65
        input.transformAndCopy(visitor, output);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        if (visitor.getAmountBefore() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            throw new AssertionError("Resources not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    69
        if (visitor.getAmountBefore() != input.getEntryCount()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            throw new AssertionError("Number of visited resources. Expected: " +
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    71
                    visitor.getAmountBefore() + ", got: " + input.getEntryCount());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    73
        if (visitor.getAmountAfter() != output.getEntryCount()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            throw new AssertionError("Number of added resources. Expected: " +
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    75
                    visitor.getAmountAfter() + ", got: " + output.getEntryCount());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    77
        output.entries().forEach(outFile -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            String path = outFile.getPath().replaceAll(SUFFIX + "$", "");
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    79
            Optional<ModuleEntry> inFile = input.findEntry(path);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    80
            if (!inFile.isPresent()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                throw new AssertionError("Unknown resource: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    83
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    86
    private static class ResourceVisitor implements Function<ModuleEntry, ModuleEntry> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        private int amountBefore;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        private int amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        @Override
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    92
        public ModuleEntry apply(ModuleEntry file) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            int index = ++amountBefore % 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            switch (index) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                case 0:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                    ++amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                    return new InMemoryImageFile(file.getModule(), file.getPath() + SUFFIX,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                            file.getType(), file.getPath());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                case 1:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                    ++amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                    return new InMemoryImageFile(file.getModule(), file.getPath(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                            file.getType(), file.getPath());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        public int getAmountAfter() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            return amountAfter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        public int getAmountBefore() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            return amountBefore;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        }
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
    private void checkNegative() throws Exception {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   117
        ModulePoolImpl input = new ModulePoolImpl();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            input.add(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            throw new AssertionError("NullPointerException is not thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        } catch (NullPointerException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            // expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            input.contains(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            throw new AssertionError("NullPointerException is not thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        } catch (NullPointerException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            // expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   130
        if (input.findEntry("unknown").isPresent()) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   131
            throw new AssertionError("ImageFileModulePool does not return null for unknown file");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   133
        if (input.contains(new InMemoryImageFile("", "unknown", ModuleEntry.Type.CONFIG, "unknown"))) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            throw new AssertionError("'contain' returns true for unknown file");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   136
        input.add(new InMemoryImageFile("", "/aaa/bbb", ModuleEntry.Type.CONFIG, ""));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        try {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   138
            input.add(new InMemoryImageFile("", "/aaa/bbb", ModuleEntry.Type.CONFIG, ""));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            throw new AssertionError("Exception expected");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        } catch (Exception e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            // expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        input.setReadOnly();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        try {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   145
            input.add(new InMemoryImageFile("", "/aaa/ccc", ModuleEntry.Type.CONFIG, ""));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            throw new AssertionError("Exception expected");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        } catch (Exception e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            // expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   152
    private static class InMemoryImageFile extends ModuleEntryImpl {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   153
        public InMemoryImageFile(String module, String path, ModuleEntry.Type type, String content) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            super(module, path, type, new ByteArrayInputStream(content.getBytes()), content.getBytes().length);
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
}