MentaContainer

Compare Revisions

Ignore whitespace Rev 140 → Rev 141

/trunk/src/test/java/org/mentacontainer/impl/GenericFactoryTest.java
3,10 → 3,8
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.impl.GenericFactory;
import org.mentacontainer.impl.MentaContainer;
import org.mentacontainer.Creator;
 
public class GenericFactoryTest {
27,7 → 25,7
Container c = new MentaContainer();
// giveMeSomething => method that will be called to return object
Factory generic = new GenericFactory(factory, "giveMeSomething");
Creator<String> generic = new GenericFactory<String>(factory, "giveMeSomething");
c.ioc("myFactory", generic);
/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
3,9 → 3,9
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Factory;
import org.mentacontainer.Container;
import org.mentacontainer.Creator;
import org.mentacontainer.Interceptor;
import org.mentacontainer.Scope;
 
public class MentaContainerTest {
436,100 → 436,4
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.Creator;
 
public class WrapperFactoryTest {
13,7 → 13,7
String s = new String("saoj");
Factory ic = new WrapperFactory(s);
Creator<String> ic = new WrapperFactory<String>(s);
Container c = new MentaContainer();
/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
3,6 → 3,7
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Creator;
import org.mentacontainer.Factory;
 
public class ClassFactoryTest {
31,13 → 32,13
MentaContainer container = new MentaContainer();
Factory c1 = new ClassFactory(container, User.class).addInitValue("saoj");
Creator<User> c1 = new ClassFactory<User>(container, User.class).addInitValue("saoj");
Factory c2 = new ClassFactory(container, User.class).addPropertyValue("username", "soliveira");
Creator<User> c2 = new ClassFactory<User>(container, User.class).addPropertyValue("username", "soliveira");
User u1 = c1.getInstance();
User u1 = c1.createInstance();
User u2 = c2.getInstance();
User u2 = c2.createInstance();
Assert.assertTrue(u1 != u2);
66,10 → 67,10
c.ioc("myString", String.class).addInitValue("hello");
Factory c1 = new ClassFactory(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
Creator<TestObj1> c1 = new ClassFactory<TestObj1>(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
.addInitValue(new User("saoj"));
TestObj1 o = c1.getInstance();
TestObj1 o = c1.createInstance();
Assert.assertEquals("hello", o.s);
Assert.assertEquals(20, o.i);
95,9 → 96,9
c.ioc("myString", String.class).addInitValue("hello");
Factory c1 = new ClassFactory(c, TestObj1_1.class).addPropertyDependency("s", "myString").addPropertyValue("i", 30);
Creator<TestObj1_1> c1 = new ClassFactory<TestObj1_1>(c, TestObj1_1.class).addPropertyDependency("s", "myString").addPropertyValue("i", 30);
TestObj1_1 o = c1.getInstance();
TestObj1_1 o = c1.createInstance();
Assert.assertEquals("hello", o.s);
Assert.assertEquals(30, o.i);
119,9 → 120,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", 20);
Creator<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", 20);
TestObj2 o = f.getInstance();
TestObj2 o = f.createInstance();
Assert.assertEquals(20, o.x);
}
131,9 → 132,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
Creator<TestObj2> f = new ClassFactory<TestObj2>(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
TestObj2 o = f.getInstance();
TestObj2 o = f.createInstance();
Assert.assertEquals(20, o.x);
}
154,9 → 155,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", 20);
Creator<TestObj2_1> f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", 20);
TestObj2_1 o = f.getInstance();
TestObj2_1 o = f.createInstance();
Assert.assertEquals(20, o.x);
}
166,9 → 167,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
Creator<TestObj2_1>f = new ClassFactory<TestObj2_1>(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
TestObj2_1 o = f.getInstance();
TestObj2_1 o = f.createInstance();
Assert.assertEquals(20, o.x);
}
188,9 → 189,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj3.class).addInitValue(20);
Creator<TestObj3> f = new ClassFactory<TestObj3>(container, TestObj3.class).addInitValue(20);
TestObj3 o = f.getInstance();
TestObj3 o = f.createInstance();
Assert.assertEquals(20, o.x);
}
209,9 → 210,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj3_1.class).addInitValue(20);
Creator<TestObj3_1> f = new ClassFactory<TestObj3_1>(container, TestObj3_1.class).addInitValue(20);
TestObj3_1 o = f.getInstance();
TestObj3_1 o = f.createInstance();
Assert.assertEquals(20, o.x);
}
232,9 → 233,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
Creator<TestObj3_2> f = new ClassFactory<TestObj3_2>(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
TestObj3_2 o = f.getInstance();
TestObj3_2 o = f.createInstance();
Assert.assertEquals(20, o.x);
Assert.assertEquals(30L, o.l);
260,9 → 261,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
Creator<TestObj4> f = new ClassFactory<TestObj4>(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
TestObj4 o = f.getInstance();
TestObj4 o = f.createInstance();
Assert.assertEquals(20, o.x);
Assert.assertEquals("hello", o.s);
280,9 → 281,9
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj22.class);
Creator<TestObj22> f = new ClassFactory<TestObj22>(container, TestObj22.class);
TestObj22 o = f.getInstance();
TestObj22 o = f.createInstance();
Assert.assertNotNull(o);