author | duke |
Wed, 05 Jul 2017 21:06:18 +0200 | |
changeset 34465 | 41a1258588da |
parent 30820 | 0d4717a011d3 |
child 41484 | 834b7539ada3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
3218 | 26 |
* @bug 4408526 6854795 |
30820 | 27 |
* @modules jdk.jartool/sun.tools.jar |
2 | 28 |
* @summary Index the non-meta files in META-INF, such as META-INF/services. |
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
3218 | 32 |
import java.util.Arrays; |
2 | 33 |
import java.util.jar.*; |
34 |
import sun.tools.jar.Main; |
|
3218 | 35 |
import java.util.zip.ZipFile; |
2 | 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 |
||
3218 | 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 |
} |
|
2 | 49 |
|
3218 | 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 { |
|
5813
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
66 |
ZipFile zf = new ZipFile(jarFile); |
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
67 |
if ( zf != null ) { |
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
68 |
boolean result = zf.getEntry(entryName) != null; |
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
69 |
zf.close(); |
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
70 |
return result; |
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
71 |
} |
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
72 |
return false; |
3218 | 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); |
|
2 | 86 |
|
87 |
// Create a jar to be indexed. |
|
3218 | 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); |
|
2 | 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)) { |
|
5813
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
104 |
index.close(); |
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
105 |
f.close(); |
2 | 106 |
return; |
107 |
} |
|
108 |
} |
|
5813
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
109 |
index.close(); |
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
110 |
f.close(); |
2 | 111 |
throw new Error(SERVICES + " not indexed."); |
112 |
} |
|
3218 | 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 |
} |
|
2 | 126 |
} |