Path: blob/master/test/jdk/tools/jlink/plugins/StringSharingPluginTest.java
41152 views
/*1* Copyright (c) 2015, 2018, 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 StringSharingPluginTest26* @author Jean-Francois Denise27* @library ../../lib28* @modules java.base/jdk.internal.jimage29* java.base/jdk.internal.jimage.decompressor30* jdk.jlink/jdk.tools.jlink.internal31* jdk.jlink/jdk.tools.jlink.internal.plugins32* jdk.jlink/jdk.tools.jlink.plugin33* jdk.jlink/jdk.tools.jmod34* jdk.jlink/jdk.tools.jimage35* jdk.jdeps/com.sun.tools.classfile36* jdk.compiler37* @run build tests.*38* @run main StringSharingPluginTest39*/4041import java.io.IOException;42import java.io.UncheckedIOException;43import java.nio.ByteBuffer;44import java.nio.ByteOrder;45import java.nio.file.Files;46import java.nio.file.Path;47import java.util.Arrays;48import java.util.HashMap;49import java.util.List;50import java.util.Map;51import java.util.function.Consumer;5253import jdk.internal.jimage.decompressor.CompressedResourceHeader;54import jdk.internal.jimage.decompressor.StringSharingDecompressor;55import jdk.tools.jlink.internal.ResourcePoolManager;56import jdk.tools.jlink.internal.StringTable;57import jdk.tools.jlink.internal.plugins.StringSharingPlugin;58import jdk.tools.jlink.plugin.ResourcePoolEntry;59import jdk.tools.jlink.plugin.ResourcePool;60import jdk.tools.jlink.plugin.Plugin;61import tests.Helper;62import tests.JImageValidator;6364public class StringSharingPluginTest {6566private static int strID = 1;6768public static void main(String[] args) throws Exception {69Helper helper = Helper.newHelper();70if (helper == null) {71// Skip test if the jmods directory is missing (e.g. exploded image)72System.err.println("Test not run, NO jmods directory");73return;74}7576List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X");77Path compiledClasses = helper.generateModuleCompiledClasses(78helper.getJmodSrcDir(), helper.getJmodClassesDir(), "composite2", classes);7980Map<String, Integer> map = new HashMap<>();81Map<Integer, String> reversedMap = new HashMap<>();8283ResourcePoolManager resources = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() {84@Override85public int addString(String str) {86Integer id = map.get(str);87if (id == null) {88id = strID;89map.put(str, id);90reversedMap.put(id, str);91strID += 1;92}93return id;94}9596@Override97public String getString(int id) {98throw new UnsupportedOperationException("Not supported yet.");99}100});101Consumer<Path> c = (p) -> {102// take only the .class resources.103if (Files.isRegularFile(p) && p.toString().endsWith(".class")104&& !p.toString().endsWith("module-info.class")) {105try {106byte[] content = Files.readAllBytes(p);107String path = p.toString().replace('\\', '/');108path = path.substring("/modules".length());109if (path.charAt(0) != '/') {110path = "/" + path;111}112ResourcePoolEntry res = ResourcePoolEntry.create(path, content);113resources.add(res);114} catch (Exception ex) {115throw new RuntimeException(ex);116}117}118};119try (java.util.stream.Stream<Path> stream = Files.walk(compiledClasses)) {120stream.forEach(c);121}122Plugin plugin = new StringSharingPlugin();123ResourcePoolManager resultMgr = new ResourcePoolManager(resources.byteOrder(), resources.getStringTable());124ResourcePool result = plugin.transform(resources.resourcePool(), resultMgr.resourcePoolBuilder());125126if (result.isEmpty()) {127throw new AssertionError("No result");128}129130result.entries().forEach(res -> {131if (res.path().endsWith(".class")) {132try {133byte[] uncompacted = StringSharingDecompressor.normalize(reversedMap::get, res.contentBytes(),134CompressedResourceHeader.getSize());135JImageValidator.readClass(uncompacted);136} catch (IOException exp) {137throw new UncheckedIOException(exp);138}139}140});141}142}143144145