|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | +import java.io.File; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.jar.JarFile; |
| 28 | +import java.util.spi.ToolProvider; |
| 29 | +import java.util.zip.ZipEntry; |
| 30 | + |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.ValueSource; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 35 | + |
| 36 | +/* |
| 37 | + * @test |
| 38 | + * @bug 8302293 |
| 39 | + * @summary verify that a JAR file creation through "jar --create" operation |
| 40 | + * works fine if the JAR file name is less than 3 characters in length |
| 41 | + * @run junit JarCreateFileNameTest |
| 42 | + */ |
| 43 | +public class JarCreateFileNameTest { |
| 44 | + |
| 45 | + private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") |
| 46 | + .orElseThrow(() -> |
| 47 | + new RuntimeException("jar tool not found") |
| 48 | + ); |
| 49 | + |
| 50 | + /* |
| 51 | + * Launches "jar --create --file" with file names of varying lengths and verifies |
| 52 | + * that the JAR file was successfully created. |
| 53 | + */ |
| 54 | + @ParameterizedTest |
| 55 | + @ValueSource(strings = {"abcd", "abc", "ab", "a", "d.jar", "ef.jar"}) |
| 56 | + void testCreate(final String targetJarFileName) throws Exception { |
| 57 | + final Path cwd = Path.of("."); |
| 58 | + final Path tmpFile = Files.createTempFile(cwd, "8302293", ".txt"); |
| 59 | + final String fileName = tmpFile.getFileName().toString(); |
| 60 | + final int exitCode = JAR_TOOL.run(System.out, System.err, |
| 61 | + "--create", "--file", targetJarFileName, fileName); |
| 62 | + assertEquals(0, exitCode, "jar command failed"); |
| 63 | + // verify the JAR file is created and contains the expected entry |
| 64 | + try (final JarFile jarFile = new JarFile(new File(targetJarFileName))) { |
| 65 | + final ZipEntry entry = jarFile.getEntry(fileName); |
| 66 | + assertNotNull(entry, "missing " + fileName + " entry in JAR file " + targetJarFileName); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
0 commit comments