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

com.github.aoreshin.junit5.extensions.TestTemplateInvocationContextBuilder Maven / Gradle / Ivy

There is a newer version: 83
Show newest version
package com.github.aoreshin.junit5.extensions;

import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;

import java.util.ArrayList;
import java.util.List;

public final class TestTemplateInvocationContextBuilder {
    private final List extensions = new ArrayList<>();
    private String displayName;
    private String displayNamePrefix;
    private String displayNamePostfix;

    public TestTemplateInvocationContextBuilder withDisplayName(String displayName) {
        this.displayName = displayName;
        return this;
    }

    public TestTemplateInvocationContextBuilder withDisplayNamePrefix(String displayNamePrefix) {
        this.displayNamePrefix = displayNamePrefix;
        return this;
    }

    public TestTemplateInvocationContextBuilder withDisplayNamePostfix(String displayNamePostfix) {
        this.displayNamePostfix = displayNamePostfix;
        return this;
    }

    public  TestTemplateInvocationContextBuilder addParameterResolver(Class parameterType, T value) {
        ParameterResolver resolver = new GenericParameterResolver<>(parameterType, value);
        extensions.add(resolver);
        return this;
    }

    public  TestTemplateInvocationContextBuilder addParameterResolver(Class parameterType, T value, String parameterName) {
        ParameterResolver resolver = new GenericParameterResolver<>(parameterType, value, parameterName);
        extensions.add(resolver);
        return this;
    }

    public TestTemplateInvocationContextBuilder addExtension(Extension extension) {
        extensions.add(extension);
        return this;
    }

    public TestTemplateInvocationContextBuilder addExtension(int index, Extension extension) {
        extensions.add(index, extension);
        return this;
    }

    public TestTemplateInvocationContext build() {
        StringBuilder stringBuilder = new StringBuilder();

        if (displayNamePrefix != null) {
            stringBuilder
                    .append(displayNamePrefix)
                    .append(" ");
        }

        stringBuilder.append(displayName);

        if (displayNamePostfix != null) {
            stringBuilder
                    .append(" ")
                    .append(displayNamePostfix);
        }

        return new TestTemplateInvocationContextImpl(stringBuilder.toString(), extensions);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy