Path: blob/master/test/jdk/tools/jar/InputFilesTest.java
41145 views
/*1* Copyright (c) 2016, 2017, 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 816594426* @summary test several jar tool input file scenarios with variations on -C27* options with/without a --release option. Some input files are28* duplicates that sometimes cause exceptions and other times do not,29* demonstrating identical behavior to JDK 8 jar tool.30* @library /test/lib31* @modules jdk.jartool32* @build jdk.test.lib.Platform33* jdk.test.lib.util.FileUtils34* @run testng InputFilesTest35*/3637import org.testng.Assert;38import org.testng.annotations.AfterMethod;39import org.testng.annotations.BeforeMethod;40import org.testng.annotations.Test;4142import java.io.ByteArrayOutputStream;43import java.io.IOException;44import java.io.PrintStream;45import java.io.UncheckedIOException;46import java.nio.file.Files;47import java.nio.file.Path;48import java.nio.file.Paths;49import java.util.Arrays;50import java.util.spi.ToolProvider;51import java.util.stream.Stream;52import java.util.zip.ZipException;5354import jdk.test.lib.util.FileUtils;5556public class InputFilesTest {57private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")58.orElseThrow(() ->59new RuntimeException("jar tool not found")60);6162private final String nl = System.lineSeparator();63private final ByteArrayOutputStream baos = new ByteArrayOutputStream();64private final PrintStream out = new PrintStream(baos);65private Runnable onCompletion;6667@BeforeMethod68public void reset() {69onCompletion = null;70}7172@AfterMethod73public void run() {74if (onCompletion != null) {75onCompletion.run();76}77}7879@Test80public void test1() throws IOException {81mkdir("test1 test2");82touch("test1/testfile1 test2/testfile2");83jar("cf test.jar -C test1 . -C test2 .");84jar("tf test.jar");85println();86String output = "META-INF/" + nl +87"META-INF/MANIFEST.MF" + nl +88"testfile1" + nl +89"testfile2" + nl;90rm("test.jar test1 test2");91Assert.assertEquals(baos.toByteArray(), output.getBytes());92}9394@Test95public void test2() throws IOException {96mkdir("test1 test2 test3 test4");97touch("test1/testfile1 test2/testfile2 test3/testfile3 test4/testfile4");98jar("cf test.jar -C test1 . -C test2 . --release 9 -C test3 . -C test4 .");99jar("tf test.jar");100println();101String output = "META-INF/" + nl +102"META-INF/MANIFEST.MF" + nl +103"testfile1" + nl +104"testfile2" + nl +105"META-INF/versions/9/" + nl +106"META-INF/versions/9/testfile3" + nl +107"META-INF/versions/9/testfile4" + nl;108rm("test.jar test1 test2 test3 test4");109Assert.assertEquals(baos.toByteArray(), output.getBytes());110}111112@Test113public void test3() throws IOException {114touch("test");115jar("cf test.jar test test");116jar("tf test.jar");117println();118String output = "META-INF/" + nl +119"META-INF/MANIFEST.MF" + nl +120"test" + nl;121rm("test.jar test");122Assert.assertEquals(baos.toByteArray(), output.getBytes());123}124125@Test126public void test4() throws IOException {127mkdir("a");128touch("a/test");129jar("cf test.jar -C a test -C a test");130jar("tf test.jar");131println();132String output = "META-INF/" + nl +133"META-INF/MANIFEST.MF" + nl +134"test" + nl;135rm("test.jar a");136Assert.assertEquals(baos.toByteArray(), output.getBytes());137}138139@Test(expectedExceptions = {ZipException.class})140public void test5() throws IOException {141mkdir("a");142touch("test a/test");143onCompletion = () -> rm("test a");144jar("cf test.jar -C a test test");145}146147@Test(expectedExceptions = {ZipException.class})148public void test6() throws IOException {149mkdir("test1 test2");150touch("test1/a test2/a");151onCompletion = () -> rm("test1 test2");152jar("cf test.jar --release 9 -C test1 a -C test2 a");153}154155private Stream<Path> mkpath(String... args) {156return Arrays.stream(args).map(d -> Paths.get(".", d.split("/")));157}158159private void mkdir(String cmdline) {160System.out.println("mkdir -p " + cmdline);161mkpath(cmdline.split(" +")).forEach(p -> {162try {163Files.createDirectories(p);164} catch (IOException x) {165throw new UncheckedIOException(x);166}167});168}169170private void touch(String cmdline) {171System.out.println("touch " + cmdline);172mkpath(cmdline.split(" +")).forEach(p -> {173try {174Files.createFile(p);175} catch (IOException x) {176throw new UncheckedIOException(x);177}178});179}180181private void rm(String cmdline) {182System.out.println("rm -rf " + cmdline);183mkpath(cmdline.split(" +")).forEach(p -> {184try {185if (Files.isDirectory(p)) {186FileUtils.deleteFileTreeWithRetry(p);187} else {188FileUtils.deleteFileIfExistsWithRetry(p);189}190} catch (IOException x) {191throw new UncheckedIOException(x);192}193});194}195196private void jar(String cmdline) throws IOException {197System.out.println("jar " + cmdline);198baos.reset();199200// the run method catches IOExceptions, we need to expose them201ByteArrayOutputStream baes = new ByteArrayOutputStream();202PrintStream err = new PrintStream(baes);203PrintStream saveErr = System.err;204System.setErr(err);205int rc = JAR_TOOL.run(out, err, cmdline.split(" +"));206System.setErr(saveErr);207if (rc != 0) {208String s = baes.toString();209if (s.startsWith("java.util.zip.ZipException: duplicate entry: ")) {210throw new ZipException(s);211}212throw new IOException(s);213}214}215216private void println() throws IOException {217System.out.println(new String(baos.toByteArray()));218}219}220221222