MentaContainer

Compare Revisions

Ignore whitespace Rev 145 → Rev 146

/trunk/src/test/java/org/mentacontainer/impl/GenericFactoryTest.java
3,8 → 3,10
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.Factory;
import org.mentacontainer.impl.GenericFactory;
import org.mentacontainer.impl.MentaContainer;
 
public class GenericFactoryTest {
25,7 → 27,7
Container c = new MentaContainer();
// giveMeSomething => method that will be called to return object
Factory<String> generic = new GenericFactory<String>(factory, "giveMeSomething");
Factory generic = new GenericFactory(factory, "giveMeSomething");
c.ioc("myFactory", generic);
/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
3,6 → 3,8
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.Scope;
 
434,4 → 436,100
Assert.assertNotNull(conn.getMyDep());
}
private static class SomeObject {
private boolean destroyed = false;
private boolean created = false;
public void destroyed() { this.destroyed = true; }
public boolean isDestroyed() { return destroyed; }
public void created() { this.created = true; }
public boolean isCreated() { return created; }
}
private static class SomeFactory implements Factory, Interceptor<SomeObject> {
@Override
public <T> T getInstance() {
return (T) new SomeObject();
}
@Override
public Class<? extends Object> getType() {
return SomeObject.class;
}
@Override
public void onCleared(SomeObject obj) {
obj.destroyed();
}
@Override
public void onCreated(SomeObject obj) {
obj.created();
}
}
@Test
public void testInterceptor() {
Container c = new MentaContainer();
c.ioc("o", new SomeFactory(), Scope.SINGLETON);
SomeObject o = c.get("o");
Assert.assertTrue(o.isCreated());
c.clear(Scope.SINGLETON);
Assert.assertEquals(true, o.isDestroyed());
c.ioc("o", new SomeFactory(), Scope.THREAD);
o = c.get("o");
Assert.assertTrue(o.isCreated());
c.clear(Scope.SINGLETON);
Assert.assertEquals(false, o.isDestroyed());
c.clear(Scope.THREAD);
Assert.assertEquals(true, o.isDestroyed());
c.ioc("o", new SomeFactory(), Scope.NONE);
o = c.get("o");
Assert.assertTrue(o.isCreated());
o = c.clear("o");
Assert.assertNull(o);
c.ioc("o", new SomeFactory(), Scope.THREAD);
o = c.clear("o");
Assert.assertNull(o);
o = c.get("o");
Assert.assertTrue(o.isCreated());
o = c.clear("o");
Assert.assertEquals(true, o.isDestroyed());
}
}
/trunk/src/test/java/org/mentacontainer/impl/WrapperFactoryTest.java
3,8 → 3,8
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.Factory;
 
public class WrapperFactoryTest {
13,7 → 13,7
String s = new String("saoj");
Factory<String> ic = new SingletonFactory<String>(s);
Factory ic = new WrapperFactory(s);
Container c = new MentaContainer();
/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
31,9 → 31,9
MentaContainer container = new MentaContainer();
Factory<User> c1 = new ClassFactory<User>(container, User.class).addInitValue("saoj");
Factory c1 = new ClassFactory(container, User.class).addInitValue("saoj");
Factory<User> c2 = new ClassFactory<User>(container, User.class).addPropertyValue("username", "soliveira");
Factory c2 = new ClassFactory(container, User.class).addPropertyValue("username", "soliveira");
User u1 = c1.getInstance();
66,7 → 66,7
c.ioc("myString", String.class).addInitValue("hello");
Factory<TestObj1> c1 = new ClassFactory<TestObj1>(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
Factory c1 = new ClassFactory(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
.addInitValue(new User("saoj"));
TestObj1 o = c1.getInstance();
95,7 → 95,7
c.ioc("myString", String.class).addInitValue("hello");
Factory<TestObj1_1> c1 = new ClassFactory<TestObj1_1>(c, TestObj1_1.class).addPropertyDependency("s", "myString").addPropertyValue("i", 30);
Factory c1 = new ClassFactory(c, TestObj1_1.class).addPropertyDependency("s", "myString").addPropertyValue("i", 30);
TestObj1_1 o = c1.getInstance();
119,7 → 119,7
MentaContainer container = new MentaContainer();
Factory<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", 20);
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", 20);
TestObj2 o = f.getInstance();
131,7 → 131,7
MentaContainer container = new MentaContainer();
Factory<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
TestObj2 o = f.getInstance();
154,7 → 154,7
MentaContainer container = new MentaContainer();
Factory<TestObj2_1> f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", 20);
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", 20);
TestObj2_1 o = f.getInstance();
166,7 → 166,7
MentaContainer container = new MentaContainer();
Factory<TestObj2_1>f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
TestObj2_1 o = f.getInstance();
188,7 → 188,7
MentaContainer container = new MentaContainer();
Factory<TestObj3> f = new ClassFactory<TestObj3>(container, TestObj3.class).addInitValue(20);
Factory f = new ClassFactory(container, TestObj3.class).addInitValue(20);
TestObj3 o = f.getInstance();
209,7 → 209,7
MentaContainer container = new MentaContainer();
Factory<TestObj3_1> f = new ClassFactory<TestObj3_1>(container, TestObj3_1.class).addInitValue(20);
Factory f = new ClassFactory(container, TestObj3_1.class).addInitValue(20);
TestObj3_1 o = f.getInstance();
232,7 → 232,7
MentaContainer container = new MentaContainer();
Factory<TestObj3_2> f = new ClassFactory<TestObj3_2>(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
Factory f = new ClassFactory(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
TestObj3_2 o = f.getInstance();
260,7 → 260,7
MentaContainer container = new MentaContainer();
Factory<TestObj4> f = new ClassFactory<TestObj4>(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
Factory f = new ClassFactory(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
TestObj4 o = f.getInstance();
280,7 → 280,7
MentaContainer container = new MentaContainer();
Factory<TestObj22> f = new ClassFactory<TestObj22>(container, TestObj22.class);
Factory f = new ClassFactory(container, TestObj22.class);
TestObj22 o = f.getInstance();