4,6 → 4,7 |
|
import org.junit.Test; |
import org.mentacontainer.Container; |
import org.mentacontainer.Scope; |
|
public class MentaContainerTest { |
|
56,7 → 57,7 |
|
Container c = new MentaContainer(); |
|
c.ioc("myStr", String.class, true).addInitValue("hello"); |
c.ioc("myStr", String.class, Scope.SINGLETON).addInitValue("hello"); |
|
Assert.assertEquals("hello", c.get("myStr")); |
|
68,6 → 69,63 |
|
Assert.assertTrue(s1.equals(s2)); |
} |
|
private static class MyThread extends Thread { |
|
private final Container c; |
private final String key; |
private String value = null; |
|
public MyThread(Container c, String key) { |
super(); |
this.c = c; |
this.key = key; |
} |
|
@Override |
public void run() { |
|
for(int i = 0; i < 50; i++) { |
|
String v = c.get(key); |
|
if (this.value != null) { |
|
Assert.assertTrue(this.value == v); |
|
} |
|
this.value = v; |
} |
} |
|
public String getValue() { return value; } |
} |
|
@Test |
public void testThreadLocal() throws Exception { |
|
final Container c = new MentaContainer(); |
|
c.ioc("myStr", String.class, Scope.THREAD).addInitValue("saoj"); |
|
String s1 = c.get("myStr"); |
|
MyThread t1 = new MyThread(c, "myStr"); |
MyThread t2 = new MyThread(c, "myStr"); |
|
t1.start(); |
t2.start(); |
|
t1.join(); |
t2.join(); |
|
String s2 = t1.getValue(); |
String s3 = t2.getValue(); |
|
Assert.assertTrue(s1 != s2); |
Assert.assertTrue(s2 != s3); |
} |
|
public static interface MyDAO { |
|
141,7 → 199,7 |
|
c.ioc("myDAO", JdbcMyDAO.class); |
|
c.ioc("aDependency", SomeDependency.class, true /* singleton */).addProperty("name", "A super dependency!"); |
c.ioc("aDependency", SomeDependency.class, Scope.SINGLETON).addProperty("name", "A super dependency!"); |
|
c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
|