jdk/test/tools/jar/UpdateJar.java
author duke
Wed, 05 Jul 2017 19:05:51 +0200
changeset 19004 72faf3342435
parent 13154 9e8a04d28ded
child 30820 0d4717a011d3
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13154
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     1
/*
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     4
 *
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     8
 *
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    13
 * accompanied this code).
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    14
 *
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    18
 *
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    21
 * questions.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    22
 */
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    23
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    24
/**
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    25
 * @test
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    26
 * @bug 7175845
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    27
 * @summary jar -uf should not change file permission
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    28
 */
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    29
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    30
import java.io.*;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    31
import java.nio.file.*;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    32
import java.nio.file.attribute.*;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    33
import java.util.Set;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    34
import sun.tools.jar.Main;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    35
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    36
public class UpdateJar {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    37
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    38
    private static void cleanup(String... fnames) throws Throwable {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    39
        for (String fname : fnames) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    40
            Files.deleteIfExists(Paths.get(fname));
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    41
        }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    42
    }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    43
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    44
    public static void realMain(String[] args) throws Throwable {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    45
        if (!System.getProperty("os.name").startsWith("Windows")) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    46
            String jar = "testUpdateJar.jar";
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    47
            String e0  = "testUpdateJar_entry0.txt";
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    48
            String e1  = "testUpdateJar_entry1.txt";
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    49
            cleanup(jar, e0, e1);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    50
            try {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    51
                try (FileOutputStream fos0 = new FileOutputStream(e0);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    52
                     FileOutputStream fos1 = new FileOutputStream(e1)) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    53
                    fos0.write(0);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    54
                    fos1.write(0);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    55
                }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    56
                String[] jarArgs = new String[] {"cfM0", jar, e0};
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    57
                if (!new Main(System.out, System.err, "jar").run(jarArgs)) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    58
                    fail("Could not create jar file.");
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    59
                }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    60
                Set<PosixFilePermission> pm = Files.getPosixFilePermissions(Paths.get(jar));
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    61
                jarArgs = new String[] {"uf", jar, e1};
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    62
                if (!new Main(System.out, System.err, "jar").run(jarArgs)) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    63
                    fail("Could not create jar file.");
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    64
                }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    65
                equal(pm, Files.getPosixFilePermissions(Paths.get(jar)));
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    66
            } finally {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    67
                cleanup(jar, e0, e1);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    68
            }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    69
        }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    70
    }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    71
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    72
    //--------------------- Infrastructure ---------------------------
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    73
    static volatile int passed = 0, failed = 0;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    74
    static void pass() {passed++;}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    75
    static void fail() {failed++; Thread.dumpStack();}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    76
    static void fail(String msg) {System.out.println(msg); fail();}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    77
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    78
    static void check(boolean cond) {if (cond) pass(); else fail();}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    79
    static void equal(Object x, Object y) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    80
        if (x == null ? y == null : x.equals(y)) pass();
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    81
        else fail(x + " not equal to " + y);}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    82
    public static void main(String[] args) throws Throwable {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    83
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    84
        System.out.println("\nPassed = " + passed + " failed = " + failed);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    85
        if (failed > 0) throw new AssertionError("Some tests failed");}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    86
}