Skip to content

Commit

Permalink
devonfw#507: improve log assertions (devonfw#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Aug 2, 2024
1 parent c89f8af commit 7def0a6
Show file tree
Hide file tree
Showing 33 changed files with 450 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeLogEntry;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.os.SystemInfoMock;

Expand All @@ -25,9 +25,9 @@ public void testMvnBuildWithoutProvidedArgumentsUsesDefaultOptions() {
BuildCommandlet buildCommandlet = context.getCommandletManager().getCommandlet(BuildCommandlet.class);
context.setCwd(context.getWorkspacePath().resolve("mvn"), context.getWorkspacePath().toString(), context.getIdeHome());
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed java in version 17.0.10_7");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed mvn in version 3.9.6");
assertLogMessage(context, IdeLogLevel.INFO, "mvn clean compile");
assertThat(context).log().hasEntries(IdeLogEntry.ofSuccess("Successfully installed java in version 17.0.10_7"),
IdeLogEntry.ofSuccess("Successfully installed mvn in version 3.9.6"),
IdeLogEntry.ofInfo("mvn clean compile"));
}

/**
Expand All @@ -42,9 +42,9 @@ public void testMvnBuildWithProvidedArguments() {
buildCommandlet.arguments.addValue("clean");
buildCommandlet.arguments.addValue("install");
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed java in version 17.0.10_7");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed mvn in version 3.9.6");
assertLogMessage(context, IdeLogLevel.INFO, "mvn clean install");
assertThat(context).log().hasEntries(IdeLogEntry.ofSuccess("Successfully installed java in version 17.0.10_7"),
IdeLogEntry.ofSuccess("Successfully installed mvn in version 3.9.6"),
IdeLogEntry.ofInfo("mvn clean install"));
}

/**
Expand All @@ -59,9 +59,9 @@ public void testGradleBuildWithProvidedArguments() {
buildCommandlet.arguments.addValue("task1");
buildCommandlet.arguments.addValue("task2");
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed java in version 17.0.10_7");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed gradle in version 8.7");
assertLogMessage(context, IdeLogLevel.INFO, "gradle task1 task2");
assertThat(context).log().hasEntries(IdeLogEntry.ofSuccess("Successfully installed java in version 17.0.10_7"),
IdeLogEntry.ofSuccess("Successfully installed gradle in version 8.7"),
IdeLogEntry.ofInfo("gradle task1 task2"));
}

/**
Expand All @@ -78,8 +78,8 @@ public void testNpmBuildWithProvidedArguments() {
buildCommandlet.arguments.addValue("start");
buildCommandlet.arguments.addValue("test");
buildCommandlet.run();
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed node in version v18.19.1");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed npm in version 9.9.2");
assertLogMessage(context, IdeLogLevel.INFO, "npm start test");
assertThat(context).log().hasEntries(IdeLogEntry.ofSuccess("Successfully installed node in version v18.19.1"),
IdeLogEntry.ofSuccess("Successfully installed npm in version 9.9.2"),
IdeLogEntry.ofInfo("npm start test"));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.devonfw.tools.ide.commandlet;

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

import org.junit.jupiter.api.Test;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeLogEntry;

/** Integration test of {@link EditionGetCommandlet}. */

Expand All @@ -29,8 +28,7 @@ public void testEditionGetCommandletRun() {
editionGet.run();

// assert
List<String> logs = context.level(IdeLogLevel.INFO).getMessages();
assertThat(logs).contains("az");
assertThat(context).log().hasMessage("az");
}

/**
Expand Down Expand Up @@ -59,7 +57,7 @@ public void testVersionGetCommandletRunPrintConfiguredEdition() {
// act
editionGet.run();
// assert
assertLogMessage(context, IdeLogLevel.INFO, "java");
assertThat(context).logAtInfo().hasMessage("java");
}

/** Test of {@link VersionGetCommandlet} run, with --installed flag, when Installed Version is null. */
Expand All @@ -74,9 +72,8 @@ public void testVersionGetCommandletRunPrintInstalledEdition() {
// act
editionGet.run();
// assert
assertLogMessage(context, IdeLogLevel.INFO, "No installation of tool java was found.");
assertLogMessage(context, IdeLogLevel.INFO, "The configured edition for tool java is java");
assertLogMessage(context, IdeLogLevel.INFO, "To install that edition call the following command:");
assertLogMessage(context, IdeLogLevel.INFO, "ide install java");
assertThat(context).log().hasEntries(IdeLogEntry.ofInfo("No installation of tool java was found."),
IdeLogEntry.ofInfo("The configured edition for tool java is java"), IdeLogEntry.ofInfo(
"To install that edition call the following command:"), IdeLogEntry.ofInfo("ide install java"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;

/** Integration test of {@link EditionListCommandlet}. */
public class EditionListCommandletTest extends AbstractIdeContextTest {
Expand All @@ -22,7 +21,6 @@ public void testEditionListCommandletRun() {
editionList.run();

// assert
assertLogMessage(context, IdeLogLevel.INFO, "mvn");
assertLogMessage(context, IdeLogLevel.INFO, "secondMvnEdition");
assertThat(context).logAtInfo().hasEntries("mvn", "secondMvnEdition");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.devonfw.tools.ide.commandlet;

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

import org.junit.jupiter.api.Test;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;

/** Integration test of {@link EditionSetCommandlet}. */
public class EditionSetCommandletTest extends AbstractIdeContextTest {
Expand All @@ -18,7 +15,7 @@ public class EditionSetCommandletTest extends AbstractIdeContextTest {
public void testEditionSetCommandletRun() {

// arrange
IdeContext context = newContext(PROJECT_BASIC);
IdeTestContext context = newContext(PROJECT_BASIC);
EditionSetCommandlet editionSet = context.getCommandletManager().getCommandlet(EditionSetCommandlet.class);
editionSet.tool.setValueAsString("mvn", context);
editionSet.edition.setValueAsString("setEdition", context);
Expand All @@ -27,8 +24,7 @@ public void testEditionSetCommandletRun() {
editionSet.run();

// assert
List<String> logs = ((IdeTestContext) context).level(IdeLogLevel.WARNING).getMessages();
assertThat(logs).containsExactly("Edition setEdition seems to be invalid");
assertThat(context).logAtWarning().hasMessage("Edition setEdition seems to be invalid");
Path settingsIdeProperties = context.getSettingsPath().resolve("ide.properties");
assertThat(settingsIdeProperties).hasContent("""
#********************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.context.IdeTestContextMock;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeLogEntry;

/**
* Test of {@link EnvironmentCommandlet}.
Expand All @@ -29,20 +29,13 @@ public void testRun() {
// act
env.run();
// assert
assertLogMessage(context, IdeLogLevel.INFO, "MVN_VERSION=\"3.9.1\""); //overwritten by conf
assertLogMessage(context, IdeLogLevel.INFO, "SOME=\"some-${UNDEFINED}\"");
assertLogMessage(context, IdeLogLevel.INFO, "BAR=\"bar-some-${UNDEFINED}\"");
assertLogMessage(context, IdeLogLevel.INFO, "IDE_TOOLS=\"mvn,eclipse\"");
assertLogMessage(context, IdeLogLevel.INFO, "ECLIPSE_VERSION=\"2023-03\"");
assertLogMessage(context, IdeLogLevel.INFO, "FOO=\"foo-bar-some-${UNDEFINED}\"");
assertLogMessage(context, IdeLogLevel.INFO, "JAVA_VERSION=\"17*\"");
assertLogMessage(context, IdeLogLevel.INFO, "INTELLIJ_EDITION=\"ultimate\"");
assertLogMessage(context, IdeLogLevel.INFO, "DOCKER_EDITION=\"docker\"");
//assert messages of debug level grouping of environment variable are present
assertLogMessage(context, IdeLogLevel.DEBUG, "from defaults:");
assertLogMessage(context, IdeLogLevel.DEBUG, "from " + settingsIdeProperties + ":");
assertLogMessage(context, IdeLogLevel.DEBUG, "from " + confIdeProperties + ":");

assertThat(context).log().hasEntriesWithNothingElseInBetween( //
IdeLogEntry.ofDebug("from defaults:"), IdeLogEntry.ofInfo("DOCKER_EDITION=\"docker\""), IdeLogEntry.ofInfo("INTELLIJ_EDITION=\"ultimate\""), //
IdeLogEntry.ofDebug("from " + settingsIdeProperties + ":"), IdeLogEntry.ofInfo("JAVA_VERSION=\"17*\""),
IdeLogEntry.ofInfo("SOME=\"some-${UNDEFINED}\""), IdeLogEntry.ofInfo("BAR=\"bar-some-${UNDEFINED}\""),
IdeLogEntry.ofInfo("IDE_TOOLS=\"mvn,eclipse\""),
IdeLogEntry.ofDebug("from " + confIdeProperties + ":"), IdeLogEntry.ofInfo("MVN_VERSION=\"3.9.1\"") //overwritten by conf
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.context.IdeTestContextMock;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.nls.NlsBundle;
import com.devonfw.tools.ide.property.KeywordProperty;
import com.devonfw.tools.ide.property.Property;
Expand Down Expand Up @@ -53,7 +52,7 @@ public void testRun() {
help.run();
// assert
assertLogoMessage(context);
assertLogMessage(context, IdeLogLevel.INFO, "Usage: ide [option]* [[commandlet] [arg]*]");
assertThat(context).logAtInfo().hasMessage("Usage: ide [option]* [[commandlet] [arg]*]");
assertOptionLogMessages(context);
}

Expand All @@ -71,9 +70,8 @@ public void testRunWithCommandlet() {
help.run();
// assert
assertLogoMessage(context);
assertLogMessage(context, IdeLogLevel.INFO, "Usage: ide [option]* mvn [<args>*]");
assertLogMessage(context, IdeLogLevel.INFO, "Tool commandlet for Maven (Build-Tool).");
assertLogMessage(context, IdeLogLevel.INFO, "usage: mvn [options] [<goal(s)>] [<phase(s)>]");
assertThat(context).logAtInfo()
.hasEntries("Usage: ide [option]* mvn [<args>*]", "Tool commandlet for Maven (Build-Tool).", "usage: mvn [options] [<goal(s)>] [<phase(s)>]");
assertOptionLogMessages(context);
}

Expand Down Expand Up @@ -139,20 +137,21 @@ public void testEnsureAllNlsPropertiesPresent(String locale) throws IOException
*/
private void assertOptionLogMessages(IdeTestContext context) {

assertLogMessage(context, IdeLogLevel.INFO, "--locale the locale (e.g. '--locale=de' for German language).");
assertLogMessage(context, IdeLogLevel.INFO, "-b | --batch enable batch mode (non-interactive).");
assertLogMessage(context, IdeLogLevel.INFO, "-d | --debug enable debug logging.");
assertLogMessage(context, IdeLogLevel.INFO, "-f | --force enable force mode.");
assertLogMessage(context, IdeLogLevel.INFO, "-o | --offline enable offline mode (skip updates or git pull, fail downloads or git clone).");
assertLogMessage(context, IdeLogLevel.INFO, "-q | --quiet disable info logging (only log success, warning or error).");
assertLogMessage(context, IdeLogLevel.INFO, "-t | --trace enable trace logging.");
assertThat(context).logAtInfo().hasEntries(
"--locale the locale (e.g. '--locale=de' for German language).",
"-b | --batch enable batch mode (non-interactive).",
"-d | --debug enable debug logging.",
"-f | --force enable force mode.",
"-o | --offline enable offline mode (skip updates or git pull, fail downloads or git clone).",
"-q | --quiet disable info logging (only log success, warning or error).",
"-t | --trace enable trace logging.");
}

/**
* Assertion for the IDE-Logo that should be displayed.
*/
private void assertLogoMessage(IdeTestContext context) {

assertLogMessage(context, IdeLogLevel.INFO, HelpCommandlet.LOGO);
assertThat(context).logAtInfo().hasMessage(HelpCommandlet.LOGO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;

public class RepositoryCommandletTest extends AbstractIdeContextTest {

Expand All @@ -33,7 +32,7 @@ public void testRunWithSpecificRepository() {
// act
rc.run();
// assert
assertLogMessage(this.context, IdeLogLevel.INFO, "Importing repository from " + PROPERTIES_FILE + " ...");
assertThat(this.context).logAtInfo().hasMessage("Importing repository from " + PROPERTIES_FILE + " ...");
}

@Test
Expand All @@ -45,8 +44,8 @@ public void testRunWithNoSpecificRepositoryAndInactive() {
// act
rc.run();
// assert
assertLogMessage(this.context, IdeLogLevel.INFO, "Importing repository from " + PROPERTIES_FILE + " ...");
assertLogMessage(this.context, IdeLogLevel.INFO, "Skipping repository - use force (-f) to setup all repositories ...");
assertThat(this.context).logAtInfo().hasEntries("Importing repository from " + PROPERTIES_FILE + " ...",
"Skipping repository - use force (-f) to setup all repositories ...");
}

@Test
Expand All @@ -62,8 +61,8 @@ public void testRunInvalidConfiguration() {
// act
rc.run();
// assert
assertLogMessage(this.context, IdeLogLevel.WARNING,
"Invalid repository configuration " + PROPERTIES_FILE + " - both 'path' and 'git-url' have to be defined.");
assertThat(this.context).logAtWarning()
.hasMessage("Invalid repository configuration " + PROPERTIES_FILE + " - both 'path' and 'git-url' have to be defined.");
}

@Test
Expand All @@ -76,7 +75,7 @@ public void testRunNoRepositoriesOrProjectsFolderFound() {
// act
rc.run();
// assert
assertLogMessage(this.context, IdeLogLevel.WARNING, "Cannot find repositories folder nor projects folder.");
assertThat(this.context).logAtWarning().hasMessage("Cannot find repositories folder nor projects folder.");
}

private void createPropertiesFile() {
Expand Down Expand Up @@ -113,4 +112,4 @@ private void saveProperties(Properties properties) {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.io.FileAccessImpl;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeLogEntry;
import com.devonfw.tools.ide.property.ToolProperty;
import com.devonfw.tools.ide.tool.dotnet.DotNet;
import com.devonfw.tools.ide.tool.eclipse.Eclipse;
Expand Down Expand Up @@ -47,9 +47,9 @@ public void testUninstallCommandletRun_WithExistingCommandlet() {
// act
uninstallCommandlet.run();
// assert
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully uninstalled " + npm);
assertLogMessage(context, IdeLogLevel.WARNING, "An installed version of " + dotnet + " does not exist");
assertThat(Files.notExists(context.getSoftwarePath().resolve(npm)));
assertThat(context).log().hasEntries(IdeLogEntry.ofSuccess("Successfully uninstalled " + npm),
IdeLogEntry.ofWarning("An installed version of " + dotnet + " does not exist"));
assertThat(context.getSoftwarePath().resolve(npm)).doesNotExist();
}

@Test
Expand All @@ -65,7 +65,7 @@ public void testUninstallCommandletRun_WithNonExistingCommandlet() {
// act
uninstallCommandlet.run();
// assert
assertLogMessage(context, IdeLogLevel.WARNING, "An installed version of " + eclipse + " does not exist");
assertThat(context).logAtWarning().hasMessage("An installed version of " + eclipse + " does not exist");
assertThat(Files.notExists(context.getSoftwarePath().resolve(eclipse)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;

/**
* Test of {@link UpdateCommandlet}.
Expand All @@ -28,7 +27,7 @@ public void testRunPullSettingsAndUpdateSoftware() {
uc.run();

// assert
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully updated settings repository.");
assertThat(context).logAtSuccess().hasMessage("Successfully updated settings repository.");
assertThat(context.getConfPath()).exists();
assertThat(context.getSoftwarePath().resolve("java")).exists();
assertThat(context.getSoftwarePath().resolve("mvn")).exists();
Expand All @@ -46,7 +45,7 @@ public void testRunTemplatesNotFound() throws IOException {
uc.run();

// assert
assertLogMessage(context, IdeLogLevel.WARNING, "Templates folder is missing in settings repository.");
assertThat(context).logAtWarning().hasEntries("Templates folder is missing in settings repository.");
}

private void deleteTemplatesFolder(IdeContext context) throws IOException {
Expand Down
Loading

0 comments on commit 7def0a6

Please sign in to comment.