Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/jar/index/MetaInf.java
38841 views
/*1* Copyright (c) 2003, 2010, 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 4408526 685479526* @summary Index the non-meta files in META-INF, such as META-INF/services.27*/2829import java.io.*;30import java.util.Arrays;31import java.util.jar.*;32import sun.tools.jar.Main;33import java.util.zip.ZipFile;3435public class MetaInf {3637static String jarName = "a.jar";38static String INDEX = "META-INF/INDEX.LIST";39static String SERVICES = "META-INF/services";40static String contents =41System.getProperty("test.src") + File.separatorChar + "jarcontents";4243static void run(String ... args) {44if (! new Main(System.out, System.err, "jar").run(args))45throw new Error("jar failed: args=" + Arrays.toString(args));46}4748static void copy(File from, File to) throws IOException {49FileInputStream in = new FileInputStream(from);50FileOutputStream out = new FileOutputStream(to);51try {52byte[] buf = new byte[8192];53int n;54while ((n = in.read(buf)) != -1)55out.write(buf, 0, n);56} finally {57in.close();58out.close();59}60}6162static boolean contains(File jarFile, String entryName)63throws IOException {64ZipFile zf = new ZipFile(jarFile);65if ( zf != null ) {66boolean result = zf.getEntry(entryName) != null;67zf.close();68return result;69}70return false;71}7273static void checkContains(File jarFile, String entryName)74throws IOException {75if (! contains(jarFile, entryName))76throw new Error(String.format("expected jar %s to contain %s",77jarFile, entryName));78}7980static void testIndex(String jarName) throws IOException {81System.err.printf("jarName=%s%n", jarName);8283File jar = new File(jarName);8485// Create a jar to be indexed.86run("cf", jarName, "-C", contents, SERVICES);8788for (int i = 0; i < 2; i++) {89run("i", jarName);90checkContains(jar, INDEX);91checkContains(jar, SERVICES);92}9394JarFile f = new JarFile(jarName);95BufferedReader index =96new BufferedReader(97new InputStreamReader(98f.getInputStream(f.getJarEntry(INDEX))));99String line;100while ((line = index.readLine()) != null) {101if (line.equals(SERVICES)) {102index.close();103f.close();104return;105}106}107index.close();108f.close();109throw new Error(SERVICES + " not indexed.");110}111112public static void main(String[] args) throws IOException {113testIndex("a.jar"); // a path with parent == null114testIndex("./a.zip"); // a path with parent != null115116// Try indexing a jar in the default temp directory.117File tmpFile = File.createTempFile("MetaInf", null, null);118try {119testIndex(tmpFile.getPath());120} finally {121tmpFile.delete();122}123}124}125126127