jdk/test/tools/jar/AddAndUpdateProfile.java
author duke
Wed, 05 Jul 2017 19:05:51 +0200
changeset 19004 72faf3342435
parent 15682 793a36de151d
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15682
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     1
/*
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     4
 *
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     8
 *
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    13
 * accompanied this code).
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    14
 *
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    18
 *
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    21
 * questions.
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    22
 */
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    23
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    24
/* @test
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    25
 * @bug 8003255
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    26
 * @compile -XDignore.symbol.file AddAndUpdateProfile.java
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    27
 * @run main AddAndUpdateProfile
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    28
 * @summary Basic test of jar tool "p" option to add or update the Profile
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    29
 *    attribute in the main manifest of a JAR file
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    30
 */
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    31
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    32
import java.util.jar.*;
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    33
import static java.util.jar.Attributes.Name.*;
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    34
import java.nio.file.*;
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    35
import java.io.IOException;
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    36
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    37
import sun.tools.jar.Main;
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    38
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    39
public class AddAndUpdateProfile {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    40
    static boolean doJar(String... args) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    41
        System.out.print("jar");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    42
        for (String arg: args)
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    43
            System.out.print(" " + arg);
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    44
        System.out.println("");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    45
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    46
        Main jartool = new Main(System.out, System.err, "jar");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    47
        return jartool.run(args);
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    48
    }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    49
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    50
    static void jar(String... args) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    51
        if (!doJar(args))
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    52
            throw new RuntimeException("jar command failed");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    53
    }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    54
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    55
    static void jarExpectingFail(String... args) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    56
        if (doJar(args))
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    57
            throw new RuntimeException("jar command not expected to succeed");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    58
    }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    59
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    60
    static void checkMainAttribute(String jarfile, Attributes.Name name,
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    61
                                   String expectedValue)
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    62
        throws IOException
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    63
    {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    64
        try (JarFile jf = new JarFile(jarfile)) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    65
            Manifest mf = jf.getManifest();
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    66
            if (mf == null && expectedValue != null)
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    67
                throw new RuntimeException("Manifest not found");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    68
            if (mf != null) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    69
                String actual = mf.getMainAttributes().getValue(name);
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    70
                if (actual != null) {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    71
                    if (!actual.equals(expectedValue))
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    72
                        throw new RuntimeException("Profile attribute has unexpected value");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    73
                } else {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    74
                    if (expectedValue != null)
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    75
                        throw new RuntimeException("Profile attribute should not be present");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    76
                }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    77
            }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    78
        }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    79
    }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    80
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    81
    public static void main(String[] args) throws Exception {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    82
        Path entry = Files.createFile(Paths.get("xfoo"));
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    83
        String jarfile = "xFoo.jar";
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    84
        try {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    85
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    86
            // create JAR file with Profile attribute
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    87
            jar("cfp", jarfile, "compact1", entry.toString());
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    88
            checkMainAttribute(jarfile, PROFILE, "compact1");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    89
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    90
            // attempt to create JAR file with Profile attribute and bad value
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    91
            jarExpectingFail("cfp", jarfile, "garbage", entry.toString());
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    92
            jarExpectingFail("cfp", jarfile, "Compact1", entry.toString());
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    93
            jarExpectingFail("cfp", jarfile, "COMPACT1", entry.toString());
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    94
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    95
            // update value of Profile attribute
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    96
            jar("ufp", jarfile, "compact2");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    97
            checkMainAttribute(jarfile, PROFILE, "compact2");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    98
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
    99
            // attempt to update value of Profile attribute to bad value
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   100
            // (update should not change the JAR file)
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   101
            jarExpectingFail("ufp", jarfile, "garbage");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   102
            checkMainAttribute(jarfile, PROFILE, "compact2");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   103
            jarExpectingFail("ufp", jarfile, "COMPACT1");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   104
            checkMainAttribute(jarfile, PROFILE, "compact2");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   105
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   106
            // create JAR file with both a Main-Class and Profile attribute
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   107
            jar("cfep", jarfile, "Foo", "compact1", entry.toString());
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   108
            checkMainAttribute(jarfile, MAIN_CLASS, "Foo");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   109
            checkMainAttribute(jarfile, PROFILE, "compact1");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   110
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   111
            // update value of Profile attribute
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   112
            jar("ufp", jarfile, "compact2");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   113
            checkMainAttribute(jarfile, PROFILE, "compact2");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   114
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   115
            // create JAR file without Profile attribute
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   116
            jar("cf", jarfile, entry.toString());
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   117
            checkMainAttribute(jarfile, PROFILE, null);
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   118
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   119
            // update value of Profile attribute
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   120
            jar("ufp", jarfile, "compact3");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   121
            checkMainAttribute(jarfile, PROFILE, "compact3");
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   122
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   123
        } finally {
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   124
            Files.deleteIfExists(Paths.get(jarfile));
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   125
            Files.delete(entry);
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   126
        }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   127
    }
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   128
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
diff changeset
   129
}