Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#342: implement unit tests for mvn ToolCommandlet #373

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
06ef780
first implementation
mvomiero May 28, 2024
acb5bfc
code cleanup
mvomiero May 31, 2024
21bdc6f
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero May 31, 2024
0224dba
commandlet arguments refactoring in test
mvomiero May 31, 2024
6d9beea
remove plugins
mvomiero May 31, 2024
cf008fd
check settingssecurityfile
mvomiero May 31, 2024
43dd789
settings.xml added to git
mvomiero Jun 3, 2024
5ee8cd7
update mvn rights
mvomiero Jun 3, 2024
a58348a
add mvn executable for windows
mvomiero Jun 3, 2024
3208742
fix operating system name
mvomiero Jun 3, 2024
4f5137e
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
mvomiero Jun 14, 2024
eed1d84
implement change requests
mvomiero Jun 14, 2024
252eaaf
implement change request
mvomiero Jun 14, 2024
5b609cc
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jun 19, 2024
e8d1609
implement assertion for content of settings files
mvomiero Jun 19, 2024
ea57690
add javadoc
mvomiero Jun 20, 2024
c2258cd
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jun 21, 2024
051ee46
Update cli/src/test/resources/ide-projects/mvn/project/workspaces/mai…
mvomiero Jun 26, 2024
d46ae28
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
hohwille Jul 1, 2024
0770b17
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jul 2, 2024
5d69643
implement change request and fix bug format encrypted values
mvomiero Jul 2, 2024
483d238
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jul 2, 2024
33408ca
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
hohwille Jul 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cli/src/main/java/com/devonfw/tools/ide/tool/mvn/Mvn.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public class Mvn extends PluginBasedCommandlet {
/** The name of the settings.xml */
public static final String SETTINGS_FILE = "settings.xml";

/** The name of the m2 repository */
public static final String M2_CONFIG_FOLDER = ".m2";
/** The name of the settings-security.xml */
public static final String SETTINGS_SECURITY_FILE = "settings-security.xml";

private static final String SETTINGS_SECURITY_FILE = "settings-security.xml";
/** The name of the M2 repo */
public static final String M2_CONFIG_FOLDER = ".m2";
hohwille marked this conversation as resolved.
Show resolved Hide resolved

hohwille marked this conversation as resolved.
Show resolved Hide resolved
private static final String DOCUMENTATION_PAGE_CONF = "https://github.com/devonfw/IDEasy/blob/main/documentation/conf.adoc";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* Mock implementation of {@link GitContext}.
*/
public class GitContextMock implements GitContext {

private static final String MOCKED_URL_VALUE = "mocked url value";

@Override
public void pullOrCloneIfNeeded(String repoUrl, String branch, Path targetRepository) {

Expand Down Expand Up @@ -49,6 +52,6 @@ public void cleanup(Path targetRepository) {
@Override
public String retrieveGitUrl(Path repository) {

return null;
return MOCKED_URL_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import com.devonfw.tools.ide.repo.ToolRepository;

import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;

/**
* Implementation of {@link IdeContext} for testing.
*/
public class IdeTestContext extends AbstractIdeTestContext {

private LinkedList<String> inputValues;

/**
* The constructor.
*
Expand Down Expand Up @@ -62,4 +66,20 @@ public static IdeTestContext of() {
return new IdeTestContext(Path.of("/"));
}

/**
* Set a mocked value to be returned by the {@link IdeContext#askForInput(String)} method
*
* @param values a {@link LinkedList} with the mocked input value
*/
public void setInputValues(List<String> values) {

this.inputValues = new LinkedList<>(values);
}

@Override
public String askForInput(String message) {

return inputValues.isEmpty() ? null : inputValues.poll();
}

}
95 changes: 95 additions & 0 deletions cli/src/test/java/com/devonfw/tools/ide/tool/mvn/MvnTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.devonfw.tools.ide.tool.mvn;

import com.devonfw.tools.ide.commandlet.InstallCommandlet;
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Integration test of {@link Mvn}.
*/
public class MvnTest extends AbstractIdeContextTest {

private static final String PROJECT_MVN = "mvn";

private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\[(.*?)\\]");
hohwille marked this conversation as resolved.
Show resolved Hide resolved

/**
* Tests the installation of {@link Mvn}
*
* @throws IOException if an I/O error occurs during the installation process
*/
@Test
public void testMvnInstall() throws IOException {

// arrange
IdeTestContext context = newContext(PROJECT_MVN);
context.setInputValues(List.of("testLogin", "testPassword"));
InstallCommandlet install = context.getCommandletManager().getCommandlet(InstallCommandlet.class);
install.tool.setValueAsString("mvn", context);

// act
install.run();

// assert
checkInstallation(context);
}

/**
* Tests the execution of {@link Mvn}
*
* @throws IOException if an I/O error occurs during the installation process
*/
@Test
public void testMvnRun() throws IOException {
// arrange
IdeTestContext context = newContext(PROJECT_MVN);
context.setInputValues(List.of("testLogin", "testPassword"));
InstallCommandlet install = context.getCommandletManager().getCommandlet(InstallCommandlet.class);
install.tool.setValueAsString("mvn", context);
Mvn commandlet = (Mvn) install.tool.getValue();
commandlet.arguments.addValue("foo");
commandlet.arguments.addValue("bar");

// act
commandlet.run();

// assert
assertLogMessage(context, IdeLogLevel.INFO, "mvn " + "foo bar");
checkInstallation(context);
}

private void checkInstallation(IdeTestContext context) throws IOException {

assertThat(context.getSoftwarePath().resolve("java/bin/java")).exists();

assertThat(context.getSoftwarePath().resolve("mvn/.ide.software.version")).exists().hasContent("3.9.7");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed mvn in version 3.9.7");

Path settingsFile = context.getConfPath().resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_FILE);
assertThat(settingsFile).exists();
assertFileContent(settingsFile, List.of("testLogin", "testPassword"));

Path settingsSecurityFile = context.getConfPath().resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_SECURITY_FILE);
assertThat(settingsSecurityFile).exists();
assertFileContent(settingsSecurityFile, List.of("masterPassword"));
}

private void assertFileContent(Path filePath, List<String> expectedValues) throws IOException {

String content = new String(Files.readAllBytes(filePath));
Matcher matcher = VARIABLE_PATTERN.matcher(content);
List<String> values = matcher.results().map(matchResult -> matchResult.group(1)).collect(Collectors.toList());

assertThat(values).containsExactlyElementsOf(expectedValues);
}
}
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/mvn/_ide/urls/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the download metadata
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the users HOME directory
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/mvn/project/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_HOME directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JAVA_VERSION=17.0.10_7
MVN_VERSION=3.9.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<localRepository>${env.M2_REPO}</localRepository>

<servers>
<server>
<id>repository</id>
<username>$[login]</username>
<password>$[password]</password>
</server>
</servers>

</settings>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the main workspace of jmc test case
mvomiero marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/mvn/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_ROOT directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "java mvn"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

if [ "$1" == "--encrypt-master-password" ]; then
echo "masterPassword"
elif [ "$1" == "--encrypt-password" ]; then
echo "$2"
else
echo "mvn $*"
fi
Loading