Path: blob/master/test/jdk/tools/jar/UpdateManifest.java
41144 views
/*1* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 6434207 6442687 698404626* @modules jdk.jartool27* @summary Ensure that jar ufm actually updates the28* existing jar file's manifest with contents of the29* manifest file.30*/3132import java.io.*;33import java.util.logging.*;34import java.util.spi.ToolProvider;35import java.util.zip.*;3637public class UpdateManifest {38static PrintStream out = System.out;39static PrintStream err = System.err;40static boolean debug = true;4142static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")43.orElseThrow(() ->44new RuntimeException("jar tool not found")45);4647static final Logger JAR_LOGGER = Logger.getLogger("java.util.jar");4849public static void realMain(String[] args) throws Throwable {50if (args.length == 0) {51debug = false;52File tmp = File.createTempFile("system-out-err", ".txt");53tmp.deleteOnExit();54out = new PrintStream(new FileOutputStream(tmp));55err = out;56// Attributes.read() can log a message we don't care to see.57JAR_LOGGER.setLevel(Level.OFF);58}5960try { testManifestExistence(); } catch (Throwable t) { unexpected(t); }61try { testManifestContents(); } catch (Throwable t) { unexpected(t); }62}6364static void testManifestExistence() throws Throwable {65// Create a file to put in a jar file66File existence = createTextFile("existence");6768// Create a jar file, specifying a Main-Class69final String jarFileName = "um-existence.jar";70new File(jarFileName).delete(); // remove pre-existing first!71int status = JAR_TOOL.run(out, err, "cfe", jarFileName,72"Hello", existence.getPath());73check(status == 0);74checkManifest(jarFileName, "Hello");7576// Update that jar file by changing the Main-Class77status = JAR_TOOL.run(out, err, "ufe", jarFileName, "Bye");78check(status == 0);79checkManifest(jarFileName, "Bye");80}8182static void testManifestContents() throws Throwable {83// Create some strings we expect to find in the updated manifest84final String animal =85"Name: animal/marsupial";86final String specTitle =87"Specification-Title: Wombat";8889// Create a text file with manifest entries90File manifestOrig = File.createTempFile("manifestOrig", ".txt");91if (!debug) manifestOrig.deleteOnExit();92PrintWriter pw = new PrintWriter(manifestOrig);93pw.println("Manifest-Version: 1.0");94pw.println("Created-By: 1.7.0-internal (Oracle Corporation)");95pw.println("");96pw.println(animal);97pw.println(specTitle);98pw.close();99100File hello = createTextFile("hello");101102// Create a jar file103final String jarFileName = "um-test.jar";104new File(jarFileName).delete(); // remove pre-existing first!105int status = JAR_TOOL.run(out, err, "cfm", jarFileName,106manifestOrig.getPath(), hello.getPath());107check(status == 0);108109// Create a new manifest, to use in updating the jar file.110File manifestUpdate = File.createTempFile("manifestUpdate", ".txt");111if (!debug) manifestUpdate.deleteOnExit();112pw = new PrintWriter(manifestUpdate);113final String createdBy =114"Created-By: 1.7.0-special (Oracle Corporation)";115final String specVersion =116"Specification-Version: 1.0.0.0";117pw.println(createdBy); // replaces line in the original118pw.println("");119pw.println(animal);120pw.println(specVersion); // addition to animal/marsupial section121pw.close();122123// Update jar file with manifest124status = JAR_TOOL.run(out, err, "ufm",125jarFileName, manifestUpdate.getPath());126check(status == 0);127128// Extract jar, and verify contents of manifest file129File f = new File(jarFileName);130if (!debug) f.deleteOnExit();131ZipFile zf = new ZipFile(f);132ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");133BufferedReader r = new BufferedReader(134new InputStreamReader(zf.getInputStream(ze)));135r.readLine(); // skip Manifest-Version136check(r.readLine().equals(createdBy));137r.readLine(); // skip blank line138check(r.readLine().equals(animal));139String s = r.readLine();140if (s.equals(specVersion)) {141check(r.readLine().equals(specTitle));142} else if (s.equals(specTitle)) {143check(r.readLine().equals(specVersion));144} else {145fail("did not match specVersion nor specTitle");146}147zf.close();148}149150// --------------------- Convenience ---------------------------151152static File createTextFile(String name) throws Throwable {153// Create a text file to put in a jar file154File rc = File.createTempFile(name, ".txt");155if (!debug) rc.deleteOnExit();156PrintWriter pw = new PrintWriter(rc);157pw.println("hello, world");158pw.close();159return rc;160}161162static void checkManifest(String jarFileName, String mainClass)163throws Throwable {164File f = new File(jarFileName);165if (!debug) f.deleteOnExit();166ZipFile zf = new ZipFile(f);167ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");168BufferedReader r = new BufferedReader(169new InputStreamReader(zf.getInputStream(ze)));170String line = r.readLine();171while (line != null && !(line.startsWith("Main-Class:"))) {172line = r.readLine();173}174if (line == null) {175fail("Didn't find Main-Class in manifest");176} else {177check(line.equals("Main-Class: " + mainClass));178}179zf.close();180}181182// --------------------- Infrastructure ---------------------------183184static volatile int passed = 0, failed = 0;185186static void pass() {187passed++;188}189190static void fail() {191failed++;192Thread.dumpStack();193}194195static void fail(String msg) {196System.out.println(msg);197fail();198}199200static void unexpected(Throwable t) {201failed++;202t.printStackTrace();203}204205static void check(boolean cond) {206if (cond)207pass();208else209fail();210}211212static void equal(Object x, Object y) {213if ((x == null) ? (y == null) : x.equals(y))214pass();215else216fail(x + " not equal to " + y);217}218219public static void main(String[] args) throws Throwable {220try {221realMain(args);222} catch (Throwable t) {223unexpected(t);224}225System.out.println("\nPassed = " + passed + " failed = " + failed);226if (failed > 0)227throw new AssertionError("Some tests failed");228}229}230231232