diff --git a/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CodeDeltaCalculator.java b/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CodeDeltaCalculator.java index 25458c65e..846ce51a0 100644 --- a/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CodeDeltaCalculator.java +++ b/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CodeDeltaCalculator.java @@ -31,7 +31,7 @@ * * @author Florian Orendi */ -public class CodeDeltaCalculator { +class CodeDeltaCalculator { static final String AMBIGUOUS_PATHS_ERROR = "Failed to map SCM paths with coverage report paths due to ambiguous fully qualified names"; static final String AMBIGUOUS_OLD_PATHS_ERROR = @@ -62,7 +62,7 @@ public class CodeDeltaCalculator { * @param scm * The selected SCM */ - public CodeDeltaCalculator(final Run build, final FilePath workspace, + CodeDeltaCalculator(final Run build, final FilePath workspace, final TaskListener listener, final String scm) { this.build = build; this.workspace = workspace; @@ -114,11 +114,11 @@ public Set getCoverageRelevantChanges(final Delta delta) { * logger * * @return the created mapping of code changes - * @throws CodeDeltaException + * @throws IllegalStateException * when creating the mapping failed due to ambiguous paths */ public Map mapScmChangesToReportPaths( - final Set changes, final Node root, final FilteredLog log) throws CodeDeltaException { + final Set changes, final Node root, final FilteredLog log) throws IllegalStateException { Set reportPaths = new HashSet<>(root.getFiles()); Set scmPaths = changes.stream().map(FileChanges::getFileName).collect(Collectors.toSet()); @@ -149,12 +149,12 @@ public Map mapScmChangesToReportPaths( * * @return the created mapping whose keys are the currently used paths and whose values are the paths before the * modifications - * @throws CodeDeltaException + * @throws IllegalStateException * if the SCM path mapping is ambiguous */ public Map createOldPathMapping(final Node root, final Node referenceRoot, final Map changes, final FilteredLog log) - throws CodeDeltaException { + throws IllegalStateException { Set oldReportPaths = new HashSet<>(referenceRoot.getFiles()); // mapping between reference and current file paths which initially contains the SCM paths with renamings Map oldPathMapping = changes.entrySet().stream() @@ -171,7 +171,7 @@ public Map createOldPathMapping(final Node root, final Node refe oldPathMapping.replace(reportPath, oldReportPath); }); if (!newReportPathsWithRename.equals(oldPathMapping.keySet())) { - throw new CodeDeltaException(AMBIGUOUS_OLD_PATHS_ERROR); + throw new IllegalStateException(AMBIGUOUS_OLD_PATHS_ERROR); } // adding the paths, which exist in both trees and contain no changes, to the mapping @@ -219,16 +219,16 @@ private Map getScmToReportPathMapping( * @param log * The log * - * @throws CodeDeltaException + * @throws IllegalStateException * when ambiguous paths has been detected */ private void verifyScmToReportPathMapping(final Map pathMapping, final FilteredLog log) - throws CodeDeltaException { + throws IllegalStateException { List notEmptyValues = pathMapping.values().stream() .filter(path -> !path.isEmpty()) .collect(Collectors.toList()); if (notEmptyValues.size() != new HashSet<>(notEmptyValues).size()) { - throw new CodeDeltaException(AMBIGUOUS_PATHS_ERROR); + throw new IllegalStateException(AMBIGUOUS_PATHS_ERROR); } log.logInfo("Successfully mapped SCM paths to coverage report paths"); } @@ -266,11 +266,11 @@ private void removeMissingReferences(final Map oldPathMapping, f * @param log * The log * - * @throws CodeDeltaException + * @throws IllegalStateException * when the mapping is ambiguous */ static void verifyOldPathMapping(final Map oldPathMapping, final FilteredLog log) - throws CodeDeltaException { + throws IllegalStateException { Set duplicates = StreamEx.of(oldPathMapping.values()) .distinct(2) .collect(Collectors.toSet()); @@ -286,7 +286,7 @@ static void verifyOldPathMapping(final Map oldPathMapping, final .collect(Collectors.joining("," + System.lineSeparator())); String errorMessage = CODE_DELTA_TO_COVERAGE_DATA_MISMATCH_ERROR_TEMPLATE + System.lineSeparator() + mismatches; - throw new CodeDeltaException(errorMessage); + throw new IllegalStateException(errorMessage); } log.logInfo("Successfully verified that the coverage data matches with the code delta"); } diff --git a/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CodeDeltaException.java b/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CodeDeltaException.java deleted file mode 100644 index bcd4bd586..000000000 --- a/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CodeDeltaException.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.jenkins.plugins.coverage.metrics; - -/** - * Exception which is thrown when preprocessing the code delta failed. - * - * @author Florian Orendi - */ -public class CodeDeltaException extends Exception { - private static final long serialVersionUID = -7255072653278584604L; - - /** - * Constructor which creates an exception with a message. - * - * @param message - * The message - */ - public CodeDeltaException(final String message) { - super(message); - } -} diff --git a/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CoverageReporter.java b/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CoverageReporter.java index 59c595753..ad86408ee 100644 --- a/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CoverageReporter.java +++ b/plugin/src/main/java/io/jenkins/plugins/coverage/metrics/CoverageReporter.java @@ -134,9 +134,9 @@ private void createDeltaReports(final Node rootNode, final FilteredLog log, fina log.logInfo("Obtaining coverage delta for files..."); fileChangesProcessor.attachFileCoverageDeltas(rootNode, referenceRoot, oldPathMapping); } - catch (CodeDeltaException e) { + catch (IllegalStateException exception) { log.logError("An error occurred while processing code and coverage changes:"); - log.logError("-> Message: " + e.getMessage()); + log.logError("-> Message: " + exception.getMessage()); log.logError("-> Skipping calculating change coverage and indirect coverage changes"); } } diff --git a/plugin/src/test/java/io/jenkins/plugins/coverage/metrics/CodeDeltaCalculatorTest.java b/plugin/src/test/java/io/jenkins/plugins/coverage/metrics/CodeDeltaCalculatorTest.java index ae1889f4d..2991f22e6 100644 --- a/plugin/src/test/java/io/jenkins/plugins/coverage/metrics/CodeDeltaCalculatorTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/coverage/metrics/CodeDeltaCalculatorTest.java @@ -79,7 +79,7 @@ void shouldGetCoverageRelevantChanges() { } @Test - void shouldMapScmChangesToReportPaths() throws CodeDeltaException { + void shouldMapScmChangesToReportPaths() throws IllegalStateException { CodeDeltaCalculator codeDeltaCalculator = createCodeDeltaCalculator(); Delta delta = createDeltaWithStubbedFileChanges(); Set changes = codeDeltaCalculator.getCoverageRelevantChanges(delta); @@ -99,7 +99,7 @@ void shouldMapScmChangesToReportPaths() throws CodeDeltaException { } @Test - void shouldCreateEmptyMappingWithoutChanges() throws CodeDeltaException { + void shouldCreateEmptyMappingWithoutChanges() throws IllegalStateException { CodeDeltaCalculator codeDeltaCalculator = createCodeDeltaCalculator(); Node tree = createStubbedCoverageTree(); FilteredLog log = createFilteredLog(); @@ -109,7 +109,7 @@ void shouldCreateEmptyMappingWithoutChanges() throws CodeDeltaException { } @Test - void shouldNotMapScmChangesWithAmbiguousPaths() throws CodeDeltaException { + void shouldNotMapScmChangesWithAmbiguousPaths() throws IllegalStateException { CodeDeltaCalculator codeDeltaCalculator = createCodeDeltaCalculator(); FilteredLog log = createFilteredLog(); @@ -123,12 +123,12 @@ void shouldNotMapScmChangesWithAmbiguousPaths() throws CodeDeltaException { when(tree.getFiles()).thenReturn(List.of(path)); assertThatThrownBy(() -> codeDeltaCalculator.mapScmChangesToReportPaths(changes, tree, log)) - .isInstanceOf(CodeDeltaException.class) + .isInstanceOf(IllegalStateException.class) .hasMessage(AMBIGUOUS_PATHS_ERROR); } @Test - void shouldCreateOldPathMapping() throws CodeDeltaException { + void shouldCreateOldPathMapping() throws IllegalStateException { CodeDeltaCalculator codeDeltaCalculator = createCodeDeltaCalculator(); FilteredLog log = createFilteredLog(); Node tree = createStubbedCoverageTree(); @@ -146,7 +146,7 @@ void shouldCreateOldPathMapping() throws CodeDeltaException { } @Test - void shouldNotCreateOldPathMappingWithMissingReferenceNodes() throws CodeDeltaException { + void shouldNotCreateOldPathMappingWithMissingReferenceNodes() throws IllegalStateException { CodeDeltaCalculator codeDeltaCalculator = createCodeDeltaCalculator(); FilteredLog log = createFilteredLog(); @@ -175,7 +175,7 @@ void shouldNotCreateOldPathMappingWithCodeDeltaMismatches() { changes.put(REPORT_PATH_MODIFY, createFileChanges(REPORT_PATH_MODIFY, OLD_SCM_PATH_RENAME, FileEditType.RENAME)); assertThatThrownBy(() -> codeDeltaCalculator.createOldPathMapping(tree, referenceTree, changes, log)) - .isInstanceOf(CodeDeltaException.class) + .isInstanceOf(IllegalStateException.class) .hasMessageStartingWith(CODE_DELTA_TO_COVERAGE_DATA_MISMATCH_ERROR_TEMPLATE) .hasMessageContainingAll( String.format("new: '%s' - former: '%s',", REPORT_PATH_RENAME, OLD_REPORT_PATH_RENAME),