author | bpb |
Fri, 06 Feb 2015 08:05:44 -0800 | |
changeset 28847 | 5667388e3a79 |
parent 22261 | e82547d2e78a |
child 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
6883
bbd0cba11983
6984046: java/jar jar/pack source needs vendor rebranding changes (jdk7 only)
sherman
parents:
5813
diff
changeset
|
2 |
* Copyright (c) 2006, 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 |
|
6883
bbd0cba11983
6984046: java/jar jar/pack source needs vendor rebranding changes (jdk7 only)
sherman
parents:
5813
diff
changeset
|
26 |
* @bug 6434207 6442687 6984046 |
2 | 27 |
* @summary Ensure that jar ufm actually updates the |
28 |
* existing jar file's manifest with contents of the |
|
29 |
* manifest file. |
|
30 |
*/ |
|
31 |
||
32 |
import java.io.*; |
|
33 |
import java.util.logging.*; |
|
34 |
import java.util.zip.*; |
|
35 |
import sun.tools.jar.Main; |
|
36 |
||
37 |
public class UpdateManifest { |
|
38 |
static PrintStream out = System.out; |
|
39 |
static PrintStream err = System.err; |
|
40 |
static boolean debug = true; |
|
41 |
||
22261
e82547d2e78a
8031525: Logger created in test/tools/jar/UpdateManifest.java might get gc'ed too early.
dfuchs
parents:
6883
diff
changeset
|
42 |
static final Logger JAR_LOGGER = Logger.getLogger("java.util.jar"); |
e82547d2e78a
8031525: Logger created in test/tools/jar/UpdateManifest.java might get gc'ed too early.
dfuchs
parents:
6883
diff
changeset
|
43 |
|
2 | 44 |
public static void realMain(String[] args) throws Throwable { |
45 |
if (args.length == 0) { |
|
46 |
debug = false; |
|
47 |
File tmp = File.createTempFile("system-out-err", ".txt"); |
|
48 |
tmp.deleteOnExit(); |
|
49 |
out = new PrintStream(new FileOutputStream(tmp)); |
|
50 |
err = out; |
|
51 |
// Attributes.read() can log a message we don't care to see. |
|
22261
e82547d2e78a
8031525: Logger created in test/tools/jar/UpdateManifest.java might get gc'ed too early.
dfuchs
parents:
6883
diff
changeset
|
52 |
JAR_LOGGER.setLevel(Level.OFF); |
2 | 53 |
} |
54 |
||
55 |
try { testManifestExistence(); } catch (Throwable t) { unexpected(t); } |
|
56 |
try { testManifestContents(); } catch (Throwable t) { unexpected(t); } |
|
57 |
} |
|
58 |
||
59 |
static void testManifestExistence() throws Throwable { |
|
60 |
// Create a file to put in a jar file |
|
61 |
File existence = createTextFile("existence"); |
|
62 |
||
63 |
// Create a jar file, specifying a Main-Class |
|
64 |
final String jarFileName = "um-existence.jar"; |
|
65 |
new File(jarFileName).delete(); // remove pre-existing first! |
|
66 |
Main jartool = new Main(out, err, "jar"); |
|
67 |
boolean status = jartool.run( |
|
68 |
new String[] { "cfe", jarFileName, "Hello", existence.getPath() }); |
|
69 |
check(status); |
|
70 |
checkManifest(jarFileName, "Hello"); |
|
71 |
||
72 |
// Update that jar file by changing the Main-Class |
|
73 |
jartool = new Main(out, err, "jar"); |
|
74 |
status = jartool.run( |
|
75 |
new String[] { "ufe", jarFileName, "Bye" }); |
|
76 |
check(status); |
|
77 |
checkManifest(jarFileName, "Bye"); |
|
78 |
} |
|
79 |
||
80 |
static void testManifestContents() throws Throwable { |
|
81 |
// Create some strings we expect to find in the updated manifest |
|
82 |
final String animal = |
|
83 |
"Name: animal/marsupial"; |
|
84 |
final String specTitle = |
|
85 |
"Specification-Title: Wombat"; |
|
86 |
||
87 |
// Create a text file with manifest entries |
|
88 |
File manifestOrig = File.createTempFile("manifestOrig", ".txt"); |
|
89 |
if (!debug) manifestOrig.deleteOnExit(); |
|
90 |
PrintWriter pw = new PrintWriter(manifestOrig); |
|
91 |
pw.println("Manifest-Version: 1.0"); |
|
6883
bbd0cba11983
6984046: java/jar jar/pack source needs vendor rebranding changes (jdk7 only)
sherman
parents:
5813
diff
changeset
|
92 |
pw.println("Created-By: 1.7.0-internal (Oracle Corporation)"); |
2 | 93 |
pw.println(""); |
94 |
pw.println(animal); |
|
95 |
pw.println(specTitle); |
|
96 |
pw.close(); |
|
97 |
||
98 |
File hello = createTextFile("hello"); |
|
99 |
||
100 |
// Create a jar file |
|
101 |
final String jarFileName = "um-test.jar"; |
|
102 |
new File(jarFileName).delete(); // remove pre-existing first! |
|
103 |
Main jartool = new Main(out, err, "jar"); |
|
104 |
boolean status = jartool.run( |
|
105 |
new String[] {"cfm", jarFileName, |
|
106 |
manifestOrig.getPath(), hello.getPath() }); |
|
107 |
check(status); |
|
108 |
||
109 |
// Create a new manifest, to use in updating the jar file. |
|
110 |
File manifestUpdate = File.createTempFile("manifestUpdate", ".txt"); |
|
111 |
if (!debug) manifestUpdate.deleteOnExit(); |
|
112 |
pw = new PrintWriter(manifestUpdate); |
|
113 |
final String createdBy = |
|
6883
bbd0cba11983
6984046: java/jar jar/pack source needs vendor rebranding changes (jdk7 only)
sherman
parents:
5813
diff
changeset
|
114 |
"Created-By: 1.7.0-special (Oracle Corporation)"; |
2 | 115 |
final String specVersion = |
116 |
"Specification-Version: 1.0.0.0"; |
|
117 |
pw.println(createdBy); // replaces line in the original |
|
118 |
pw.println(""); |
|
119 |
pw.println(animal); |
|
120 |
pw.println(specVersion); // addition to animal/marsupial section |
|
121 |
pw.close(); |
|
122 |
||
123 |
// Update jar file with manifest |
|
124 |
jartool = new Main(out, err, "jar"); |
|
125 |
status = jartool.run( |
|
126 |
new String[] { "ufm", jarFileName, manifestUpdate.getPath() }); |
|
127 |
check(status); |
|
128 |
||
129 |
// Extract jar, and verify contents of manifest file |
|
130 |
File f = new File(jarFileName); |
|
131 |
if (!debug) f.deleteOnExit(); |
|
132 |
ZipFile zf = new ZipFile(f); |
|
133 |
ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF"); |
|
134 |
BufferedReader r = new BufferedReader( |
|
135 |
new InputStreamReader(zf.getInputStream(ze))); |
|
136 |
r.readLine(); // skip Manifest-Version |
|
137 |
check(r.readLine().equals(createdBy)); |
|
138 |
r.readLine(); // skip blank line |
|
139 |
check(r.readLine().equals(animal)); |
|
140 |
String s = r.readLine(); |
|
141 |
if (s.equals(specVersion)) { |
|
142 |
check(r.readLine().equals(specTitle)); |
|
143 |
} else if (s.equals(specTitle)) { |
|
144 |
check(r.readLine().equals(specVersion)); |
|
145 |
} else { |
|
146 |
fail("did not match specVersion nor specTitle"); |
|
147 |
} |
|
5813
8e455cd3e807
6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents:
5506
diff
changeset
|
148 |
zf.close(); |
2 | 149 |
} |
150 |
||
151 |
// --------------------- Convenience --------------------------- |
|
152 |
||
153 |
static File createTextFile(String name) throws Throwable { |
|
154 |
// Create a text file to put in a jar file |
|
155 |
File rc = File.createTempFile(name, ".txt"); |
|
156 |
if (!debug) rc.deleteOnExit(); |
|
157 |
PrintWriter pw = new PrintWriter(rc); |
|
158 |
pw.println("hello, world"); |
|
159 |
pw.close(); |
|
160 |
return rc; |
|
161 |
} |
|
162 |
||
163 |
static void checkManifest(String jarFileName, String mainClass) |
|
164 |
throws Throwable { |
|
165 |
File f = new File(jarFileName); |
|
166 |
if (!debug) f.deleteOnExit(); |
|
167 |
ZipFile zf = new ZipFile(f); |
|
168 |
ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF"); |
|
169 |
BufferedReader r = new BufferedReader( |
|
170 |
new InputStreamReader(zf.getInputStream(ze))); |
|
171 |
String line = r.readLine(); |
|
172 |
while (line != null && !(line.startsWith("Main-Class:"))) { |
|
173 |
line = r.readLine(); |
|
174 |
} |
|
175 |
if (line == null) { |
|
176 |
fail("Didn't find Main-Class in manifest"); |
|
177 |
} else { |
|
178 |
check(line.equals("Main-Class: " + mainClass)); |
|
179 |
} |
|
180 |
zf.close(); |
|
181 |
} |
|
182 |
||
183 |
// --------------------- Infrastructure --------------------------- |
|
184 |
||
185 |
static volatile int passed = 0, failed = 0; |
|
186 |
||
187 |
static void pass() { |
|
188 |
passed++; |
|
189 |
} |
|
190 |
||
191 |
static void fail() { |
|
192 |
failed++; |
|
193 |
Thread.dumpStack(); |
|
194 |
} |
|
195 |
||
196 |
static void fail(String msg) { |
|
197 |
System.out.println(msg); |
|
198 |
fail(); |
|
199 |
} |
|
200 |
||
201 |
static void unexpected(Throwable t) { |
|
202 |
failed++; |
|
203 |
t.printStackTrace(); |
|
204 |
} |
|
205 |
||
206 |
static void check(boolean cond) { |
|
207 |
if (cond) |
|
208 |
pass(); |
|
209 |
else |
|
210 |
fail(); |
|
211 |
} |
|
212 |
||
213 |
static void equal(Object x, Object y) { |
|
214 |
if ((x == null) ? (y == null) : x.equals(y)) |
|
215 |
pass(); |
|
216 |
else |
|
217 |
fail(x + " not equal to " + y); |
|
218 |
} |
|
219 |
||
220 |
public static void main(String[] args) throws Throwable { |
|
221 |
try { |
|
222 |
realMain(args); |
|
223 |
} catch (Throwable t) { |
|
224 |
unexpected(t); |
|
225 |
} |
|
226 |
System.out.println("\nPassed = " + passed + " failed = " + failed); |
|
227 |
if (failed > 0) |
|
228 |
throw new AssertionError("Some tests failed"); |
|
229 |
} |
|
230 |
} |