Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/JarIndex/JarIndexMergeTest.java
38838 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* @compile -XDignore.symbol.file JarIndexMergeTest.java27* @run main JarIndexMergeTest28* @summary InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()29* @author Diego Belfer30*/3132import java.io.File;33import java.io.FileNotFoundException;34import java.io.FileOutputStream;35import java.io.IOException;36import java.util.LinkedList;37import java.util.jar.JarEntry;38import java.util.jar.JarOutputStream;39// implementation specific API40import sun.misc.JarIndex;4142public class JarIndexMergeTest {43static final String slash = File.separator;44static final String testClassesDir = System.getProperty("test.classes", ".");45static final File tmpFolder = new File(testClassesDir);4647public static void main(String[] args) throws Exception {48File jar1 = buildJar1();49File jar2 = buildJar2();5051JarIndex jarIndex1 = new JarIndex(new String[] { jar1.getAbsolutePath() });52JarIndex jarIndex2 = new JarIndex(new String[] { jar2.getAbsolutePath() });5354jarIndex1.merge(jarIndex2, null);5556assertFileResolved(jarIndex2, "com/test1/resource1.file",57jar1.getAbsolutePath());58assertFileResolved(jarIndex2, "com/test2/resource2.file",59jar2.getAbsolutePath());60}6162static void assertFileResolved(JarIndex jarIndex2, String file,63String jarName) {64@SuppressWarnings("unchecked")65LinkedList<String> jarLists = (LinkedList<String>)jarIndex2.get(file);66if (jarLists == null || jarLists.size() == 0 ||67!jarName.equals(jarLists.get(0))) {68throw new RuntimeException(69"Unexpected result: the merged index must resolve file: "70+ file);71}72}7374private static File buildJar1() throws FileNotFoundException, IOException {75JarBuilder jar1Builder = new JarBuilder(tmpFolder, "jar1-merge.jar");76jar1Builder.addResourceFile("com/test1/resource1.file", "resource1");77return jar1Builder.build();78}7980private static File buildJar2() throws FileNotFoundException, IOException {81JarBuilder jar2Builder = new JarBuilder(tmpFolder, "jar2-merge.jar");82jar2Builder.addResourceFile("com/test2/resource2.file", "resource2");83return jar2Builder.build();84}8586/*87* Helper class for building jar files88*/89public static class JarBuilder {90private JarOutputStream os;91private File jarFile;9293public JarBuilder(File tmpFolder, String jarName)94throws FileNotFoundException, IOException95{96this.jarFile = new File(tmpFolder, jarName);97this.os = new JarOutputStream(new FileOutputStream(jarFile));98}99100public void addResourceFile(String pathFromRoot, String content)101throws IOException102{103JarEntry entry = new JarEntry(pathFromRoot);104os.putNextEntry(entry);105os.write(content.getBytes("ASCII"));106os.closeEntry();107}108109public File build() throws IOException {110os.close();111return jarFile;112}113}114}115116117118