com.teamscale.service.testimpact.prioritization.PrioritizableTestBase Maven / Gradle / Ivy
Show all versions of teamscale-commons Show documentation
package com.teamscale.service.testimpact.prioritization;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Sets;
import com.teamscale.index.testimpact.MethodId;
/**
* Base class for test and test clusters. It holds common static info like duration and the covered
* methods, but also dynamic content such as the current score needed for the prioritization and the
* additional covered methods. Additional means relative to the tests that have already been
* selected in the current prioritization round.
*/
public abstract class PrioritizableTestBase implements IPrioritizableTests, Serializable {
private static final long serialVersionUID = 1L;
/**
* Covered method that represent additional coverage in the current prioritization round. This is a
* mapping from all methods that are both covered by the current test and should be tested because
* they changed in the given timeframe. The value is true when the method has not been executed by
* any other test in the current prioritization round and can therefore be treated as additionally
* covered.
*
* The value is initialized with {@link #initAdditionallyCoveredMethods(Set)}, values are set to
* false when new tests are selected via
* {@link IPrioritizableTests#removeFromCurrentAdditionallyCoveredMethods(Collection)} and finally
* reset after every round with {@link #resetCurrentAdditionallyCoveredMethods()}.
*/
@JsonIgnore
protected final Set coveredChangedMethods = new HashSet<>();
/**
* The number of additionally covered methods that are not yet covered in the current round. This
* matches the number of entries with value true
in {@link #coveredChangedMethods}.
*/
@JsonIgnore
private int currentAdditionallyCoveredMethodsCount = 0;
/**
* The {@link MethodId}s covered by the test or set of tests.
*/
@JsonIgnore
protected Set coveredMethods = null;
/** Duration in ms. May be null if not set. */
@JsonProperty("durationInMs")
@Nullable
protected Long durationInMs = null;
/**
* Field for tracking {@link IPrioritizableTests#getCurrentScore()}.
*/
@JsonProperty("currentScore")
private double currentScore;
@JsonProperty("numberOfAdditionallyCoveredMethods")
private int numberOfAdditionallyCoveredMethods = 0;
/**
* Field for storing the tests rank. The rank is the 1-based index of the test in the prioritized
* list.
*/
@JsonProperty("rank")
private Integer rank;
@JsonIgnore
private Set changedMethodLocations = Collections.emptySet();
public PrioritizableTestBase() {
}
public PrioritizableTestBase(Set changedMethodLocations) {
this.changedMethodLocations = changedMethodLocations;
}
/**
* Sets the methods that we are interested in testing e.g. because they changed in the given
* timeframe.
*/
@Override
public void initAdditionallyCoveredMethods(Set methodsToTest) {
Sets.SetView intersection = Sets.intersection(getCoveredMethods(), methodsToTest);
coveredChangedMethods.addAll(intersection);
currentAdditionallyCoveredMethodsCount = coveredChangedMethods.size();
}
/**
* Sets the number of additionally covered methods.
*/
public void setNumberOfAdditionallyCoveredMethods(int additionallyCoveredMethods) {
this.numberOfAdditionallyCoveredMethods = additionallyCoveredMethods;
}
@Override
public void resetCurrentAdditionallyCoveredMethods() {
currentAdditionallyCoveredMethodsCount = coveredChangedMethods.size();
}
@Override
public void removeFromCurrentAdditionallyCoveredMethods(Collection methodsToRemove) {
for (MethodId methodId : methodsToRemove) {
if (coveredChangedMethods.contains(methodId)) {
currentAdditionallyCoveredMethodsCount--;
}
}
}
@Override
public long getNumberOfCoveredMethods() {
return getCoveredMethods().size();
}
@Override
public long getNumberOfCurrentAdditionallyCoveredMethods() {
return currentAdditionallyCoveredMethodsCount;
}
@Override
public Set getCoveredChangedMethods() {
return coveredChangedMethods;
}
@Override
public void setCurrentScore(double currentScore) {
this.currentScore = currentScore;
}
public Integer getRank() {
return rank;
}
@Override
public void setRank(int rank) {
this.rank = rank;
}
@Override
public double getCurrentScore() {
return currentScore;
}
@Override
public Set getChangedMethodLocations() {
return changedMethodLocations;
}
@Override
public void incrementNumberOfAdditionallyCoveredMethods() {
numberOfAdditionallyCoveredMethods++;
}
public int getNumberOfAdditionallyCoveredMethods() {
return numberOfAdditionallyCoveredMethods;
}
/**
* Removes all {@link MethodId} containing fields to reduce the necessary amount of storage for the
* test (see TS-36443).
*/
public void clearMethodIds() {
coveredMethods.clear();
coveredChangedMethods.clear();
changedMethodLocations.clear();
}
}