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

bdi.junit.PicoContainerRule Maven / Gradle / Ivy

package bdi.junit;

import org.junit.rules.ExternalResource;

/**
 * @author @aloyer
 */
public class PicoContainerRule extends ExternalResource {

    private final PicoContainer context = new PicoContainer();
    private ComponentLifecycle lifecycle = new ComponentLifecycle();
    private boolean started;

    public PicoContainerRule(Class... components) {
        declare(components);
    }

    public PicoContainerRule using(ComponentLifecycle lifecycle) {
        checkIfNotStarted();
        this.lifecycle = lifecycle;
        return this;
    }

    public PicoContainerRule declare(Class... components) {
        checkIfNotStarted();
        for (Class component : components) {
            context.addClass(component);
        }
        return this;
    }

    private void checkIfNotStarted() {
        if (started)
            throw new IllegalStateException("Container already started");
    }

    public  T get(Class type) {
        if (!started)
            startContext();
        return context.getInstance(type);
    }

    @Override
    protected void before() throws Throwable {
    }

    private void startContext() {
        started = true;
        context.start();
        lifecycle.before(context.underlying().getComponents());
    }

    @Override
    protected void after() {
        lifecycle.after(context.underlying().getComponents());
        context.stop();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy