All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.teamscale.service.testimpact.prioritization.PrioritizableTest Maven / Gradle / Ivy

There is a newer version: 2025.1.0-rc2
Show newest version
package com.teamscale.service.testimpact.prioritization;

import java.util.Collections;
import java.util.Objects;
import java.util.Set;

import org.conqat.engine.sourcecode.coverage.TestDetails;
import org.conqat.engine.sourcecode.coverage.TestUniformPathUtils;
import org.conqat.lib.commons.js_export.ExportToTypeScript;
import org.conqat.lib.commons.uniformpath.UniformPath;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.teamscale.commons.lang.ToStringHelpers;
import com.teamscale.index.testimpact.MethodId;

/**
 * {@link TestDetails} with information about their partition as well as
 * tracking data used during prioritization of tests. Two instances are
 * considered equal if the test details are equals.
 */
@ExportToTypeScript
public class PrioritizableTest extends PrioritizableTestBase {

	/**
	 * The uniform path the test (without -test- prefix). Currently cannot be of
	 * type {@link UniformPath} as the class is exported to JS.
	 */
	@JsonProperty("uniformPath")
	private String uniformPath;

	/** The reason the test has been selected. */
	@JsonProperty("selectionReason")
	private ETestSelectionReason selectionReason;

	/** Partition of the test. */
	@JsonProperty("partition")
	private String partition;

	@JsonIgnore
	private int numberOfCoveredMethods = 0;

	@JsonCreator
	public PrioritizableTest() {
	}

	public PrioritizableTest(String uniformPath, ETestSelectionReason selectionReason, String partition) {
		this(uniformPath, selectionReason, partition, Collections.emptySet());
	}

	public PrioritizableTest(String uniformPath, ETestSelectionReason selectionReason, String partition,
			Set changedMethodLocations) {
		super(changedMethodLocations);
		this.uniformPath = uniformPath;
		this.selectionReason = selectionReason;
		this.partition = partition;
		this.setCoveredMethods(changedMethodLocations);
	}

	/** Create an instance for added or modified tests. */
	public static PrioritizableTest createForAddedOrModifiedTest(String partition, String uniformPath) {
		return new PrioritizableTest(uniformPath, ETestSelectionReason.ADDED_OR_MODIFIED_TEST, partition);
	}

	/** @see #durationInMs */
	public void setDurationInMs(Long durationInMs) {
		this.durationInMs = durationInMs;
	}

	/** Returns true if the duration is available. */
	boolean isDurationSet() {
		return durationInMs != null;
	}

	/** @see #coveredMethods */
	public void setCoveredMethods(Set coveredMethods) {
		this.coveredMethods = coveredMethods;
		this.numberOfCoveredMethods = coveredMethods.size();
	}

	/** @see #partition */
	public String getPartition() {
		return partition;
	}

	@Override
	public Set getSelectionReasons() {
		return Collections.singleton(selectionReason);
	}

	@Override
	public long getDurationInMs(long durationValueIfNotSet) {
		if (!isDurationSet()) {
			return durationValueIfNotSet;
		}

		return durationInMs;
	}

	@Override
	public Set getCoveredMethods() {
		if (coveredMethods == null) {
			return Collections.emptySet();
		}
		return Collections.unmodifiableSet(coveredMethods);
	}

	@Override
	public long getNumberOfCoveredMethods() {
		return this.numberOfCoveredMethods;
	}

	/** The test execution path. */
	public String getUniformPath() {
		return toUniformPath().toString();
	}

	/** Compute the test execution path. */
	public UniformPath toUniformPath() {
		return TestUniformPathUtils.convertToTestUniformPath(uniformPath);
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}
		PrioritizableTest that = (PrioritizableTest) o;
		return Objects.equals(uniformPath, that.uniformPath) && Objects.equals(partition, that.partition);
	}

	@Override
	public int hashCode() {
		return Objects.hash(uniformPath, partition);
	}

	/**
	 * Used for testing only.
	 */
	@Override
	public String toString() {
		return ToStringHelpers.toReflectiveStringHelper(this).toString();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy