Path: blob/master/test/jdk/tools/jlink/plugins/LastSorterTest.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Test last sorter property26* @author Jean-Francois Denise27* @modules jdk.jlink/jdk.tools.jlink28* jdk.jlink/jdk.tools.jlink.internal29* jdk.jlink/jdk.tools.jlink.plugin30* @run main/othervm LastSorterTest31*/3233import java.util.ArrayList;34import java.util.Collections;35import java.util.HashMap;36import java.util.List;37import java.util.Map;3839import jdk.tools.jlink.internal.ImagePluginConfiguration;40import jdk.tools.jlink.internal.ImagePluginStack;41import jdk.tools.jlink.internal.Jlink;42import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;43import jdk.tools.jlink.internal.PluginRepository;44import jdk.tools.jlink.internal.ResourcePoolManager;45import jdk.tools.jlink.plugin.Plugin;46import jdk.tools.jlink.plugin.ResourcePool;47import jdk.tools.jlink.plugin.ResourcePoolBuilder;48import jdk.tools.jlink.plugin.ResourcePoolEntry;4950public class LastSorterTest {5152public LastSorterTest() {53for (int i = 1; i <= 6; i++) {54PluginRepository.registerPlugin(new SorterPlugin("sorterplugin" + i));55}56}5758public static void main(String[] args) throws Exception {59new LastSorterTest().test();60}6162public void test() throws Exception {63checkUnknownPlugin();6465checkOrderAfterLastSorter();6667checkPositiveCase();6869checkTwoLastSorters();70}7172private void checkTwoLastSorters() throws Exception {73List<Plugin> plugins = new ArrayList<>();74plugins.add(createPlugin("sorterplugin5", "/a"));75plugins.add(createPlugin("sorterplugin6", "/a"));76PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins,77null, "sorterplugin5");7879ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config);8081// check order82ResourcePoolManager res = fillOutResourceResourcePool();8384try {85stack.visitResources(res);86throw new AssertionError("Exception expected: Order of resources is already frozen." +87"Plugin sorterplugin6 is badly located");88} catch (Exception e) {89// expected90}91}9293private ResourcePoolManager fillOutResourceResourcePool() throws Exception {94ResourcePoolManager res = new ResourcePoolManager();95res.add(ResourcePoolEntry.create("/eee/bbb/res1.class", new byte[90]));96res.add(ResourcePoolEntry.create("/aaaa/bbb/res2.class", new byte[90]));97res.add(ResourcePoolEntry.create("/bbb/aa/res1.class", new byte[90]));98res.add(ResourcePoolEntry.create("/aaaa/bbb/res3.class", new byte[90]));99res.add(ResourcePoolEntry.create("/bbb/aa/res2.class", new byte[90]));100res.add(ResourcePoolEntry.create("/fff/bbb/res1.class", new byte[90]));101res.add(ResourcePoolEntry.create("/aaaa/bbb/res1.class", new byte[90]));102res.add(ResourcePoolEntry.create("/bbb/aa/res3.class", new byte[90]));103res.add(ResourcePoolEntry.create("/ccc/bbb/res1.class", new byte[90]));104res.add(ResourcePoolEntry.create("/ddd/bbb/res1.class", new byte[90]));105return res;106}107108private static Plugin createPlugin(String name, String arg) {109Map<String, String> conf = new HashMap<>();110conf.put(name, arg);111return Jlink.newPlugin(name, conf, null);112}113114private void checkPositiveCase() throws Exception {115List<Plugin> plugins = new ArrayList<>();116plugins.add(createPlugin("sorterplugin1", "/c"));117plugins.add(createPlugin("sorterplugin2", "/b"));118plugins.add(createPlugin("sorterplugin3", "/a"));119120PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins,121null, "sorterplugin3");122123ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config);124125// check order126ResourcePoolManager res = fillOutResourceResourcePool();127128stack.visitResources(res);129}130131private void checkUnknownPlugin() {132List<Plugin> plugins = new ArrayList<>();133plugins.add(createPlugin("sorterplugin1", "/1"));134plugins.add(createPlugin("sorterplugin2", "/1"));135plugins.add(createPlugin("sorterplugin3", "/1"));136plugins.add(createPlugin("sorterplugin4", "/1"));137138PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins,139null, "sorterplugin5");140try {141ImagePluginConfiguration.parseConfiguration(config);142throw new AssertionError("Unknown plugin should have failed.");143} catch (Exception ex) {144// XXX OK expected145}146}147148private void checkOrderAfterLastSorter() throws Exception {149List<Plugin> plugins = new ArrayList<>();150plugins.add(createPlugin("sorterplugin1", "/c"));151plugins.add(createPlugin("sorterplugin2", "/b"));152plugins.add(createPlugin("sorterplugin3", "/a"));153plugins.add(createPlugin("sorterplugin4", "/d"));154155PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins,156null, "sorterplugin3");157158ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config);159160// check order161ResourcePoolManager res = fillOutResourceResourcePool();162try {163stack.visitResources(res);164throw new AssertionError("Order was changed after the last sorter, but no exception occurred");165} catch (Exception ex) {166// XXX OK expected167}168}169170public static class SorterPlugin implements Plugin {171172private final String name;173private String starts;174175private SorterPlugin(String name) {176this.name = name;177}178179@Override180public ResourcePool transform(ResourcePool resources, ResourcePoolBuilder output) {181List<ResourcePoolEntry> paths = new ArrayList<>();182resources.entries().forEach(res -> {183if (res.path().startsWith(starts)) {184paths.add(0, res);185} else {186paths.add(res);187}188});189190for (ResourcePoolEntry r : paths) {191output.add(r);192}193194return output.build();195}196197@Override198public String getName() {199return name;200}201202@Override203public void configure(Map<String, String> config) {204String arguments = config.get(name);205this.starts = arguments;206}207}208}209210211