From 8ea27ba2b39c40f11193a776e7be6ae1063a63d9 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Wed, 29 Apr 2026 09:23:25 +0530 Subject: [PATCH 1/4] util for prepare-config goal and task --- .../plugins/util/PrepareConfigUtil.java | 166 ++++++++++ .../plugins/util/PrepareConfigUtilTest.java | 297 ++++++++++++++++++ 2 files changed, 463 insertions(+) create mode 100644 src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java create mode 100644 src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java new file mode 100644 index 00000000..7020de9c --- /dev/null +++ b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java @@ -0,0 +1,166 @@ +/** + * (C) Copyright IBM Corporation 2026. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.openliberty.tools.common.plugins.util; + +import java.io.File; +import java.io.IOException; + +/** + * Utility class for prepare-config goal/task functionality. + * Provides common logic for creating mock Liberty server structures + * and managing configuration preparation across Maven and Gradle plugins. + */ +public class PrepareConfigUtil { + + /** + * Create a mock Liberty server structure in the build output directory. + * This mimics the actual Liberty server directory structure without installing Liberty. + * + *

Structure created:

+ *
+     * buildDir/tmp/
+     *   └── wlp/
+     *       └── usr/
+     *           └── servers/
+     *               └── {serverName}/
+     *                   ├── server.xml
+     *                   ├── bootstrap.properties
+     *                   ├── server.env
+     *                   └── jvm.options
+     * 
+ * + * @param buildDirectory The build output directory (e.g., target/ for Maven, build/ for Gradle) + * @param serverName The name of the Liberty server + * @return The mock server directory (buildDir/tmp/wlp/usr/servers/{serverName}) + * @throws IOException if directory creation fails + */ + public static File createMockLibertyServerStructure(File buildDirectory, String serverName) throws IOException { + if (buildDirectory == null) { + throw new IllegalArgumentException("Build directory cannot be null"); + } + if (serverName == null || serverName.trim().isEmpty()) { + throw new IllegalArgumentException("Server name cannot be null or empty"); + } + + // Create tmp directory in build output + File tmpDir = new File(buildDirectory, "tmp"); + + // Create Liberty server structure: wlp/usr/servers/{serverName} + File wlpDir = new File(tmpDir, "wlp"); + File usrDir = new File(wlpDir, "usr"); + File serversDir = new File(usrDir, "servers"); + File mockServerDir = new File(serversDir, serverName); + + // Create all directories + if (!mockServerDir.exists()) { + if (!mockServerDir.mkdirs()) { + throw new IOException("Failed to create mock server directory: " + mockServerDir.getAbsolutePath()); + } + } + + return mockServerDir; + } + + /** + * Get the mock install directory path. + * + * @param buildDirectory The build output directory + * @return The mock install directory (buildDir/tmp/wlp) + */ + public static File getMockInstallDirectory(File buildDirectory) { + File tmpDir = new File(buildDirectory, "tmp"); + return new File(tmpDir, "wlp"); + } + + /** + * Get the mock user directory path. + * + * @param buildDirectory The build output directory + * @return The mock user directory (buildDir/tmp/wlp/usr) + */ + public static File getMockUserDirectory(File buildDirectory) { + File mockInstallDir = getMockInstallDirectory(buildDirectory); + return new File(mockInstallDir, "usr"); + } + + /** + * Get the mock servers directory path. + * + * @param buildDirectory The build output directory + * @return The mock servers directory (buildDir/tmp/wlp/usr/servers) + */ + public static File getMockServersDirectory(File buildDirectory) { + File mockUserDir = getMockUserDirectory(buildDirectory); + return new File(mockUserDir, "servers"); + } + + /** + * Get the mock server directory path for a specific server. + * + * @param buildDirectory The build output directory + * @param serverName The name of the Liberty server + * @return The mock server directory (buildDir/tmp/wlp/usr/servers/{serverName}) + */ + public static File getMockServerDirectory(File buildDirectory, String serverName) { + File mockServersDir = getMockServersDirectory(buildDirectory); + return new File(mockServersDir, serverName); + } + + /** + * Validate that the mock server structure exists. + * + * @param buildDirectory The build output directory + * @param serverName The name of the Liberty server + * @return true if the mock server structure exists, false otherwise + */ + public static boolean mockServerStructureExists(File buildDirectory, String serverName) { + File mockServerDir = getMockServerDirectory(buildDirectory, serverName); + return mockServerDir.exists() && mockServerDir.isDirectory(); + } + + /** + * Clean up the mock server structure. + * + * @param buildDirectory The build output directory + * @throws IOException if cleanup fails + */ + public static void cleanMockServerStructure(File buildDirectory) throws IOException { + File tmpDir = new File(buildDirectory, "tmp"); + if (tmpDir.exists()) { + deleteDirectory(tmpDir); + } + } + + /** + * Recursively delete a directory and its contents. + * + * @param directory The directory to delete + * @throws IOException if deletion fails + */ + private static void deleteDirectory(File directory) throws IOException { + if (directory.isDirectory()) { + File[] files = directory.listFiles(); + if (files != null) { + for (File file : files) { + deleteDirectory(file); + } + } + } + if (!directory.delete()) { + throw new IOException("Failed to delete: " + directory.getAbsolutePath()); + } + } +} \ No newline at end of file diff --git a/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java new file mode 100644 index 00000000..8e12e7bf --- /dev/null +++ b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java @@ -0,0 +1,297 @@ +/** + * (C) Copyright IBM Corporation 2026. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.openliberty.tools.common.plugins.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test class for PrepareConfigUtil. + * Tests the creation and management of mock Liberty server structures. + */ +public class PrepareConfigUtilTest { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private File buildDirectory; + private static final String SERVER_NAME = "testServer"; + private static final String ANOTHER_SERVER_NAME = "anotherServer"; + + @Before + public void setUp() throws IOException { + buildDirectory = tempFolder.newFolder("build"); + } + + @After + public void tearDown() { + // Cleanup is handled by TemporaryFolder rule + } + + /** + * Test creating a mock Liberty server structure. + * Verifies that all required directories are created. + */ + @Test + public void testCreateMockLibertyServerStructure() throws IOException { + File mockServerDir = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME); + + assertNotNull("Mock server directory should not be null", mockServerDir); + assertTrue("Mock server directory should exist", mockServerDir.exists()); + assertTrue("Mock server directory should be a directory", mockServerDir.isDirectory()); + + // Verify the directory structure + File tmpDir = new File(buildDirectory, "tmp"); + assertTrue("tmp directory should exist", tmpDir.exists()); + + File wlpDir = new File(tmpDir, "wlp"); + assertTrue("wlp directory should exist", wlpDir.exists()); + + File usrDir = new File(wlpDir, "usr"); + assertTrue("usr directory should exist", usrDir.exists()); + + File serversDir = new File(usrDir, "servers"); + assertTrue("servers directory should exist", serversDir.exists()); + + File serverDir = new File(serversDir, SERVER_NAME); + assertTrue("Server directory should exist", serverDir.exists()); + assertEquals("Mock server directory should match expected path", serverDir, mockServerDir); + } + + /** + * Test creating mock server structure with null build directory. + * Should throw IllegalArgumentException. + */ + @Test(expected = IllegalArgumentException.class) + public void testCreateMockServerStructureWithNullBuildDir() throws IOException { + PrepareConfigUtil.createMockLibertyServerStructure(null, SERVER_NAME); + } + + /** + * Test creating mock server structure with null server name. + * Should throw IllegalArgumentException. + */ + @Test(expected = IllegalArgumentException.class) + public void testCreateMockServerStructureWithNullServerName() throws IOException { + PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, null); + } + + /** + * Test creating mock server structure with empty server name. + * Should throw IllegalArgumentException. + */ + @Test(expected = IllegalArgumentException.class) + public void testCreateMockServerStructureWithEmptyServerName() throws IOException { + PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, ""); + } + + /** + * Test creating mock server structure with whitespace-only server name. + * Should throw IllegalArgumentException. + */ + @Test(expected = IllegalArgumentException.class) + public void testCreateMockServerStructureWithWhitespaceServerName() throws IOException { + PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, " "); + } + + /** + * Test creating mock server structure when it already exists. + * Should not fail and should return the existing directory. + */ + @Test + public void testCreateMockServerStructureWhenAlreadyExists() throws IOException { + // Create the structure first time + File mockServerDir1 = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME); + assertTrue("First creation should succeed", mockServerDir1.exists()); + + // Create the structure second time + File mockServerDir2 = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME); + assertTrue("Second creation should succeed", mockServerDir2.exists()); + assertEquals("Both calls should return the same directory", mockServerDir1, mockServerDir2); + } + + /** + * Test getMockInstallDirectory method. + */ + @Test + public void testGetMockInstallDirectory() { + File mockInstallDir = PrepareConfigUtil.getMockInstallDirectory(buildDirectory); + + assertNotNull("Mock install directory should not be null", mockInstallDir); + assertEquals("Mock install directory should be build/tmp/wlp", + new File(new File(buildDirectory, "tmp"), "wlp"), mockInstallDir); + } + + /** + * Test getMockUserDirectory method. + */ + @Test + public void testGetMockUserDirectory() { + File mockUserDir = PrepareConfigUtil.getMockUserDirectory(buildDirectory); + + assertNotNull("Mock user directory should not be null", mockUserDir); + assertEquals("Mock user directory should be build/tmp/wlp/usr", + new File(new File(new File(buildDirectory, "tmp"), "wlp"), "usr"), mockUserDir); + } + + /** + * Test getMockServersDirectory method. + */ + @Test + public void testGetMockServersDirectory() { + File mockServersDir = PrepareConfigUtil.getMockServersDirectory(buildDirectory); + + assertNotNull("Mock servers directory should not be null", mockServersDir); + assertEquals("Mock servers directory should be build/tmp/wlp/usr/servers", + new File(new File(new File(new File(buildDirectory, "tmp"), "wlp"), "usr"), "servers"), + mockServersDir); + } + + /** + * Test getMockServerDirectory method. + */ + @Test + public void testGetMockServerDirectory() { + File mockServerDir = PrepareConfigUtil.getMockServerDirectory(buildDirectory, SERVER_NAME); + + assertNotNull("Mock server directory should not be null", mockServerDir); + assertTrue("Mock server directory path should end with server name", + mockServerDir.getPath().endsWith(SERVER_NAME)); + } + + /** + * Test mockServerStructureExists method when structure does not exist. + */ + @Test + public void testMockServerStructureExistsWhenNotExists() { + boolean exists = PrepareConfigUtil.mockServerStructureExists(buildDirectory, SERVER_NAME); + assertFalse("Mock server structure should not exist initially", exists); + } + + /** + * Test mockServerStructureExists method when structure exists. + */ + @Test + public void testMockServerStructureExistsWhenExists() throws IOException { + // Create the structure + PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME); + + // Check if it exists + boolean exists = PrepareConfigUtil.mockServerStructureExists(buildDirectory, SERVER_NAME); + assertTrue("Mock server structure should exist after creation", exists); + } + + /** + * Test creating multiple server structures in the same build directory. + */ + @Test + public void testCreateMultipleServerStructures() throws IOException { + File mockServerDir1 = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME); + File mockServerDir2 = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, ANOTHER_SERVER_NAME); + + assertTrue("First server directory should exist", mockServerDir1.exists()); + assertTrue("Second server directory should exist", mockServerDir2.exists()); + assertTrue("Server directories should be different", !mockServerDir1.equals(mockServerDir2)); + + // Verify both exist + assertTrue("First server structure should exist", + PrepareConfigUtil.mockServerStructureExists(buildDirectory, SERVER_NAME)); + assertTrue("Second server structure should exist", + PrepareConfigUtil.mockServerStructureExists(buildDirectory, ANOTHER_SERVER_NAME)); + } + + /** + * Test cleanMockServerStructure method. + */ + @Test + public void testCleanMockServerStructure() throws IOException { + // Create the structure + File mockServerDir = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME); + assertTrue("Mock server structure should exist before cleanup", mockServerDir.exists()); + + // Clean the structure + PrepareConfigUtil.cleanMockServerStructure(buildDirectory); + + // Verify it's cleaned + assertFalse("Mock server structure should not exist after cleanup", mockServerDir.exists()); + File tmpDir = new File(buildDirectory, "tmp"); + assertFalse("tmp directory should not exist after cleanup", tmpDir.exists()); + } + + /** + * Test cleanMockServerStructure when structure doesn't exist. + * Should not throw an exception. + */ + @Test + public void testCleanMockServerStructureWhenNotExists() throws IOException { + File tmpDir = new File(buildDirectory, "tmp"); + assertFalse("tmp directory should not exist initially", tmpDir.exists()); + + // Should not throw exception + PrepareConfigUtil.cleanMockServerStructure(buildDirectory); + + assertFalse("tmp directory should still not exist", tmpDir.exists()); + } + + /** + * Test that directory paths are consistent across utility methods. + */ + @Test + public void testDirectoryPathConsistency() throws IOException { + File mockServerDir = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME); + File expectedServerDir = PrepareConfigUtil.getMockServerDirectory(buildDirectory, SERVER_NAME); + + assertEquals("Created server directory should match getMockServerDirectory result", + expectedServerDir, mockServerDir); + + // Verify parent directories + File mockInstallDir = PrepareConfigUtil.getMockInstallDirectory(buildDirectory); + File mockUserDir = PrepareConfigUtil.getMockUserDirectory(buildDirectory); + File mockServersDir = PrepareConfigUtil.getMockServersDirectory(buildDirectory); + + assertEquals("User directory parent should be install directory", + mockInstallDir, mockUserDir.getParentFile()); + assertEquals("Servers directory parent should be user directory", + mockUserDir, mockServersDir.getParentFile()); + assertEquals("Server directory parent should be servers directory", + mockServersDir, mockServerDir.getParentFile()); + } + + /** + * Test creating server structure with special characters in server name. + */ + @Test + public void testCreateServerStructureWithSpecialCharacters() throws IOException { + String specialServerName = "test-server_123"; + File mockServerDir = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, specialServerName); + + assertTrue("Server directory with special characters should be created", mockServerDir.exists()); + assertTrue("Server directory name should contain special characters", + mockServerDir.getName().equals(specialServerName)); + } +} \ No newline at end of file From f959d992f1b7faf404ac5d935904736ee5d157d0 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Thu, 30 Apr 2026 10:32:19 +0530 Subject: [PATCH 2/4] moved lcls common code to PrepareConfigUtil --- .../plugins/util/PrepareConfigUtil.java | 129 ++++++++ .../plugins/util/PrepareConfigUtilTest.java | 285 ++++++++++++++++++ 2 files changed, 414 insertions(+) diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java index 7020de9c..0a108352 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java @@ -17,6 +17,10 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; /** * Utility class for prepare-config goal/task functionality. @@ -131,6 +135,131 @@ public static boolean mockServerStructureExists(File buildDirectory, String serv return mockServerDir.exists() && mockServerDir.isDirectory(); } + /** + * Get the modification time of the build file (pom.xml or build.gradle). + * Checks for Maven pom.xml first, then Gradle build.gradle, then build.gradle.kts. + * + * @param projectDirectory The project root directory + * @return The modification time in milliseconds, or null if no build file found + */ + public static Long getBuildFileModificationTime(File projectDirectory) { + if (projectDirectory == null || !projectDirectory.exists()) { + return null; + } + + try { + // Check for Maven pom.xml + Path pomPath = Paths.get(projectDirectory.getAbsolutePath(), "pom.xml"); + if (Files.exists(pomPath)) { + return Files.getLastModifiedTime(pomPath).toMillis(); + } + + // Check for Gradle build.gradle + Path gradlePath = Paths.get(projectDirectory.getAbsolutePath(), "build.gradle"); + if (Files.exists(gradlePath)) { + return Files.getLastModifiedTime(gradlePath).toMillis(); + } + + // Check for Gradle build.gradle.kts + Path gradleKtsPath = Paths.get(projectDirectory.getAbsolutePath(), "build.gradle.kts"); + if (Files.exists(gradleKtsPath)) { + return Files.getLastModifiedTime(gradleKtsPath).toMillis(); + } + } catch (IOException e) { + // Return null if unable to get modification time + return null; + } + + return null; + } + + /** + * Get the path to liberty-plugin-config.xml file. + * Checks Maven target directory first, then Gradle build directory. + * + * @param projectDirectory The project root directory + * @return The path to liberty-plugin-config.xml, or null if not found + */ + public static File getConfigFilePath(File projectDirectory) { + if (projectDirectory == null || !projectDirectory.exists()) { + return null; + } + + // Try Maven target directory first + File mavenConfig = new File(projectDirectory, "target/liberty-plugin-config.xml"); + if (mavenConfig.exists()) { + return mavenConfig; + } + + // Try Gradle build directory + File gradleConfig = new File(projectDirectory, "build/liberty-plugin-config.xml"); + if (gradleConfig.exists()) { + return gradleConfig; + } + + // Return Maven path as default (even if it doesn't exist yet) + return mavenConfig; + } + + /** + * Check if the liberty-plugin-config.xml file points to a mock server directory. + * This is determined by checking if the config file content contains "tmp" directory reference. + * + * @param configFile The liberty-plugin-config.xml file + * @return true if the config points to a mock server, false otherwise + */ + public static boolean isMockServerInConfig(File configFile) { + if (configFile == null || !configFile.exists()) { + return false; + } + + try { + String content = new String(Files.readAllBytes(configFile.toPath()), StandardCharsets.UTF_8); + return content.contains("tmp"); + } catch (IOException e) { + return false; + } + } + + /** + * Validate that the mock server structure exists and is properly configured. + * This performs comprehensive validation including: + * - Mock server directory exists + * - Config file exists and points to mock server + * - Mock server directory structure is intact + * + * @param buildDirectory The build output directory + * @param serverName The name of the Liberty server + * @return true if the mock server structure is valid, false otherwise + */ + public static boolean validateMockServerStructure(File buildDirectory, String serverName) { + if (buildDirectory == null || serverName == null || serverName.trim().isEmpty()) { + return false; + } + + // Check if mock server directory exists + if (!mockServerStructureExists(buildDirectory, serverName)) { + return false; + } + + // Check if tmp directory exists + File tmpDir = new File(buildDirectory, "tmp"); + if (!tmpDir.exists() || !tmpDir.isDirectory()) { + return false; + } + + // Verify the complete directory structure + File mockInstallDir = getMockInstallDirectory(buildDirectory); + File mockUserDir = getMockUserDirectory(buildDirectory); + File mockServersDir = getMockServersDirectory(buildDirectory); + File mockServerDir = getMockServerDirectory(buildDirectory, serverName); + + return mockInstallDir.exists() && mockInstallDir.isDirectory() && + mockUserDir.exists() && mockUserDir.isDirectory() && + mockServersDir.exists() && mockServersDir.isDirectory() && + mockServerDir.exists() && mockServerDir.isDirectory(); + } + /** * Clean up the mock server structure. * diff --git a/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java index 8e12e7bf..383c9612 100644 --- a/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java +++ b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java @@ -22,7 +22,9 @@ import static org.junit.Assert.fail; import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; import org.junit.After; import org.junit.Before; @@ -294,4 +296,287 @@ public void testCreateServerStructureWithSpecialCharacters() throws IOException assertTrue("Server directory name should contain special characters", mockServerDir.getName().equals(specialServerName)); } + + /** + * Test getBuildFileModificationTime with Maven pom.xml. + */ + @Test + public void testGetBuildFileModificationTimeWithMaven() throws IOException { + File pomFile = new File(buildDirectory, "pom.xml"); + pomFile.createNewFile(); + + Long modTime = PrepareConfigUtil.getBuildFileModificationTime(buildDirectory); + + assertNotNull("Modification time should not be null", modTime); + assertTrue("Modification time should be positive", modTime > 0); + } + + /** + * Test getBuildFileModificationTime with Gradle build.gradle. + */ + @Test + public void testGetBuildFileModificationTimeWithGradle() throws IOException { + File gradleFile = new File(buildDirectory, "build.gradle"); + gradleFile.createNewFile(); + + Long modTime = PrepareConfigUtil.getBuildFileModificationTime(buildDirectory); + + assertNotNull("Modification time should not be null", modTime); + assertTrue("Modification time should be positive", modTime > 0); + } + + /** + * Test getBuildFileModificationTime with Gradle build.gradle.kts. + */ + @Test + public void testGetBuildFileModificationTimeWithGradleKts() throws IOException { + File gradleKtsFile = new File(buildDirectory, "build.gradle.kts"); + gradleKtsFile.createNewFile(); + + Long modTime = PrepareConfigUtil.getBuildFileModificationTime(buildDirectory); + + assertNotNull("Modification time should not be null", modTime); + assertTrue("Modification time should be positive", modTime > 0); + } + + /** + * Test getBuildFileModificationTime with no build file. + */ + @Test + public void testGetBuildFileModificationTimeWithNoBuildFile() { + Long modTime = PrepareConfigUtil.getBuildFileModificationTime(buildDirectory); + + assertEquals("Modification time should be null when no build file exists", null, modTime); + } + + /** + * Test getBuildFileModificationTime with null directory. + */ + @Test + public void testGetBuildFileModificationTimeWithNullDirectory() { + Long modTime = PrepareConfigUtil.getBuildFileModificationTime(null); + + assertEquals("Modification time should be null for null directory", null, modTime); + } + + /** + * Test getBuildFileModificationTime prefers pom.xml over build.gradle. + */ + @Test + public void testGetBuildFileModificationTimePrefersMaven() throws IOException, InterruptedException { + File pomFile = new File(buildDirectory, "pom.xml"); + pomFile.createNewFile(); + + // Sleep to ensure different timestamps + Thread.sleep(10); + + File gradleFile = new File(buildDirectory, "build.gradle"); + gradleFile.createNewFile(); + + Long modTime = PrepareConfigUtil.getBuildFileModificationTime(buildDirectory); + Long pomTime = pomFile.lastModified(); + + assertNotNull("Modification time should not be null", modTime); + assertEquals("Should return pom.xml modification time", pomTime, modTime); + } + + /** + * Test getConfigFilePath with Maven target directory. + */ + @Test + public void testGetConfigFilePathWithMaven() throws IOException { + File targetDir = new File(buildDirectory, "target"); + targetDir.mkdirs(); + File configFile = new File(targetDir, "liberty-plugin-config.xml"); + configFile.createNewFile(); + + File result = PrepareConfigUtil.getConfigFilePath(buildDirectory); + + assertNotNull("Config file path should not be null", result); + assertTrue("Config file should exist", result.exists()); + assertEquals("Should return Maven config file", configFile, result); + } + + /** + * Test getConfigFilePath with Gradle build directory. + */ + @Test + public void testGetConfigFilePathWithGradle() throws IOException { + File buildDir = new File(buildDirectory, "build"); + buildDir.mkdirs(); + File configFile = new File(buildDir, "liberty-plugin-config.xml"); + configFile.createNewFile(); + + File result = PrepareConfigUtil.getConfigFilePath(buildDirectory); + + assertNotNull("Config file path should not be null", result); + assertTrue("Config file should exist", result.exists()); + assertEquals("Should return Gradle config file", configFile, result); + } + + /** + * Test getConfigFilePath with no config file (returns default Maven path). + */ + @Test + public void testGetConfigFilePathWithNoConfigFile() { + File result = PrepareConfigUtil.getConfigFilePath(buildDirectory); + + assertNotNull("Config file path should not be null", result); + assertFalse("Config file should not exist", result.exists()); + assertTrue("Should return Maven target path by default", + result.getPath().contains("target")); + } + + /** + * Test getConfigFilePath with null directory. + */ + @Test + public void testGetConfigFilePathWithNullDirectory() { + File result = PrepareConfigUtil.getConfigFilePath(null); + + assertEquals("Config file path should be null for null directory", null, result); + } + + /** + * Test getConfigFilePath prefers Maven over Gradle. + */ + @Test + public void testGetConfigFilePathPrefersMaven() throws IOException { + // Create both Maven and Gradle config files + File targetDir = new File(buildDirectory, "target"); + targetDir.mkdirs(); + File mavenConfig = new File(targetDir, "liberty-plugin-config.xml"); + mavenConfig.createNewFile(); + + File buildDir = new File(buildDirectory, "build"); + buildDir.mkdirs(); + File gradleConfig = new File(buildDir, "liberty-plugin-config.xml"); + gradleConfig.createNewFile(); + + File result = PrepareConfigUtil.getConfigFilePath(buildDirectory); + + assertEquals("Should prefer Maven config file", mavenConfig, result); + } + + /** + * Test isMockServerInConfig with mock server reference. + */ + @Test + public void testIsMockServerInConfigWithMockServer() throws IOException { + File configFile = new File(buildDirectory, "liberty-plugin-config.xml"); + String content = "\n" + + "\n" + + " /path/to/tmp/wlp\n" + + ""; + Files.write(configFile.toPath(), content.getBytes()); + + boolean result = PrepareConfigUtil.isMockServerInConfig(configFile); + + assertTrue("Should detect mock server reference", result); + } + + /** + * Test isMockServerInConfig without mock server reference. + */ + @Test + public void testIsMockServerInConfigWithoutMockServer() throws IOException { + File configFile = new File(buildDirectory, "liberty-plugin-config.xml"); + String content = "\n" + + "\n" + + " /path/to/wlp\n" + + ""; + Files.write(configFile.toPath(), content.getBytes()); + + boolean result = PrepareConfigUtil.isMockServerInConfig(configFile); + + assertFalse("Should not detect mock server reference", result); + } + + /** + * Test isMockServerInConfig with null file. + */ + @Test + public void testIsMockServerInConfigWithNullFile() { + boolean result = PrepareConfigUtil.isMockServerInConfig(null); + + assertFalse("Should return false for null file", result); + } + + /** + * Test isMockServerInConfig with non-existent file. + */ + @Test + public void testIsMockServerInConfigWithNonExistentFile() { + File configFile = new File(buildDirectory, "non-existent.xml"); + + boolean result = PrepareConfigUtil.isMockServerInConfig(configFile); + + assertFalse("Should return false for non-existent file", result); + } + + /** + * Test validateMockServerStructure with valid structure. + */ + @Test + public void testValidateMockServerStructureWithValidStructure() throws IOException { + PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME); + + boolean result = PrepareConfigUtil.validateMockServerStructure(buildDirectory, SERVER_NAME); + + assertTrue("Should validate successfully with complete structure", result); + } + + /** + * Test validateMockServerStructure with missing structure. + */ + @Test + public void testValidateMockServerStructureWithMissingStructure() { + boolean result = PrepareConfigUtil.validateMockServerStructure(buildDirectory, SERVER_NAME); + + assertFalse("Should fail validation when structure doesn't exist", result); + } + + /** + * Test validateMockServerStructure with null build directory. + */ + @Test + public void testValidateMockServerStructureWithNullBuildDir() { + boolean result = PrepareConfigUtil.validateMockServerStructure(null, SERVER_NAME); + + assertFalse("Should fail validation with null build directory", result); + } + + /** + * Test validateMockServerStructure with null server name. + */ + @Test + public void testValidateMockServerStructureWithNullServerName() { + boolean result = PrepareConfigUtil.validateMockServerStructure(buildDirectory, null); + + assertFalse("Should fail validation with null server name", result); + } + + /** + * Test validateMockServerStructure with empty server name. + */ + @Test + public void testValidateMockServerStructureWithEmptyServerName() { + boolean result = PrepareConfigUtil.validateMockServerStructure(buildDirectory, ""); + + assertFalse("Should fail validation with empty server name", result); + } + + /** + * Test validateMockServerStructure with incomplete structure (missing tmp directory). + */ + @Test + public void testValidateMockServerStructureWithIncompleteStructure() throws IOException { + // Create only the server directory without proper parent structure + File serverDir = new File(buildDirectory, "servers/" + SERVER_NAME); + serverDir.mkdirs(); + + boolean result = PrepareConfigUtil.validateMockServerStructure(buildDirectory, SERVER_NAME); + + assertFalse("Should fail validation with incomplete structure", result); + } } \ No newline at end of file From 2341146fa65a5aeab7bcb5837791fa4f6bb60ba7 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Thu, 7 May 2026 20:49:58 +0530 Subject: [PATCH 3/4] changes to pass temporary variable directory. defaulted to tmp/liberty-var-cache --- .../plugins/util/PrepareConfigUtil.java | 192 +++++++++++++++--- .../plugins/util/PrepareConfigUtilTest.java | 161 +++++++++++++-- 2 files changed, 307 insertions(+), 46 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java index 0a108352..9807d91f 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java @@ -29,13 +29,19 @@ */ public class PrepareConfigUtil { + /** + * Default name for the temporary directory used for mock Liberty server structures. + */ + public static final String DEFAULT_TEMP_DIR_NAME = "tmp/liberty-var-cache"; + /** * Create a mock Liberty server structure in the build output directory. * This mimics the actual Liberty server directory structure without installing Liberty. + * Uses the default temporary directory name. * *

Structure created:

*
-     * buildDir/tmp/
+     * buildDir/tmp/liberty-var-cache/
      *   └── wlp/
      *       └── usr/
      *           └── servers/
@@ -48,19 +54,49 @@ public class PrepareConfigUtil {
      *
      * @param buildDirectory The build output directory (e.g., target/ for Maven, build/ for Gradle)
      * @param serverName The name of the Liberty server
-     * @return The mock server directory (buildDir/tmp/wlp/usr/servers/{serverName})
+     * @return The mock server directory (buildDir/tmp/liberty-var-cache/wlp/usr/servers/{serverName})
      * @throws IOException if directory creation fails
      */
     public static File createMockLibertyServerStructure(File buildDirectory, String serverName) throws IOException {
+        return createMockLibertyServerStructure(buildDirectory, serverName, DEFAULT_TEMP_DIR_NAME);
+    }
+
+    /**
+     * Create a mock Liberty server structure in the build output directory with a custom temporary directory name.
+     * This mimics the actual Liberty server directory structure without installing Liberty.
+     *
+     * 

Structure created:

+ *
+     * buildDir/{tempDirName}/
+     *   └── wlp/
+     *       └── usr/
+     *           └── servers/
+     *               └── {serverName}/
+     *                   ├── server.xml
+     *                   ├── bootstrap.properties
+     *                   ├── server.env
+     *                   └── jvm.options
+     * 
+ * + * @param buildDirectory The build output directory (e.g., target/ for Maven, build/ for Gradle) + * @param serverName The name of the Liberty server + * @param tempDirName The name of the temporary directory (e.g., "liberty-var-cache", "tmp") + * @return The mock server directory (buildDir/{tempDirName}/wlp/usr/servers/{serverName}) + * @throws IOException if directory creation fails + */ + public static File createMockLibertyServerStructure(File buildDirectory, String serverName, String tempDirName) throws IOException { if (buildDirectory == null) { throw new IllegalArgumentException("Build directory cannot be null"); } if (serverName == null || serverName.trim().isEmpty()) { throw new IllegalArgumentException("Server name cannot be null or empty"); } + if (tempDirName == null || tempDirName.trim().isEmpty()) { + throw new IllegalArgumentException("Temporary directory name cannot be null or empty"); + } - // Create tmp directory in build output - File tmpDir = new File(buildDirectory, "tmp"); + // Create temporary directory in build output + File tmpDir = new File(buildDirectory, tempDirName); // Create Liberty server structure: wlp/usr/servers/{serverName} File wlpDir = new File(tmpDir, "wlp"); @@ -79,59 +115,116 @@ public static File createMockLibertyServerStructure(File buildDirectory, String } /** - * Get the mock install directory path. + * Get the mock install directory path using the default temporary directory name. * * @param buildDirectory The build output directory - * @return The mock install directory (buildDir/tmp/wlp) + * @return The mock install directory (buildDir/tmp/liberty-var-cache/wlp) */ public static File getMockInstallDirectory(File buildDirectory) { - File tmpDir = new File(buildDirectory, "tmp"); + return getMockInstallDirectory(buildDirectory, DEFAULT_TEMP_DIR_NAME); + } + + /** + * Get the mock install directory path with a custom temporary directory name. + * + * @param buildDirectory The build output directory + * @param tempDirName The name of the temporary directory + * @return The mock install directory (buildDir/{tempDirName}/wlp) + */ + public static File getMockInstallDirectory(File buildDirectory, String tempDirName) { + File tmpDir = new File(buildDirectory, tempDirName); return new File(tmpDir, "wlp"); } /** - * Get the mock user directory path. + * Get the mock user directory path using the default temporary directory name. * * @param buildDirectory The build output directory - * @return The mock user directory (buildDir/tmp/wlp/usr) + * @return The mock user directory (buildDir/tmp/liberty-var-cache/wlp/usr) */ public static File getMockUserDirectory(File buildDirectory) { - File mockInstallDir = getMockInstallDirectory(buildDirectory); + return getMockUserDirectory(buildDirectory, DEFAULT_TEMP_DIR_NAME); + } + + /** + * Get the mock user directory path with a custom temporary directory name. + * + * @param buildDirectory The build output directory + * @param tempDirName The name of the temporary directory + * @return The mock user directory (buildDir/{tempDirName}/wlp/usr) + */ + public static File getMockUserDirectory(File buildDirectory, String tempDirName) { + File mockInstallDir = getMockInstallDirectory(buildDirectory, tempDirName); return new File(mockInstallDir, "usr"); } /** - * Get the mock servers directory path. + * Get the mock servers directory path using the default temporary directory name. * * @param buildDirectory The build output directory - * @return The mock servers directory (buildDir/tmp/wlp/usr/servers) + * @return The mock servers directory (buildDir/tmp/liberty-var-cache/wlp/usr/servers) */ public static File getMockServersDirectory(File buildDirectory) { - File mockUserDir = getMockUserDirectory(buildDirectory); + return getMockServersDirectory(buildDirectory, DEFAULT_TEMP_DIR_NAME); + } + + /** + * Get the mock servers directory path with a custom temporary directory name. + * + * @param buildDirectory The build output directory + * @param tempDirName The name of the temporary directory + * @return The mock servers directory (buildDir/{tempDirName}/wlp/usr/servers) + */ + public static File getMockServersDirectory(File buildDirectory, String tempDirName) { + File mockUserDir = getMockUserDirectory(buildDirectory, tempDirName); return new File(mockUserDir, "servers"); } /** - * Get the mock server directory path for a specific server. + * Get the mock server directory path for a specific server using the default temporary directory name. * * @param buildDirectory The build output directory * @param serverName The name of the Liberty server - * @return The mock server directory (buildDir/tmp/wlp/usr/servers/{serverName}) + * @return The mock server directory (buildDir/tmp/liberty-var-cache/wlp/usr/servers/{serverName}) */ public static File getMockServerDirectory(File buildDirectory, String serverName) { - File mockServersDir = getMockServersDirectory(buildDirectory); + return getMockServerDirectory(buildDirectory, serverName, DEFAULT_TEMP_DIR_NAME); + } + + /** + * Get the mock server directory path for a specific server with a custom temporary directory name. + * + * @param buildDirectory The build output directory + * @param serverName The name of the Liberty server + * @param tempDirName The name of the temporary directory + * @return The mock server directory (buildDir/{tempDirName}/wlp/usr/servers/{serverName}) + */ + public static File getMockServerDirectory(File buildDirectory, String serverName, String tempDirName) { + File mockServersDir = getMockServersDirectory(buildDirectory, tempDirName); return new File(mockServersDir, serverName); } /** - * Validate that the mock server structure exists. + * Validate that the mock server structure exists using the default temporary directory name. * * @param buildDirectory The build output directory * @param serverName The name of the Liberty server * @return true if the mock server structure exists, false otherwise */ public static boolean mockServerStructureExists(File buildDirectory, String serverName) { - File mockServerDir = getMockServerDirectory(buildDirectory, serverName); + return mockServerStructureExists(buildDirectory, serverName, DEFAULT_TEMP_DIR_NAME); + } + + /** + * Validate that the mock server structure exists with a custom temporary directory name. + * + * @param buildDirectory The build output directory + * @param serverName The name of the Liberty server + * @param tempDirName The name of the temporary directory + * @return true if the mock server structure exists, false otherwise + */ + public static boolean mockServerStructureExists(File buildDirectory, String serverName, String tempDirName) { + File mockServerDir = getMockServerDirectory(buildDirectory, serverName, tempDirName); return mockServerDir.exists() && mockServerDir.isDirectory(); } @@ -203,26 +296,38 @@ public static File getConfigFilePath(File projectDirectory) { /** * Check if the liberty-plugin-config.xml file points to a mock server directory. - * This is determined by checking if the config file content contains "tmp" directory reference. + * This is determined by checking if the config file content contains the temporary directory reference. * * @param configFile The liberty-plugin-config.xml file * @return true if the config points to a mock server, false otherwise */ public static boolean isMockServerInConfig(File configFile) { + return isMockServerInConfig(configFile, DEFAULT_TEMP_DIR_NAME); + } + + /** + * Check if the liberty-plugin-config.xml file points to a mock server directory with a custom temporary directory name. + * This is determined by checking if the config file content contains the specified temporary directory reference. + * + * @param configFile The liberty-plugin-config.xml file + * @param tempDirName The name of the temporary directory to check for + * @return true if the config points to a mock server, false otherwise + */ + public static boolean isMockServerInConfig(File configFile, String tempDirName) { if (configFile == null || !configFile.exists()) { return false; } try { String content = new String(Files.readAllBytes(configFile.toPath()), StandardCharsets.UTF_8); - return content.contains("tmp"); + return content.contains(tempDirName); } catch (IOException e) { return false; } } /** - * Validate that the mock server structure exists and is properly configured. + * Validate that the mock server structure exists and is properly configured using the default temporary directory name. * This performs comprehensive validation including: * - Mock server directory exists * - Config file exists and points to mock server @@ -233,26 +338,42 @@ public static boolean isMockServerInConfig(File configFile) { * @return true if the mock server structure is valid, false otherwise */ public static boolean validateMockServerStructure(File buildDirectory, String serverName) { + return validateMockServerStructure(buildDirectory, serverName, DEFAULT_TEMP_DIR_NAME); + } + + /** + * Validate that the mock server structure exists and is properly configured with a custom temporary directory name. + * This performs comprehensive validation including: + * - Mock server directory exists + * - Config file exists and points to mock server + * - Mock server directory structure is intact + * + * @param buildDirectory The build output directory + * @param serverName The name of the Liberty server + * @param tempDirName The name of the temporary directory + * @return true if the mock server structure is valid, false otherwise + */ + public static boolean validateMockServerStructure(File buildDirectory, String serverName, String tempDirName) { if (buildDirectory == null || serverName == null || serverName.trim().isEmpty()) { return false; } // Check if mock server directory exists - if (!mockServerStructureExists(buildDirectory, serverName)) { + if (!mockServerStructureExists(buildDirectory, serverName, tempDirName)) { return false; } - // Check if tmp directory exists - File tmpDir = new File(buildDirectory, "tmp"); + // Check if temporary directory exists + File tmpDir = new File(buildDirectory, tempDirName); if (!tmpDir.exists() || !tmpDir.isDirectory()) { return false; } // Verify the complete directory structure - File mockInstallDir = getMockInstallDirectory(buildDirectory); - File mockUserDir = getMockUserDirectory(buildDirectory); - File mockServersDir = getMockServersDirectory(buildDirectory); - File mockServerDir = getMockServerDirectory(buildDirectory, serverName); + File mockInstallDir = getMockInstallDirectory(buildDirectory, tempDirName); + File mockUserDir = getMockUserDirectory(buildDirectory, tempDirName); + File mockServersDir = getMockServersDirectory(buildDirectory, tempDirName); + File mockServerDir = getMockServerDirectory(buildDirectory, serverName, tempDirName); return mockInstallDir.exists() && mockInstallDir.isDirectory() && mockUserDir.exists() && mockUserDir.isDirectory() && @@ -261,13 +382,24 @@ public static boolean validateMockServerStructure(File buildDirectory, String se } /** - * Clean up the mock server structure. + * Clean up the mock server structure using the default temporary directory name. * * @param buildDirectory The build output directory * @throws IOException if cleanup fails */ public static void cleanMockServerStructure(File buildDirectory) throws IOException { - File tmpDir = new File(buildDirectory, "tmp"); + cleanMockServerStructure(buildDirectory, DEFAULT_TEMP_DIR_NAME); + } + + /** + * Clean up the mock server structure with a custom temporary directory name. + * + * @param buildDirectory The build output directory + * @param tempDirName The name of the temporary directory + * @throws IOException if cleanup fails + */ + public static void cleanMockServerStructure(File buildDirectory, String tempDirName) throws IOException { + File tmpDir = new File(buildDirectory, tempDirName); if (tmpDir.exists()) { deleteDirectory(tmpDir); } diff --git a/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java index 383c9612..13eddf79 100644 --- a/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java +++ b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java @@ -67,9 +67,39 @@ public void testCreateMockLibertyServerStructure() throws IOException { assertTrue("Mock server directory should exist", mockServerDir.exists()); assertTrue("Mock server directory should be a directory", mockServerDir.isDirectory()); - // Verify the directory structure - File tmpDir = new File(buildDirectory, "tmp"); - assertTrue("tmp directory should exist", tmpDir.exists()); + // Verify the directory structure using default temp dir name + File tmpDir = new File(buildDirectory, PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME); + assertTrue("liberty-var-cache directory should exist", tmpDir.exists()); + + File wlpDir = new File(tmpDir, "wlp"); + assertTrue("wlp directory should exist", wlpDir.exists()); + + File usrDir = new File(wlpDir, "usr"); + assertTrue("usr directory should exist", usrDir.exists()); + + File serversDir = new File(usrDir, "servers"); + assertTrue("servers directory should exist", serversDir.exists()); + + File serverDir = new File(serversDir, SERVER_NAME); + assertTrue("Server directory should exist", serverDir.exists()); + assertEquals("Mock server directory should match expected path", serverDir, mockServerDir); + } + + /** + * Test creating a mock Liberty server structure with custom temp directory name. + */ + @Test + public void testCreateMockLibertyServerStructureWithCustomTempDir() throws IOException { + String customTempDir = "my-custom-temp"; + File mockServerDir = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME, customTempDir); + + assertNotNull("Mock server directory should not be null", mockServerDir); + assertTrue("Mock server directory should exist", mockServerDir.exists()); + assertTrue("Mock server directory should be a directory", mockServerDir.isDirectory()); + + // Verify the directory structure with custom temp dir + File tmpDir = new File(buildDirectory, customTempDir); + assertTrue("Custom temp directory should exist", tmpDir.exists()); File wlpDir = new File(tmpDir, "wlp"); assertTrue("wlp directory should exist", wlpDir.exists()); @@ -145,8 +175,21 @@ public void testGetMockInstallDirectory() { File mockInstallDir = PrepareConfigUtil.getMockInstallDirectory(buildDirectory); assertNotNull("Mock install directory should not be null", mockInstallDir); - assertEquals("Mock install directory should be build/tmp/wlp", - new File(new File(buildDirectory, "tmp"), "wlp"), mockInstallDir); + assertEquals("Mock install directory should be build/liberty-var-cache/wlp", + new File(new File(buildDirectory, PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME), "wlp"), mockInstallDir); + } + + /** + * Test getMockInstallDirectory with custom temp directory. + */ + @Test + public void testGetMockInstallDirectoryWithCustomTempDir() { + String customTempDir = "my-temp"; + File mockInstallDir = PrepareConfigUtil.getMockInstallDirectory(buildDirectory, customTempDir); + + assertNotNull("Mock install directory should not be null", mockInstallDir); + assertEquals("Mock install directory should use custom temp dir", + new File(new File(buildDirectory, customTempDir), "wlp"), mockInstallDir); } /** @@ -157,8 +200,21 @@ public void testGetMockUserDirectory() { File mockUserDir = PrepareConfigUtil.getMockUserDirectory(buildDirectory); assertNotNull("Mock user directory should not be null", mockUserDir); - assertEquals("Mock user directory should be build/tmp/wlp/usr", - new File(new File(new File(buildDirectory, "tmp"), "wlp"), "usr"), mockUserDir); + assertEquals("Mock user directory should be build/liberty-var-cache/wlp/usr", + new File(new File(new File(buildDirectory, PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME), "wlp"), "usr"), mockUserDir); + } + + /** + * Test getMockUserDirectory with custom temp directory. + */ + @Test + public void testGetMockUserDirectoryWithCustomTempDir() { + String customTempDir = "my-temp"; + File mockUserDir = PrepareConfigUtil.getMockUserDirectory(buildDirectory, customTempDir); + + assertNotNull("Mock user directory should not be null", mockUserDir); + assertEquals("Mock user directory should use custom temp dir", + new File(new File(new File(buildDirectory, customTempDir), "wlp"), "usr"), mockUserDir); } /** @@ -169,8 +225,22 @@ public void testGetMockServersDirectory() { File mockServersDir = PrepareConfigUtil.getMockServersDirectory(buildDirectory); assertNotNull("Mock servers directory should not be null", mockServersDir); - assertEquals("Mock servers directory should be build/tmp/wlp/usr/servers", - new File(new File(new File(new File(buildDirectory, "tmp"), "wlp"), "usr"), "servers"), + assertEquals("Mock servers directory should be build/liberty-var-cache/wlp/usr/servers", + new File(new File(new File(new File(buildDirectory, PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME), "wlp"), "usr"), "servers"), + mockServersDir); + } + + /** + * Test getMockServersDirectory with custom temp directory. + */ + @Test + public void testGetMockServersDirectoryWithCustomTempDir() { + String customTempDir = "my-temp"; + File mockServersDir = PrepareConfigUtil.getMockServersDirectory(buildDirectory, customTempDir); + + assertNotNull("Mock servers directory should not be null", mockServersDir); + assertEquals("Mock servers directory should use custom temp dir", + new File(new File(new File(new File(buildDirectory, customTempDir), "wlp"), "usr"), "servers"), mockServersDir); } @@ -241,8 +311,27 @@ public void testCleanMockServerStructure() throws IOException { // Verify it's cleaned assertFalse("Mock server structure should not exist after cleanup", mockServerDir.exists()); - File tmpDir = new File(buildDirectory, "tmp"); - assertFalse("tmp directory should not exist after cleanup", tmpDir.exists()); + File tmpDir = new File(buildDirectory, PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME); + assertFalse("liberty-var-cache directory should not exist after cleanup", tmpDir.exists()); + } + + /** + * Test cleanMockServerStructure with custom temp directory. + */ + @Test + public void testCleanMockServerStructureWithCustomTempDir() throws IOException { + String customTempDir = "my-temp"; + // Create the structure with custom temp dir + File mockServerDir = PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME, customTempDir); + assertTrue("Mock server structure should exist before cleanup", mockServerDir.exists()); + + // Clean the structure + PrepareConfigUtil.cleanMockServerStructure(buildDirectory, customTempDir); + + // Verify it's cleaned + assertFalse("Mock server structure should not exist after cleanup", mockServerDir.exists()); + File tmpDir = new File(buildDirectory, customTempDir); + assertFalse("Custom temp directory should not exist after cleanup", tmpDir.exists()); } /** @@ -251,13 +340,13 @@ public void testCleanMockServerStructure() throws IOException { */ @Test public void testCleanMockServerStructureWhenNotExists() throws IOException { - File tmpDir = new File(buildDirectory, "tmp"); - assertFalse("tmp directory should not exist initially", tmpDir.exists()); + File tmpDir = new File(buildDirectory, PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME); + assertFalse("liberty-var-cache directory should not exist initially", tmpDir.exists()); // Should not throw exception PrepareConfigUtil.cleanMockServerStructure(buildDirectory); - assertFalse("tmp directory should still not exist", tmpDir.exists()); + assertFalse("liberty-var-cache directory should still not exist", tmpDir.exists()); } /** @@ -466,7 +555,7 @@ public void testIsMockServerInConfigWithMockServer() throws IOException { File configFile = new File(buildDirectory, "liberty-plugin-config.xml"); String content = "\n" + "\n" + - " /path/to/tmp/wlp\n" + + " /path/to/tmp/liberty-var-cache/wlp\n" + ""; Files.write(configFile.toPath(), content.getBytes()); @@ -475,6 +564,24 @@ public void testIsMockServerInConfigWithMockServer() throws IOException { assertTrue("Should detect mock server reference", result); } + /** + * Test isMockServerInConfig with custom temp directory reference. + */ + @Test + public void testIsMockServerInConfigWithCustomTempDir() throws IOException { + String customTempDir = "my-custom-temp"; + File configFile = new File(buildDirectory, "liberty-plugin-config.xml"); + String content = "\n" + + "\n" + + " /path/to/" + customTempDir + "/wlp\n" + + ""; + Files.write(configFile.toPath(), content.getBytes()); + + boolean result = PrepareConfigUtil.isMockServerInConfig(configFile, customTempDir); + + assertTrue("Should detect custom temp dir reference", result); + } + /** * Test isMockServerInConfig without mock server reference. */ @@ -567,7 +674,7 @@ public void testValidateMockServerStructureWithEmptyServerName() { } /** - * Test validateMockServerStructure with incomplete structure (missing tmp directory). + * Test validateMockServerStructure with incomplete structure (missing temp directory). */ @Test public void testValidateMockServerStructureWithIncompleteStructure() throws IOException { @@ -579,4 +686,26 @@ public void testValidateMockServerStructureWithIncompleteStructure() throws IOEx assertFalse("Should fail validation with incomplete structure", result); } + + /** + * Test validateMockServerStructure with custom temp directory. + */ + @Test + public void testValidateMockServerStructureWithCustomTempDir() throws IOException { + String customTempDir = "my-temp"; + PrepareConfigUtil.createMockLibertyServerStructure(buildDirectory, SERVER_NAME, customTempDir); + + boolean result = PrepareConfigUtil.validateMockServerStructure(buildDirectory, SERVER_NAME, customTempDir); + + assertTrue("Should validate successfully with custom temp dir", result); + } + + /** + * Test that default constant is set correctly. + */ + @Test + public void testDefaultTempDirNameConstant() { + assertEquals("Default temp dir name should be tmp/liberty-var-cache", + "tmp/liberty-var-cache", PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME); + } } \ No newline at end of file From b743a6b5bec4ac2897874deca8319df48d5c9c20 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Fri, 8 May 2026 10:00:09 +0530 Subject: [PATCH 4/4] using buildDIr/.libertyls-var-cache for temporary directory --- .../tools/common/plugins/util/PrepareConfigUtil.java | 10 +++++----- .../common/plugins/util/PrepareConfigUtilTest.java | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java index 9807d91f..5c072df5 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java @@ -32,7 +32,7 @@ public class PrepareConfigUtil { /** * Default name for the temporary directory used for mock Liberty server structures. */ - public static final String DEFAULT_TEMP_DIR_NAME = "tmp/liberty-var-cache"; + public static final String DEFAULT_TEMP_DIR_NAME = ".libertyls-var-cache"; /** * Create a mock Liberty server structure in the build output directory. @@ -41,7 +41,7 @@ public class PrepareConfigUtil { * *

Structure created:

*
-     * buildDir/tmp/liberty-var-cache/
+     * buildDir/.libertyls-var-cache/
      *   └── wlp/
      *       └── usr/
      *           └── servers/
@@ -54,7 +54,7 @@ public class PrepareConfigUtil {
      *
      * @param buildDirectory The build output directory (e.g., target/ for Maven, build/ for Gradle)
      * @param serverName The name of the Liberty server
-     * @return The mock server directory (buildDir/tmp/liberty-var-cache/wlp/usr/servers/{serverName})
+     * @return The mock server directory (buildDir/.libertyls-var-cache/wlp/usr/servers/{serverName})
      * @throws IOException if directory creation fails
      */
     public static File createMockLibertyServerStructure(File buildDirectory, String serverName) throws IOException {
@@ -80,7 +80,7 @@ public static File createMockLibertyServerStructure(File buildDirectory, String
      *
      * @param buildDirectory The build output directory (e.g., target/ for Maven, build/ for Gradle)
      * @param serverName The name of the Liberty server
-     * @param tempDirName The name of the temporary directory (e.g., "liberty-var-cache", "tmp")
+     * @param tempDirName The name of the temporary directory (e.g., ".libertyls-var-cache", "my-temp")
      * @return The mock server directory (buildDir/{tempDirName}/wlp/usr/servers/{serverName})
      * @throws IOException if directory creation fails
      */
@@ -185,7 +185,7 @@ public static File getMockServersDirectory(File buildDirectory, String tempDirNa
      *
      * @param buildDirectory The build output directory
      * @param serverName The name of the Liberty server
-     * @return The mock server directory (buildDir/tmp/liberty-var-cache/wlp/usr/servers/{serverName})
+     * @return The mock server directory (buildDir/.libertyls-var-cache/wlp/usr/servers/{serverName})
      */
     public static File getMockServerDirectory(File buildDirectory, String serverName) {
         return getMockServerDirectory(buildDirectory, serverName, DEFAULT_TEMP_DIR_NAME);
diff --git a/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java
index 13eddf79..498c4d1c 100644
--- a/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java
+++ b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java
@@ -555,7 +555,7 @@ public void testIsMockServerInConfigWithMockServer() throws IOException {
         File configFile = new File(buildDirectory, "liberty-plugin-config.xml");
         String content = "\n" +
                 "\n" +
-                "  /path/to/tmp/liberty-var-cache/wlp\n" +
+                "  /path/to/.libertyls-var-cache/wlp\n" +
                 "";
         Files.write(configFile.toPath(), content.getBytes());
         
@@ -705,7 +705,7 @@ public void testValidateMockServerStructureWithCustomTempDir() throws IOExceptio
      */
     @Test
     public void testDefaultTempDirNameConstant() {
-        assertEquals("Default temp dir name should be tmp/liberty-var-cache",
-                "tmp/liberty-var-cache", PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME);
+        assertEquals("Default temp dir name should be .libertyls-var-cache",
+                ".libertyls-var-cache", PrepareConfigUtil.DEFAULT_TEMP_DIR_NAME);
     }
 }
\ No newline at end of file