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 /** 25 * @test 26 * @bug 6434207 6442687 6984046 27 * @modules jdk.jartool/sun.tools.jar 28 * @summary Ensure that jar ufm actually updates the 29 * existing jar file's manifest with contents of the 30 * manifest file. 31 */ 32 33 import java.io.*; 34 import java.util.logging.*; 35 import java.util.zip.*; 36 import sun.tools.jar.Main; 37 38 public class UpdateManifest { 39 static PrintStream out = System.out; 40 static PrintStream err = System.err; 41 static boolean debug = true; 42 43 static final Logger JAR_LOGGER = Logger.getLogger("java.util.jar"); 44 45 public static void realMain(String[] args) throws Throwable { 46 if (args.length == 0) { 47 debug = false; 48 File tmp = File.createTempFile("system-out-err", ".txt"); 49 tmp.deleteOnExit(); 50 out = new PrintStream(new FileOutputStream(tmp)); 51 err = out; 52 // Attributes.read() can log a message we don't care to see. 53 JAR_LOGGER.setLevel(Level.OFF); 54 } 55 56 try { testManifestExistence(); } catch (Throwable t) { unexpected(t); } 57 try { testManifestContents(); } catch (Throwable t) { unexpected(t); } 58 } 59 60 static void testManifestExistence() throws Throwable { 61 // Create a file to put in a jar file 62 File existence = createTextFile("existence"); 63 64 // Create a jar file, specifying a Main-Class 65 final String jarFileName = "um-existence.jar"; 66 new File(jarFileName).delete(); // remove pre-existing first! 67 Main jartool = new Main(out, err, "jar"); 68 boolean status = jartool.run( 69 new String[] { "cfe", jarFileName, "Hello", existence.getPath() }); 70 check(status); 71 checkManifest(jarFileName, "Hello"); 72 73 // Update that jar file by changing the Main-Class 74 jartool = new Main(out, err, "jar"); 75 status = jartool.run( 76 new String[] { "ufe", jarFileName, "Bye" }); 77 check(status); 78 checkManifest(jarFileName, "Bye"); 79 } 80 81 static void testManifestContents() throws Throwable { 82 // Create some strings we expect to find in the updated manifest 83 final String animal = 84 "Name: animal/marsupial"; 85 final String specTitle = 86 "Specification-Title: Wombat"; 87 88 // Create a text file with manifest entries 89 File manifestOrig = File.createTempFile("manifestOrig", ".txt"); 90 if (!debug) manifestOrig.deleteOnExit(); 91 PrintWriter pw = new PrintWriter(manifestOrig); 92 pw.println("Manifest-Version: 1.0"); 93 pw.println("Created-By: 1.7.0-internal (Oracle Corporation)"); 94 pw.println(""); 95 pw.println(animal); 96 pw.println(specTitle); 97 pw.close(); 98 99 File hello = createTextFile("hello"); 100 101 // Create a jar file 102 final String jarFileName = "um-test.jar"; 103 new File(jarFileName).delete(); // remove pre-existing first! 104 Main jartool = new Main(out, err, "jar"); 105 boolean status = jartool.run( 106 new String[] {"cfm", jarFileName, 107 manifestOrig.getPath(), hello.getPath() }); 108 check(status); 109 110 // Create a new manifest, to use in updating the jar file. 111 File manifestUpdate = File.createTempFile("manifestUpdate", ".txt"); 112 if (!debug) manifestUpdate.deleteOnExit(); 113 pw = new PrintWriter(manifestUpdate); 114 final String createdBy = 115 "Created-By: 1.7.0-special (Oracle Corporation)"; 116 final String specVersion = 117 "Specification-Version: 1.0.0.0"; 118 pw.println(createdBy); // replaces line in the original 119 pw.println(""); 120 pw.println(animal); 121 pw.println(specVersion); // addition to animal/marsupial section 122 pw.close(); 123 124 // Update jar file with manifest 125 jartool = new Main(out, err, "jar"); 126 status = jartool.run( 127 new String[] { "ufm", jarFileName, manifestUpdate.getPath() }); 128 check(status); 129 130 // Extract jar, and verify contents of manifest file 131 File f = new File(jarFileName); 132 if (!debug) f.deleteOnExit(); 133 ZipFile zf = new ZipFile(f); 134 ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF"); 135 BufferedReader r = new BufferedReader( 136 new InputStreamReader(zf.getInputStream(ze))); 137 r.readLine(); // skip Manifest-Version 138 check(r.readLine().equals(createdBy)); 139 r.readLine(); // skip blank line 140 check(r.readLine().equals(animal)); 141 String s = r.readLine(); 142 if (s.equals(specVersion)) { 143 check(r.readLine().equals(specTitle)); 144 } else if (s.equals(specTitle)) { 145 check(r.readLine().equals(specVersion)); 146 } else { 147 fail("did not match specVersion nor specTitle"); 148 } | 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 /** 25 * @test 26 * @bug 6434207 6442687 6984046 27 * @modules jdk.jartool 28 * @summary Ensure that jar ufm actually updates the 29 * existing jar file's manifest with contents of the 30 * manifest file. 31 */ 32 33 import java.io.*; 34 import java.util.logging.*; 35 import java.util.spi.ToolProvider; 36 import java.util.zip.*; 37 38 public class UpdateManifest { 39 static PrintStream out = System.out; 40 static PrintStream err = System.err; 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 ); 47 48 static final Logger JAR_LOGGER = Logger.getLogger("java.util.jar"); 49 50 public static void realMain(String[] args) throws Throwable { 51 if (args.length == 0) { 52 debug = false; 53 File tmp = File.createTempFile("system-out-err", ".txt"); 54 tmp.deleteOnExit(); 55 out = new PrintStream(new FileOutputStream(tmp)); 56 err = out; 57 // Attributes.read() can log a message we don't care to see. 58 JAR_LOGGER.setLevel(Level.OFF); 59 } 60 61 try { testManifestExistence(); } catch (Throwable t) { unexpected(t); } 62 try { testManifestContents(); } catch (Throwable t) { unexpected(t); } 63 } 64 65 static void testManifestExistence() throws Throwable { 66 // Create a file to put in a jar file 67 File existence = createTextFile("existence"); 68 69 // Create a jar file, specifying a Main-Class 70 final String jarFileName = "um-existence.jar"; 71 new File(jarFileName).delete(); // remove pre-existing first! 72 int status = JAR_TOOL.run(out, err, "cfe", jarFileName, 73 "Hello", existence.getPath()); 74 check(status == 0); 75 checkManifest(jarFileName, "Hello"); 76 77 // Update that jar file by changing the Main-Class 78 status = JAR_TOOL.run(out, err, "ufe", jarFileName, "Bye"); 79 check(status == 0); 80 checkManifest(jarFileName, "Bye"); 81 } 82 83 static void testManifestContents() throws Throwable { 84 // Create some strings we expect to find in the updated manifest 85 final String animal = 86 "Name: animal/marsupial"; 87 final String specTitle = 88 "Specification-Title: Wombat"; 89 90 // Create a text file with manifest entries 91 File manifestOrig = File.createTempFile("manifestOrig", ".txt"); 92 if (!debug) manifestOrig.deleteOnExit(); 93 PrintWriter pw = new PrintWriter(manifestOrig); 94 pw.println("Manifest-Version: 1.0"); 95 pw.println("Created-By: 1.7.0-internal (Oracle Corporation)"); 96 pw.println(""); 97 pw.println(animal); 98 pw.println(specTitle); 99 pw.close(); 100 101 File hello = createTextFile("hello"); 102 103 // Create a jar file 104 final String jarFileName = "um-test.jar"; 105 new File(jarFileName).delete(); // remove pre-existing first! 106 int status = JAR_TOOL.run(out, err, "cfm", jarFileName, 107 manifestOrig.getPath(), hello.getPath()); 108 check(status == 0); 109 110 // Create a new manifest, to use in updating the jar file. 111 File manifestUpdate = File.createTempFile("manifestUpdate", ".txt"); 112 if (!debug) manifestUpdate.deleteOnExit(); 113 pw = new PrintWriter(manifestUpdate); 114 final String createdBy = 115 "Created-By: 1.7.0-special (Oracle Corporation)"; 116 final String specVersion = 117 "Specification-Version: 1.0.0.0"; 118 pw.println(createdBy); // replaces line in the original 119 pw.println(""); 120 pw.println(animal); 121 pw.println(specVersion); // addition to animal/marsupial section 122 pw.close(); 123 124 // Update jar file with manifest 125 status = JAR_TOOL.run(out, err, "ufm", 126 jarFileName, manifestUpdate.getPath()); 127 check(status == 0); 128 129 // Extract jar, and verify contents of manifest file 130 File f = new File(jarFileName); 131 if (!debug) f.deleteOnExit(); 132 ZipFile zf = new ZipFile(f); 133 ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF"); 134 BufferedReader r = new BufferedReader( 135 new InputStreamReader(zf.getInputStream(ze))); 136 r.readLine(); // skip Manifest-Version 137 check(r.readLine().equals(createdBy)); 138 r.readLine(); // skip blank line 139 check(r.readLine().equals(animal)); 140 String s = r.readLine(); 141 if (s.equals(specVersion)) { 142 check(r.readLine().equals(specTitle)); 143 } else if (s.equals(specTitle)) { 144 check(r.readLine().equals(specVersion)); 145 } else { 146 fail("did not match specVersion nor specTitle"); 147 } |