Rev 44 | Rev 58 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 44 | Rev 57 | ||
---|---|---|---|
Line 2... | Line 2... | ||
2 | 2 | ||
3 | import junit.framework.Assert; |
3 | import junit.framework.Assert; |
4 | 4 | ||
5 | import org.junit.Test; |
5 | import org.junit.Test; |
6 | import org.mentacontainer.Container; |
6 | import org.mentacontainer.Container; |
- | 7 | import org.mentacontainer.Scope; |
|
7 | 8 | ||
8 | public class MentaContainerTest { |
9 | public class MentaContainerTest { |
9 | 10 | ||
10 | @Test |
11 | @Test |
11 | public void testSimpleGet() { |
12 | public void testSimpleGet() { |
Line 54... | Line 55... | ||
54 | @Test |
55 | @Test |
55 | public void testSingleton() { |
56 | public void testSingleton() { |
56 | 57 | ||
57 | Container c = new MentaContainer(); |
58 | Container c = new MentaContainer(); |
58 | 59 | ||
59 | c.ioc("myStr", String.class, |
60 | c.ioc("myStr", String.class, Scope.SINGLETON).addInitValue("hello"); |
60 | 61 | ||
61 | Assert.assertEquals("hello", c.get("myStr")); |
62 | Assert.assertEquals("hello", c.get("myStr")); |
62 | 63 | ||
63 | String s1 = c.get("myStr"); |
64 | String s1 = c.get("myStr"); |
64 | 65 | ||
65 | String s2 = c.get("myStr"); |
66 | String s2 = c.get("myStr"); |
66 | 67 | ||
67 | Assert.assertTrue(s1 == s2); |
68 | Assert.assertTrue(s1 == s2); |
68 | 69 | ||
69 | Assert.assertTrue(s1.equals(s2)); |
70 | Assert.assertTrue(s1.equals(s2)); |
- | 71 | }
|
|
- | 72 | ||
- | 73 | private static class MyThread extends Thread { |
|
- | 74 | ||
- | 75 | private final Container c; |
|
- | 76 | private final String key; |
|
- | 77 | private String value = null; |
|
- | 78 | ||
- | 79 | public MyThread(Container c, String key) { |
|
- | 80 | super(); |
|
- | 81 | this.c = c; |
|
- | 82 | this.key = key; |
|
- | 83 | }
|
|
- | 84 | ||
- | 85 | @Override |
|
- | 86 | public void run() { |
|
- | 87 | ||
- | 88 | for(int i = 0; i < 50; i++) { |
|
- | 89 | ||
- | 90 | String v = c.get(key); |
|
- | 91 | ||
- | 92 | if (this.value != null) { |
|
- | 93 | ||
- | 94 | Assert.assertTrue(this.value == v); |
|
- | 95 | ||
- | 96 | }
|
|
- | 97 | ||
- | 98 | this.value = v; |
|
- | 99 | }
|
|
- | 100 | }
|
|
- | 101 | ||
- | 102 | public String getValue() { return value; } |
|
- | 103 | }
|
|
- | 104 | ||
- | 105 | @Test |
|
- | 106 | public void testThreadLocal() throws Exception { |
|
- | 107 | ||
- | 108 | final Container c = new MentaContainer(); |
|
- | 109 | ||
- | 110 | c.ioc("myStr", String.class, Scope.THREAD).addInitValue("saoj"); |
|
- | 111 | ||
- | 112 | String s1 = c.get("myStr"); |
|
- | 113 | ||
- | 114 | MyThread t1 = new MyThread(c, "myStr"); |
|
- | 115 | MyThread t2 = new MyThread(c, "myStr"); |
|
- | 116 | ||
- | 117 | t1.start(); |
|
- | 118 | t2.start(); |
|
- | 119 | ||
- | 120 | t1.join(); |
|
- | 121 | t2.join(); |
|
- | 122 | ||
- | 123 | String s2 = t1.getValue(); |
|
- | 124 | String s3 = t2.getValue(); |
|
- | 125 | ||
- | 126 | Assert.assertTrue(s1 != s2); |
|
- | 127 | Assert.assertTrue(s2 != s3); |
|
70 | }
|
128 | }
|
71 | 129 | ||
72 | public static interface MyDAO { |
130 | public static interface MyDAO { |
73 | 131 | ||
74 | public Object getSomething(); |
132 | public Object getSomething(); |
Line 139... | Line 197... | ||
139 | 197 | ||
140 | Container c = new MentaContainer(); |
198 | Container c = new MentaContainer(); |
141 | 199 | ||
142 | c.ioc("myDAO", JdbcMyDAO.class); |
200 | c.ioc("myDAO", JdbcMyDAO.class); |
143 | 201 | ||
144 | c.ioc("aDependency", SomeDependency.class, |
202 | c.ioc("aDependency", SomeDependency.class, Scope.SINGLETON).addProperty("name", "A super dependency!"); |
145 | 203 | ||
146 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
204 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
147 | 205 | ||
148 | c.autowire("conn" /* the property name that will receive the dependency */, |
206 | c.autowire("conn" /* the property name that will receive the dependency */, |
149 | Connection.class /* the type of the property that will receive the dependency */, |
207 | Connection.class /* the type of the property that will receive the dependency */, |