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..5c072df5 --- /dev/null +++ b/src/main/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtil.java @@ -0,0 +1,427 @@ +/** + * (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; +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. + * Provides common logic for creating mock Liberty server structures + * and managing configuration preparation across Maven and Gradle plugins. + */ +public class PrepareConfigUtil { + + /** + * Default name for the temporary directory used for mock Liberty server structures. + */ + public static final String DEFAULT_TEMP_DIR_NAME = ".libertyls-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/.libertyls-var-cache/
+ * └── 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/.libertyls-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., ".libertyls-var-cache", "my-temp")
+ * @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 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");
+ 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 using the default temporary directory name.
+ *
+ * @param buildDirectory The build output directory
+ * @return The mock install directory (buildDir/tmp/liberty-var-cache/wlp)
+ */
+ public static File getMockInstallDirectory(File buildDirectory) {
+ 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 using the default temporary directory name.
+ *
+ * @param buildDirectory The build output directory
+ * @return The mock user directory (buildDir/tmp/liberty-var-cache/wlp/usr)
+ */
+ public static File getMockUserDirectory(File 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 using the default temporary directory name.
+ *
+ * @param buildDirectory The build output directory
+ * @return The mock servers directory (buildDir/tmp/liberty-var-cache/wlp/usr/servers)
+ */
+ public static File getMockServersDirectory(File 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 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/.libertyls-var-cache/wlp/usr/servers/{serverName})
+ */
+ public static File getMockServerDirectory(File buildDirectory, String serverName) {
+ 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 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) {
+ 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();
+ }
+
+ /**
+ * 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 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(tempDirName);
+ } catch (IOException e) {
+ return false;
+ }
+ }
+
+ /**
+ * 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
+ * - 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) {
+ 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, tempDirName)) {
+ return false;
+ }
+
+ // 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, 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() &&
+ mockServersDir.exists() && mockServersDir.isDirectory() &&
+ mockServerDir.exists() && mockServerDir.isDirectory();
+ }
+
+ /**
+ * 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 {
+ 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);
+ }
+ }
+
+ /**
+ * 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..498c4d1c
--- /dev/null
+++ b/src/test/java/io/openliberty/tools/common/plugins/util/PrepareConfigUtilTest.java
@@ -0,0 +1,711 @@
+/**
+ * (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.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+
+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 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());
+
+ 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/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);
+ }
+
+ /**
+ * 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/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);
+ }
+
+ /**
+ * 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/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);
+ }
+
+ /**
+ * 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, 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());
+ }
+
+ /**
+ * Test cleanMockServerStructure when structure doesn't exist.
+ * Should not throw an exception.
+ */
+ @Test
+ public void testCleanMockServerStructureWhenNotExists() throws IOException {
+ 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("liberty-var-cache 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));
+ }
+
+ /**
+ * 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" +
+ "