LastSorterTest.java revision 16491:7515d03dd907
1218887Sdim/* 2218887Sdim * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3218887Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4218887Sdim * 5218887Sdim * This code is free software; you can redistribute it and/or modify it 6218887Sdim * under the terms of the GNU General Public License version 2 only, as 7218887Sdim * published by the Free Software Foundation. 8218887Sdim * 9218887Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 10218887Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11218887Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12218887Sdim * version 2 for more details (a copy is included in the LICENSE file that 13218887Sdim * accompanied this code). 14218887Sdim * 15218887Sdim * You should have received a copy of the GNU General Public License version 16239462Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17218887Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18221345Sdim * 19218887Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20218887Sdim * or visit www.oracle.com if you need additional information or have any 21218887Sdim * questions. 22218887Sdim */ 23218887Sdim 24218887Sdim/* 25218887Sdim * @test 26218887Sdim * @summary Test last sorter property 27218887Sdim * @author Jean-Francois Denise 28218887Sdim * @modules jdk.jlink/jdk.tools.jlink 29218887Sdim * jdk.jlink/jdk.tools.jlink.internal 30221345Sdim * jdk.jlink/jdk.tools.jlink.plugin 31226633Sdim * @run main/othervm LastSorterTest 32221345Sdim */ 33218887Sdim 34218887Sdimimport java.util.ArrayList; 35218887Sdimimport java.util.Collections; 36218887Sdimimport java.util.HashMap; 37218887Sdimimport java.util.List; 38218887Sdimimport java.util.Map; 39218887Sdim 40218887Sdimimport jdk.tools.jlink.internal.ImagePluginConfiguration; 41218887Sdimimport jdk.tools.jlink.internal.ImagePluginStack; 42218887Sdimimport jdk.tools.jlink.internal.Jlink; 43218887Sdimimport jdk.tools.jlink.internal.Jlink.PluginsConfiguration; 44218887Sdimimport jdk.tools.jlink.internal.PluginRepository; 45218887Sdimimport jdk.tools.jlink.internal.ResourcePoolManager; 46218887Sdimimport jdk.tools.jlink.plugin.Plugin; 47218887Sdimimport jdk.tools.jlink.plugin.ResourcePool; 48218887Sdimimport jdk.tools.jlink.plugin.ResourcePoolBuilder; 49218887Sdimimport jdk.tools.jlink.plugin.ResourcePoolEntry; 50218887Sdim 51218887Sdimpublic class LastSorterTest { 52218887Sdim 53218887Sdim public LastSorterTest() { 54218887Sdim for (int i = 1; i <= 6; i++) { 55218887Sdim PluginRepository.registerPlugin(new SorterPlugin("sorterplugin" + i)); 56218887Sdim } 57218887Sdim } 58218887Sdim 59218887Sdim public static void main(String[] args) throws Exception { 60218887Sdim new LastSorterTest().test(); 61218887Sdim } 62218887Sdim 63218887Sdim public void test() throws Exception { 64218887Sdim checkUnknownPlugin(); 65218887Sdim 66218887Sdim checkOrderAfterLastSorter(); 67218887Sdim 68218887Sdim checkPositiveCase(); 69218887Sdim 70218887Sdim checkTwoLastSorters(); 71218887Sdim } 72218887Sdim 73218887Sdim private void checkTwoLastSorters() throws Exception { 74226633Sdim List<Plugin> plugins = new ArrayList<>(); 75218887Sdim plugins.add(createPlugin("sorterplugin5", "/a")); 76218887Sdim plugins.add(createPlugin("sorterplugin6", "/a")); 77218887Sdim PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins, 78218887Sdim null, "sorterplugin5"); 79218887Sdim 80218887Sdim ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config); 81218887Sdim 82218887Sdim // check order 83218887Sdim ResourcePoolManager res = fillOutResourceResourcePool(); 84218887Sdim 85218887Sdim try { 86218887Sdim stack.visitResources(res); 87218887Sdim throw new AssertionError("Exception expected: Order of resources is already frozen." + 88218887Sdim "Plugin sorterplugin6 is badly located"); 89218887Sdim } catch (Exception e) { 90218887Sdim // expected 91218887Sdim } 92218887Sdim } 93218887Sdim 94218887Sdim private ResourcePoolManager fillOutResourceResourcePool() throws Exception { 95218887Sdim ResourcePoolManager res = new ResourcePoolManager(); 96218887Sdim res.add(ResourcePoolEntry.create("/eee/bbb/res1.class", new byte[90])); 97218887Sdim res.add(ResourcePoolEntry.create("/aaaa/bbb/res2.class", new byte[90])); 98218887Sdim res.add(ResourcePoolEntry.create("/bbb/aa/res1.class", new byte[90])); 99218887Sdim res.add(ResourcePoolEntry.create("/aaaa/bbb/res3.class", new byte[90])); 100218887Sdim res.add(ResourcePoolEntry.create("/bbb/aa/res2.class", new byte[90])); 101218887Sdim res.add(ResourcePoolEntry.create("/fff/bbb/res1.class", new byte[90])); 102218887Sdim res.add(ResourcePoolEntry.create("/aaaa/bbb/res1.class", new byte[90])); 103218887Sdim res.add(ResourcePoolEntry.create("/bbb/aa/res3.class", new byte[90])); 104243830Sdim res.add(ResourcePoolEntry.create("/ccc/bbb/res1.class", new byte[90])); 105218887Sdim res.add(ResourcePoolEntry.create("/ddd/bbb/res1.class", new byte[90])); 106218887Sdim return res; 107218887Sdim } 108218887Sdim 109218887Sdim private static Plugin createPlugin(String name, String arg) { 110218887Sdim Map<String, String> conf = new HashMap<>(); 111218887Sdim conf.put(name, arg); 112218887Sdim return Jlink.newPlugin(name, conf, null); 113226633Sdim } 114218887Sdim 115218887Sdim private void checkPositiveCase() throws Exception { 116218887Sdim List<Plugin> plugins = new ArrayList<>(); 117218887Sdim plugins.add(createPlugin("sorterplugin1", "/c")); 118218887Sdim plugins.add(createPlugin("sorterplugin2", "/b")); 119218887Sdim plugins.add(createPlugin("sorterplugin3", "/a")); 120218887Sdim 121218887Sdim PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins, 122218887Sdim null, "sorterplugin3"); 123218887Sdim 124218887Sdim ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config); 125218887Sdim 126218887Sdim // check order 127221345Sdim ResourcePoolManager res = fillOutResourceResourcePool(); 128226633Sdim 129218887Sdim stack.visitResources(res); 130218887Sdim } 131226633Sdim 132218887Sdim private void checkUnknownPlugin() { 133218887Sdim List<Plugin> plugins = new ArrayList<>(); 134218887Sdim plugins.add(createPlugin("sorterplugin1", "/1")); 135218887Sdim plugins.add(createPlugin("sorterplugin2", "/1")); 136218887Sdim plugins.add(createPlugin("sorterplugin3", "/1")); 137218887Sdim plugins.add(createPlugin("sorterplugin4", "/1")); 138218887Sdim 139218887Sdim PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins, 140218887Sdim null, "sorterplugin5"); 141218887Sdim try { 142218887Sdim ImagePluginConfiguration.parseConfiguration(config); 143218887Sdim throw new AssertionError("Unknown plugin should have failed."); 144218887Sdim } catch (Exception ex) { 145218887Sdim // XXX OK expected 146218887Sdim } 147218887Sdim } 148218887Sdim 149218887Sdim private void checkOrderAfterLastSorter() throws Exception { 150218887Sdim List<Plugin> plugins = new ArrayList<>(); 151218887Sdim plugins.add(createPlugin("sorterplugin1", "/c")); 152218887Sdim plugins.add(createPlugin("sorterplugin2", "/b")); 153218887Sdim plugins.add(createPlugin("sorterplugin3", "/a")); 154218887Sdim plugins.add(createPlugin("sorterplugin4", "/d")); 155218887Sdim 156218887Sdim PluginsConfiguration config = new Jlink.PluginsConfiguration(plugins, 157218887Sdim null, "sorterplugin3"); 158218887Sdim 159218887Sdim ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(config); 160218887Sdim 161218887Sdim // check order 162218887Sdim ResourcePoolManager res = fillOutResourceResourcePool(); 163218887Sdim try { 164218887Sdim stack.visitResources(res); 165218887Sdim throw new AssertionError("Order was changed after the last sorter, but no exception occurred"); 166218887Sdim } catch (Exception ex) { 167218887Sdim // XXX OK expected 168218887Sdim } 169218887Sdim } 170218887Sdim 171218887Sdim public static class SorterPlugin implements Plugin { 172218887Sdim 173218887Sdim private final String name; 174218887Sdim private String starts; 175218887Sdim 176218887Sdim private SorterPlugin(String name) { 177218887Sdim this.name = name; 178218887Sdim } 179218887Sdim 180218887Sdim @Override 181218887Sdim public ResourcePool transform(ResourcePool resources, ResourcePoolBuilder output) { 182218887Sdim List<ResourcePoolEntry> paths = new ArrayList<>(); 183218887Sdim resources.entries().forEach(res -> { 184218887Sdim if (res.path().startsWith(starts)) { 185218887Sdim paths.add(0, res); 186218887Sdim } else { 187218887Sdim paths.add(res); 188218887Sdim } 189218887Sdim }); 190218887Sdim 191218887Sdim for (ResourcePoolEntry r : paths) { 192218887Sdim output.add(r); 193218887Sdim } 194218887Sdim 195218887Sdim return output.build(); 196218887Sdim } 197218887Sdim 198218887Sdim @Override 199218887Sdim public String getName() { 200218887Sdim return name; 201218887Sdim } 202218887Sdim 203218887Sdim @Override 204218887Sdim public void configure(Map<String, String> config) { 205218887Sdim String arguments = config.get(name); 206218887Sdim this.starts = arguments; 207218887Sdim } 208218887Sdim } 209218887Sdim} 210218887Sdim