test/jdk/tools/jlink/plugins/StringSharingPluginTest.java
author mikael
Tue, 02 Oct 2018 22:36:08 -0700
changeset 51994 7577686cc9bd
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211350: Remove jprt support Reviewed-by: dholmes, erikj, mchung, alanb, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
51994
7577686cc9bd 8211350: Remove jprt support
mikael
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
36511
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 StringSharingPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @library ../../lib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @modules java.base/jdk.internal.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *          java.base/jdk.internal.jimage.decompressor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 *          jdk.jlink/jdk.tools.jlink.internal.plugins
43185
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 39834
diff changeset
    33
 *          jdk.jlink/jdk.tools.jlink.plugin
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 *          jdk.jlink/jdk.tools.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 *          jdk.jdeps/com.sun.tools.classfile
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * @run build tests.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * @run main StringSharingPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    42
import java.io.IOException;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    43
import java.io.UncheckedIOException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.function.Consumer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.internal.jimage.decompressor.CompressedResourceHeader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import jdk.internal.jimage.decompressor.StringSharingDecompressor;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    56
import jdk.tools.jlink.internal.ResourcePoolManager;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import jdk.tools.jlink.internal.StringTable;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.tools.jlink.internal.plugins.StringSharingPlugin;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    59
import jdk.tools.jlink.plugin.ResourcePoolEntry;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    60
import jdk.tools.jlink.plugin.ResourcePool;
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39151
diff changeset
    61
import jdk.tools.jlink.plugin.Plugin;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
import tests.Helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
import tests.JImageValidator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
public class StringSharingPluginTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    private static int strID = 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        Helper helper = Helper.newHelper();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        if (helper == null) {
51994
7577686cc9bd 8211350: Remove jprt support
mikael
parents: 47216
diff changeset
    72
            // Skip test if the jmods directory is missing (e.g. exploded image)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            System.err.println("Test not run, NO jmods directory");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        Path compiledClasses = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                helper.getJmodSrcDir(), helper.getJmodClassesDir(), "composite2", classes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        Map<String, Integer> map = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        Map<Integer, String> reversedMap = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    84
        ResourcePoolManager resources = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                Integer id = map.get(str);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                if (id == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                    id = strID;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                    map.put(str, id);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                    reversedMap.put(id, str);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                    strID += 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                return id;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                throw new UnsupportedOperationException("Not supported yet.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        Consumer<Path> c = (p) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            // take only the .class resources.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            if (Files.isRegularFile(p) && p.toString().endsWith(".class")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                    && !p.toString().endsWith("module-info.class")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                    byte[] content = Files.readAllBytes(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                    String path = p.toString().replace('\\', '/');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                    path = path.substring("/modules".length());
39151
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents: 38320
diff changeset
   110
                    if (path.charAt(0) != '/') {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents: 38320
diff changeset
   111
                        path = "/" + path;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents: 38320
diff changeset
   112
                    }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   113
                    ResourcePoolEntry res = ResourcePoolEntry.create(path, content);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                    resources.add(res);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                    throw new RuntimeException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                }
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
        try (java.util.stream.Stream<Path> stream = Files.walk(compiledClasses)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            stream.forEach(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        }
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39151
diff changeset
   123
        Plugin plugin = new StringSharingPlugin();
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   124
        ResourcePoolManager resultMgr = new ResourcePoolManager(resources.byteOrder(), resources.getStringTable());
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   125
        ResourcePool result = plugin.transform(resources.resourcePool(), resultMgr.resourcePoolBuilder());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        if (result.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            throw new AssertionError("No result");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   131
        result.entries().forEach(res -> {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   132
            if (res.path().endsWith(".class")) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   133
                try {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   134
                    byte[] uncompacted = StringSharingDecompressor.normalize(reversedMap::get, res.contentBytes(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                        CompressedResourceHeader.getSize());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   136
                    JImageValidator.readClass(uncompacted);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   137
                } catch (IOException exp) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   138
                    throw new UncheckedIOException(exp);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   139
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   141
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
}