Path: blob/master/test/jdk/tools/jar/index/MetaInf.java
41149 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* @modules jdk.jartool27* @summary Index the non-meta files in META-INF, such as META-INF/services.28*/2930import java.io.*;31import java.util.Arrays;32import java.util.jar.*;33import java.util.spi.ToolProvider;34import java.util.zip.ZipFile;3536public class MetaInf {37static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")38.orElseThrow(() ->39new RuntimeException("jar tool not found")40);4142static String jarName = "a.jar";43static String INDEX = "META-INF/INDEX.LIST";44static String SERVICES = "META-INF/services";45static String contents =46System.getProperty("test.src") + File.separatorChar + "jarcontents";4748static void run(String ... args) {49if (JAR_TOOL.run(System.out, System.err, args) != 0)50throw new Error("jar failed: args=" + Arrays.toString(args));51}5253static void copy(File from, File to) throws IOException {54FileInputStream in = new FileInputStream(from);55FileOutputStream out = new FileOutputStream(to);56try {57byte[] buf = new byte[8192];58int n;59while ((n = in.read(buf)) != -1)60out.write(buf, 0, n);61} finally {62in.close();63out.close();64}65}6667static boolean contains(File jarFile, String entryName)68throws IOException {69ZipFile zf = new ZipFile(jarFile);70if ( zf != null ) {71boolean result = zf.getEntry(entryName) != null;72zf.close();73return result;74}75return false;76}7778static void checkContains(File jarFile, String entryName)79throws IOException {80if (! contains(jarFile, entryName))81throw new Error(String.format("expected jar %s to contain %s",82jarFile, entryName));83}8485static void testIndex(String jarName) throws IOException {86System.err.printf("jarName=%s%n", jarName);8788File jar = new File(jarName);8990// Create a jar to be indexed.91run("cf", jarName, "-C", contents, SERVICES);9293for (int i = 0; i < 2; i++) {94run("i", jarName);95checkContains(jar, INDEX);96checkContains(jar, SERVICES);97}9899JarFile f = new JarFile(jarName);100BufferedReader index =101new BufferedReader(102new InputStreamReader(103f.getInputStream(f.getJarEntry(INDEX))));104String line;105while ((line = index.readLine()) != null) {106if (line.equals(SERVICES)) {107index.close();108f.close();109return;110}111}112index.close();113f.close();114throw new Error(SERVICES + " not indexed.");115}116117public static void main(String[] args) throws IOException {118testIndex("a.jar"); // a path with parent == null119testIndex("./a.zip"); // a path with parent != null120121// Try indexing a jar in the default temp directory.122File tmpFile = File.createTempFile("MetaInf", null, null);123try {124testIndex(tmpFile.getPath());125} finally {126tmpFile.delete();127}128}129}130131132