From 00fe9f7bdfd245791bca6b5b1b2d0a98d41af221 Mon Sep 17 00:00:00 2001 From: Alexey Semenyuk Date: Mon, 28 Oct 2024 17:22:38 +0000 Subject: [PATCH] 8343100: Consolidate EmptyFolderTest and EmptyFolderPackageTest jpackage tests into single java file Reviewed-by: almatvee --- .../helpers/jdk/jpackage/test/TKit.java | 32 ++++-- .../jdk/jpackage/test/WindowsHelper.java | 2 +- .../tools/jpackage/share/EmptyFolderBase.java | 72 -------------- .../share/EmptyFolderPackageTest.java | 70 ------------- .../tools/jpackage/share/EmptyFolderTest.java | 99 ++++++++++++++++--- 5 files changed, 108 insertions(+), 167 deletions(-) delete mode 100644 test/jdk/tools/jpackage/share/EmptyFolderBase.java delete mode 100644 test/jdk/tools/jpackage/share/EmptyFolderPackageTest.java diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/TKit.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/TKit.java index cd32f32f63d..af892f18f7b 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/TKit.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/TKit.java @@ -673,26 +673,40 @@ final public class TKit { assertTrue(path.toFile().exists(), String.format( "Check [%s] path exists", path)); } else { - assertFalse(path.toFile().exists(), String.format( + assertTrue(!path.toFile().exists(), String.format( "Check [%s] path doesn't exist", path)); } } - public static void assertPathNotEmptyDirectory(Path path) { - if (Files.isDirectory(path)) { + public static void assertDirectoryNotEmpty(Path path) { + assertDirectoryExists(path, Optional.of(false)); + } + + public static void assertDirectoryEmpty(Path path) { + assertDirectoryExists(path, Optional.of(true)); + } + + public static void assertDirectoryExists(Path path, Optional isEmptyCheck) { + assertPathExists(path, true); + boolean isDirectory = Files.isDirectory(path); + if (isEmptyCheck.isEmpty() || !isDirectory) { + assertTrue(isDirectory, String.format("Check [%s] is a directory", path)); + } else { ThrowingRunnable.toRunnable(() -> { try (var files = Files.list(path)) { - TKit.assertFalse(files.findFirst().isEmpty(), String.format - ("Check [%s] is not an empty directory", path)); + boolean actualIsEmpty = files.findFirst().isEmpty(); + if (isEmptyCheck.get()) { + TKit.assertTrue(actualIsEmpty, String.format("Check [%s] is not an empty directory", path)); + } else { + TKit.assertTrue(!actualIsEmpty, String.format("Check [%s] is an empty directory", path)); + } } }).run(); - } + } } public static void assertDirectoryExists(Path path) { - assertPathExists(path, true); - assertTrue(path.toFile().isDirectory(), String.format( - "Check [%s] is a directory", path)); + assertDirectoryExists(path, Optional.empty()); } public static void assertSymbolicLinkExists(Path path) { diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java index 1a4dbd22897..b7fd904325d 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java @@ -322,7 +322,7 @@ public class WindowsHelper { Path shortcutPath = shortcutsRoot.resolve(startMenuShortcutPath); verifyShortcut(shortcutPath, exists); if (!exists) { - TKit.assertPathNotEmptyDirectory(shortcutPath.getParent()); + TKit.assertDirectoryNotEmpty(shortcutPath.getParent()); } } diff --git a/test/jdk/tools/jpackage/share/EmptyFolderBase.java b/test/jdk/tools/jpackage/share/EmptyFolderBase.java deleted file mode 100644 index 092996ad039..00000000000 --- a/test/jdk/tools/jpackage/share/EmptyFolderBase.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; -import jdk.jpackage.test.TKit; - -public class EmptyFolderBase { - - // Note: To specify file use ".txt" extension. - // Note: createDirStrcture() will call mkdir() or createNewFile() for paths defined - // in dirStruct, so make sure paths are defined in order. - - // folder-empty - // folder-not-empty - // folder-not-empty/folder-empty - // folder-not-empty/another-folder-empty - // folder-not-empty/folder-non-empty2 - // folder-not-empty/folder-non-empty2/file.txt - private static final String [] DIR_STRUCT = { - "folder-empty", - "folder-not-empty", - "folder-not-empty" + File.separator + "folder-empty", - "folder-not-empty" + File.separator + "another-folder-empty", - "folder-not-empty" + File.separator + "folder-non-empty2", - "folder-not-empty" + File.separator + "folder-non-empty2" + File.separator + - "file.txt" - }; - - // See dirStruct - public static void createDirStrcture(Path inputPath) throws IOException { - File input = new File(inputPath.toString()); - input.mkdir(); - - for (String p : DIR_STRUCT) { - File f = new File(input, p); - if (p.endsWith(".txt")) { - f.createNewFile(); - } else { - f.mkdir(); - } - } - } - - public static void validateDirStrcture(Path appDirPath) { - for (String p : DIR_STRUCT) { - Path path = appDirPath.resolve(p); - TKit.assertPathExists(path, true); - } - } -} diff --git a/test/jdk/tools/jpackage/share/EmptyFolderPackageTest.java b/test/jdk/tools/jpackage/share/EmptyFolderPackageTest.java deleted file mode 100644 index c7af050c685..00000000000 --- a/test/jdk/tools/jpackage/share/EmptyFolderPackageTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -import java.nio.file.Path; -import jdk.jpackage.internal.ApplicationLayout; -import jdk.jpackage.test.PackageTest; -import jdk.jpackage.test.PackageType; -import jdk.jpackage.test.Annotations.Test; - -/** - * Tests generation of packages with input folder containing empty folders. - */ - -/* - * @test - * @summary jpackage with input containing empty folders - * @library ../helpers - * @library /test/lib - * @key jpackagePlatformPackage - * @build EmptyFolderBase - * @build jdk.jpackage.test.* - * @build EmptyFolderPackageTest - * @modules jdk.jpackage/jdk.jpackage.internal - * @run main/othervm/timeout=720 -Xmx512m jdk.jpackage.test.Main - * --jpt-run=EmptyFolderPackageTest - */ -public class EmptyFolderPackageTest { - - @Test - public static void test() throws Exception { - new PackageTest().configureHelloApp() - .addInitializer(cmd -> { - Path input = cmd.inputDir(); - EmptyFolderBase.createDirStrcture(input); - }) - .addInstallVerifier(cmd -> { - if (cmd.packageType() == PackageType.WIN_MSI) { - if (cmd.isPackageUnpacked("Not running file " - + "structure check for empty folders")) { - return; - } - } - - ApplicationLayout appLayout = cmd.appLayout(); - Path appDir = appLayout.appDirectory(); - EmptyFolderBase.validateDirStrcture(appDir); - }) - .run(); - } -} diff --git a/test/jdk/tools/jpackage/share/EmptyFolderTest.java b/test/jdk/tools/jpackage/share/EmptyFolderTest.java index a41c4a66702..230d8a039ea 100644 --- a/test/jdk/tools/jpackage/share/EmptyFolderTest.java +++ b/test/jdk/tools/jpackage/share/EmptyFolderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,44 +21,113 @@ * questions. */ +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; -import jdk.jpackage.test.JPackageCommand; +import jdk.jpackage.test.PackageTest; import jdk.jpackage.test.Annotations.Test; -import jdk.jpackage.internal.ApplicationLayout; +import jdk.jpackage.test.JPackageCommand; +import jdk.jpackage.test.PackageType; +import jdk.jpackage.test.TKit; /** - * Tests generation of app image with input folder containing empty folders. + * Tests generation of packages and app image with input folder containing empty folders. */ /* * @test - * @summary jpackage with input containing empty folders + * @summary jpackage for package with input containing empty folders + * @library ../helpers + * @library /test/lib + * @key jpackagePlatformPackage + * @build jdk.jpackage.test.* + * @build EmptyFolderTest + * @modules jdk.jpackage/jdk.jpackage.internal + * @run main/othervm/timeout=720 -Xmx512m jdk.jpackage.test.Main + * --jpt-run=EmptyFolderTest.testPackage + */ + +/* + * @test + * @summary jpackage for app image with input containing empty folders * @library ../helpers * @library /test/lib - * @build EmptyFolderBase * @build jdk.jpackage.test.* * @build EmptyFolderTest * @modules jdk.jpackage/jdk.jpackage.internal * @run main/othervm -Xmx512m jdk.jpackage.test.Main - * --jpt-run=EmptyFolderTest + * --jpt-run=EmptyFolderTest.testAppImage */ + public class EmptyFolderTest { @Test - public static void test() throws Exception { - JPackageCommand cmd = JPackageCommand.helloAppImage(); + public static void testPackage() { + new PackageTest() + .configureHelloApp() + .addInitializer(EmptyFolderTest::createDirTree) + .addInitializer(cmd -> { + cmd.setArgumentValue("--name", "EmptyFolderPackageTest"); + }) + .addInstallVerifier(EmptyFolderTest::validateDirTree) + .run(); + } + + @Test + public static void testAppImage() throws IOException { + var cmd = JPackageCommand.helloAppImage(); // Add more files into input folder - Path input = cmd.inputDir(); - EmptyFolderBase.createDirStrcture(input); + createDirTree(cmd); // Create app image cmd.executeAndAssertHelloAppImageCreated(); - // Verify directory strcture - ApplicationLayout appLayout = cmd.appLayout(); - Path appDir = appLayout.appDirectory(); - EmptyFolderBase.validateDirStrcture(appDir); + // Verify directory structure + validateDirTree(cmd); } + private static void createDirTree(JPackageCommand cmd) throws IOException { + var baseDir = cmd.inputDir(); + for (var path : DIR_STRUCT) { + path = baseDir.resolve(path); + if (isFile(path)) { + Files.createDirectories(path.getParent()); + Files.write(path, new byte[0]); + } else { + Files.createDirectories(path); + } + } + } + + private static void validateDirTree(JPackageCommand cmd) { + var outputBaseDir = cmd.appLayout().appDirectory(); + var inputBaseDir = cmd.inputDir(); + for (var path : DIR_STRUCT) { + Path outputPath = outputBaseDir.resolve(path); + if (isFile(outputPath)) { + TKit.assertFileExists(outputPath); + } else if (!PackageType.WINDOWS.contains(cmd.packageType())) { + TKit.assertDirectoryExists(outputPath); + } else if (inputBaseDir.resolve(path).toFile().list().length == 0) { + // MSI packages don't support empty folders + TKit.assertPathExists(outputPath, false); + } else { + TKit.assertDirectoryNotEmpty(outputPath); + } + } + } + + private static boolean isFile(Path path) { + return path.getFileName().toString().endsWith(".txt"); + } + + // Note: To specify file use ".txt" extension. + private static final Path [] DIR_STRUCT = { + Path.of("folder-empty"), + Path.of("folder-not-empty"), + Path.of("folder-not-empty", "folder-empty"), + Path.of("folder-not-empty", "another-folder-empty"), + Path.of("folder-not-empty", "folder-non-empty2", "file.txt") + }; }