Skip to content

Commit

Permalink
Fix several warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Sep 17, 2024
1 parent 61544e5 commit f39ab59
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 52 deletions.
7 changes: 4 additions & 3 deletions src/main/java/edu/hm/hafner/coverage/Coverage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.hm.hafner.coverage;

import java.util.Locale;
import java.util.Objects;
import java.util.function.UnaryOperator;

Expand Down Expand Up @@ -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());
}

/**
Expand Down
31 changes: 16 additions & 15 deletions src/main/java/edu/hm/hafner/coverage/FileNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,15 +179,15 @@ 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));
var leftFunctionCall = new CoverageMetricsValues(functionCallCoveredPerLine.getOrDefault(line, 0), functionCallMissedPerLine.getOrDefault(line, 0));
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()) {
Expand All @@ -196,25 +197,25 @@ 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",

Check warning on line 206 in src/main/java/edu/hm/hafner/coverage/FileNode.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 200-206 are not covered by tests
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);
updateLineCoverage(line, lineCoverage);
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);
}
Expand All @@ -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()) {
Expand All @@ -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<Integer, Integer> localCoveredPerLine, final NavigableMap<Integer, Integer> localMissedPerLine) {
if (leftCovered > rightCovered) {
if (leftCovered > rightCovered) {
localCoveredPerLine.put(line, leftCovered);
localMissedPerLine.put(line, leftMissed);
}
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/hm/hafner/coverage/ModuleNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
20 changes: 12 additions & 8 deletions src/main/java/edu/hm/hafner/coverage/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -466,6 +469,12 @@ public Optional<FileNode> findFile(final String searchPath) {
return find(Metric.FILE, searchPath).map(FileNode.class::cast);
}

private Optional<FileNode> 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.
*
Expand Down Expand Up @@ -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>",

Check warning on line 815 in src/main/java/edu/hm/hafner/coverage/Node.java

View check run for this annotation

ci.jenkins.io / Mutation Coverage

Mutation survived

One mutation survived in line 815 (EmptyObjectReturnValsMutator)
Raw output
Survived mutations:
- replaced return value with "" for edu/hm/hafner/coverage/Node::lambda$toString$15 (org.pitest.mutationtest.engine.gregor.mutators.returns.EmptyObjectReturnValsMutator)
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() {
Expand Down Expand Up @@ -944,6 +953,7 @@ public PackageNode createPackageNode(final String packageName) {
return addChildNode(new PackageNode(packageName));
}

@CanIgnoreReturnValue
private <T extends Node> T addChildNode(final T child) {
addChild(child);
return child;
Expand Down Expand Up @@ -995,12 +1005,6 @@ public PackageNode findOrCreatePackageNode(final String packageName) {
return findPackage(normalizedPackageName).orElseGet(() -> createPackageNode(normalizedPackageName));
}

private Optional<FileNode> 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.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/coverage/Percentage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
47 changes: 25 additions & 22 deletions src/main/java/edu/hm/hafner/coverage/parser/VectorCastParser.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Metric, Coverage> coverageMap) throws XMLStreamException {
protected boolean processStartElement(final StartElement nextElement, final StartElement element,
final FileNode fileNode, final Map<Metric, Coverage> coverageMap) throws XMLStreamException {
boolean runReadClassOrMethod = false;

if (LINE.equals(nextElement.getName())) {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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<Metric, Coverage> 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<Metric, Coverage> 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
Expand All @@ -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<Metric, Coverage> coverageMap = new HashMap<>();
Map<Metric, Coverage> coverageMap = new EnumMap<>(Metric.class);

coverageMap.put(Metric.LINE, Coverage.nullObject(Metric.LINE));
coverageMap.put(Metric.BRANCH, Coverage.nullObject(Metric.BRANCH));
Expand Down
1 change: 1 addition & 0 deletions src/test/java/edu/hm/hafner/coverage/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void shouldThrowExceptionOnInvalidStringRepresentation() {
}

@Test
@SuppressWarnings("Varifier")
void shouldGetValue() {
var linesOfCode = new LinesOfCode(10);
var cyclomaticComplexity = new CyclomaticComplexity(20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit f39ab59

Please sign in to comment.