source: trunk/openjdk/jdk/test/tools/jar/UpdateJar.java

Last change on this file was 406, checked in by dmik, 13 years ago

trunk: Merged in openjdk6 b27 from branches/vendor/oracle.

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
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 7199153
27 * @summary jar -uf should not change file permission
28 */
29
30import java.io.*;
31import sun.tools.jar.Main;
32
33public class UpdateJar {
34
35 private static void cleanup(File f) throws Throwable {
36 if (f.exists())
37 f.delete();
38 }
39
40 private static String permission(String fname) throws Throwable {
41 Process p = Runtime.getRuntime().exec("ls -l " + fname);
42 InputStream is = null;
43 String ret = null;
44 if (p != null && (is = p.getInputStream()) != null) {
45 p.waitFor();
46 BufferedReader br = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"));
47 try {
48 ret = br.readLine();
49 if (ret != null)
50 ret = ret.split(" ")[0];
51 } finally {
52 br.close();
53 }
54 }
55 return ret;
56 }
57
58 public static void realMain(String[] args) throws Throwable {
59 if (!System.getProperty("os.name").startsWith("Windows")) {
60 String jarName = "testUpdateJar.jar";
61 String e0Name = "testUpdateJar_entry0.txt";
62 String e1Name = "testUpdateJar_entry1.txt";
63
64 File jar = new File(jarName);
65 File e0 = new File(e0Name);
66 File e1 = new File(e1Name);
67 try {
68 cleanup(jar);
69 cleanup(e0);
70 cleanup(e1);
71
72 FileOutputStream fos = new FileOutputStream(e0Name);
73 fos.write(0);
74 fos.close();
75
76 fos = new FileOutputStream(e1Name);
77 fos.write(0);
78 fos.close();
79
80 String[] jarArgs = new String[] {"cfM0", jarName, e0Name};
81 if (!new Main(System.out, System.err, "jar").run(jarArgs)) {
82 fail("Could not create jar file.");
83 }
84 String pm = permission(jarName);
85 jarArgs = new String[] {"uf", jarName, e1Name};
86 if (!new Main(System.out, System.err, "jar").run(jarArgs)) {
87 fail("Could not create jar file.");
88 }
89 equal(pm, permission(jarName));
90
91 } finally {
92 cleanup(jar);
93 cleanup(e0);
94 cleanup(e1);
95
96 }
97 }
98 }
99
100 //--------------------- Infrastructure ---------------------------
101 static volatile int passed = 0, failed = 0;
102 static void pass() {passed++;}
103 static void fail() {failed++; Thread.dumpStack();}
104 static void fail(String msg) {System.out.println(msg); fail();}
105 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
106 static void check(boolean cond) {if (cond) pass(); else fail();}
107 static void equal(Object x, Object y) {
108 if (x == null ? y == null : x.equals(y)) pass();
109 else fail(x + " not equal to " + y);}
110 public static void main(String[] args) throws Throwable {
111 try {realMain(args);} catch (Throwable t) {unexpected(t);}
112 System.out.println("\nPassed = " + passed + " failed = " + failed);
113 if (failed > 0) throw new AssertionError("Some tests failed");}
114}
Note: See TracBrowser for help on using the repository browser.