From 31d87196e64a0cce783ea3d2bde6ced538d766c1 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Mon, 17 Feb 2025 17:36:46 +0100 Subject: [PATCH] #1024: added tests of WindowsHelperImpl --- .../devonfw/tools/ide/os/WindowsHelper.java | 1 - .../tools/ide/os/WindowsHelperImpl.java | 11 ++++ .../ide/context/AbstractIdeTestContext.java | 3 ++ .../tools/ide/os/WindowsHelperImplTest.java | 51 +++++++++++++++++++ .../basic/project/home/environment.properties | 4 +- 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 cli/src/test/java/com/devonfw/tools/ide/os/WindowsHelperImplTest.java diff --git a/cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelper.java b/cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelper.java index 8500b3bce..a2dc85cd6 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelper.java +++ b/cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelper.java @@ -35,5 +35,4 @@ static WindowsHelper get(IdeContext context) { // IdeContext API is already too large return ((AbstractIdeContext) context).getWindowsHelper(); } - } diff --git a/cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java b/cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java index b57a975ce..24607456f 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java @@ -44,6 +44,17 @@ public String getRegistryValue(String path, String key) { ProcessResult result = this.context.newProcess().executable("reg").addArgs("query", path, "/v", key).run(ProcessMode.DEFAULT_CAPTURE); List out = result.getOut(); + return retrieveRegString(key, out); + } + + /** + * Parses the result of a registry query and outputs the given key. + * + * @param key the key to look for. + * @param out List of keys from registry query result. + * @return the registry value. + */ + protected String retrieveRegString(String key, List out) { for (String line : out) { int i = line.indexOf(key); if (i >= 0) { diff --git a/cli/src/test/java/com/devonfw/tools/ide/context/AbstractIdeTestContext.java b/cli/src/test/java/com/devonfw/tools/ide/context/AbstractIdeTestContext.java index d439a58ee..2fab99e10 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/context/AbstractIdeTestContext.java +++ b/cli/src/test/java/com/devonfw/tools/ide/context/AbstractIdeTestContext.java @@ -293,4 +293,7 @@ protected Path getIdeRootPathFromEnv() { return null; } + public void setUserEnvironmentVariable(String userEnvironmentVariable) { + + } } diff --git a/cli/src/test/java/com/devonfw/tools/ide/os/WindowsHelperImplTest.java b/cli/src/test/java/com/devonfw/tools/ide/os/WindowsHelperImplTest.java new file mode 100644 index 000000000..5add21cfa --- /dev/null +++ b/cli/src/test/java/com/devonfw/tools/ide/os/WindowsHelperImplTest.java @@ -0,0 +1,51 @@ +package com.devonfw.tools.ide.os; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.devonfw.tools.ide.context.AbstractIdeContextTest; +import com.devonfw.tools.ide.context.AbstractIdeTestContext; +import com.devonfw.tools.ide.context.IdeSlf4jContext; + +/** + * Tests for {@link WindowsHelperImpl}. + */ +public class WindowsHelperImplTest extends AbstractIdeContextTest { + + /** + * Tests if the USER_PATH registry entry can be parsed properly. + */ + @Test + public void testWindowsHelperParseRegString() { + // arrange + AbstractIdeTestContext context = new IdeSlf4jContext(); + WindowsHelperImpl helper = new WindowsHelperImpl(context); + List output = new ArrayList<>(); + output.add(""); + output.add("HKEY_CURRENT_USER\\Environment"); + output.add(" PATH REG_SZ D:\\projects\\_ide\\installation\\bin;"); + output.add(""); + // act + String regString = helper.retrieveRegString("PATH", output); + // assert + assertThat(regString).isEqualTo("D:\\projects\\_ide\\installation\\bin;"); + } + + /** + * Tests if an empty list of outputs will result in null. + */ + @Test + public void testWindowsHelperParseEmptyRegStringReturnsNull() { + // arrange + AbstractIdeTestContext context = new IdeSlf4jContext(); + WindowsHelperImpl helper = new WindowsHelperImpl(context); + List output = new ArrayList<>(); + // act + String regString = helper.retrieveRegString("PATH", output); + // assert + assertThat(regString).isNull(); + } + +} diff --git a/cli/src/test/resources/ide-projects/basic/project/home/environment.properties b/cli/src/test/resources/ide-projects/basic/project/home/environment.properties index 588d80d74..11d416647 100644 --- a/cli/src/test/resources/ide-projects/basic/project/home/environment.properties +++ b/cli/src/test/resources/ide-projects/basic/project/home/environment.properties @@ -1,2 +1,4 @@ # if this file is present, it will replace and mock System.getenv in the text context -PATH="/usr/bin:${IDE_ROOT}/_ide/bin" +#PATH="/usr/bin:${IDE_ROOT}/_ide/bin" +PATH=${IDE_ROOT}/_ide/bin +