Path: blob/master/test/jdk/tools/jar/UpdateJar.java
41144 views
/*1* Copyright (c) 2012, 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 717584526* @modules jdk.jartool27* @summary jar -uf should not change file permission28*/2930import java.io.*;31import java.nio.file.*;32import java.nio.file.attribute.*;33import java.util.Set;34import java.util.spi.ToolProvider;3536public class UpdateJar {37private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")38.orElseThrow(() ->39new RuntimeException("jar tool not found")40);4142private static void cleanup(String... fnames) throws Throwable {43for (String fname : fnames) {44Files.deleteIfExists(Paths.get(fname));45}46}4748public static void realMain(String[] args) throws Throwable {49if (!System.getProperty("os.name").startsWith("Windows")) {50String jar = "testUpdateJar.jar";51String e0 = "testUpdateJar_entry0.txt";52String e1 = "testUpdateJar_entry1.txt";53cleanup(jar, e0, e1);54try {55try (FileOutputStream fos0 = new FileOutputStream(e0);56FileOutputStream fos1 = new FileOutputStream(e1)) {57fos0.write(0);58fos1.write(0);59}60String[] jarArgs = new String[] {"cfM0", jar, e0};61if (JAR_TOOL.run(System.out, System.err, jarArgs) != 0) {62fail("Could not create jar file.");63}64Set<PosixFilePermission> pm = Files.getPosixFilePermissions(Paths.get(jar));65jarArgs = new String[] {"uf", jar, e1};66if (JAR_TOOL.run(System.out, System.err, jarArgs) != 0) {67fail("Could not create jar file.");68}69equal(pm, Files.getPosixFilePermissions(Paths.get(jar)));70} finally {71cleanup(jar, e0, e1);72}73}74}7576//--------------------- Infrastructure ---------------------------77static volatile int passed = 0, failed = 0;78static void pass() {passed++;}79static void fail() {failed++; Thread.dumpStack();}80static void fail(String msg) {System.out.println(msg); fail();}81static void unexpected(Throwable t) {failed++; t.printStackTrace();}82static void check(boolean cond) {if (cond) pass(); else fail();}83static void equal(Object x, Object y) {84if (x == null ? y == null : x.equals(y)) pass();85else fail(x + " not equal to " + y);}86public static void main(String[] args) throws Throwable {87try {realMain(args);} catch (Throwable t) {unexpected(t);}88System.out.println("\nPassed = " + passed + " failed = " + failed);89if (failed > 0) throw new AssertionError("Some tests failed");}90}919293