Path: blob/master/test/jdk/tools/jlink/ImageFileCreatorTest.java
41145 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*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.DataOutputStream;26import java.io.IOException;27import java.io.InputStream;28import java.nio.ByteOrder;29import java.nio.file.Path;30import java.util.ArrayList;31import java.util.Collections;32import java.util.HashSet;33import java.util.List;34import java.util.Set;35import java.util.stream.Stream;36import jdk.tools.jlink.internal.Archive;37import jdk.tools.jlink.internal.ImageFileCreator;38import jdk.tools.jlink.internal.ImagePluginStack;39import jdk.tools.jlink.internal.ExecutableImage;40import jdk.tools.jlink.builder.ImageBuilder;41import jdk.tools.jlink.plugin.ResourcePool;424344/*45* @test46* @summary ImageFileCreator class test47* @author Jean-Francois Denise48* @modules jdk.jlink/jdk.tools.jlink.internal49* jdk.jlink/jdk.tools.jlink.builder50* jdk.jlink/jdk.tools.jlink.plugin51* java.base/jdk.internal.jimage52* @run main/othervm -verbose:gc -Xmx1g ImageFileCreatorTest53*/54public class ImageFileCreatorTest {5556private static class TestArchive implements Archive {5758private final String name;59private final List<Entry> entries = new ArrayList<>();6061private TestArchive(String name, List<String> entries) {62this.name = name;63for (String p : entries) {64this.entries.add(new TestEntry(p, p));65}66}6768@Override69public String moduleName() {70return name;71}7273@Override74public Stream<Entry> entries() {75return entries.stream();76}7778@Override79public Path getPath() {80return null;81}8283@Override84public void open() throws IOException {85}8687@Override88public void close() throws IOException {89}9091private class TestEntry extends Entry {9293TestEntry(String path, String name) {94super(TestArchive.this, path, name, Entry.EntryType.CLASS_OR_RESOURCE);95}9697@Override98public long size() {99return 0;100}101102@Override103public InputStream stream() throws IOException {104return new ByteArrayInputStream(new byte[0]);105}106}107}108109public static void main(String[] args) throws Exception {110111{112List<String> entries = new ArrayList<>();113entries.add("classes/class");114test(entries);115}116117{118// Add an entry that is a directory, that is wrong119List<String> entries = new ArrayList<>();120entries.add("classes");121entries.add("classes/class");122test(entries);123}124125{126// Add an entry that is wrongly prefixed by /127// /bad//classes/class is the resource added128// /bad/classes/class is the metadata node built.129List<String> entries = new ArrayList<>();130entries.add("/classes/class");131test(entries);132}133134{135// Trailing '/' is wrong136List<String> entries = new ArrayList<>();137entries.add("classes/class/");138test(entries);139}140141{142// Too much '/' characters143List<String> entries = new ArrayList<>();144entries.add("classes//class/");145test(entries);146}147148{149// Too much '/' characters150List<String> entries = new ArrayList<>();151entries.add("classes/////class/");152test(entries);153}154155{156// Single '/' character157List<String> entries = new ArrayList<>();158entries.add("/");159test(entries);160}161162{163// 2 '/' characters164List<String> entries = new ArrayList<>();165entries.add("//");166test(entries);167}168169{170// 3 '/' characters171List<String> entries = new ArrayList<>();172entries.add("///");173test(entries);174}175176{177// no character178List<String> entries = new ArrayList<>();179entries.add("");180test(entries);181}182183{184// all together185List<String> entries = new ArrayList<>();186entries.add("");187entries.add("///");188entries.add("//");189entries.add("/");190entries.add("classes/////class/");191entries.add("classes//class/");192entries.add("classes/class/");193entries.add("/classes/class");194entries.add("classes");195entries.add("classes/class");196test(entries);197}198199}200201private static void test(List<String> entries) throws Exception {202TestArchive arch = new TestArchive("bad", entries);203Set<Archive> archives = new HashSet<>();204archives.add(arch);205ImageBuilder noopBuilder = new ImageBuilder() {206207@Override208public DataOutputStream getJImageOutputStream() {209return new DataOutputStream(new ByteArrayOutputStream());210}211212@Override213public ExecutableImage getExecutableImage() {214return null;215}216217@Override218public void storeFiles(ResourcePool content) {219}220};221222ImagePluginStack stack = new ImagePluginStack(noopBuilder, Collections.emptyList(),223null, false);224225ImageFileCreator.create(archives, ByteOrder.nativeOrder(), stack);226}227}228229230