Path: blob/master/test/jdk/sun/misc/JarIndex/JarIndexMergeTest.java
41152 views
/*1* Copyright (c) 2012, 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* @bug 690199226* @summary InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()27* @modules java.base/jdk.internal.util.jar28* @compile -XDignore.symbol.file JarIndexMergeTest.java29* @run main JarIndexMergeTest30* @author Diego Belfer31*/3233import java.io.File;34import java.io.FileNotFoundException;35import java.io.FileOutputStream;36import java.io.IOException;37import java.util.LinkedList;38import java.util.jar.JarEntry;39import java.util.jar.JarOutputStream;40// implementation specific API41import jdk.internal.util.jar.JarIndex;4243public class JarIndexMergeTest {44static final String slash = File.separator;45static final String testClassesDir = System.getProperty("test.classes", ".");46static final File tmpFolder = new File(testClassesDir);4748public static void main(String[] args) throws Exception {49File jar1 = buildJar1();50File jar2 = buildJar2();5152JarIndex jarIndex1 = new JarIndex(new String[] { jar1.getAbsolutePath() });53JarIndex jarIndex2 = new JarIndex(new String[] { jar2.getAbsolutePath() });5455jarIndex1.merge(jarIndex2, null);5657assertFileResolved(jarIndex2, "com/test1/resource1.file",58jar1.getAbsolutePath());59assertFileResolved(jarIndex2, "com/test2/resource2.file",60jar2.getAbsolutePath());61}6263static void assertFileResolved(JarIndex jarIndex2, String file,64String jarName) {65@SuppressWarnings("unchecked")66LinkedList<String> jarLists = (LinkedList<String>)jarIndex2.get(file);67if (jarLists == null || jarLists.size() == 0 ||68!jarName.equals(jarLists.get(0))) {69throw new RuntimeException(70"Unexpected result: the merged index must resolve file: "71+ file);72}73}7475private static File buildJar1() throws FileNotFoundException, IOException {76JarBuilder jar1Builder = new JarBuilder(tmpFolder, "jar1-merge.jar");77jar1Builder.addResourceFile("com/test1/resource1.file", "resource1");78return jar1Builder.build();79}8081private static File buildJar2() throws FileNotFoundException, IOException {82JarBuilder jar2Builder = new JarBuilder(tmpFolder, "jar2-merge.jar");83jar2Builder.addResourceFile("com/test2/resource2.file", "resource2");84return jar2Builder.build();85}8687/*88* Helper class for building jar files89*/90public static class JarBuilder {91private JarOutputStream os;92private File jarFile;9394public JarBuilder(File tmpFolder, String jarName)95throws FileNotFoundException, IOException96{97this.jarFile = new File(tmpFolder, jarName);98this.os = new JarOutputStream(new FileOutputStream(jarFile));99}100101public void addResourceFile(String pathFromRoot, String content)102throws IOException103{104JarEntry entry = new JarEntry(pathFromRoot);105os.putNextEntry(entry);106os.write(content.getBytes("ASCII"));107os.closeEntry();108}109110public File build() throws IOException {111os.close();112return jarFile;113}114}115}116117118119