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 /* @test
  25  * @bug 8003255
  26  * @compile -XDignore.symbol.file AddAndUpdateProfile.java
  27  * @run main AddAndUpdateProfile
  28  * @summary Basic test of jar tool "p" option to add or update the Profile
  29  *    attribute in the main manifest of a JAR file
  30  */
  31 
  32 import java.util.jar.*;
  33 import static java.util.jar.Attributes.Name.*;
  34 import java.nio.file.*;
  35 import java.io.IOException;
  36 
  37 import sun.tools.jar.Main;
  38 
  39 public class AddAndUpdateProfile {
  40     static boolean doJar(String... args) {
  41         System.out.print("jar");
  42         for (String arg: args)
  43             System.out.print(" " + arg);
  44         System.out.println("");
  45 
  46         Main jartool = new Main(System.out, System.err, "jar");
  47         return jartool.run(args);
  48     }
  49 
  50     static void jar(String... args) {
  51         if (!doJar(args))
  52             throw new RuntimeException("jar command failed");
  53     }
  54 
  55     static void jarExpectingFail(String... args) {
  56         if (doJar(args))
  57             throw new RuntimeException("jar command not expected to succeed");
  58     }
  59 
  60     static void checkMainAttribute(String jarfile, Attributes.Name name,
  61                                    String expectedValue)
  62         throws IOException
  63     {
  64         try (JarFile jf = new JarFile(jarfile)) {
  65             Manifest mf = jf.getManifest();
  66             if (mf == null && expectedValue != null)
  67                 throw new RuntimeException("Manifest not found");
  68             if (mf != null) {
  69                 String actual = mf.getMainAttributes().getValue(name);
  70                 if (actual != null) {
  71                     if (!actual.equals(expectedValue))
  72                         throw new RuntimeException("Profile attribute has unexpected value");
  73                 } else {
  74                     if (expectedValue != null)
  75                         throw new RuntimeException("Profile attribute should not be present");
  76                 }
  77             }
  78         }
  79     }
  80 
  81     public static void main(String[] args) throws Exception {
  82         Path entry = Files.createFile(Paths.get("xfoo"));
  83         String jarfile = "xFoo.jar";
  84         try {
  85 
  86             // create JAR file with Profile attribute
  87             jar("cfp", jarfile, "compact1", entry.toString());
  88             checkMainAttribute(jarfile, PROFILE, "compact1");
  89 
  90             // attempt to create JAR file with Profile attribute and bad value
  91             jarExpectingFail("cfp", jarfile, "garbage", entry.toString());
  92             jarExpectingFail("cfp", jarfile, "Compact1", entry.toString());
  93             jarExpectingFail("cfp", jarfile, "COMPACT1", entry.toString());
  94 
  95             // update value of Profile attribute
  96             jar("ufp", jarfile, "compact2");
  97             checkMainAttribute(jarfile, PROFILE, "compact2");
  98 
  99             // attempt to update value of Profile attribute to bad value
 100             // (update should not change the JAR file)
 101             jarExpectingFail("ufp", jarfile, "garbage");
 102             checkMainAttribute(jarfile, PROFILE, "compact2");
 103             jarExpectingFail("ufp", jarfile, "COMPACT1");
 104             checkMainAttribute(jarfile, PROFILE, "compact2");
 105 
 106             // create JAR file with both a Main-Class and Profile attribute
 107             jar("cfep", jarfile, "Foo", "compact1", entry.toString());
 108             checkMainAttribute(jarfile, MAIN_CLASS, "Foo");
 109             checkMainAttribute(jarfile, PROFILE, "compact1");
 110 
 111             // update value of Profile attribute
 112             jar("ufp", jarfile, "compact2");
 113             checkMainAttribute(jarfile, PROFILE, "compact2");
 114 
 115             // create JAR file without Profile attribute
 116             jar("cf", jarfile, entry.toString());
 117             checkMainAttribute(jarfile, PROFILE, null);
 118 
 119             // update value of Profile attribute
 120             jar("ufp", jarfile, "compact3");
 121             checkMainAttribute(jarfile, PROFILE, "compact3");
 122 
 123         } finally {
 124             Files.deleteIfExists(Paths.get(jarfile));
 125             Files.delete(entry);
 126         }
 127     }
 128 
 129 }