From e517a38e442ae4cf96d406b2c106a32bff721158 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 16 Jan 2025 21:12:39 +0100 Subject: [PATCH] #898: fixed exception adjusted tests --- ...AbstractVersionOrEditionGetCommandlet.java | 14 +-- .../commandlet/EditionGetCommandletTest.java | 45 +++++-- .../commandlet/VersionGetCommandletTest.java | 112 ++++++++++++++++-- .../project/conf/ide.properties | 2 + .../project/software/java/bin/.gitkeep | 0 .../software/mvn/.devon.software.version | 1 + 6 files changed, 145 insertions(+), 29 deletions(-) delete mode 100644 cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/software/java/bin/.gitkeep create mode 100644 cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/software/mvn/.devon.software.version diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/AbstractVersionOrEditionGetCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/AbstractVersionOrEditionGetCommandlet.java index 84777ed77..2ca13fd78 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/AbstractVersionOrEditionGetCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/AbstractVersionOrEditionGetCommandlet.java @@ -76,6 +76,9 @@ public void run() { Object installedValue = getInstalledValue(commandlet); boolean getInstalledValue = this.installed.isTrue(); boolean getConfiguredValue = this.configured.isTrue(); + if (installedValue == null && getInstalledValue && !getConfiguredValue) { + throw new CliException("Tool " + commandlet + " is not installed.", 1); + } if (getInstalledValue == getConfiguredValue) { if (getInstalledValue) { // both --configured and --installed logToolInfo(logger, commandlet, configuredValue, installedValue); @@ -91,7 +94,6 @@ public void run() { } else { if (getInstalledValue) { if (installedValue == null) { - logToolInfo(logger, commandlet, configuredValue, null); } else { logger.log(installedValue.toString()); @@ -107,15 +109,11 @@ private void logToolInfo(IdeSubLogger logger, ToolCommandlet commandlet, Object String property = getPropertyToGet(); String toolName = commandlet.getName(); if (installedValue == null) { - throw new CliException("No installation of tool " + toolName + " was found.", 1); - } else { - logger.log("The installed {} for tool {} is {}.", property, toolName, installedValue); - } - if (configuredValue == null) { - logger.log("There is no configured {} for tool {}.", property, toolName); + logger.log("No installation of tool {} was found.", toolName); } else { - logger.log("The configured {} for tool {} is {}.", property, toolName, configuredValue); + logger.log("The installed {} for tool {} is {}", property, toolName, installedValue); } + logger.log("The configured {} for tool {} is {}", property, toolName, configuredValue); if (!Objects.equals(configuredValue, installedValue)) { logger.log("To install that {} call the following command:", property); logger.log("ide install {}", toolName); diff --git a/cli/src/test/java/com/devonfw/tools/ide/commandlet/EditionGetCommandletTest.java b/cli/src/test/java/com/devonfw/tools/ide/commandlet/EditionGetCommandletTest.java index 535385aa2..47d0caa51 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/commandlet/EditionGetCommandletTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/commandlet/EditionGetCommandletTest.java @@ -48,8 +48,8 @@ public void testEditionGetCommandletRunWithoutFlagsAndNotInstalled() { // assert assertThat(context).log().hasEntries( IdeLogEntry.ofWarning("Undefined edition " + tool + " of tool " + tool), - IdeLogEntry.ofProcessable("The installed edition for tool " + tool + " is " + tool + "."), - IdeLogEntry.ofProcessable("The configured edition for tool " + tool + " is " + tool + ".")); + IdeLogEntry.ofProcessable("The installed edition for tool " + tool + " is " + tool), + IdeLogEntry.ofProcessable("The configured edition for tool " + tool + " is " + tool)); } /** Test of {@link VersionGetCommandlet} run with --configured flag and installed tool */ @@ -67,6 +67,21 @@ public void testEditionGetCommandletConfiguredEditionAndInstalled() { assertThat(context).log(IdeLogLevel.PROCESSABLE).hasMessage("java"); } + /** Test of {@link VersionGetCommandlet} run with --configured flag and not installed tool */ + @Test + public void testEditionGetCommandletNotInstalledConfiguredEdition() { + + // arrange + IdeTestContext context = newContext(PROJECT); + EditionGetCommandlet editionGet = context.getCommandletManager().getCommandlet(EditionGetCommandlet.class); + editionGet.tool.setValueAsString("tomcat", context); + editionGet.configured.setValue(true); + // act + editionGet.run(); + // assert + assertThat(context).log(IdeLogLevel.PROCESSABLE).hasMessage("basic"); + } + /** Test of {@link VersionGetCommandlet} run with --configured flag but tool was not installed */ @Test public void testEditionGetCommandletConfiguredEditionButNotInstalled() { @@ -82,7 +97,7 @@ public void testEditionGetCommandletConfiguredEditionButNotInstalled() { assertThat(context).log(IdeLogLevel.PROCESSABLE).hasMessage("az"); } - /** Test of {@link EditionGetCommandlet} run, when tool is not installed but --installed flag was used. */ + /** Test of {@link EditionGetCommandlet} run, when tool is not installed but --installed flag was used and --configured was not used. */ @Test public void testEditionGetCommandletToolNotInstalledButInstalledFlagInUseThrowsException() { @@ -95,35 +110,39 @@ public void testEditionGetCommandletToolNotInstalledButInstalledFlagInUseThrowsE assertThrows(CliException.class, () -> editionGet.run()); } - /** Test of {@link EditionGetCommandlet} run, with --installed flag, when Installed Version is null. */ + /** Test of {@link EditionGetCommandlet} run, with --installed flag, when tool is installed. */ @Test - public void testEditionGetCommandletInstalledEditionToolNotInstalled() { + public void testEditionGetCommandletInstalledEditionToolInstalled() { // arrange IdeTestContext context = newContext(PROJECT); EditionGetCommandlet editionGet = context.getCommandletManager().getCommandlet(EditionGetCommandlet.class); - editionGet.tool.setValueAsString("java", context); + editionGet.tool.setValueAsString("mvn", context); editionGet.installed.setValue(true); // act editionGet.run(); // assert - assertThat(context).log().hasEntries(IdeLogEntry.ofProcessable("java")); + assertThat(context).log(IdeLogLevel.PROCESSABLE).hasMessage("mvn"); } - - /** Test of {@link EditionGetCommandlet} run, with --installed flag, when tool is installed. */ + /** Test of {@link EditionGetCommandlet} run, with --installed and --configured flag, when tool is not installed. */ @Test - public void testEditionGetCommandletInstalledEditionToolInstalled() { + public void testEditionGetCommandletInstalledConfiguredEditionToolNotInstalled() { // arrange + String tool = "tomcat"; IdeTestContext context = newContext(PROJECT); - mockInstallTool(context, "mvn"); EditionGetCommandlet editionGet = context.getCommandletManager().getCommandlet(EditionGetCommandlet.class); - editionGet.tool.setValueAsString("mvn", context); + editionGet.tool.setValueAsString(tool, context); + editionGet.configured.setValue(true); editionGet.installed.setValue(true); // act editionGet.run(); // assert - assertThat(context).log(IdeLogLevel.PROCESSABLE).hasMessage("mvn"); + assertThat(context).log().hasEntries( + IdeLogEntry.ofProcessable("No installation of tool " + tool + " was found."), + IdeLogEntry.ofProcessable("The configured edition for tool " + tool + " is basic"), + IdeLogEntry.ofProcessable("ide install " + tool) + ); } } diff --git a/cli/src/test/java/com/devonfw/tools/ide/commandlet/VersionGetCommandletTest.java b/cli/src/test/java/com/devonfw/tools/ide/commandlet/VersionGetCommandletTest.java index 5daaf9106..8a2a68f14 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/commandlet/VersionGetCommandletTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/commandlet/VersionGetCommandletTest.java @@ -19,21 +19,75 @@ public class VersionGetCommandletTest extends AbstractIdeContextTest { private static final String PROJECT = "edition-version-get-uninstall"; /** - * Test of {@link VersionGetCommandlet} run, when Installed Version is null. + * Test of {@link VersionGetCommandlet} run, when Installed Version is null but configured version is set. */ @Test - public void testVersionGetCommandletNotInstalledRunThrowsException() { + public void testVersionGetCommandletNotInstalledRun() { // arrange IdeTestContext context = newContext(PROJECT); VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class); - versionGet.tool.setValueAsString("java", context); + versionGet.tool.setValueAsString("az", context); + // act + versionGet.run(); + // assert + assertThat(context).log().hasEntries( + IdeLogEntry.ofProcessable("No installation of tool az was found."), + IdeLogEntry.ofProcessable("The configured version for tool az is 1.0.1"), + IdeLogEntry.ofProcessable("ide install az")); + } + + /** + * Test of {@link VersionGetCommandlet} run, when --installed flag is set, --configured is not set and installed version is null. + */ + @Test + public void testVersionGetCommandletNotInstalledWithInstalledFlagRunThrowsException() { + + // arrange + IdeTestContext context = newContext(PROJECT); + VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class); + versionGet.tool.setValueAsString("tomcat", context); + versionGet.installed.setValue(true); // act/assert assertThrows(CliException.class, () -> versionGet.run()); } /** - * Test of {@link VersionGetCommandlet} run. + * Test of {@link VersionGetCommandlet} run, when --configured flag is set, configured version is not set and installed version is null. + */ + @Test + public void testVersionGetCommandletNotInstalledConfiguredWithConfiguredFlagRun() { + + // arrange + IdeTestContext context = newContext(PROJECT_SETTINGS); + VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class); + versionGet.tool.setValueAsString("az", context); + versionGet.configured.setValue(true); + // act + versionGet.run(); + // assert + assertThat(context).log(IdeLogLevel.PROCESSABLE).hasMessage("*"); + } + + /** + * Test of {@link VersionGetCommandlet} run, when --configured flag is set and Installed Version is null. + */ + @Test + public void testVersionGetCommandletNotInstalledWithConfigured() { + + // arrange + IdeTestContext context = newContext(PROJECT); + VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class); + versionGet.tool.setValueAsString("az", context); + versionGet.configured.setValue(true); + // act + versionGet.run(); + // assert + assertThat(context).log(IdeLogLevel.PROCESSABLE).hasMessage("1.0.1"); + } + + /** + * Test of {@link VersionGetCommandlet} run, where "configured" flag is set and the configured version is 3.9.1. */ @Test public void testVersionGetCommandletConfiguredRun() { @@ -50,13 +104,13 @@ public void testVersionGetCommandletConfiguredRun() { } /** - * Test of {@link VersionGetCommandlet} run with the installed flag, where the installed version is 3.9.4. + * Test of {@link VersionGetCommandlet} run with the "installed" flag, where the installed version is 3.9.4. */ @Test public void testVersionGetCommandletInstalledRun() { // arrange - IdeTestContext context = newContext(PROJECT_SETTINGS); + IdeTestContext context = newContext(PROJECT); VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class); // act versionGet.tool.setValueAsString("mvn", context); @@ -83,6 +137,48 @@ public void testVersionGetCommandletConfiguredStarRun() { assertThat(context).log(IdeLogLevel.PROCESSABLE).hasMessage("*"); } + /** + * Test of {@link VersionGetCommandlet} run with the "configured" and the "installed" flag, where the configured version is "any" (*). + */ + @Test + public void testVersionGetCommandletConfiguredInstalledRun() { + + // arrange + IdeTestContext context = newContext(PROJECT_SETTINGS); + VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class); + // act + versionGet.tool.setValueAsString("mvn", context); + versionGet.configured.setValue(true); + versionGet.installed.setValue(true); + versionGet.run(); + // assert + assertThat(context).log().hasEntries( + IdeLogEntry.ofProcessable("The installed version for tool mvn is 3.9.4"), + IdeLogEntry.ofProcessable("The configured version for tool mvn is *"), + IdeLogEntry.ofProcessable("ide install mvn")); + } + + /** + * Test of {@link VersionGetCommandlet} run with the "configured" and the "installed" flag, where the configured version is 3.9.1. + */ + @Test + public void testVersionGetCommandletConfiguredInstalledSpecificRun() { + + // arrange + IdeTestContext context = newContext(PROJECT); + VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class); + // act + versionGet.tool.setValueAsString("mvn", context); + versionGet.configured.setValue(true); + versionGet.installed.setValue(true); + versionGet.run(); + // assert + assertThat(context).log().hasEntries( + IdeLogEntry.ofProcessable("The installed version for tool mvn is 3.9.4"), + IdeLogEntry.ofProcessable("The configured version for tool mvn is 3.9.1"), + IdeLogEntry.ofProcessable("ide install mvn")); + } + /** * Test of {@link VersionGetCommandlet} run, where a specific version is installed (mvn 3.9.4) but no specific version is configured (configured version *). */ @@ -97,8 +193,8 @@ public void testVersionGetCommandletMatchInstalledToConfiguredStarRun() { versionGet.run(); // assert assertThat(context).log().hasEntries( - IdeLogEntry.ofProcessable("The installed version for tool mvn is 3.9.4."), - IdeLogEntry.ofProcessable("The configured version for tool mvn is *."), + IdeLogEntry.ofProcessable("The installed version for tool mvn is 3.9.4"), + IdeLogEntry.ofProcessable("The configured version for tool mvn is *"), IdeLogEntry.ofProcessable("ide install mvn") ); } diff --git a/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/conf/ide.properties b/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/conf/ide.properties index e9e1fcdde..31961502c 100644 --- a/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/conf/ide.properties +++ b/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/conf/ide.properties @@ -1 +1,3 @@ MVN_VERSION=3.9.1 +AZ_VERSION=1.0.1 +TOMCAT_EDITION=basic diff --git a/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/software/java/bin/.gitkeep b/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/software/java/bin/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/software/mvn/.devon.software.version b/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/software/mvn/.devon.software.version new file mode 100644 index 000000000..e0d61b5b0 --- /dev/null +++ b/cli/src/test/resources/ide-projects/edition-version-get-uninstall/project/software/mvn/.devon.software.version @@ -0,0 +1 @@ +3.9.4