|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8276764 |
| 27 | + * @summary test that the jar content ordering is sorted |
| 28 | + * @library /test/lib |
| 29 | + * @modules jdk.jartool |
| 30 | + * @build jdk.test.lib.Platform |
| 31 | + * jdk.test.lib.util.FileUtils |
| 32 | + * @run testng ContentOrder |
| 33 | + */ |
| 34 | + |
| 35 | +import org.testng.Assert; |
| 36 | +import org.testng.annotations.AfterMethod; |
| 37 | +import org.testng.annotations.BeforeMethod; |
| 38 | +import org.testng.annotations.Test; |
| 39 | + |
| 40 | +import java.io.ByteArrayOutputStream; |
| 41 | +import java.io.IOException; |
| 42 | +import java.io.PrintStream; |
| 43 | +import java.io.UncheckedIOException; |
| 44 | +import java.io.File; |
| 45 | +import java.nio.file.Files; |
| 46 | +import java.nio.file.Path; |
| 47 | +import java.nio.file.Paths; |
| 48 | +import java.util.Arrays; |
| 49 | +import java.util.spi.ToolProvider; |
| 50 | +import java.util.stream.Stream; |
| 51 | +import java.util.zip.ZipException; |
| 52 | + |
| 53 | +import jdk.test.lib.util.FileUtils; |
| 54 | + |
| 55 | +public class ContentOrder { |
| 56 | + private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") |
| 57 | + .orElseThrow(() -> |
| 58 | + new RuntimeException("jar tool not found") |
| 59 | + ); |
| 60 | + |
| 61 | + private final String nl = System.lineSeparator(); |
| 62 | + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 63 | + private final PrintStream out = new PrintStream(baos); |
| 64 | + private Runnable onCompletion; |
| 65 | + |
| 66 | + @BeforeMethod |
| 67 | + public void reset() { |
| 68 | + onCompletion = null; |
| 69 | + } |
| 70 | + |
| 71 | + @AfterMethod |
| 72 | + public void run() { |
| 73 | + if (onCompletion != null) { |
| 74 | + onCompletion.run(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Test that the jar content ordering when processing a single directory is sorted |
| 79 | + @Test |
| 80 | + public void testSingleDir() throws IOException { |
| 81 | + mkdir("testjar/Ctest1", "testjar/Btest2/subdir1", "testjar/Atest3"); |
| 82 | + touch("testjar/Ctest1/testfile1", "testjar/Ctest1/testfile2", "testjar/Ctest1/testfile3"); |
| 83 | + touch("testjar/Btest2/subdir1/testfileC", "testjar/Btest2/subdir1/testfileB", "testjar/Btest2/subdir1/testfileA"); |
| 84 | + touch("testjar/Atest3/fileZ", "testjar/Atest3/fileY", "testjar/Atest3/fileX"); |
| 85 | + |
| 86 | + onCompletion = () -> rm("test.jar", "testjar"); |
| 87 | + |
| 88 | + jar("cf test.jar testjar"); |
| 89 | + jar("tf test.jar"); |
| 90 | + System.out.println(new String(baos.toByteArray())); |
| 91 | + String output = "META-INF/" + nl + |
| 92 | + "META-INF/MANIFEST.MF" + nl + |
| 93 | + "testjar/" + nl + |
| 94 | + "testjar/Atest3/" + nl + |
| 95 | + "testjar/Atest3/fileX" + nl + |
| 96 | + "testjar/Atest3/fileY" + nl + |
| 97 | + "testjar/Atest3/fileZ" + nl + |
| 98 | + "testjar/Btest2/" + nl + |
| 99 | + "testjar/Btest2/subdir1/" + nl + |
| 100 | + "testjar/Btest2/subdir1/testfileA" + nl + |
| 101 | + "testjar/Btest2/subdir1/testfileB" + nl + |
| 102 | + "testjar/Btest2/subdir1/testfileC" + nl + |
| 103 | + "testjar/Ctest1/" + nl + |
| 104 | + "testjar/Ctest1/testfile1" + nl + |
| 105 | + "testjar/Ctest1/testfile2" + nl + |
| 106 | + "testjar/Ctest1/testfile3" + nl; |
| 107 | + Assert.assertEquals(baos.toByteArray(), output.getBytes()); |
| 108 | + } |
| 109 | + |
| 110 | + // Test that when specifying multiple directories or releases that the sort |
| 111 | + // ordering is done on each directory and release, reserving the order of |
| 112 | + // the directories/releases specified on the command line |
| 113 | + @Test |
| 114 | + public void testMultiDirWithReleases() throws IOException { |
| 115 | + mkdir("testjar/foo/classes", |
| 116 | + "testjar/foo11/classes/Zclasses", |
| 117 | + "testjar/foo11/classes/Yclasses", |
| 118 | + "testjar/foo17/classes/Bclasses", |
| 119 | + "testjar/foo17/classes/Aclasses"); |
| 120 | + touch("testjar/foo/classes/testfile1", "testjar/foo/classes/testfile2"); |
| 121 | + touch("testjar/foo11/classes/Zclasses/testfile1", "testjar/foo11/classes/Zclasses/testfile2"); |
| 122 | + touch("testjar/foo11/classes/Yclasses/testfileA", "testjar/foo11/classes/Yclasses/testfileB"); |
| 123 | + touch("testjar/foo17/classes/Bclasses/testfile1", "testjar/foo17/classes/Bclasses/testfile2"); |
| 124 | + touch("testjar/foo17/classes/Aclasses/testfileA", "testjar/foo17/classes/Aclasses/testfileB"); |
| 125 | + |
| 126 | + onCompletion = () -> rm("test.jar", "testjar"); |
| 127 | + |
| 128 | + jar("cf test.jar -C testjar/foo classes " + |
| 129 | + "--release 17 -C testjar/foo17 classes/Bclasses -C testjar/foo17 classes/Aclasses " + |
| 130 | + "--release 11 -C testjar/foo11 classes/Zclasses -C testjar/foo11 classes/Yclasses"); |
| 131 | + jar("tf test.jar"); |
| 132 | + System.out.println(new String(baos.toByteArray())); |
| 133 | + String output = "META-INF/" + nl + |
| 134 | + "META-INF/MANIFEST.MF" + nl + |
| 135 | + "classes/" + nl + |
| 136 | + "classes/testfile1" + nl + |
| 137 | + "classes/testfile2" + nl + |
| 138 | + "META-INF/versions/17/classes/Bclasses/" + nl + |
| 139 | + "META-INF/versions/17/classes/Bclasses/testfile1" + nl + |
| 140 | + "META-INF/versions/17/classes/Bclasses/testfile2" + nl + |
| 141 | + "META-INF/versions/17/classes/Aclasses/" + nl + |
| 142 | + "META-INF/versions/17/classes/Aclasses/testfileA" + nl + |
| 143 | + "META-INF/versions/17/classes/Aclasses/testfileB" + nl + |
| 144 | + "META-INF/versions/11/classes/Zclasses/" + nl + |
| 145 | + "META-INF/versions/11/classes/Zclasses/testfile1" + nl + |
| 146 | + "META-INF/versions/11/classes/Zclasses/testfile2" + nl + |
| 147 | + "META-INF/versions/11/classes/Yclasses/" + nl + |
| 148 | + "META-INF/versions/11/classes/Yclasses/testfileA" + nl + |
| 149 | + "META-INF/versions/11/classes/Yclasses/testfileB" + nl; |
| 150 | + Assert.assertEquals(baos.toByteArray(), output.getBytes()); |
| 151 | + } |
| 152 | + |
| 153 | + private Stream<Path> mkpath(String... args) { |
| 154 | + return Arrays.stream(args).map(d -> Paths.get(".", d.split("/"))); |
| 155 | + } |
| 156 | + |
| 157 | + private void mkdir(String... dirs) { |
| 158 | + System.out.println("mkdir -p " + Arrays.toString(dirs)); |
| 159 | + Arrays.stream(dirs).forEach(p -> { |
| 160 | + try { |
| 161 | + Files.createDirectories((new File(p)).toPath()); |
| 162 | + } catch (IOException x) { |
| 163 | + throw new UncheckedIOException(x); |
| 164 | + } |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + private void touch(String... files) { |
| 169 | + System.out.println("touch " + Arrays.toString(files)); |
| 170 | + Arrays.stream(files).forEach(p -> { |
| 171 | + try { |
| 172 | + Files.createFile((new File(p)).toPath()); |
| 173 | + } catch (IOException x) { |
| 174 | + throw new UncheckedIOException(x); |
| 175 | + } |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + private void rm(String... files) { |
| 180 | + System.out.println("rm -rf " + Arrays.toString(files)); |
| 181 | + Arrays.stream(files).forEach(p -> { |
| 182 | + try { |
| 183 | + Path path = (new File(p)).toPath(); |
| 184 | + if (Files.isDirectory(path)) { |
| 185 | + FileUtils.deleteFileTreeWithRetry(path); |
| 186 | + } else { |
| 187 | + FileUtils.deleteFileIfExistsWithRetry(path); |
| 188 | + } |
| 189 | + } catch (IOException x) { |
| 190 | + throw new UncheckedIOException(x); |
| 191 | + } |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + private void jar(String cmdline) throws IOException { |
| 196 | + System.out.println("jar " + cmdline); |
| 197 | + baos.reset(); |
| 198 | + |
| 199 | + // the run method catches IOExceptions, we need to expose them |
| 200 | + ByteArrayOutputStream baes = new ByteArrayOutputStream(); |
| 201 | + PrintStream err = new PrintStream(baes); |
| 202 | + PrintStream saveErr = System.err; |
| 203 | + System.setErr(err); |
| 204 | + int rc = JAR_TOOL.run(out, err, cmdline.split(" +")); |
| 205 | + System.setErr(saveErr); |
| 206 | + if (rc != 0) { |
| 207 | + String s = baes.toString(); |
| 208 | + if (s.startsWith("java.util.zip.ZipException: duplicate entry: ")) { |
| 209 | + throw new ZipException(s); |
| 210 | + } |
| 211 | + throw new IOException(s); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
1 commit comments
openjdk-notifier[bot] commentedon Jan 14, 2022
Review
Issues