Path: blob/master/test/jdk/tools/jar/ContentOrder.java
66643 views
/*1* Copyright (c) 2021, 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 827676426* @summary test that the jar content ordering is sorted27* @library /test/lib28* @modules jdk.jartool29* @build jdk.test.lib.Platform30* jdk.test.lib.util.FileUtils31* @run testng ContentOrder32*/3334import org.testng.Assert;35import org.testng.annotations.AfterMethod;36import org.testng.annotations.BeforeMethod;37import org.testng.annotations.Test;3839import java.io.ByteArrayOutputStream;40import java.io.IOException;41import java.io.PrintStream;42import java.io.UncheckedIOException;43import java.io.File;44import java.nio.file.Files;45import java.nio.file.Path;46import java.nio.file.Paths;47import java.util.Arrays;48import java.util.spi.ToolProvider;49import java.util.stream.Stream;50import java.util.zip.ZipException;5152import jdk.test.lib.util.FileUtils;5354public class ContentOrder {55private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")56.orElseThrow(() ->57new RuntimeException("jar tool not found")58);5960private final String nl = System.lineSeparator();61private final ByteArrayOutputStream baos = new ByteArrayOutputStream();62private final PrintStream out = new PrintStream(baos);63private Runnable onCompletion;6465@BeforeMethod66public void reset() {67onCompletion = null;68}6970@AfterMethod71public void run() {72if (onCompletion != null) {73onCompletion.run();74}75}7677// Test that the jar content ordering when processing a single directory is sorted78@Test79public void testSingleDir() throws IOException {80mkdir("testjar/Ctest1", "testjar/Btest2/subdir1", "testjar/Atest3");81touch("testjar/Ctest1/testfile1", "testjar/Ctest1/testfile2", "testjar/Ctest1/testfile3");82touch("testjar/Btest2/subdir1/testfileC", "testjar/Btest2/subdir1/testfileB", "testjar/Btest2/subdir1/testfileA");83touch("testjar/Atest3/fileZ", "testjar/Atest3/fileY", "testjar/Atest3/fileX");8485onCompletion = () -> rm("test.jar", "testjar");8687jar("cf test.jar testjar");88jar("tf test.jar");89System.out.println(new String(baos.toByteArray()));90String output = "META-INF/" + nl +91"META-INF/MANIFEST.MF" + nl +92"testjar/" + nl +93"testjar/Atest3/" + nl +94"testjar/Atest3/fileX" + nl +95"testjar/Atest3/fileY" + nl +96"testjar/Atest3/fileZ" + nl +97"testjar/Btest2/" + nl +98"testjar/Btest2/subdir1/" + nl +99"testjar/Btest2/subdir1/testfileA" + nl +100"testjar/Btest2/subdir1/testfileB" + nl +101"testjar/Btest2/subdir1/testfileC" + nl +102"testjar/Ctest1/" + nl +103"testjar/Ctest1/testfile1" + nl +104"testjar/Ctest1/testfile2" + nl +105"testjar/Ctest1/testfile3" + nl;106Assert.assertEquals(baos.toByteArray(), output.getBytes());107}108109// Test that when specifying multiple directories or releases that the sort110// ordering is done on each directory and release, reserving the order of111// the directories/releases specified on the command line112@Test113public void testMultiDirWithReleases() throws IOException {114mkdir("testjar/foo/classes",115"testjar/foo11/classes/Zclasses",116"testjar/foo11/classes/Yclasses",117"testjar/foo17/classes/Bclasses",118"testjar/foo17/classes/Aclasses");119touch("testjar/foo/classes/testfile1", "testjar/foo/classes/testfile2");120touch("testjar/foo11/classes/Zclasses/testfile1", "testjar/foo11/classes/Zclasses/testfile2");121touch("testjar/foo11/classes/Yclasses/testfileA", "testjar/foo11/classes/Yclasses/testfileB");122touch("testjar/foo17/classes/Bclasses/testfile1", "testjar/foo17/classes/Bclasses/testfile2");123touch("testjar/foo17/classes/Aclasses/testfileA", "testjar/foo17/classes/Aclasses/testfileB");124125onCompletion = () -> rm("test.jar", "testjar");126127jar("cf test.jar -C testjar/foo classes " +128"--release 17 -C testjar/foo17 classes/Bclasses -C testjar/foo17 classes/Aclasses " +129"--release 11 -C testjar/foo11 classes/Zclasses -C testjar/foo11 classes/Yclasses");130jar("tf test.jar");131System.out.println(new String(baos.toByteArray()));132String output = "META-INF/" + nl +133"META-INF/MANIFEST.MF" + nl +134"classes/" + nl +135"classes/testfile1" + nl +136"classes/testfile2" + nl +137"META-INF/versions/17/classes/Bclasses/" + nl +138"META-INF/versions/17/classes/Bclasses/testfile1" + nl +139"META-INF/versions/17/classes/Bclasses/testfile2" + nl +140"META-INF/versions/17/classes/Aclasses/" + nl +141"META-INF/versions/17/classes/Aclasses/testfileA" + nl +142"META-INF/versions/17/classes/Aclasses/testfileB" + nl +143"META-INF/versions/11/classes/Zclasses/" + nl +144"META-INF/versions/11/classes/Zclasses/testfile1" + nl +145"META-INF/versions/11/classes/Zclasses/testfile2" + nl +146"META-INF/versions/11/classes/Yclasses/" + nl +147"META-INF/versions/11/classes/Yclasses/testfileA" + nl +148"META-INF/versions/11/classes/Yclasses/testfileB" + nl;149Assert.assertEquals(baos.toByteArray(), output.getBytes());150}151152private Stream<Path> mkpath(String... args) {153return Arrays.stream(args).map(d -> Paths.get(".", d.split("/")));154}155156private void mkdir(String... dirs) {157System.out.println("mkdir -p " + Arrays.toString(dirs));158Arrays.stream(dirs).forEach(p -> {159try {160Files.createDirectories((new File(p)).toPath());161} catch (IOException x) {162throw new UncheckedIOException(x);163}164});165}166167private void touch(String... files) {168System.out.println("touch " + Arrays.toString(files));169Arrays.stream(files).forEach(p -> {170try {171Files.createFile((new File(p)).toPath());172} catch (IOException x) {173throw new UncheckedIOException(x);174}175});176}177178private void rm(String... files) {179System.out.println("rm -rf " + Arrays.toString(files));180Arrays.stream(files).forEach(p -> {181try {182Path path = (new File(p)).toPath();183if (Files.isDirectory(path)) {184FileUtils.deleteFileTreeWithRetry(path);185} else {186FileUtils.deleteFileIfExistsWithRetry(path);187}188} catch (IOException x) {189throw new UncheckedIOException(x);190}191});192}193194private void jar(String cmdline) throws IOException {195System.out.println("jar " + cmdline);196baos.reset();197198// the run method catches IOExceptions, we need to expose them199ByteArrayOutputStream baes = new ByteArrayOutputStream();200PrintStream err = new PrintStream(baes);201PrintStream saveErr = System.err;202System.setErr(err);203int rc = JAR_TOOL.run(out, err, cmdline.split(" +"));204System.setErr(saveErr);205if (rc != 0) {206String s = baes.toString();207if (s.startsWith("java.util.zip.ZipException: duplicate entry: ")) {208throw new ZipException(s);209}210throw new IOException(s);211}212}213}214215216