提交 5792ae01 编写于 作者: M martin

6854795: Miscellaneous improvements to "jar"

Summary: cleanup of jar/Main.java (Initial patch by tobyr@google.com, additional review by jeremymanson@google.com, ulf.zibis@gmx.de)
Reviewed-by: sherman, alanb
上级 452c37e7
...@@ -23,13 +23,15 @@ ...@@ -23,13 +23,15 @@
/* /*
* @test * @test
* @bug 4408526 * @bug 4408526 6854795
* @summary Index the non-meta files in META-INF, such as META-INF/services. * @summary Index the non-meta files in META-INF, such as META-INF/services.
*/ */
import java.io.*; import java.io.*;
import java.util.Arrays;
import java.util.jar.*; import java.util.jar.*;
import sun.tools.jar.Main; import sun.tools.jar.Main;
import java.util.zip.ZipFile;
public class MetaInf { public class MetaInf {
...@@ -39,29 +41,51 @@ public class MetaInf { ...@@ -39,29 +41,51 @@ public class MetaInf {
static String contents = static String contents =
System.getProperty("test.src") + File.separatorChar + "jarcontents"; System.getProperty("test.src") + File.separatorChar + "jarcontents";
// Options passed to "jar" command. static void run(String ... args) {
static String[] jarArgs1 = new String[] { if (! new Main(System.out, System.err, "jar").run(args))
"cf", jarName, "-C", contents, SERVICES throw new Error("jar failed: args=" + Arrays.toString(args));
}; }
static String[] jarArgs2 = new String[] {
"i", jarName
};
public static void main(String[] args) throws IOException { static void copy(File from, File to) throws IOException {
FileInputStream in = new FileInputStream(from);
FileOutputStream out = new FileOutputStream(to);
try {
byte[] buf = new byte[8192];
int n;
while ((n = in.read(buf)) != -1)
out.write(buf, 0, n);
} finally {
in.close();
out.close();
}
}
// Create a jar to be indexed. static boolean contains(File jarFile, String entryName)
Main jarTool = new Main(System.out, System.err, "jar"); throws IOException {
if (!jarTool.run(jarArgs1)) { return new ZipFile(jarFile).getEntry(entryName) != null;
throw new Error("Could not create jar file."); }
static void checkContains(File jarFile, String entryName)
throws IOException {
if (! contains(jarFile, entryName))
throw new Error(String.format("expected jar %s to contain %s",
jarFile, entryName));
} }
// Index the jar. static void testIndex(String jarName) throws IOException {
jarTool = new Main(System.out, System.err, "jar"); System.err.printf("jarName=%s%n", jarName);
if (!jarTool.run(jarArgs2)) {
throw new Error("Could not index jar file."); File jar = new File(jarName);
// Create a jar to be indexed.
run("cf", jarName, "-C", contents, SERVICES);
for (int i = 0; i < 2; i++) {
run("i", jarName);
checkContains(jar, INDEX);
checkContains(jar, SERVICES);
} }
// Read the index. Verify that META-INF/services is indexed.
JarFile f = new JarFile(jarName); JarFile f = new JarFile(jarName);
BufferedReader index = BufferedReader index =
new BufferedReader( new BufferedReader(
...@@ -75,4 +99,17 @@ public class MetaInf { ...@@ -75,4 +99,17 @@ public class MetaInf {
} }
throw new Error(SERVICES + " not indexed."); throw new Error(SERVICES + " not indexed.");
} }
public static void main(String[] args) throws IOException {
testIndex("a.jar"); // a path with parent == null
testIndex("./a.zip"); // a path with parent != null
// Try indexing a jar in the default temp directory.
File tmpFile = File.createTempFile("MetaInf", null, null);
try {
testIndex(tmpFile.getPath());
} finally {
tmpFile.delete();
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册