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 void jar(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 if (!jartool.run(args)) 48 throw new RuntimeException("jar command failed"); 49 } 50 51 static void checkMainAttribute(String jarfile, Attributes.Name name, 52 String expectedValue) 53 throws IOException 54 { 55 try (JarFile jf = new JarFile(jarfile)) { 56 Manifest mf = jf.getManifest(); 57 if (mf == null && expectedValue != null) 58 throw new RuntimeException("Manifest not found"); 59 if (mf != null) { 60 String actual = mf.getMainAttributes().getValue(name); 61 if (actual != null) { 62 if (!actual.equals(expectedValue)) 63 throw new RuntimeException("Profile attribute has unexpected value"); 64 } else { 65 if (expectedValue != null) 66 throw new RuntimeException("Profile attribute should not be present"); 67 } 68 } 69 } 70 } 71 72 public static void main(String[] args) throws Exception { 73 Path entry = Files.createFile(Paths.get("xfoo")); 74 String jarfile = "xFoo.jar"; 75 try { 76 77 // create JAR file with Profile attribute 78 jar("cfp", jarfile, "compact1", entry.toString()); 79 checkMainAttribute(jarfile, PROFILE, "compact1"); 80 81 // update value of Profile attribute 82 jar("ufp", jarfile, "compact2"); 83 checkMainAttribute(jarfile, PROFILE, "compact2"); 84 85 // create JAR file with both a Main-Class and Profile attribute 86 jar("cfep", jarfile, "Foo", "compact1", entry.toString()); 87 checkMainAttribute(jarfile, MAIN_CLASS, "Foo"); 88 checkMainAttribute(jarfile, PROFILE, "compact1"); 89 90 // update value of Profile attribute 91 jar("ufp", jarfile, "compact2"); 92 checkMainAttribute(jarfile, PROFILE, "compact2"); 93 94 // create JAR file without Profile attribute 95 jar("cf", jarfile, entry.toString()); 96 checkMainAttribute(jarfile, PROFILE, null); 97 98 // update value of Profile attribute 99 jar("ufp", jarfile, "compact3"); 100 checkMainAttribute(jarfile, PROFILE, "compact3"); 101 102 } finally { 103 Files.deleteIfExists(Paths.get(jarfile)); 104 Files.delete(entry); 105 } 106 } 107 108 }