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

de.leanovate.pragmatic.ioc.TestScope Maven / Gradle / Ivy

The newest version!
package de.leanovate.pragmatic.ioc;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

public class TestScope extends AbstractScope {
    static ThreadLocal> PER_THREAD_INSTANCES = ThreadLocal.withInitial(HashMap::new);

    static ThreadLocal> MOCKER = new ThreadLocal<>();

    @Override
    @SuppressWarnings("unchecked")
    public  T getInstance(Class injectedClass, Optional name, Supplier supplier) {

        final String key = name.map((n) -> injectedClass.getName() + ":" + n).orElse(injectedClass.getName());
        final Map instances = PER_THREAD_INSTANCES.get();

        T instance = (T) instances.get(key);

        if (instance == null) {
            if (MOCKER.get() != null) {
                instance = (T) MOCKER.get().apply(injectedClass);
            } else {
                instance = createInstance(key, supplier);
            }
            instances.put(key, instance);
        }

        return instance;
    }

    @Override
    public void bind(final Object instance) {

        Objects.requireNonNull(instance);
        final Map instances = PER_THREAD_INSTANCES.get();
        instances.put(instance.getClass().getName(), instance);
    }

    @Override
    public  void bind(final Class injectedClass, final T instance) {

        final Map instances = PER_THREAD_INSTANCES.get();
        instances.put(Objects.requireNonNull(injectedClass).getName(), Objects.requireNonNull(instance));
    }

    public  T bindMock(final Class injectedClass) {

        if (MOCKER.get() == null) {
            throw new RuntimeException("No mocker defined");
        }
        @SuppressWarnings("unchecked")
        T instance = (T) MOCKER.get().apply(injectedClass);
        bind(injectedClass, instance);
        return instance;
    }

    public void reset(final Function mocker) {

        PER_THREAD_INSTANCES.remove();
        if (mocker != null) {
            MOCKER.set(mocker);
        } else {
            MOCKER.remove();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy