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

junitparams.internal.parameters.ParametersFromCustomProvider Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package junitparams.internal.parameters;

import org.junit.runners.model.FrameworkMethod;

import junitparams.custom.ParametersProvider;
import junitparams.internal.annotation.CustomParametersDescriptor;
import junitparams.internal.annotation.FrameworkMethodAnnotations;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

class ParametersFromCustomProvider implements ParametrizationStrategy {

    private final FrameworkMethodAnnotations frameworkMethodAnnotations;
    private final FrameworkMethod frameworkMethod;

    ParametersFromCustomProvider(FrameworkMethod frameworkMethod) {
        this.frameworkMethod = frameworkMethod;
        frameworkMethodAnnotations = new FrameworkMethodAnnotations(frameworkMethod);
    }

    @Override
    public boolean isApplicable() {
        return frameworkMethodAnnotations.hasCustomParameters();
    }

    @Override
    public Object[] getParameters() {
        CustomParametersDescriptor parameters = frameworkMethodAnnotations.getCustomParameters();
        ParametersProvider provider = instantiate(parameters.provider());
        provider.initialize(parameters.annotation(), frameworkMethod);
        return provider.getParameters();
    }

    private ParametersProvider instantiate(Class providerClass) {
        try {
            return providerClass.getConstructor().newInstance();
        } catch (Exception ignored) {
            throw new RuntimeException("Your Provider class must have a public no-arg constructor!");
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy