Path: blob/master/test/jdk/tools/jlink/plugins/PrevisitorTest.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 previsitor26* @author Andrei Eremeev27* @modules jdk.jlink/jdk.tools.jlink28* jdk.jlink/jdk.tools.jlink.internal29* jdk.jlink/jdk.tools.jlink.plugin30* @run main/othervm PrevisitorTest31*/32import java.nio.ByteOrder;33import java.util.ArrayList;34import java.util.Collection;35import java.util.Collections;36import java.util.HashMap;37import java.util.List;38import java.util.Map;39import java.util.Optional;40import java.util.stream.Collectors;4142import jdk.tools.jlink.internal.ImagePluginConfiguration;43import jdk.tools.jlink.internal.Jlink;44import jdk.tools.jlink.internal.PluginRepository;45import jdk.tools.jlink.internal.ImagePluginStack;46import jdk.tools.jlink.internal.ResourcePoolManager;47import jdk.tools.jlink.internal.ResourcePoolManager.ResourcePoolImpl;48import jdk.tools.jlink.internal.ResourcePrevisitor;49import jdk.tools.jlink.internal.StringTable;50import jdk.tools.jlink.plugin.Plugin;51import jdk.tools.jlink.plugin.ResourcePool;52import jdk.tools.jlink.plugin.ResourcePoolBuilder;53import jdk.tools.jlink.plugin.ResourcePoolEntry;5455public class PrevisitorTest {5657public static void main(String[] args) throws Exception {58new PrevisitorTest().test();59}6061private static Plugin createPlugin(String name) {62return Jlink.newPlugin(name, Collections.emptyMap(), null);63}6465public void test() throws Exception {66CustomPlugin plugin = new CustomPlugin();67PluginRepository.registerPlugin(plugin);68List<Plugin> plugins = new ArrayList<>();69plugins.add(createPlugin(CustomPlugin.NAME));70ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new Jlink.PluginsConfiguration(plugins,71null, null));72ResourcePoolManager inResources = new ResourcePoolManager(ByteOrder.nativeOrder(), new CustomStringTable());73inResources.add(ResourcePoolEntry.create("/aaa/bbb/res1.class", new byte[90]));74inResources.add(ResourcePoolEntry.create("/aaa/bbb/res2.class", new byte[90]));75inResources.add(ResourcePoolEntry.create("/aaa/bbb/res3.class", new byte[90]));76inResources.add(ResourcePoolEntry.create("/aaa/ddd/res1.class", new byte[90]));77inResources.add(ResourcePoolEntry.create("/aaa/res1.class", new byte[90]));78ResourcePool outResources = stack.visitResources(inResources);79Collection<String> input = inResources.entries()80.map(Object::toString)81.collect(Collectors.toList());82Collection<String> output = outResources.entries()83.map(Object::toString)84.collect(Collectors.toList());85if (!input.equals(output)) {86throw new AssertionError("Input and output resources differ: input: "87+ input + ", output: " + output);88}89}9091private static class CustomStringTable implements StringTable {9293private final List<String> strings = new ArrayList<>();9495@Override96public int addString(String str) {97strings.add(str);98return strings.size() - 1;99}100101@Override102public String getString(int id) {103return strings.get(id);104}105106public int size() {107return strings.size();108}109}110111private static class CustomPlugin implements Plugin, ResourcePrevisitor {112113private static String NAME = "plugin";114115private boolean isPrevisitCalled = false;116117@Override118public ResourcePool transform(ResourcePool inResources, ResourcePoolBuilder outResources) {119if (!isPrevisitCalled) {120throw new AssertionError("Previsit was not called");121}122CustomStringTable table = (CustomStringTable)((ResourcePoolImpl)inResources).getStringTable();123if (table.size() == 0) {124throw new AssertionError("Table is empty");125}126Map<String, Integer> count = new HashMap<>();127for (int i = 0; i < table.size(); ++i) {128String s = table.getString(i);129Optional<ResourcePoolEntry> e = inResources.findEntry(s);130if (e.isPresent()) {131throw new AssertionError();132}133count.compute(s, (k, c) -> 1 + (c == null ? 0 : c));134}135count.forEach((k, v) -> {136if (v != 1) {137throw new AssertionError("Expected one entry in the table, got: " + v + " for " + k);138}139});140inResources.entries().forEach(r -> {141outResources.add(r);142});143144return outResources.build();145}146147@Override148public String getName() {149return NAME;150}151152@Override153public void previsit(ResourcePool resources, StringTable strings) {154isPrevisitCalled = true;155resources.entries().forEach(r -> {156String s = r.path();157int lastIndexOf = s.lastIndexOf('/');158if (lastIndexOf >= 0) {159strings.addString(s.substring(0, lastIndexOf));160}161});162}163}164}165166167