jdk/test/tools/jlink/plugins/OrderResourcesPluginTest.java
author sundar
Thu, 30 Jun 2016 22:24:01 +0530
changeset 39321 c60f34e8c057
parent 39042 52db877f18db
child 39834 53a6fb443c20
permissions -rw-r--r--
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API Reviewed-by: jlaskey, 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 sorter plugin
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
 *          jdk.jlink/jdk.tools.jlink.internal.plugins
37893
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
    30
 * @run main OrderResourcesPluginTest
36511
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.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Collection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Map;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    39
import java.util.stream.Collectors;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    41
import jdk.tools.jlink.internal.ModulePoolImpl;
37893
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
    42
import jdk.tools.jlink.internal.plugins.OrderResourcesPlugin;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    43
import jdk.tools.jlink.plugin.ModuleEntry;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    44
import jdk.tools.jlink.plugin.ModulePool;
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39042
diff changeset
    45
import jdk.tools.jlink.plugin.Plugin;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
37893
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
    47
public class OrderResourcesPluginTest {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
37893
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
    50
        new OrderResourcesPluginTest().test();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    public void test() throws Exception {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    54
        ModuleEntry[] array = {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    55
                ModuleEntry.create("/module1/toto1.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    56
                ModuleEntry.create("/module2/toto2.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    57
                ModuleEntry.create("/module3/toto3.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    58
                ModuleEntry.create("/module3/toto3/module-info.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    59
                ModuleEntry.create("/zazou/toto.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    60
                ModuleEntry.create("/module4/zazou.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    61
                ModuleEntry.create("/module5/toto5.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    62
                ModuleEntry.create("/module6/toto6/module-info.class", new byte[0])
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    65
        ModuleEntry[] sorted = {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    66
                ModuleEntry.create("/zazou/toto.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    67
                ModuleEntry.create("/module3/toto3/module-info.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    68
                ModuleEntry.create("/module6/toto6/module-info.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    69
                ModuleEntry.create("/module1/toto1.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    70
                ModuleEntry.create("/module2/toto2.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    71
                ModuleEntry.create("/module3/toto3.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    72
                ModuleEntry.create("/module4/zazou.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    73
                ModuleEntry.create("/module5/toto5.class", new byte[0])
37893
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
    74
        };
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    76
        ModuleEntry[] sorted2 = {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    77
            ModuleEntry.create("/module5/toto5.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    78
            ModuleEntry.create("/module6/toto6/module-info.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    79
            ModuleEntry.create("/module4/zazou.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    80
            ModuleEntry.create("/module3/toto3.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    81
            ModuleEntry.create("/module3/toto3/module-info.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    82
            ModuleEntry.create("/module1/toto1.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    83
            ModuleEntry.create("/module2/toto2.class", new byte[0]),
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    84
            ModuleEntry.create("/zazou/toto.class", new byte[0])
37893
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
    85
        };
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    87
        ModulePool resources = new ModulePoolImpl();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    88
        for (ModuleEntry r : array) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            resources.add(r);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    93
            ModulePool out = new ModulePoolImpl();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            Map<String, String> config = new HashMap<>();
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
    95
            config.put(OrderResourcesPlugin.NAME, "/zazou/**,**/module-info.class");
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39042
diff changeset
    96
            Plugin p = new OrderResourcesPlugin();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            p.configure(config);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            p.visit(resources, out);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
    99
            check(out.entries().collect(Collectors.toList()), sorted);
36511
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
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            // Order of resources in the file, then un-ordered resources.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            File order = new File("resources.order");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            order.createNewFile();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            StringBuilder builder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            // 5 first resources come from file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            for (int i = 0; i < 5; i++) {
37893
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
   109
                String path = sorted2[i].getPath();
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
   110
                int index = path.indexOf('/', 1);
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
   111
                path = path.substring(index + 1, path.length() - ".class".length());
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
   112
                builder.append(path).append("\n");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            Files.write(order.toPath(), builder.toString().getBytes());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
   116
            ModulePool out = new ModulePoolImpl();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            Map<String, String> config = new HashMap<>();
37893
f1cc7d17e66b 8156781: change to jlink has result in test failure
jlaskey
parents: 36511
diff changeset
   118
            config.put(OrderResourcesPlugin.NAME, "@" + order.getAbsolutePath());
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39042
diff changeset
   119
            Plugin p = new OrderResourcesPlugin();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            p.configure(config);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            p.visit(resources, out);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
   122
            check(out.entries().collect(Collectors.toList()), sorted2);
36511
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
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
   127
    private void check(Collection<ModuleEntry> outResources,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
   128
            ModuleEntry[] sorted) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        if (outResources.size() != sorted.length) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            throw new AssertionError("Wrong number of resources:\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                    + "expected: " + Arrays.toString(sorted) + ",\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                    + "     got: " + outResources);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        int i = 0;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37893
diff changeset
   135
        for (ModuleEntry r : outResources) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            System.err.println("Resource: " + r);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            if (!sorted[i].getPath().equals(r.getPath())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                throw new AssertionError("Resource not properly sorted, difference at: " + i + "\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                        + "expected: " + Arrays.toString(sorted) + ",\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                        + "     got: " + outResources);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
            i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
}