jdk/test/tools/jar/index/MetaInf.java
changeset 3218 855a6257b729
parent 2 90ce3da70b43
child 3288 db82a42da273
equal deleted inserted replaced
3217:07b65d4b6227 3218:855a6257b729
    21  * have any questions.
    21  * have any questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 4408526
    26  * @bug 4408526 6854795
    27  * @summary Index the non-meta files in META-INF, such as META-INF/services.
    27  * @summary Index the non-meta files in META-INF, such as META-INF/services.
    28  */
    28  */
    29 
    29 
    30 import java.io.*;
    30 import java.io.*;
       
    31 import java.util.Arrays;
    31 import java.util.jar.*;
    32 import java.util.jar.*;
    32 import sun.tools.jar.Main;
    33 import sun.tools.jar.Main;
       
    34 import java.util.zip.ZipFile;
    33 
    35 
    34 public class MetaInf {
    36 public class MetaInf {
    35 
    37 
    36     static String jarName = "a.jar";
    38     static String jarName = "a.jar";
    37     static String INDEX = "META-INF/INDEX.LIST";
    39     static String INDEX = "META-INF/INDEX.LIST";
    38     static String SERVICES = "META-INF/services";
    40     static String SERVICES = "META-INF/services";
    39     static String contents =
    41     static String contents =
    40         System.getProperty("test.src") + File.separatorChar + "jarcontents";
    42         System.getProperty("test.src") + File.separatorChar + "jarcontents";
    41 
    43 
    42     // Options passed to "jar" command.
    44     static void run(String ... args) {
    43     static String[] jarArgs1 = new String[] {
    45         if (! new Main(System.out, System.err, "jar").run(args))
    44         "cf", jarName, "-C", contents, SERVICES
    46             throw new Error("jar failed: args=" + Arrays.toString(args));
    45     };
    47     }
    46     static String[] jarArgs2 = new String[] {
       
    47         "i", jarName
       
    48     };
       
    49 
    48 
    50     public static void main(String[] args) throws IOException {
    49     static void copy(File from, File to) throws IOException {
       
    50         FileInputStream in = new FileInputStream(from);
       
    51         FileOutputStream out = new FileOutputStream(to);
       
    52         try {
       
    53             byte[] buf = new byte[8192];
       
    54             int n;
       
    55             while ((n = in.read(buf)) != -1)
       
    56                 out.write(buf, 0, n);
       
    57         } finally {
       
    58             in.close();
       
    59             out.close();
       
    60         }
       
    61     }
       
    62 
       
    63     static boolean contains(File jarFile, String entryName)
       
    64         throws IOException {
       
    65         return new ZipFile(jarFile).getEntry(entryName) != null;
       
    66     }
       
    67 
       
    68     static void checkContains(File jarFile, String entryName)
       
    69         throws IOException {
       
    70         if (! contains(jarFile, entryName))
       
    71             throw new Error(String.format("expected jar %s to contain %s",
       
    72                                           jarFile, entryName));
       
    73     }
       
    74 
       
    75     static void testIndex(String jarName) throws IOException {
       
    76         System.err.printf("jarName=%s%n", jarName);
       
    77 
       
    78         File jar = new File(jarName);
    51 
    79 
    52         // Create a jar to be indexed.
    80         // Create a jar to be indexed.
    53         Main jarTool = new Main(System.out, System.err, "jar");
    81         run("cf", jarName, "-C", contents, SERVICES);
    54         if (!jarTool.run(jarArgs1)) {
    82 
    55             throw new Error("Could not create jar file.");
    83         for (int i = 0; i < 2; i++) {
       
    84             run("i", jarName);
       
    85             checkContains(jar, INDEX);
       
    86             checkContains(jar, SERVICES);
    56         }
    87         }
    57 
    88 
    58         // Index the jar.
       
    59         jarTool = new Main(System.out, System.err, "jar");
       
    60         if (!jarTool.run(jarArgs2)) {
       
    61             throw new Error("Could not index jar file.");
       
    62         }
       
    63 
       
    64         // Read the index.  Verify that META-INF/services is indexed.
       
    65         JarFile f = new JarFile(jarName);
    89         JarFile f = new JarFile(jarName);
    66         BufferedReader index =
    90         BufferedReader index =
    67             new BufferedReader(
    91             new BufferedReader(
    68                     new InputStreamReader(
    92                     new InputStreamReader(
    69                             f.getInputStream(f.getJarEntry(INDEX))));
    93                             f.getInputStream(f.getJarEntry(INDEX))));
    73                 return;
    97                 return;
    74             }
    98             }
    75         }
    99         }
    76         throw new Error(SERVICES + " not indexed.");
   100         throw new Error(SERVICES + " not indexed.");
    77     }
   101     }
       
   102 
       
   103     public static void main(String[] args) throws IOException {
       
   104         testIndex("a.jar");             // a path with parent == null
       
   105         testIndex("./a.zip");           // a path with parent != null
       
   106 
       
   107         // Try indexing a jar in the default temp directory.
       
   108         File tmpFile = File.createTempFile("MetaInf", null, null);
       
   109         try {
       
   110             testIndex(tmpFile.getPath());
       
   111         } finally {
       
   112             tmpFile.delete();
       
   113         }
       
   114     }
    78 }
   115 }