Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/jar/UpdateManifest.java
38833 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* @summary Ensure that jar ufm actually updates the27* existing jar file's manifest with contents of the28* manifest file.29*/3031import java.io.*;32import java.util.logging.*;33import java.util.zip.*;34import sun.tools.jar.Main;3536public class UpdateManifest {37static PrintStream out = System.out;38static PrintStream err = System.err;39static boolean debug = true;4041static final Logger JAR_LOGGER = Logger.getLogger("java.util.jar");4243public static void realMain(String[] args) throws Throwable {44if (args.length == 0) {45debug = false;46File tmp = File.createTempFile("system-out-err", ".txt");47tmp.deleteOnExit();48out = new PrintStream(new FileOutputStream(tmp));49err = out;50// Attributes.read() can log a message we don't care to see.51JAR_LOGGER.setLevel(Level.OFF);52}5354try { testManifestExistence(); } catch (Throwable t) { unexpected(t); }55try { testManifestContents(); } catch (Throwable t) { unexpected(t); }56}5758static void testManifestExistence() throws Throwable {59// Create a file to put in a jar file60File existence = createTextFile("existence");6162// Create a jar file, specifying a Main-Class63final String jarFileName = "um-existence.jar";64new File(jarFileName).delete(); // remove pre-existing first!65Main jartool = new Main(out, err, "jar");66boolean status = jartool.run(67new String[] { "cfe", jarFileName, "Hello", existence.getPath() });68check(status);69checkManifest(jarFileName, "Hello");7071// Update that jar file by changing the Main-Class72jartool = new Main(out, err, "jar");73status = jartool.run(74new String[] { "ufe", jarFileName, "Bye" });75check(status);76checkManifest(jarFileName, "Bye");77}7879static void testManifestContents() throws Throwable {80// Create some strings we expect to find in the updated manifest81final String animal =82"Name: animal/marsupial";83final String specTitle =84"Specification-Title: Wombat";8586// Create a text file with manifest entries87File manifestOrig = File.createTempFile("manifestOrig", ".txt");88if (!debug) manifestOrig.deleteOnExit();89PrintWriter pw = new PrintWriter(manifestOrig);90pw.println("Manifest-Version: 1.0");91pw.println("Created-By: 1.7.0-internal (Oracle Corporation)");92pw.println("");93pw.println(animal);94pw.println(specTitle);95pw.close();9697File hello = createTextFile("hello");9899// Create a jar file100final String jarFileName = "um-test.jar";101new File(jarFileName).delete(); // remove pre-existing first!102Main jartool = new Main(out, err, "jar");103boolean status = jartool.run(104new String[] {"cfm", jarFileName,105manifestOrig.getPath(), hello.getPath() });106check(status);107108// Create a new manifest, to use in updating the jar file.109File manifestUpdate = File.createTempFile("manifestUpdate", ".txt");110if (!debug) manifestUpdate.deleteOnExit();111pw = new PrintWriter(manifestUpdate);112final String createdBy =113"Created-By: 1.7.0-special (Oracle Corporation)";114final String specVersion =115"Specification-Version: 1.0.0.0";116pw.println(createdBy); // replaces line in the original117pw.println("");118pw.println(animal);119pw.println(specVersion); // addition to animal/marsupial section120pw.close();121122// Update jar file with manifest123jartool = new Main(out, err, "jar");124status = jartool.run(125new String[] { "ufm", jarFileName, manifestUpdate.getPath() });126check(status);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