1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4408526 6854795
  27  * @modules jdk.jartool/sun.tools.jar
  28  * @summary Index the non-meta files in META-INF, such as META-INF/services.
  29  */
  30 
  31 import java.io.*;
  32 import java.util.Arrays;
  33 import java.util.jar.*;
  34 import sun.tools.jar.Main;
  35 import java.util.zip.ZipFile;
  36 
  37 public class MetaInf {
  38 
  39     static String jarName = "a.jar";
  40     static String INDEX = "META-INF/INDEX.LIST";
  41     static String SERVICES = "META-INF/services";
  42     static String contents =
  43         System.getProperty("test.src") + File.separatorChar + "jarcontents";
  44 
  45     static void run(String ... args) {
  46         if (! new Main(System.out, System.err, "jar").run(args))
  47             throw new Error("jar failed: args=" + Arrays.toString(args));
  48     }
  49 
  50     static void copy(File from, File to) throws IOException {
  51         FileInputStream in = new FileInputStream(from);
  52         FileOutputStream out = new FileOutputStream(to);
  53         try {
  54             byte[] buf = new byte[8192];
  55             int n;
  56             while ((n = in.read(buf)) != -1)
  57                 out.write(buf, 0, n);
  58         } finally {
  59             in.close();
  60             out.close();
  61         }
  62     }
  63 
  64     static boolean contains(File jarFile, String entryName)
  65         throws IOException {
  66         ZipFile zf = new ZipFile(jarFile);
  67         if ( zf != null ) {
  68             boolean result = zf.getEntry(entryName) != null;
  69             zf.close();
  70             return result;
  71         }
  72         return false;
  73     }
  74 
  75     static void checkContains(File jarFile, String entryName)
  76         throws IOException {
  77         if (! contains(jarFile, entryName))
  78             throw new Error(String.format("expected jar %s to contain %s",
  79                                           jarFile, entryName));
  80     }
  81 
  82     static void testIndex(String jarName) throws IOException {
  83         System.err.printf("jarName=%s%n", jarName);
  84 
  85         File jar = new File(jarName);
  86 
  87         // Create a jar to be indexed.
  88         run("cf", jarName, "-C", contents, SERVICES);
  89 
  90         for (int i = 0; i < 2; i++) {
  91             run("i", jarName);
  92             checkContains(jar, INDEX);
  93             checkContains(jar, SERVICES);
  94         }
  95 
  96         JarFile f = new JarFile(jarName);
  97         BufferedReader index =
  98             new BufferedReader(
  99                     new InputStreamReader(
 100                             f.getInputStream(f.getJarEntry(INDEX))));
 101         String line;
 102         while ((line = index.readLine()) != null) {
 103             if (line.equals(SERVICES)) {
 104                 index.close();
 105                 f.close();
 106                 return;
 107             }
 108         }
 109         index.close();
 110         f.close();
 111         throw new Error(SERVICES + " not indexed.");
 112     }
 113 
 114     public static void main(String[] args) throws IOException {
 115         testIndex("a.jar");             // a path with parent == null
 116         testIndex("./a.zip");           // a path with parent != null
 117 
 118         // Try indexing a jar in the default temp directory.
 119         File tmpFile = File.createTempFile("MetaInf", null, null);
 120         try {
 121             testIndex(tmpFile.getPath());
 122         } finally {
 123             tmpFile.delete();
 124         }
 125     }
 126 }