|
1 /* |
|
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
|
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 * |
|
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. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @bug 7175845 |
|
27 * @summary jar -uf should not change file permission |
|
28 */ |
|
29 |
|
30 import java.io.*; |
|
31 import java.nio.file.*; |
|
32 import java.nio.file.attribute.*; |
|
33 import java.util.Set; |
|
34 import sun.tools.jar.Main; |
|
35 |
|
36 public class UpdateJar { |
|
37 |
|
38 private static void cleanup(String... fnames) throws Throwable { |
|
39 for (String fname : fnames) { |
|
40 Files.deleteIfExists(Paths.get(fname)); |
|
41 } |
|
42 } |
|
43 |
|
44 public static void realMain(String[] args) throws Throwable { |
|
45 if (!System.getProperty("os.name").startsWith("Windows")) { |
|
46 String jar = "testUpdateJar.jar"; |
|
47 String e0 = "testUpdateJar_entry0.txt"; |
|
48 String e1 = "testUpdateJar_entry1.txt"; |
|
49 cleanup(jar, e0, e1); |
|
50 try { |
|
51 try (FileOutputStream fos0 = new FileOutputStream(e0); |
|
52 FileOutputStream fos1 = new FileOutputStream(e1)) { |
|
53 fos0.write(0); |
|
54 fos1.write(0); |
|
55 } |
|
56 String[] jarArgs = new String[] {"cfM0", jar, e0}; |
|
57 if (!new Main(System.out, System.err, "jar").run(jarArgs)) { |
|
58 fail("Could not create jar file."); |
|
59 } |
|
60 Set<PosixFilePermission> pm = Files.getPosixFilePermissions(Paths.get(jar)); |
|
61 jarArgs = new String[] {"uf", jar, e1}; |
|
62 if (!new Main(System.out, System.err, "jar").run(jarArgs)) { |
|
63 fail("Could not create jar file."); |
|
64 } |
|
65 equal(pm, Files.getPosixFilePermissions(Paths.get(jar))); |
|
66 } finally { |
|
67 cleanup(jar, e0, e1); |
|
68 } |
|
69 } |
|
70 } |
|
71 |
|
72 //--------------------- Infrastructure --------------------------- |
|
73 static volatile int passed = 0, failed = 0; |
|
74 static void pass() {passed++;} |
|
75 static void fail() {failed++; Thread.dumpStack();} |
|
76 static void fail(String msg) {System.out.println(msg); fail();} |
|
77 static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
78 static void check(boolean cond) {if (cond) pass(); else fail();} |
|
79 static void equal(Object x, Object y) { |
|
80 if (x == null ? y == null : x.equals(y)) pass(); |
|
81 else fail(x + " not equal to " + y);} |
|
82 public static void main(String[] args) throws Throwable { |
|
83 try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
84 System.out.println("\nPassed = " + passed + " failed = " + failed); |
|
85 if (failed > 0) throw new AssertionError("Some tests failed");} |
|
86 } |