jdk/test/tools/jlink/plugins/CompressorPluginTest.java
author jlaskey
Thu, 16 Jun 2016 09:09:53 -0300
changeset 39042 52db877f18db
parent 38320 e24c7029e8ba
child 39321 c60f34e8c057
permissions -rw-r--r--
8159206: All jlink or jmod tests failing Reviewed-by: alanb, mchung
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 zip compressor
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 java.base/jdk.internal.jimage.decompressor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.internal.plugins
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @run main CompressorPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.FileSystem;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.FileSystemNotFoundException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.FileSystems;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.file.ProviderNotFoundException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Iterator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Properties;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.regex.Pattern;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.internal.jimage.decompressor.CompressedResourceHeader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import jdk.internal.jimage.decompressor.ResourceDecompressor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import jdk.internal.jimage.decompressor.ResourceDecompressorFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.internal.jimage.decompressor.StringSharingDecompressorFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import jdk.internal.jimage.decompressor.ZipDecompressorFactory;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    56
import jdk.tools.jlink.internal.ModulePoolImpl;
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.DefaultCompressPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.tools.jlink.internal.plugins.StringSharingPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import jdk.tools.jlink.internal.plugins.ZipPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import jdk.tools.jlink.plugin.Plugin;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    62
import jdk.tools.jlink.plugin.ModuleEntry;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    63
import jdk.tools.jlink.plugin.ModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
import jdk.tools.jlink.plugin.TransformerPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
public class CompressorPluginTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    private static int strID = 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        new CompressorPluginTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        FileSystem fs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            fs = FileSystems.getFileSystem(URI.create("jrt:/"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        } catch (ProviderNotFoundException | FileSystemNotFoundException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            System.err.println("Not an image build, test skipped.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        Path javabase = fs.getPath("/modules/java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        checkCompress(gatherResources(javabase), new ZipPlugin(), null,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                    new ZipDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    89
        ModulePool classes = gatherClasses(javabase);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        // compress = String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        checkCompress(classes, new StringSharingPlugin(), null,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                    new StringSharingDecompressorFactory()});
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        // compress == ZIP + String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        Properties options = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        options.setProperty(ZipPlugin.NAME, "");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        checkCompress(classes, new DefaultCompressPlugin(), options,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                    new ZipDecompressorFactory(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        // compress == ZIP + String sharing + filter
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        options.setProperty(DefaultCompressPlugin.FILTER,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   106
                "**Exception.class");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        checkCompress(classes, new DefaultCompressPlugin(), options,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                    new ZipDecompressorFactory(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                    new StringSharingDecompressorFactory()
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   111
                }, Collections.singletonList(".*Exception.class"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        // compress level 1 == ZIP
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        Properties options1 = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        options1.setProperty(DefaultCompressPlugin.NAME,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                "1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                options1,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                    new ZipDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        // compress level 1 == ZIP
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        options1.setProperty(DefaultCompressPlugin.FILTER,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   125
                "**Exception.class");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                options1,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                    new ZipDecompressorFactory()
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   130
                }, Collections.singletonList(".*Exception.class"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        // compress level 2 == ZIP + String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        Properties options2 = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        options2.setProperty(DefaultCompressPlugin.NAME,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                "2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                options2,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                    new ZipDecompressorFactory(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                    new StringSharingDecompressorFactory()
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
        // compress level 2 == ZIP + String sharing + filter
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        options2.setProperty(DefaultCompressPlugin.FILTER,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   145
                "**Exception.class");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                options2,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                    new ZipDecompressorFactory(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                    new StringSharingDecompressorFactory()
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   151
                }, Collections.singletonList(".*Exception.class"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        // compress level 0 == String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        Properties options0 = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        options0.setProperty(DefaultCompressPlugin.NAME, "0");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                options0,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        // compress level 0 == String sharing + filter
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        options0.setProperty(DefaultCompressPlugin.FILTER,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   164
                "**Exception.class");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
                options0,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
                    new StringSharingDecompressorFactory()
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   169
                }, Collections.singletonList(".*Exception.class"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   172
    private ModulePool gatherResources(Path module) throws Exception {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   173
        ModulePool pool = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        try (Stream<Path> stream = Files.walk(module)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                Path p = iterator.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                if (Files.isRegularFile(p)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
                    byte[] content = Files.readAllBytes(p);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   190
                    pool.add(ModuleEntry.create(p.toString(), content));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        return pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   197
    private ModulePool gatherClasses(Path module) throws Exception {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   198
        ModulePool pool = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                return -1;
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
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        try (Stream<Path> stream = Files.walk(module)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                Path p = iterator.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                    byte[] content = Files.readAllBytes(p);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   215
                    pool.add(ModuleEntry.create(p.toString(), content));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
        return pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   222
    private void checkCompress(ModulePool resources, Plugin prov,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
            Properties config,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            ResourceDecompressorFactory[] factories) throws Exception {
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   225
        checkCompress(resources, prov, config, factories, Collections.emptyList());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   228
    private void checkCompress(ModulePool resources, Plugin prov,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            Properties config,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
            ResourceDecompressorFactory[] factories,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   231
            List<String> includes) throws Exception {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   232
        long[] original = new long[1];
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   233
        long[] compressed = new long[1];
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   234
        resources.entries().forEach(resource -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            List<Pattern> includesPatterns = includes.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                    .map(Pattern::compile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                    .collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            Map<String, String> props = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            if (config != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                for (String p : config.stringPropertyNames()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                    props.put(p, config.getProperty(p));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            prov.configure(props);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
            final Map<Integer, String> strings = new HashMap<>();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   247
            ModulePoolImpl inputResources = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
                public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                    int id = strID;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                    strID += 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
                    strings.put(id, str);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                    return id;
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
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                    return strings.get(id);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
            inputResources.add(resource);
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   262
            ModulePool compressedResources = applyCompressor(prov, inputResources, resource, includesPatterns);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   263
            original[0] += resource.getLength();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   264
            compressed[0] += compressedResources.findEntry(resource.getPath()).get().getLength();
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   265
            applyDecompressors(factories, inputResources, compressedResources, strings, includesPatterns);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   266
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
        String compressors = Stream.of(factories)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                .map(Object::getClass)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
                .map(Class::getSimpleName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                .collect(Collectors.joining(", "));
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   271
        String size = "Compressed size: " + compressed[0] + ", original size: " + original[0];
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
        System.out.println("Used " + compressors + ". " + size);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   273
        if (original[0] <= compressed[0]) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
            throw new AssertionError("java.base not compressed.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   278
    private ModulePool applyCompressor(Plugin plugin,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   279
            ModulePoolImpl inputResources,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   280
            ModuleEntry res,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   281
            List<Pattern> includesPatterns) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
        TransformerPlugin compressor = (TransformerPlugin) plugin;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   283
        ModulePool compressedModulePool = new ModulePoolImpl(ByteOrder.nativeOrder(), inputResources.getStringTable());
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   284
        compressor.visit(inputResources, compressedModulePool);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
        String path = res.getPath();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   286
        ModuleEntry compressed = compressedModulePool.findEntry(path).get();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
        CompressedResourceHeader header
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                = CompressedResourceHeader.readFromResource(ByteOrder.nativeOrder(), compressed.getBytes());
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   289
        if (isIncluded(includesPatterns, path)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
            if (header == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
                throw new AssertionError("Path should be compressed: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
            if (header.getDecompressorNameOffset() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
                throw new AssertionError("Invalid plugin offset "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                        + header.getDecompressorNameOffset());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            if (header.getResourceSize() <= 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
                throw new AssertionError("Invalid compressed size "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                        + header.getResourceSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        } else if (header != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
            throw new AssertionError("Path should not be compressed: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   304
        return compressedModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
    private void applyDecompressors(ResourceDecompressorFactory[] decompressors,
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   308
            ModulePool inputResources,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   309
            ModulePool compressedResources,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
            Map<Integer, String> strings,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   311
            List<Pattern> includesPatterns) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   312
        compressedResources.entries().forEach(compressed -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
            CompressedResourceHeader header = CompressedResourceHeader.readFromResource(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
                    ByteOrder.nativeOrder(), compressed.getBytes());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            String path = compressed.getPath();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   316
            ModuleEntry orig = inputResources.findEntry(path).get();
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   317
            if (!isIncluded(includesPatterns, path)) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   318
                return;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
            byte[] decompressed = compressed.getBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            for (ResourceDecompressorFactory factory : decompressors) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   322
                try {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   323
                    ResourceDecompressor decompressor = factory.newDecompressor(new Properties());
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   324
                    decompressed = decompressor.decompress(
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
                        strings::get, decompressed,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                        CompressedResourceHeader.getSize(), header.getUncompressedSize());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   327
                } catch (Exception exp) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   328
                    throw new RuntimeException(exp);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   329
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
            if (decompressed.length != orig.getLength()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
                throw new AssertionError("Invalid uncompressed size "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
                        + header.getUncompressedSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            byte[] origContent = orig.getBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            for (int i = 0; i < decompressed.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
                if (decompressed[i] != origContent[i]) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
                    throw new AssertionError("Decompressed and original differ at index " + i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   342
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   345
    private boolean isIncluded(List<Pattern> includesPatterns, String path) {
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   346
        return includesPatterns.isEmpty() ||
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   347
               includesPatterns.stream().anyMatch((pattern) -> pattern.matcher(path).matches());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
}