jdk/test/tools/jar/UpdateManifest.java
changeset 41484 834b7539ada3
parent 30820 0d4717a011d3
equal deleted inserted replaced
41483:99e81f03a628 41484:834b7539ada3
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 6434207 6442687 6984046
    26  * @bug 6434207 6442687 6984046
    27  * @modules jdk.jartool/sun.tools.jar
    27  * @modules jdk.jartool
    28  * @summary Ensure that jar ufm actually updates the
    28  * @summary Ensure that jar ufm actually updates the
    29  * existing jar file's manifest with contents of the
    29  * existing jar file's manifest with contents of the
    30  * manifest file.
    30  * manifest file.
    31  */
    31  */
    32 
    32 
    33 import java.io.*;
    33 import java.io.*;
    34 import java.util.logging.*;
    34 import java.util.logging.*;
       
    35 import java.util.spi.ToolProvider;
    35 import java.util.zip.*;
    36 import java.util.zip.*;
    36 import sun.tools.jar.Main;
       
    37 
    37 
    38 public class UpdateManifest {
    38 public class UpdateManifest {
    39     static PrintStream out = System.out;
    39     static PrintStream out = System.out;
    40     static PrintStream err = System.err;
    40     static PrintStream err = System.err;
    41     static boolean debug = true;
    41     static boolean debug = true;
       
    42 
       
    43     static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
       
    44         .orElseThrow(() ->
       
    45             new RuntimeException("jar tool not found")
       
    46         );
    42 
    47 
    43     static final Logger JAR_LOGGER = Logger.getLogger("java.util.jar");
    48     static final Logger JAR_LOGGER = Logger.getLogger("java.util.jar");
    44 
    49 
    45     public static void realMain(String[] args) throws Throwable {
    50     public static void realMain(String[] args) throws Throwable {
    46         if (args.length == 0) {
    51         if (args.length == 0) {
    62         File existence = createTextFile("existence");
    67         File existence = createTextFile("existence");
    63 
    68 
    64         // Create a jar file, specifying a Main-Class
    69         // Create a jar file, specifying a Main-Class
    65         final String jarFileName = "um-existence.jar";
    70         final String jarFileName = "um-existence.jar";
    66         new File(jarFileName).delete(); // remove pre-existing first!
    71         new File(jarFileName).delete(); // remove pre-existing first!
    67         Main jartool = new Main(out, err, "jar");
    72         int status = JAR_TOOL.run(out, err, "cfe", jarFileName,
    68         boolean status = jartool.run(
    73                                   "Hello", existence.getPath());
    69             new String[] { "cfe", jarFileName, "Hello", existence.getPath() });
    74         check(status == 0);
    70         check(status);
       
    71         checkManifest(jarFileName, "Hello");
    75         checkManifest(jarFileName, "Hello");
    72 
    76 
    73         // Update that jar file by changing the Main-Class
    77         // Update that jar file by changing the Main-Class
    74         jartool = new Main(out, err, "jar");
    78         status = JAR_TOOL.run(out, err, "ufe", jarFileName, "Bye");
    75         status = jartool.run(
    79         check(status == 0);
    76             new String[] { "ufe", jarFileName, "Bye" });
       
    77         check(status);
       
    78         checkManifest(jarFileName, "Bye");
    80         checkManifest(jarFileName, "Bye");
    79     }
    81     }
    80 
    82 
    81     static void testManifestContents() throws Throwable {
    83     static void testManifestContents() throws Throwable {
    82         // Create some strings we expect to find in the updated manifest
    84         // Create some strings we expect to find in the updated manifest
    99         File hello = createTextFile("hello");
   101         File hello = createTextFile("hello");
   100 
   102 
   101         // Create a jar file
   103         // Create a jar file
   102         final String jarFileName = "um-test.jar";
   104         final String jarFileName = "um-test.jar";
   103         new File(jarFileName).delete(); // remove pre-existing first!
   105         new File(jarFileName).delete(); // remove pre-existing first!
   104         Main jartool = new Main(out, err, "jar");
   106         int status = JAR_TOOL.run(out, err, "cfm", jarFileName,
   105         boolean status = jartool.run(
   107                                   manifestOrig.getPath(), hello.getPath());
   106                                 new String[] {"cfm", jarFileName,
   108         check(status == 0);
   107                                     manifestOrig.getPath(), hello.getPath() });
       
   108         check(status);
       
   109 
   109 
   110         // Create a new manifest, to use in updating the jar file.
   110         // Create a new manifest, to use in updating the jar file.
   111         File manifestUpdate = File.createTempFile("manifestUpdate", ".txt");
   111         File manifestUpdate = File.createTempFile("manifestUpdate", ".txt");
   112         if (!debug) manifestUpdate.deleteOnExit();
   112         if (!debug) manifestUpdate.deleteOnExit();
   113         pw = new PrintWriter(manifestUpdate);
   113         pw = new PrintWriter(manifestUpdate);
   120         pw.println(animal);
   120         pw.println(animal);
   121         pw.println(specVersion); // addition to animal/marsupial section
   121         pw.println(specVersion); // addition to animal/marsupial section
   122         pw.close();
   122         pw.close();
   123 
   123 
   124         // Update jar file with manifest
   124         // Update jar file with manifest
   125         jartool = new Main(out, err, "jar");
   125         status = JAR_TOOL.run(out, err, "ufm",
   126         status = jartool.run(
   126                               jarFileName, manifestUpdate.getPath());
   127             new String[] { "ufm", jarFileName, manifestUpdate.getPath() });
   127         check(status == 0);
   128         check(status);
       
   129 
   128 
   130         // Extract jar, and verify contents of manifest file
   129         // Extract jar, and verify contents of manifest file
   131         File f = new File(jarFileName);
   130         File f = new File(jarFileName);
   132         if (!debug) f.deleteOnExit();
   131         if (!debug) f.deleteOnExit();
   133         ZipFile zf = new ZipFile(f);
   132         ZipFile zf = new ZipFile(f);