package com.calclab.suco.tests.ioc.module;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.calclab.suco.client.SucoFactory;
import com.calclab.suco.client.ioc.Container;
import com.calclab.suco.client.ioc.module.AbstractModule;
import com.calclab.suco.client.ioc.module.Factory;
public class FactoryTest {
public static class ABModule extends AbstractModule {
@Override
protected void onInstall() {
register(new Factory<ClassA>(ClassA.class) {
@Override
public ClassA create() {
return new ClassA();
}
@Override
public void onAfterCreated(final ClassA instance) {
$(ClassB.class);
}
});
register(new Factory<ClassB>(ClassB.class) {
@Override
public ClassB create() {
return new ClassB($(ClassA.class));
}
});
}
}
public static class ClassA {
private boolean isBCreated;
public ClassA() {
isBCreated = false;
}
public boolean isBCreated() {
return isBCreated;
}
public void setBCreated(final boolean isBCreated) {
this.isBCreated = isBCreated;
}
}
public static class ClassB {
public ClassB(final ClassA a) {
a.setBCreated(true);
}
}
@Test
public void theTest() {
final Container container = SucoFactory.create();
new ABModule().onInstall(container);
final ClassA instanceA = container.getInstance(ClassA.class);
assertTrue(instanceA.isBCreated());
}
}