package com.calclab.suco.tests.ioc.module;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
import com.calclab.suco.client.ioc.Container;
import com.calclab.suco.client.ioc.Decorator;
import com.calclab.suco.client.ioc.Provider;
import com.calclab.suco.client.ioc.decorator.NoDecoration;
import com.calclab.suco.client.ioc.module.ModuleBuilderImpl;
import com.calclab.suco.client.ioc.module.SucoModule;
import com.calclab.suco.tests.ioc.TestHelper;
import com.calclab.suco.tests.ioc.TestHelper.VerificableFactory;
public class ModuleBuiderTest {
private Container container;
private ModuleBuilderImpl builder;
@Before
public void before() {
container = mock(Container.class);
builder = new ModuleBuilderImpl();
builder.setContainer(container);
}
@Test
public void shouldRegisterFactoriesWithoutDecoration() {
final VerificableFactory<Object> f1 = TestHelper.factory();
final VerificableFactory<Object> f2 = TestHelper.factory();
builder.register(f1, f2);
verify(container).registerProvider(same(NoDecoration.instance), eq(Object.class), same(f1));
}
@Test
public void shouldRegisterProviderWithDecorator() {
final Decorator d = NoDecoration.instance;
final Class<Object> cType = Object.class;
final Provider<Object> provider = TestHelper.provider();
builder.register(d, cType, provider);
verify(container).registerProvider(d, cType, provider);
}
@Test
public void shouldRegisterProviderWithDecoratorType() {
final Class<NoDecoration> dType = NoDecoration.class;
final Class<Object> cType = Object.class;
final Provider<Object> provider = TestHelper.provider();
builder.register(dType, cType, provider);
verify(container).getInstance(dType);
verify(container).registerProvider(null, cType, provider);
}
@Test
public void shouldRegisterProviderWithoutDecorator() {
final Class<Object> cType = Object.class;
final Provider<Object> provider = TestHelper.provider();
builder.register(cType, provider);
verify(container).registerProvider(null, cType, provider);
}
@Test
public void testInstall() {
final SucoModule m1 = mock(SucoModule.class);
final SucoModule m2 = mock(SucoModule.class);
builder.install(m1, m2);
verify(m1).onInstall(container);
verify(m2).onInstall(container);
}
}