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)))); |