From 9e6bb4ad1987f3a2e6e5fba9b90b06c73ec1dbf3 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Wed, 6 Mar 2024 16:07:05 +0100 Subject: [PATCH 1/3] Replace empty property with "-". Otherwise the hyperlink is not clickable. --- .../analysis/core/model/PropertyStatistics.java | 2 +- .../core/model/PropertyStatisticsTest.java | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java b/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java index 2181260f92..10e2cbd815 100644 --- a/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java +++ b/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java @@ -83,7 +83,7 @@ public String getProperty() { * @return the display name */ public String getDisplayName(final String key) { - return propertyFormatter.apply(key); + return StringUtils.defaultIfBlank(propertyFormatter.apply(key), "-"); } /** diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java index 8104f629cc..30c596d800 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java @@ -21,7 +21,7 @@ class PropertyStatisticsTest { private static final String KEY = "key"; /** - * Verifies that getTotal() returns the total number of issues if there is one issues. + * Verifies that getTotal() returns the total number of issues if there is one issue. */ @Test void shouldReturnTotalNumber() { @@ -37,7 +37,7 @@ void shouldReturnTotalNumber() { } /** - * Verifies that getTotal() returns the total number of issues if there is one issues. + * Verifies that getTotal() returns the total number of issues if there is one issue. */ @Test void shouldReturnTotalNumberMoreIssues() { @@ -136,7 +136,7 @@ void shouldReturnAllKeys() { } /** - * Verifies that getKeys() returns empty string if the instances for this property is empty. + * Verifies that getKeys() returns "-" string if the instances for this property are empty. */ @Test void shouldReturnEmptyStringKeys() { @@ -147,7 +147,7 @@ void shouldReturnEmptyStringKeys() { PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", Function.identity()); - assertThat(statistics).hasOnlyKeys(""); + assertThat(statistics).hasOnlyKeys("-"); } } @@ -178,7 +178,7 @@ void shouldReturnMaxValueZero() { } /** - * Verifies that getMax() returns one if there is one issues. + * Verifies that getMax() returns one if there is one issue. */ @Test void shouldReturnMaxValue() { @@ -245,7 +245,7 @@ void shouldReturnCountEmpty() { } /** - * Verifies that getCount() returns one if there is one issues for the specified property instance. + * Verifies that getCount() returns one if there is one issue for the specified property instance. */ @Test void shouldReturnCountOne() { @@ -546,7 +546,7 @@ void shouldReturnNewCountOne() { } /** - * Verifies that getNewCount() returns zero if there is one issues for the specified property instance, but it isn't new. + * Verifies that getNewCount() returns zero if there is one issue for the specified property instance, but it isn't new. */ @Test void shouldReturnNewCountZero() { From 3a6c847a31c820bd30f8eaeaccbc1a18ad698efe Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Wed, 6 Mar 2024 22:51:21 +0100 Subject: [PATCH 2/3] Refactor test case. --- .../core/model/PropertyStatistics.java | 4 +- .../src/main/resources/issues/property.jelly | 2 +- .../core/model/PropertyStatisticsTest.java | 591 ++++-------------- 3 files changed, 135 insertions(+), 462 deletions(-) diff --git a/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java b/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java index 10e2cbd815..13835089ea 100644 --- a/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java +++ b/plugin/src/main/java/io/jenkins/plugins/analysis/core/model/PropertyStatistics.java @@ -151,9 +151,9 @@ public long getNewCount(final String key) { * @param key * the property instance * - * @return the number of high-severity issues + * @return the number of error issues */ - public long getErrorsCount(final String key) { + public long getErrorCount(final String key) { return getReportFor(key).getSizeOf(Severity.ERROR); } diff --git a/plugin/src/main/resources/issues/property.jelly b/plugin/src/main/resources/issues/property.jelly index 161abbfb6b..41d7488b60 100644 --- a/plugin/src/main/resources/issues/property.jelly +++ b/plugin/src/main/resources/issues/property.jelly @@ -47,7 +47,7 @@ ${count} ${newCount} - diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java index 30c596d800..5d9a80c8a4 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java @@ -1,11 +1,14 @@ package io.jenkins.plugins.analysis.core.model; import java.util.NoSuchElementException; -import java.util.Set; import java.util.function.Function; +import org.apache.commons.lang3.StringUtils; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.jupiter.api.Test; +import com.google.errorprone.annotations.MustBeClosed; + import edu.hm.hafner.analysis.IssueBuilder; import edu.hm.hafner.analysis.Report; import edu.hm.hafner.analysis.Severity; @@ -20,80 +23,111 @@ class PropertyStatisticsTest { private static final String KEY = "key"; - /** - * Verifies that getTotal() returns the total number of issues if there is one issue. - */ @Test - void shouldReturnTotalNumber() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setCategory("error").build()); + void shouldHandleEmptyReport() { + PropertyStatistics statistics = createStatistics(new Report()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); + assertThat(statistics) + .hasTotal(0) + .hasTotalNewIssues(0) + .hasNoKeys() + .hasProperty("category") + .hasMax(0); - assertThat(statistics).hasTotal(issues.size()); - } + assertThatExceptionIsThrownBy(() -> statistics.getCount("error")); + assertThatExceptionIsThrownBy(() -> statistics.getErrorCount("error")); + assertThatExceptionIsThrownBy(() -> statistics.getHighCount("error")); + assertThatExceptionIsThrownBy(() -> statistics.getNormalCount("error")); + assertThatExceptionIsThrownBy(() -> statistics.getLowCount("error")); + + assertThat(statistics.getNewCount("error")).isEqualTo(0); + assertThat(statistics.getDisplayName("error")).isEqualTo("error"); + assertThat(statistics.getToolTip("error")).isEqualTo(StringUtils.EMPTY); + } + + private void assertThatExceptionIsThrownBy( + final ThrowingCallable callable) { + assertThatExceptionOfType(NoSuchElementException.class) + .isThrownBy(callable) + .withMessageContaining("error"); } - /** - * Verifies that getTotal() returns the total number of issues if there is one issue. - */ @Test - void shouldReturnTotalNumberMoreIssues() { - try (IssueBuilder builder = new IssueBuilder()) { + void shouldHandleReportWithOneIssue() { + try (IssueBuilder builder = createBuilder()) { Report issues = new Report(); - issues.add(builder.setCategory("errorA").build()); - issues.add(builder.setCategory("errorB").build()); + issues.add(builder.setCategory("error").build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); + PropertyStatistics statistics = createStatistics(issues); - assertThat(statistics).hasTotal(2); - } - } + assertThat(statistics).hasTotal(1) + .hasTotalNewIssues(0) + .hasOnlyKeys("error") + .hasProperty("category") + .hasMax(1); - /** - * Verifies that getTotal() returns the total number of issues if there are no issues. - */ - @Test - void shouldReturnTotalNumberZero() { - Report issues = new Report(); + assertThat(statistics.getCount("error")).isEqualTo(1); + assertThat(statistics.getErrorCount("error")).isEqualTo(0); + assertThat(statistics.getHighCount("error")).isEqualTo(1); + assertThat(statistics.getNormalCount("error")).isEqualTo(0); + assertThat(statistics.getLowCount("error")).isEqualTo(0); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", Function.identity()); + assertThat(statistics.getNewCount("error")).isEqualTo(0); + assertThat(statistics.getDisplayName("error")).isEqualTo("error"); + assertThat(statistics.getToolTip("error")).isEqualTo(StringUtils.EMPTY); + } + } - assertThat(statistics).hasTotal(0); + private PropertyStatistics createStatistics(final Report issues) { + return new PropertyStatistics(issues, new Report(), + "category", Function.identity()); } /** - * Verifies that getProperty() returns the name of this property. + * Verifies that getTotal() returns the total number of issues if there is one issue. */ @Test - void shouldReturnPropertyString() { - PropertyStatistics statistics = new PropertyStatistics(new Report(), new Report(), "category", Function.identity()); + void shouldHandleReportWithTwoIssues() { + try (IssueBuilder builder = createBuilder()) { + Report issues = new Report(); + issues.add(builder.setCategory("errorA").build()); + issues.add(builder.setCategory("errorB").build()); - String actualProperty = statistics.getProperty(); + PropertyStatistics statistics = createStatistics(issues); - assertThat(actualProperty).isEqualTo("category"); - } + assertThat(statistics).hasTotal(2) + .hasTotalNewIssues(0) + .hasOnlyKeys("errorA", "errorB") + .hasProperty("category") + .hasMax(1); - /** - * Verifies that getDisplayName() returns a display name for the specified property instance when key is valid. - */ - @Test - void shouldReturnDisplayNameString() { - PropertyStatistics statistics = new PropertyStatistics(new Report(), new Report(), "category", Function.identity()); + assertThat(statistics.getCount("errorA")).isEqualTo(1); + assertThat(statistics.getErrorCount("errorA")).isEqualTo(0); + assertThat(statistics.getHighCount("errorA")).isEqualTo(1); + assertThat(statistics.getNormalCount("errorA")).isEqualTo(0); + assertThat(statistics.getLowCount("errorA")).isEqualTo(0); + + assertThat(statistics.getNewCount("errorA")).isEqualTo(0); + assertThat(statistics.getDisplayName("errorA")).isEqualTo("errorA"); + assertThat(statistics.getToolTip("errorA")).isEqualTo(StringUtils.EMPTY); - String actualDisplayName = statistics.getDisplayName("name"); + assertThat(statistics.getCount("errorB")).isEqualTo(1); + assertThat(statistics.getErrorCount("errorB")).isEqualTo(0); + assertThat(statistics.getHighCount("errorB")).isEqualTo(1); + assertThat(statistics.getNormalCount("errorB")).isEqualTo(0); + assertThat(statistics.getLowCount("errorB")).isEqualTo(0); - assertThat(actualDisplayName).isEqualTo("name"); - assertThat(statistics.getToolTip("name")).isEmpty(); + assertThat(statistics.getNewCount("errorB")).isEqualTo(0); + assertThat(statistics.getDisplayName("errorB")).isEqualTo("errorB"); + assertThat(statistics.getToolTip("errorB")).isEqualTo(StringUtils.EMPTY); + } } @Test void shouldReturnToolTip() { PropertyStatistics statistics = new PropertyStatistics( - new Report(), new Report(), "category", string -> KEY.equals(string) ? KEY : "tooltip"); + new Report(), new Report(), "category", + string -> KEY.equals(string) ? KEY : "tooltip"); assertThat(statistics.getDisplayName(KEY)).isEqualTo(KEY); assertThat(statistics.getToolTip(KEY)).isEmpty(); @@ -102,113 +136,41 @@ void shouldReturnToolTip() { assertThat(statistics.getToolTip("name")).isEqualTo("name"); } - /** - * Verifies that getKeys() returns one instances for this property. - */ - @Test - void shouldReturnKeys() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setCategory(KEY).build()); - - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - assertThat(statistics).hasOnlyKeys(KEY); - } - } - - /** - * Verifies that getKeys() returns all instances for this property. - */ - @Test - void shouldReturnAllKeys() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setCategory("keyA").build()); - issues.add(builder.setCategory("keyB").build()); - - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - assertThat(statistics).hasOnlyKeys("keyA", "keyB"); - } - } - - /** - * Verifies that getKeys() returns "-" string if the instances for this property are empty. - */ @Test - void shouldReturnEmptyStringKeys() { - try (IssueBuilder builder = new IssueBuilder()) { + void shouldReplaceEmptyStringInDisplayName() { + try (IssueBuilder builder = createBuilder()) { Report issues = new Report(); issues.add(builder.setCategory("").build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - assertThat(statistics).hasOnlyKeys("-"); - } - } - - /** - * Verifies that getKeys() returns nothing if there are no instances for this property. - */ - @Test - void shouldReturnEmptyKeys() { - PropertyStatistics statistics = new PropertyStatistics(new Report(), new Report(), "category", Function.identity()); - - Set actualProperty = statistics.getKeys(); - - assertThat(actualProperty).isEmpty(); - } - - /** - * Verifies that getMax() returns zero if there are no issues. - */ - @Test - void shouldReturnMaxValueZero() { - Report issues = new Report(); - - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", Function.identity()); - - int value = statistics.getMax(); - - assertThat(value).isEqualTo(0); - } + PropertyStatistics statistics = createStatistics(issues); - /** - * Verifies that getMax() returns one if there is one issue. - */ - @Test - void shouldReturnMaxValue() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); + assertThat(statistics).hasTotal(1) + .hasTotalNewIssues(0) + .hasOnlyKeys("") + .hasProperty("category") + .hasMax(1); - int value = statistics.getMax(); + assertThat(statistics.getCount("")).isEqualTo(1); + assertThat(statistics.getErrorCount("")).isEqualTo(0); + assertThat(statistics.getHighCount("")).isEqualTo(1); + assertThat(statistics.getNormalCount("")).isEqualTo(0); + assertThat(statistics.getLowCount("")).isEqualTo(0); - assertThat(value).isEqualTo(1); + assertThat(statistics.getNewCount("")).isEqualTo(0); + assertThat(statistics.getDisplayName("")).isEqualTo("-"); + assertThat(statistics.getToolTip("")).isEqualTo(StringUtils.EMPTY); } } - /** - * Verifies that getMax() returns the maximum number of issues for each property instance. - */ @Test - void shouldReturnMaxValueTwo() { - try (IssueBuilder builder = new IssueBuilder()) { + void shouldReturnMaxWhenTwoIssuesHaveSameCategory() { + try (IssueBuilder builder = createBuilder()) { Report issues = new Report(); issues.add(builder.setCategory("ab").setPackageName("P1").build()); issues.add(builder.setCategory("ab").setPackageName("P2").build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - int value = statistics.getMax(); + PropertyStatistics statistics = createStatistics(issues); - assertThat(value).isEqualTo(2); + verifyTwoIssuesWithSameCategory(statistics, 2); } } @@ -217,292 +179,42 @@ void shouldReturnMaxValueTwo() { */ @Test void shouldReturnMaxValueDifferentCategories() { - try (IssueBuilder builder = new IssueBuilder()) { + try (IssueBuilder builder = createBuilder()) { Report issues = new Report(); issues.add(builder.setCategory("ab").setPackageName("P1").build()); issues.add(builder.setCategory("ab").setPackageName("P2").build()); issues.add(builder.setCategory("abc").setPackageName("P2").build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - int value = statistics.getMax(); - - assertThat(value).isEqualTo(2); - } - } - - /** - * Verifies that getCount() throw an exception. - */ - @Test - void shouldReturnCountEmpty() { - PropertyStatistics statistics = new PropertyStatistics(new Report(), new Report(), "category", Function.identity()); - - String key = KEY; - assertThatThrownBy(() -> statistics.getCount(key)) - .isInstanceOf(NoSuchElementException.class) - .hasMessageContaining(key); - } - - /** - * Verifies that getCount() returns one if there is one issue for the specified property instance. - */ - @Test - void shouldReturnCountOne() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getCount(KEY); + PropertyStatistics statistics = createStatistics(issues); - assertThat(value).isEqualTo(1); + verifyTwoIssuesWithSameCategory(statistics, 3); + assertThat(statistics).hasKeys("ab", "abc"); } } - /** - * Verifies that getCount() returns the maximum number of issues for the specified property instance. - */ - @Test - void shouldReturnCountThree() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setCategory(KEY).build()); - issues.add(builder.setCategory(KEY).setPackageName("P1").build()); - issues.add(builder.setCategory(KEY).setPackageName("P2").build()); - issues.add(builder.setCategory("key1").setPackageName("P1").build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getCount(KEY); - - assertThat(value).isEqualTo(3); - } + @MustBeClosed @SuppressWarnings("resource") + private IssueBuilder createBuilder() { + return new IssueBuilder().setSeverity(Severity.WARNING_HIGH); } - /** - * Verifies that getHighCount() returns the number of issues with Severity#HIGH. - */ - @Test - void shouldReturnHighCountOne() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_HIGH).setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); + private void verifyTwoIssuesWithSameCategory(final PropertyStatistics statistics, final int total) { + assertThat(statistics) + .hasTotal(total) + .hasTotalNewIssues(0) + .hasKeys("ab") + .hasProperty("category") + .hasMax(2); - long value = statistics.getHighCount(KEY); + assertThat(statistics.getCount("ab")).isEqualTo(2); + assertThat(statistics.getErrorCount("ab")).isEqualTo(0); + assertThat(statistics.getHighCount("ab")).isEqualTo(2); + assertThat(statistics.getNormalCount("ab")).isEqualTo(0); + assertThat(statistics.getLowCount("ab")).isEqualTo(0); - assertThat(value).isEqualTo(1); - } + assertThat(statistics.getNewCount("ab")).isEqualTo(0); + assertThat(statistics.getDisplayName("ab")).isEqualTo("ab"); + assertThat(statistics.getToolTip("ab")).isEqualTo(StringUtils.EMPTY); } - /** - * Verifies that getHighCount() returns the number of issues with Severity#HIGH. - */ - @Test - void shouldReturnHighCountTwo() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_HIGH).setCategory(KEY).setOrigin("A").build()); - issues.add(builder.setSeverity(Severity.WARNING_HIGH).setCategory(KEY).setOrigin("B").build()); - issues.add(builder.setSeverity(Severity.WARNING_LOW).setCategory(KEY).setOrigin("B").build()); - issues.add(builder.setSeverity(Severity.WARNING_NORMAL).setCategory(KEY).setOrigin("B").build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getHighCount(KEY); - - assertThat(value).isEqualTo(2); - } - } - - /** - * Verifies that getHighCount() returns zero if there are not issues with Severity#HIGH. - */ - @Test - void shouldReturnHighCountZero() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_LOW).setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getHighCount(KEY); - - assertThat(value).isEqualTo(0); - } - } - - /** - * Verifies that getHighCount() throw null pointer exception. - */ - @Test - void shouldReturnHighCountException() { - Report issues = new Report(); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", Function.identity()); - - assertThatThrownBy(() -> statistics.getHighCount(KEY)) - .isInstanceOf(NoSuchElementException.class) - .hasMessageContaining(KEY); - } - - /** - * Verifies that getNormalCount() returns the number of issues with Severity#NORMAL. - */ - @Test - void shouldReturnNormalCountOne() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_NORMAL).setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getNormalCount(KEY); - - assertThat(value).isEqualTo(1); - } - } - - /** - * Verifies that getNormalCount() returns the number of issues with Severity#NORMAL. - */ - @Test - void shouldReturnNormalCountTwo() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_NORMAL).setCategory(KEY).setOrigin("A").build()); - issues.add(builder.setSeverity(Severity.WARNING_NORMAL).setCategory(KEY).setOrigin("B").build()); - issues.add(builder.setSeverity(Severity.WARNING_LOW).setCategory(KEY).setOrigin("B").build()); - issues.add(builder.setSeverity(Severity.WARNING_HIGH).setCategory(KEY).setOrigin("B").build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getNormalCount(KEY); - - assertThat(value).isEqualTo(2); - } - } - - /** - * Verifies that getNormalCount() throw null pointer exception. - */ - @Test - void shouldReturnNormalCountException() { - PropertyStatistics statistics = new PropertyStatistics(new Report(), new Report(), "category", Function.identity()); - - assertThatThrownBy(() -> statistics.getNormalCount(KEY)) - .isInstanceOf(NoSuchElementException.class) - .hasMessageContaining(KEY); - } - - /** - * Verifies that getNormalCount() returns zero if there are not issues with Severity#NORMAL. - */ - @Test - void shouldReturnNormalCountZero() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_LOW).setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getNormalCount(KEY); - - assertThat(value).isEqualTo(0); - } - } - - /** - * Verifies that getLowCount() returns the number of issues with Severity#LOW. - */ - @Test - void shouldReturnLowCountOne() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_LOW).setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getLowCount(KEY); - - assertThat(value).isEqualTo(1); - } - } - - /** - * Verifies that getLowCount() throw null pointer exception. - */ - @Test - void shouldReturnLowCountException() { - PropertyStatistics statistics = new PropertyStatistics(new Report(), new Report(), "category", Function.identity()); - - assertThatThrownBy(() -> statistics.getLowCount(KEY)) - .isInstanceOf(NoSuchElementException.class) - .hasMessageContaining(KEY); - } - - /** - * Verifies that getLowCount() returns the number of issues with Severity#LOW. - */ - @Test - void shouldReturnLowCountTwo() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_LOW).setCategory(KEY).setOrigin("A").build()); - issues.add(builder.setSeverity(Severity.WARNING_LOW).setCategory(KEY).setOrigin("B").build()); - issues.add(builder.setSeverity(Severity.WARNING_NORMAL).setCategory(KEY).setOrigin("B").build()); - issues.add(builder.setSeverity(Severity.WARNING_HIGH).setCategory(KEY).setOrigin("B").build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getLowCount(KEY); - - assertThat(value).isEqualTo(2); - } - } - - /** - * Verifies that getLowCount() returns zero if there are not issues with Severity#LOW. - */ - @Test - void shouldReturnLowCountZero() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setSeverity(Severity.WARNING_HIGH).setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getLowCount(KEY); - - assertThat(value).isEqualTo(0); - } - } - - /** - * Verifies that getNewIssues() returns the amount of new issues. - * In this case there are 2 issues, 0 new. - */ - @Test - void shouldReturnZeroNewIssues() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setCategory(KEY).setOrigin("A").build()); - issues.add(builder.setCategory(KEY).setOrigin("B").build()); - Report newIssues = new Report(); - PropertyStatistics statistics = new PropertyStatistics(issues, newIssues, "category", Function.identity()); - - int newAmount = statistics.getTotalNewIssues(); - - assertThat(newAmount).isEqualTo(0); - } - } - - /** - * Verifies that getNewIssues() returns the total count of issues with age 1 - * In this case the issues and the new issues are identical, both size 2. - */ @Test void shouldReturnTwoNewIssues() { try (IssueBuilder builder = new IssueBuilder()) { @@ -511,54 +223,15 @@ void shouldReturnTwoNewIssues() { issues.add(builder.setCategory(KEY).setOrigin("B").build()); PropertyStatistics statistics = new PropertyStatistics(issues, issues, "category", Function.identity()); - int newIssues = statistics.getTotalNewIssues(); - - assertThat(newIssues).isEqualTo(2); - } - } - - /** - * Verifies that getNewCount() returns 0 if a key doesn't exist. - */ - @Test - void shouldReturnNewCountEmpty() { - PropertyStatistics statistics = new PropertyStatistics(new Report(), new Report(), "category", Function.identity()); - long newIssues = statistics.getNewCount(KEY); - assertThat(newIssues).isEqualTo(0); - } - - /** - * Verifies that getNewCount() returns one if there is one new issues for the specified property instance. - */ - @Test - void shouldReturnNewCountOne() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setCategory(KEY).build()); - Report newIssues = new Report(); - newIssues.add(builder.setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, newIssues, "category", Function.identity()); - - long value = statistics.getNewCount(KEY); - - assertThat(value).isEqualTo(1); - } - } - - /** - * Verifies that getNewCount() returns zero if there is one issue for the specified property instance, but it isn't new. - */ - @Test - void shouldReturnNewCountZero() { - try (IssueBuilder builder = new IssueBuilder()) { - Report issues = new Report(); - issues.add(builder.setCategory(KEY).build()); - PropertyStatistics statistics = new PropertyStatistics(issues, new Report(), "category", - Function.identity()); - - long value = statistics.getNewCount(KEY); + assertThat(statistics) + .hasTotal(2) + .hasTotalNewIssues(2) + .hasOnlyKeys(KEY) + .hasProperty("category") + .hasMax(2); - assertThat(value).isEqualTo(0); + assertThat(statistics.getNewCount(KEY)).isEqualTo(2); + assertThat(statistics.getNewCount("other")).isEqualTo(0); } } } From a5b5bc7fd2a239cde990f38f0795ffbaa94f4f28 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Thu, 7 Mar 2024 08:21:26 +0100 Subject: [PATCH 3/3] Fix test case as there is no empty modul name anymore. --- .../plugins/analysis/warnings/steps/ModuleDetectorITest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/steps/ModuleDetectorITest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/steps/ModuleDetectorITest.java index 98e3a70629..20c4a7a089 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/steps/ModuleDetectorITest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/steps/ModuleDetectorITest.java @@ -97,11 +97,11 @@ class ModuleDetectorITest extends IntegrationTestWithJenkinsPerSuite { private static final String ANT_BUILD_FILE_LOCATION = "ant/"; private static final String OSGI_BUILD_FILE_LOCATION = "osgi/"; private static final String DEFAULT_DEBUG_LOG_LINE = "Resolving module names from module definitions (build.xml, pom.xml, or Manifest.mf files)"; - private static final String EMPTY_MODULE_NAME = ""; + private static final String EMPTY_MODULE_NAME = "-"; private static final String PROPERTY = "moduleName"; /** - * Verifies that the HTML output is correct if there are OSGI, Maven and Ant modules used within the build. This + * Verifies that the HTML output is correct if there are OSGI, Maven, and Ant modules used within the build. This * test doesn't check for correct precedence in every possible case as this might fail. */ @Test