Skip to content

Commit 31d8719

Browse files
devonfw#1024: added tests of WindowsHelperImpl
1 parent 075a79c commit 31d8719

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelper.java

-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@ static WindowsHelper get(IdeContext context) {
3535
// IdeContext API is already too large
3636
return ((AbstractIdeContext) context).getWindowsHelper();
3737
}
38-
3938
}

cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java

+11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public String getRegistryValue(String path, String key) {
4444

4545
ProcessResult result = this.context.newProcess().executable("reg").addArgs("query", path, "/v", key).run(ProcessMode.DEFAULT_CAPTURE);
4646
List<String> out = result.getOut();
47+
return retrieveRegString(key, out);
48+
}
49+
50+
/**
51+
* Parses the result of a registry query and outputs the given key.
52+
*
53+
* @param key the key to look for.
54+
* @param out List of keys from registry query result.
55+
* @return the registry value.
56+
*/
57+
protected String retrieveRegString(String key, List<String> out) {
4758
for (String line : out) {
4859
int i = line.indexOf(key);
4960
if (i >= 0) {

cli/src/test/java/com/devonfw/tools/ide/context/AbstractIdeTestContext.java

+3
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,7 @@ protected Path getIdeRootPathFromEnv() {
293293
return null;
294294
}
295295

296+
public void setUserEnvironmentVariable(String userEnvironmentVariable) {
297+
298+
}
296299
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.devonfw.tools.ide.os;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
9+
import com.devonfw.tools.ide.context.AbstractIdeTestContext;
10+
import com.devonfw.tools.ide.context.IdeSlf4jContext;
11+
12+
/**
13+
* Tests for {@link WindowsHelperImpl}.
14+
*/
15+
public class WindowsHelperImplTest extends AbstractIdeContextTest {
16+
17+
/**
18+
* Tests if the USER_PATH registry entry can be parsed properly.
19+
*/
20+
@Test
21+
public void testWindowsHelperParseRegString() {
22+
// arrange
23+
AbstractIdeTestContext context = new IdeSlf4jContext();
24+
WindowsHelperImpl helper = new WindowsHelperImpl(context);
25+
List<String> output = new ArrayList<>();
26+
output.add("");
27+
output.add("HKEY_CURRENT_USER\\Environment");
28+
output.add(" PATH REG_SZ D:\\projects\\_ide\\installation\\bin;");
29+
output.add("");
30+
// act
31+
String regString = helper.retrieveRegString("PATH", output);
32+
// assert
33+
assertThat(regString).isEqualTo("D:\\projects\\_ide\\installation\\bin;");
34+
}
35+
36+
/**
37+
* Tests if an empty list of outputs will result in null.
38+
*/
39+
@Test
40+
public void testWindowsHelperParseEmptyRegStringReturnsNull() {
41+
// arrange
42+
AbstractIdeTestContext context = new IdeSlf4jContext();
43+
WindowsHelperImpl helper = new WindowsHelperImpl(context);
44+
List<String> output = new ArrayList<>();
45+
// act
46+
String regString = helper.retrieveRegString("PATH", output);
47+
// assert
48+
assertThat(regString).isNull();
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# if this file is present, it will replace and mock System.getenv in the text context
2-
PATH="/usr/bin:${IDE_ROOT}/_ide/bin"
2+
#PATH="/usr/bin:${IDE_ROOT}/_ide/bin"
3+
PATH=${IDE_ROOT}/_ide/bin
4+

0 commit comments

Comments
 (0)