Skip to content

Commit

Permalink
Merge c2258cd into ee596da
Browse files Browse the repository at this point in the history
  • Loading branch information
mvomiero authored Jun 21, 2024
2 parents ee596da + c2258cd commit b44f945
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 4 deletions.
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";

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("\\[(.*?)\\]");

/**
* 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
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

0 comments on commit b44f945

Please sign in to comment.