Path: blob/master/test/jdk/tools/jlink/plugins/OrderResourcesPluginTest.java
41149 views
/*1* Copyright (c) 2015, 2020, 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 sorter plugin26* @author Jean-Francois Denise27* @modules jdk.jlink/jdk.tools.jlink.internal28* jdk.jlink/jdk.tools.jlink.internal.plugins29* jdk.jlink/jdk.tools.jlink.plugin30* @run main OrderResourcesPluginTest31*/3233import java.io.File;34import java.nio.file.Files;35import java.util.Arrays;36import java.util.Collection;37import java.util.HashMap;38import java.util.Map;39import java.util.stream.Collectors;4041import jdk.tools.jlink.internal.ResourcePoolManager;42import jdk.tools.jlink.internal.plugins.OrderResourcesPlugin;43import jdk.tools.jlink.plugin.ResourcePoolEntry;44import jdk.tools.jlink.plugin.ResourcePool;45import jdk.tools.jlink.plugin.Plugin;4647public class OrderResourcesPluginTest {4849public static void main(String[] args) throws Exception {50new OrderResourcesPluginTest().test();51}5253public void test() throws Exception {54ResourcePoolEntry[] array = {55ResourcePoolEntry.create("/module1/toto1.class", new byte[0]),56ResourcePoolEntry.create("/module2/toto2.class", new byte[0]),57ResourcePoolEntry.create("/module3/toto3.class", new byte[0]),58ResourcePoolEntry.create("/module3/toto3/module-info.class", new byte[0]),59ResourcePoolEntry.create("/zazou/toto.class", new byte[0]),60ResourcePoolEntry.create("/module4/zazou.class", new byte[0]),61ResourcePoolEntry.create("/module5/toto5.class", new byte[0]),62ResourcePoolEntry.create("/module6/toto6/module-info.class", new byte[0])63};6465ResourcePoolEntry[] sorted = {66ResourcePoolEntry.create("/zazou/toto.class", new byte[0]),67ResourcePoolEntry.create("/module3/toto3/module-info.class", new byte[0]),68ResourcePoolEntry.create("/module6/toto6/module-info.class", new byte[0]),69ResourcePoolEntry.create("/module1/toto1.class", new byte[0]),70ResourcePoolEntry.create("/module2/toto2.class", new byte[0]),71ResourcePoolEntry.create("/module3/toto3.class", new byte[0]),72ResourcePoolEntry.create("/module4/zazou.class", new byte[0]),73ResourcePoolEntry.create("/module5/toto5.class", new byte[0])74};7576ResourcePoolEntry[] sorted2 = {77ResourcePoolEntry.create("/module5/toto5.class", new byte[0]),78ResourcePoolEntry.create("/module6/toto6/module-info.class", new byte[0]),79ResourcePoolEntry.create("/module4/zazou.class", new byte[0]),80ResourcePoolEntry.create("/module3/toto3.class", new byte[0]),81ResourcePoolEntry.create("/module3/toto3/module-info.class", new byte[0]),82ResourcePoolEntry.create("/module1/toto1.class", new byte[0]),83ResourcePoolEntry.create("/module2/toto2.class", new byte[0]),84ResourcePoolEntry.create("/zazou/toto.class", new byte[0])85};8687ResourcePoolManager resources = new ResourcePoolManager();88for (ResourcePoolEntry r : array) {89resources.add(r);90}9192{93ResourcePoolManager out = new ResourcePoolManager();94Map<String, String> config = new HashMap<>();95config.put("order-resources", "/zazou/**,**/module-info.class");96Plugin p = new OrderResourcesPlugin();97p.configure(config);98ResourcePool resPool = p.transform(resources.resourcePool(), out.resourcePoolBuilder());99check(out.entries().collect(Collectors.toList()), sorted);100}101102{103// Order of resources in the file, then un-ordered resources.104File order = new File("resources.order");105order.createNewFile();106StringBuilder builder = new StringBuilder();107// 5 first resources come from file108for (int i = 0; i < 5; i++) {109String path = sorted2[i].path();110int index = path.indexOf('/', 1);111path = path.substring(index + 1, path.length() - ".class".length());112builder.append(path).append("\n");113}114Files.write(order.toPath(), builder.toString().getBytes());115116ResourcePoolManager out = new ResourcePoolManager();117Map<String, String> config = new HashMap<>();118config.put("order-resources", "@" + order.getAbsolutePath());119Plugin p = new OrderResourcesPlugin();120p.configure(config);121ResourcePool resPool = p.transform(resources.resourcePool(), out.resourcePoolBuilder());122check(out.entries().collect(Collectors.toList()), sorted2);123124}125}126127private void check(Collection<ResourcePoolEntry> outResources,128ResourcePoolEntry[] sorted) {129if (outResources.size() != sorted.length) {130throw new AssertionError("Wrong number of resources:\n"131+ "expected: " + Arrays.toString(sorted) + ",\n"132+ " got: " + outResources);133}134int i = 0;135for (ResourcePoolEntry r : outResources) {136System.err.println("Resource: " + r);137if (!sorted[i].path().equals(r.path())) {138throw new AssertionError("Resource not properly sorted, difference at: " + i + "\n"139+ "expected: " + Arrays.toString(sorted) + ",\n"140+ " got: " + outResources);141}142i++;143}144}145}146147148