0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
1384N/A * @bug 4408526 6854795
0N/A * @summary Index the non-meta files in META-INF, such as META-INF/services.
0N/A */
0N/A
0N/Aimport java.io.*;
1384N/Aimport java.util.Arrays;
0N/Aimport java.util.jar.*;
0N/Aimport sun.tools.jar.Main;
1384N/Aimport java.util.zip.ZipFile;
0N/A
0N/Apublic class MetaInf {
0N/A
0N/A static String jarName = "a.jar";
0N/A static String INDEX = "META-INF/INDEX.LIST";
0N/A static String SERVICES = "META-INF/services";
0N/A static String contents =
0N/A System.getProperty("test.src") + File.separatorChar + "jarcontents";
0N/A
1384N/A static void run(String ... args) {
1384N/A if (! new Main(System.out, System.err, "jar").run(args))
1384N/A throw new Error("jar failed: args=" + Arrays.toString(args));
1384N/A }
0N/A
1384N/A static void copy(File from, File to) throws IOException {
1384N/A FileInputStream in = new FileInputStream(from);
1384N/A FileOutputStream out = new FileOutputStream(to);
1384N/A try {
1384N/A byte[] buf = new byte[8192];
1384N/A int n;
1384N/A while ((n = in.read(buf)) != -1)
1384N/A out.write(buf, 0, n);
1384N/A } finally {
1384N/A in.close();
1384N/A out.close();
1384N/A }
1384N/A }
1384N/A
1384N/A static boolean contains(File jarFile, String entryName)
1384N/A throws IOException {
2500N/A ZipFile zf = new ZipFile(jarFile);
2500N/A if ( zf != null ) {
2500N/A boolean result = zf.getEntry(entryName) != null;
2500N/A zf.close();
2500N/A return result;
2500N/A }
2500N/A return false;
1384N/A }
1384N/A
1384N/A static void checkContains(File jarFile, String entryName)
1384N/A throws IOException {
1384N/A if (! contains(jarFile, entryName))
1384N/A throw new Error(String.format("expected jar %s to contain %s",
1384N/A jarFile, entryName));
1384N/A }
1384N/A
1384N/A static void testIndex(String jarName) throws IOException {
1384N/A System.err.printf("jarName=%s%n", jarName);
1384N/A
1384N/A File jar = new File(jarName);
0N/A
0N/A // Create a jar to be indexed.
1384N/A run("cf", jarName, "-C", contents, SERVICES);
1384N/A
1384N/A for (int i = 0; i < 2; i++) {
1384N/A run("i", jarName);
1384N/A checkContains(jar, INDEX);
1384N/A checkContains(jar, SERVICES);
0N/A }
0N/A
0N/A JarFile f = new JarFile(jarName);
0N/A BufferedReader index =
0N/A new BufferedReader(
0N/A new InputStreamReader(
0N/A f.getInputStream(f.getJarEntry(INDEX))));
0N/A String line;
0N/A while ((line = index.readLine()) != null) {
0N/A if (line.equals(SERVICES)) {
2500N/A index.close();
2500N/A f.close();
0N/A return;
0N/A }
0N/A }
2500N/A index.close();
2500N/A f.close();
0N/A throw new Error(SERVICES + " not indexed.");
0N/A }
1384N/A
1384N/A public static void main(String[] args) throws IOException {
1384N/A testIndex("a.jar"); // a path with parent == null
1384N/A testIndex("./a.zip"); // a path with parent != null
1384N/A
1384N/A // Try indexing a jar in the default temp directory.
1384N/A File tmpFile = File.createTempFile("MetaInf", null, null);
1384N/A try {
1384N/A testIndex(tmpFile.getPath());
1384N/A } finally {
1384N/A tmpFile.delete();
1384N/A }
1384N/A }
0N/A}