From f39ab59d9c786320d2c5581a5db0e97b94cbd97f Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Tue, 17 Sep 2024 22:57:58 +0200 Subject: [PATCH] Fix several warnings. --- .../java/edu/hm/hafner/coverage/Coverage.java | 7 +-- .../java/edu/hm/hafner/coverage/FileNode.java | 31 ++++++------ .../edu/hm/hafner/coverage/ModuleNode.java | 3 +- .../java/edu/hm/hafner/coverage/Node.java | 20 ++++---- .../edu/hm/hafner/coverage/Percentage.java | 4 +- .../coverage/parser/VectorCastParser.java | 47 ++++++++++--------- .../edu/hm/hafner/coverage/ValueTest.java | 1 + .../coverage/parser/VectorCastParserTest.java | 2 +- 8 files changed, 63 insertions(+), 52 deletions(-) diff --git a/src/main/java/edu/hm/hafner/coverage/Coverage.java b/src/main/java/edu/hm/hafner/coverage/Coverage.java index 3c502d0a..b73aaf36 100644 --- a/src/main/java/edu/hm/hafner/coverage/Coverage.java +++ b/src/main/java/edu/hm/hafner/coverage/Coverage.java @@ -1,5 +1,6 @@ package edu.hm.hafner.coverage; +import java.util.Locale; import java.util.Objects; import java.util.function.UnaryOperator; @@ -207,14 +208,14 @@ public int hashCode() { public String toString() { int total = getTotal(); if (total > 0) { - return String.format("%s: %s (%d/%d)", getMetric(), getCoveredPercentage(), covered, total); + return String.format(Locale.ENGLISH, "%s: %s (%d/%d)", getMetric(), getCoveredPercentage(), covered, total); } - return String.format("%s: n/a", getMetric()); + return String.format(Locale.ENGLISH, "%s: n/a", getMetric()); } @Override public String serialize() { - return String.format("%s: %d/%d", getMetric(), getCovered(), getTotal()); + return String.format(Locale.ENGLISH, "%s: %d/%d", getMetric(), getCovered(), getTotal()); } /** diff --git a/src/main/java/edu/hm/hafner/coverage/FileNode.java b/src/main/java/edu/hm/hafner/coverage/FileNode.java index 0cf29c86..86a62bf8 100644 --- a/src/main/java/edu/hm/hafner/coverage/FileNode.java +++ b/src/main/java/edu/hm/hafner/coverage/FileNode.java @@ -4,6 +4,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.NavigableMap; import java.util.NavigableSet; @@ -178,7 +179,7 @@ private void mergeCounters(final FileNode otherFile) { var branchCoverage = new CoverageBuilder().withMetric(Metric.BRANCH).withCovered(0).withMissed(0); var mcdcPairCoverage = new CoverageBuilder().withMetric(Metric.MCDC_PAIR).withCovered(0).withMissed(0); var functionCallCoverage = new CoverageBuilder().withMetric(Metric.FUNCTION_CALL).withCovered(0).withMissed(0); - + for (final int line : lines) { var left = new CoverageMetricsValues(coveredPerLine.getOrDefault(line, 0), missedPerLine.getOrDefault(line, 0)); var leftMcdcPair = new CoverageMetricsValues(mcdcPairCoveredPerLine.getOrDefault(line, 0), mcdcPairMissedPerLine.getOrDefault(line, 0)); @@ -186,7 +187,7 @@ private void mergeCounters(final FileNode otherFile) { var right = new CoverageMetricsValues(otherFile.coveredPerLine.getOrDefault(line, 0), otherFile.missedPerLine.getOrDefault(line, 0)); var rightMcdcPair = new CoverageMetricsValues(otherFile.mcdcPairCoveredPerLine.getOrDefault(line, 0), otherFile.mcdcPairMissedPerLine.getOrDefault(line, 0)); var rightFunctionCall = new CoverageMetricsValues(otherFile.functionCallCoveredPerLine.getOrDefault(line, 0), otherFile.functionCallMissedPerLine.getOrDefault(line, 0)); - + // check for errors in branch, mcdc pair and function call coverages if (left.totalsNotEqual(right)) { if (left.noMissing() || right.noMissing()) { @@ -196,16 +197,16 @@ private void mergeCounters(final FileNode otherFile) { } else { throw new IllegalArgumentException( - String.format("Cannot merge coverage information for line %d in %s", + String.format(Locale.ENGLISH, "Cannot merge coverage information for line %d in %s", line, this)); } } else if (leftMcdcPair.totalsNotEqual(rightMcdcPair) || leftFunctionCall.totalsNotEqual(rightFunctionCall)) { throw new IllegalArgumentException( - String.format("Cannot merge coverage information for line %d in %s", + String.format(Locale.ENGLISH, "Cannot merge coverage information for line %d in %s", line, this)); } - + if (left.hasAnyInfo()) { // exact branch coverage cannot be computed, so choose the higher value mergeLeftRight(line, left.getCovered(), left.getMissed(), right.getCovered(), right.getMissed(), coveredPerLine, missedPerLine); @@ -213,8 +214,8 @@ else if (leftMcdcPair.totalsNotEqual(rightMcdcPair) || leftFunctionCall.totalsNo updateBranchCoverage(line, branchCoverage); } else if (leftMcdcPair.hasAnyInfo()) { - mergeLeftRight(line, leftMcdcPair.getCovered(), leftMcdcPair.getMissed(), - rightMcdcPair.getCovered(), rightMcdcPair.getMissed(), + mergeLeftRight(line, leftMcdcPair.getCovered(), leftMcdcPair.getMissed(), + rightMcdcPair.getCovered(), rightMcdcPair.getMissed(), mcdcPairCoveredPerLine, mcdcPairMissedPerLine); updateMcdcPairCoverage(line, mcdcPairCoverage); } @@ -229,14 +230,14 @@ else if (leftFunctionCall.hasAnyInfo()) { updateLineCoverage(line, lineCoverage); } } - + setValues(lineCoverage, branchCoverage, mcdcPairCoverage, functionCallCoverage); otherFile.getValues().stream() .filter(value -> value.getMetric() == Metric.COMPLEXITY) .forEach(this::addValue); } - + private void setValues(final CoverageBuilder lineCoverage, final CoverageBuilder branchCoverage, final CoverageBuilder mcdcPairCoverage, final CoverageBuilder functionCallCoverage) { var lineValue = lineCoverage.build(); if (lineValue.isSet()) { @@ -257,10 +258,10 @@ private void setValues(final CoverageBuilder lineCoverage, final CoverageBuilder addValue(functionCallValue); } } - - private void mergeLeftRight(final int line, final int leftCovered, final int leftMissed, final int rightCovered, final int rightMissed, + + private void mergeLeftRight(final int line, final int leftCovered, final int leftMissed, final int rightCovered, final int rightMissed, final NavigableMap localCoveredPerLine, final NavigableMap localMissedPerLine) { - if (leftCovered > rightCovered) { + if (leftCovered > rightCovered) { localCoveredPerLine.put(line, leftCovered); localMissedPerLine.put(line, leftMissed); } @@ -602,7 +603,7 @@ public FileNode addCounters(final int lineNumber, final int covered, final int m return this; } - + /** * Add the MCDC coverage counters for the specified line. * @@ -622,7 +623,7 @@ public FileNode addMcdcPairCounters(final int lineNumber, final int covered, fin return this; } - + /** * Add the function call coverage counters for the specified line. * @@ -641,7 +642,7 @@ public FileNode addFunctionCallCounters(final int lineNumber, final int covered, functionCallMissedPerLine.put(lineNumber, missed); return this; - } + } public int[] getCoveredCounters() { return entriesToArray(coveredPerLine); diff --git a/src/main/java/edu/hm/hafner/coverage/ModuleNode.java b/src/main/java/edu/hm/hafner/coverage/ModuleNode.java index 9f2afabd..d4c17edc 100644 --- a/src/main/java/edu/hm/hafner/coverage/ModuleNode.java +++ b/src/main/java/edu/hm/hafner/coverage/ModuleNode.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -148,6 +149,6 @@ public int hashCode() { @Override public String toString() { - return String.format("[%s] %s <%d> %s", getMetric(), getName(), getChildren().size(), getSourceFolders()); + return String.format(Locale.ENGLISH, "[%s] %s <%d> %s", getMetric(), getName(), getChildren().size(), getSourceFolders()); } } diff --git a/src/main/java/edu/hm/hafner/coverage/Node.java b/src/main/java/edu/hm/hafner/coverage/Node.java index 40a5d6b9..9245e1ca 100644 --- a/src/main/java/edu/hm/hafner/coverage/Node.java +++ b/src/main/java/edu/hm/hafner/coverage/Node.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.NavigableMap; @@ -22,6 +23,8 @@ import org.apache.commons.lang3.math.Fraction; import org.apache.commons.lang3.tuple.ImmutablePair; +import com.google.errorprone.annotations.CanIgnoreReturnValue; + import edu.hm.hafner.util.Ensure; import edu.hm.hafner.util.TreeString; import edu.umd.cs.findbugs.annotations.CheckForNull; @@ -466,6 +469,12 @@ public Optional findFile(final String searchPath) { return find(Metric.FILE, searchPath).map(FileNode.class::cast); } + private Optional findFile(final String fileName, final String relativePath) { + return getAllFileNodes().stream().filter(fileNode -> + fileNode.getName().equals(fileName) + && fileNode.getRelativePath().equals(relativePath)).findAny(); + } + /** * Searches for a class within this node that has the given name. * @@ -803,9 +812,9 @@ public int hashCode() { @Override public String toString() { return getValue(Metric.LINE) - .map(lineCoverage -> String.format("[%s] %s <%d, %s>", + .map(lineCoverage -> String.format(Locale.ENGLISH, "[%s] %s <%d, %s>", getMetric(), getName(), getChildren().size(), lineCoverage)) - .orElse(String.format("[%s] %s <%d>", getMetric(), getName(), children.size())); + .orElse(String.format(Locale.ENGLISH, "[%s] %s <%d>", getMetric(), getName(), children.size())); } public boolean isEmpty() { @@ -944,6 +953,7 @@ public PackageNode createPackageNode(final String packageName) { return addChildNode(new PackageNode(packageName)); } + @CanIgnoreReturnValue private T addChildNode(final T child) { addChild(child); return child; @@ -995,12 +1005,6 @@ public PackageNode findOrCreatePackageNode(final String packageName) { return findPackage(normalizedPackageName).orElseGet(() -> createPackageNode(normalizedPackageName)); } - private Optional findFile(final String fileName, final String relativePath) { - return getAllFileNodes().stream().filter(fileNode -> - fileNode.getName().equals(fileName) - && fileNode.getRelativePath().equals(relativePath)).findAny(); - } - /** * Returns whether this node is an aggregation of other nodes. Aggregation nodes do not store values, they rather * aggregate the values of their children. diff --git a/src/main/java/edu/hm/hafner/coverage/Percentage.java b/src/main/java/edu/hm/hafner/coverage/Percentage.java index dcd148de..9a6bc279 100644 --- a/src/main/java/edu/hm/hafner/coverage/Percentage.java +++ b/src/main/java/edu/hm/hafner/coverage/Percentage.java @@ -101,7 +101,7 @@ private Percentage(final int items, final int total) { } if (items > total) { throw new IllegalArgumentException( - String.format("The number of items %d must be less or equal the total number %d.", + String.format(Locale.ENGLISH, "The number of items %d must be less or equal the total number %d.", items, total)); } this.items = items; @@ -196,7 +196,7 @@ public int hashCode() { * @return a string representation for this {@link Percentage} */ public String serializeToString() { - return String.format("%d/%d", getItems(), getTotal()); + return String.format(Locale.ENGLISH, "%d/%d", getItems(), getTotal()); } @Override diff --git a/src/main/java/edu/hm/hafner/coverage/parser/VectorCastParser.java b/src/main/java/edu/hm/hafner/coverage/parser/VectorCastParser.java index 1012a813..08f6f82b 100644 --- a/src/main/java/edu/hm/hafner/coverage/parser/VectorCastParser.java +++ b/src/main/java/edu/hm/hafner/coverage/parser/VectorCastParser.java @@ -1,6 +1,7 @@ package edu.hm.hafner.coverage.parser; -import java.util.HashMap; +import java.util.EnumMap; +import java.util.List; import java.util.Map; import java.util.regex.Pattern; import javax.xml.namespace.QName; @@ -62,15 +63,15 @@ private Coverage processClassMethodStart(final StartElement nextElement, final C var localFunctionCoverage = functionCoverage; if (nextElement.getName().equals(METHOD)) { - Coverage functionMethodCoverage; - functionMethodCoverage = readFunctionCoverage(nextElement); + Coverage functionMethodCoverage = readFunctionCoverage(nextElement); localFunctionCoverage = localFunctionCoverage.add(functionMethodCoverage); } return localFunctionCoverage; } - protected boolean processStartElement(final StartElement nextElement, final StartElement element, final FileNode fileNode, final Map coverageMap) throws XMLStreamException { + protected boolean processStartElement(final StartElement nextElement, final StartElement element, + final FileNode fileNode, final Map coverageMap) throws XMLStreamException { boolean runReadClassOrMethod = false; if (LINE.equals(nextElement.getName())) { @@ -83,25 +84,25 @@ protected boolean processStartElement(final StartElement nextElement, final Star currentLineCoverage = computeLineCoverage(lineBranchCoverage.getCovered()); //repeating - coverageMap.put(Metric.BRANCH, coverageMap.get(Metric.BRANCH).add(lineBranchCoverage)); + coverageMap.merge(Metric.BRANCH, lineBranchCoverage, Coverage::add); if (getOptionalValueOf(nextElement, MCDCPAIR_COVERAGE).isPresent()) { mcdcPairLineCoverage = readMcdcPairCoverage(nextElement); //repeating - coverageMap.put(Metric.MCDC_PAIR, coverageMap.get(Metric.MCDC_PAIR).add(mcdcPairLineCoverage)); + coverageMap.merge(Metric.MCDC_PAIR, mcdcPairLineCoverage, Coverage::add); } if (getOptionalValueOf(nextElement, FUNCTIONCALL_COVERAGE).isPresent()) { functionCallLineCoverage = readFunctionCallCoverage(nextElement); //repeating - coverageMap.put(Metric.FUNCTION_CALL, coverageMap.get(Metric.FUNCTION_CALL).add(functionCallLineCoverage)); + coverageMap.merge(Metric.FUNCTION_CALL, functionCallLineCoverage, Coverage::add); } } else if (getOptionalValueOf(nextElement, FUNCTIONCALL_COVERAGE).isPresent()) { functionCallLineCoverage = readFunctionCallCoverage(nextElement); - coverageMap.put(Metric.FUNCTION_CALL, coverageMap.get(Metric.FUNCTION_CALL).add(functionCallLineCoverage)); + coverageMap.merge(Metric.FUNCTION_CALL, functionCallLineCoverage, Coverage::add); int lineHits = getIntegerValueOf(nextElement, HITS); currentLineCoverage = computeLineCoverage(lineHits); @@ -113,7 +114,7 @@ else if (getOptionalValueOf(nextElement, FUNCTIONCALL_COVERAGE).isPresent()) { lineBranchCoverage = currentLineCoverage; } - coverageMap.put(Metric.LINE, coverageMap.get(Metric.LINE).add(currentLineCoverage)); + coverageMap.merge(Metric.LINE, currentLineCoverage, Coverage::add); if (CLASS.equals(element.getName())) { // Use the line counters at the class level for a file int lineNumber = getIntegerValueOf(nextElement, NUMBER); @@ -124,29 +125,31 @@ else if (getOptionalValueOf(nextElement, FUNCTIONCALL_COVERAGE).isPresent()) { } } else if (classOrMethodElement(nextElement)) { - coverageMap.put(Metric.METHOD, processClassMethodStart(nextElement, coverageMap.get(Metric.METHOD))); + coverageMap.put(Metric.METHOD, processClassMethodStart(nextElement, getValueFromMap(coverageMap, Metric.METHOD))); runReadClassOrMethod = true; } return runReadClassOrMethod; } + private Coverage getValueFromMap(final Map coverageMap, final Metric metric) { + return coverageMap.getOrDefault(metric, Coverage.nullObject(metric)); + } + private boolean classOrMethodElement(final StartElement nextElement) { return METHOD.equals(nextElement.getName()) || CLASS.equals(nextElement.getName()); } protected void processClassMethodEnd(final Node node, final Map coverageMap) { - node.addValue(coverageMap.get(Metric.LINE)); - - if (coverageMap.get(Metric.MCDC_PAIR).isSet()) { - node.addValue(coverageMap.get(Metric.MCDC_PAIR)); - } - if (coverageMap.get(Metric.FUNCTION_CALL).isSet()) { - node.addValue(coverageMap.get(Metric.FUNCTION_CALL)); - } - if (coverageMap.get(Metric.BRANCH).isSet()) { - node.addValue(coverageMap.get(Metric.BRANCH)); - } + node.addValue(getValueFromMap(coverageMap, Metric.LINE)); + + List.of(Metric.MCDC_PAIR, Metric.FUNCTION_CALL, Metric.BRANCH) + .forEach(metric -> { + var coverage = getValueFromMap(coverageMap, metric); + if (coverage.isSet()) { + node.addValue(coverage); + } + }); } @Override @@ -155,7 +158,7 @@ protected void readClassOrMethod(final XMLEventReader reader, final FileNode fileNode, final Node parentNode, final StartElement element, final String fileName, final FilteredLog log) throws XMLStreamException { - Map coverageMap = new HashMap<>(); + Map coverageMap = new EnumMap<>(Metric.class); coverageMap.put(Metric.LINE, Coverage.nullObject(Metric.LINE)); coverageMap.put(Metric.BRANCH, Coverage.nullObject(Metric.BRANCH)); diff --git a/src/test/java/edu/hm/hafner/coverage/ValueTest.java b/src/test/java/edu/hm/hafner/coverage/ValueTest.java index 3f018191..9250e873 100644 --- a/src/test/java/edu/hm/hafner/coverage/ValueTest.java +++ b/src/test/java/edu/hm/hafner/coverage/ValueTest.java @@ -85,6 +85,7 @@ void shouldThrowExceptionOnInvalidStringRepresentation() { } @Test + @SuppressWarnings("Varifier") void shouldGetValue() { var linesOfCode = new LinesOfCode(10); var cyclomaticComplexity = new CyclomaticComplexity(20); diff --git a/src/test/java/edu/hm/hafner/coverage/parser/VectorCastParserTest.java b/src/test/java/edu/hm/hafner/coverage/parser/VectorCastParserTest.java index 38aacca7..e40d9967 100644 --- a/src/test/java/edu/hm/hafner/coverage/parser/VectorCastParserTest.java +++ b/src/test/java/edu/hm/hafner/coverage/parser/VectorCastParserTest.java @@ -252,7 +252,7 @@ private void verifyMcdcFccProjectMetrics(final Node root) { } private void verifyMcdcFccProject(final Node root) { - CoverageBuilder builder = new CoverageBuilder(); + var builder = new CoverageBuilder(); assertThat(root.aggregateValues()).containsExactly( builder.withMetric(MODULE).withCovered(1).withMissed(0).build(), builder.withMetric(PACKAGE).withCovered(5).withMissed(0).build(),