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

com.undefinedlabs.scope.rules.model.JUnit4ScopeDescription Maven / Gradle / Ivy

Go to download

Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly identify, troubleshoot and fix failed builds. This artifact contains the classes to instrument the JUnit4 tests.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules.model;

import com.undefinedlabs.scope.utils.HashUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.runner.Description;

public class JUnit4ScopeDescription {

    private final String className;
    private final String methodName;
    private final String iteration;
    private final String hashedIteration;
    private final boolean isParameterized;

    public JUnit4ScopeDescription(final Description description) {
        this.className = StringUtils.isNotEmpty(description.getClassName()) ? description.getClassName() : "";
        this.iteration = extractIteration(description.getMethodName());
        this.hashedIteration = (this.iteration != null ? "[" + HashUtils.sha1Hex(this.iteration, 8) + "]" : "");
        this.methodName = StringUtils.isNotEmpty(description.getMethodName()) ? removeIteration(description.getMethodName()): "";
        this.isParameterized = StringUtils.isNotEmpty(description.getMethodName()) && description.getMethodName().contains("[");
    }

    private String extractIteration(String methodName) {
        if(methodName == null || !methodName.contains("[")) {
            return null;
        }

        return methodName.substring(methodName.indexOf("[") + 1, methodName.indexOf("]"));
    }

    private String removeIteration(String methodName) {
        if(!methodName.contains("[")){
            return methodName;
        }

        final String iteration = methodName.substring(methodName.indexOf("["), methodName.indexOf("]") + 1);
        return methodName.replace(iteration, "");
    }

    public String getClassName() {
        return className;
    }

    public String getMethodName() {
        return methodName;
    }

    public String getIteration() {
        return iteration;
    }

    public String getHashedIteration() {
        return hashedIteration;
    }

    public boolean isParameterized() {
        return isParameterized;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        JUnit4ScopeDescription that = (JUnit4ScopeDescription) o;

        return new EqualsBuilder()
                .append(isParameterized, that.isParameterized)
                .append(className, that.className)
                .append(methodName, that.methodName)
                .append(iteration, that.iteration)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(className)
                .append(methodName)
                .append(iteration)
                .append(isParameterized)
                .toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("className", className)
                .append("methodName", methodName)
                .append("iteration", iteration)
                .append("isParameterized", isParameterized)
                .toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy