Rev 57 | Rev 64 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4 | soliveira | 1 | package org.mentacontainer.impl; |
2 | |||
3 | import junit.framework.Assert; |
||
4 | |||
5 | import org.junit.Test; |
||
6 | import org.mentacontainer.Container; |
||
57 | soliveira | 7 | import org.mentacontainer.Scope; |
4 | soliveira | 8 | |
9 | public class MentaContainerTest { |
||
20 | soliveira | 10 | |
11 | @Test |
||
12 | public void testSimpleGet() { |
||
13 | |||
14 | Container c = new MentaContainer(); |
||
15 | |||
16 | c.ioc("myStr", String.class); |
||
17 | |||
18 | Assert.assertEquals("", c.get("myStr")); |
||
19 | |||
41 | soliveira | 20 | String s1 = c.get("myStr"); |
20 | soliveira | 21 | |
41 | soliveira | 22 | String s2 = c.get("myStr"); |
20 | soliveira | 23 | |
24 | Assert.assertTrue(s1 != s2); |
||
25 | |||
26 | Assert.assertTrue(s1.equals(s2)); |
||
27 | } |
||
28 | |||
29 | @Test |
||
30 | public void testConstructorInit() { |
||
31 | |||
32 | Container c = new MentaContainer(); |
||
33 | |||
34 | c.ioc("myStr", String.class).addInitValue("hello"); |
||
35 | |||
36 | Assert.assertEquals("hello", c.get("myStr")); |
||
37 | |||
41 | soliveira | 38 | String s1 = c.get("myStr"); |
20 | soliveira | 39 | |
41 | soliveira | 40 | String s2 = c.get("myStr"); |
20 | soliveira | 41 | |
42 | Assert.assertTrue(s1 != s2); |
||
43 | |||
44 | Assert.assertTrue(s1.equals(s2)); |
||
45 | |||
46 | c.ioc("anotherStr", String.class).addInitValue("hi"); |
||
47 | |||
41 | soliveira | 48 | String s3 = c.get("anotherStr"); |
20 | soliveira | 49 | |
50 | Assert.assertTrue(s1 != s3); |
||
51 | |||
52 | Assert.assertFalse(s1.equals(s3)); |
||
53 | } |
||
54 | |||
55 | @Test |
||
56 | public void testSingleton() { |
||
57 | |||
58 | Container c = new MentaContainer(); |
||
59 | |||
57 | soliveira | 60 | c.ioc("myStr", String.class, Scope.SINGLETON).addInitValue("hello"); |
20 | soliveira | 61 | |
62 | Assert.assertEquals("hello", c.get("myStr")); |
||
63 | |||
41 | soliveira | 64 | String s1 = c.get("myStr"); |
20 | soliveira | 65 | |
41 | soliveira | 66 | String s2 = c.get("myStr"); |
20 | soliveira | 67 | |
68 | Assert.assertTrue(s1 == s2); |
||
69 | |||
70 | Assert.assertTrue(s1.equals(s2)); |
||
71 | } |
||
57 | soliveira | 72 | |
58 | soliveira | 73 | @Test |
74 | public void testCheckAndClear() { |
||
75 | |||
76 | Container c = new MentaContainer(); |
||
77 | |||
78 | c.ioc("myStr", String.class, Scope.SINGLETON).addInitValue("hello"); |
||
79 | |||
80 | Assert.assertEquals(false, c.check("myStr")); |
||
81 | |||
82 | String s1 = c.get("myStr"); |
||
83 | |||
84 | Assert.assertEquals(true, c.check("myStr")); |
||
85 | |||
86 | String s2 = c.get("myStr"); |
||
87 | |||
88 | Assert.assertTrue(s1 == s2); |
||
89 | |||
90 | c.clear(Scope.SINGLETON); |
||
91 | |||
92 | Assert.assertEquals(false, c.check("myStr")); |
||
93 | |||
94 | String s3 = c.get("myStr"); |
||
95 | |||
96 | Assert.assertTrue(s3 != s2); |
||
97 | } |
||
98 | |||
57 | soliveira | 99 | private static class MyThread extends Thread { |
100 | |||
101 | private final Container c; |
||
102 | private final String key; |
||
103 | private String value = null; |
||
104 | |||
105 | public MyThread(Container c, String key) { |
||
106 | super(); |
||
107 | this.c = c; |
||
108 | this.key = key; |
||
109 | } |
||
110 | |||
111 | @Override |
||
112 | public void run() { |
||
113 | |||
114 | for(int i = 0; i < 50; i++) { |
||
115 | |||
116 | String v = c.get(key); |
||
117 | |||
118 | if (this.value != null) { |
||
119 | |||
120 | Assert.assertTrue(this.value == v); |
||
121 | |||
122 | } |
||
123 | |||
124 | this.value = v; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | public String getValue() { return value; } |
||
129 | } |
||
130 | |||
131 | @Test |
||
132 | public void testThreadLocal() throws Exception { |
||
133 | |||
134 | final Container c = new MentaContainer(); |
||
135 | |||
136 | c.ioc("myStr", String.class, Scope.THREAD).addInitValue("saoj"); |
||
137 | |||
138 | String s1 = c.get("myStr"); |
||
139 | |||
140 | MyThread t1 = new MyThread(c, "myStr"); |
||
141 | MyThread t2 = new MyThread(c, "myStr"); |
||
142 | |||
143 | t1.start(); |
||
144 | t2.start(); |
||
145 | |||
146 | t1.join(); |
||
147 | t2.join(); |
||
148 | |||
149 | String s2 = t1.getValue(); |
||
150 | String s3 = t2.getValue(); |
||
151 | |||
152 | Assert.assertTrue(s1 != s2); |
||
153 | Assert.assertTrue(s2 != s3); |
||
154 | } |
||
20 | soliveira | 155 | |
26 | soliveira | 156 | public static interface MyDAO { |
157 | |||
158 | public Object getSomething(); |
||
159 | |||
160 | } |
||
161 | |||
27 | soliveira | 162 | public static class SomeDependency { |
163 | |||
164 | private String name; |
||
165 | |||
166 | public SomeDependency() { } |
||
167 | |||
168 | public void setName(String name) { |
||
169 | this.name = name; |
||
170 | } |
||
171 | |||
172 | public String getName() { |
||
173 | return name; |
||
174 | } |
||
175 | } |
||
176 | |||
26 | soliveira | 177 | public static class Connection { |
178 | |||
179 | private final String name; |
||
180 | |||
27 | soliveira | 181 | private SomeDependency dep; |
182 | |||
26 | soliveira | 183 | public Connection(String name) { |
184 | this.name = name; |
||
185 | } |
||
186 | |||
187 | public String toString() { |
||
188 | return getClass().getName() + ": " + name; |
||
189 | } |
||
190 | |||
191 | public String getName() { return name; } |
||
27 | soliveira | 192 | |
193 | public void setMyDep(SomeDependency dep) { this.dep = dep; } |
||
194 | |||
195 | public SomeDependency getMyDep() { return dep; } |
||
26 | soliveira | 196 | } |
197 | |||
27 | soliveira | 198 | |
199 | |||
26 | soliveira | 200 | public static class JdbcMyDAO implements MyDAO { |
201 | |||
202 | private Connection conn; |
||
203 | |||
204 | public void setConn(Connection conn) { this.conn = conn; } |
||
205 | |||
206 | public Connection getConn() { return conn; } |
||
207 | |||
208 | public Object getSomething() { |
||
209 | |||
28 | soliveira | 210 | // use the connection to do something... |
26 | soliveira | 211 | |
28 | soliveira | 212 | Assert.assertNotNull(conn); // it cannot be null! |
213 | |||
214 | // also test if the connection also received the myDep dependency... |
||
215 | |||
216 | Assert.assertNotNull(conn.getMyDep()); |
||
217 | |||
218 | return conn.toString(); |
||
26 | soliveira | 219 | } |
220 | } |
||
221 | |||
28 | soliveira | 222 | private Container getConfiguredContainer() { |
26 | soliveira | 223 | |
224 | Container c = new MentaContainer(); |
||
225 | |||
226 | c.ioc("myDAO", JdbcMyDAO.class); |
||
227 | |||
57 | soliveira | 228 | c.ioc("aDependency", SomeDependency.class, Scope.SINGLETON).addProperty("name", "A super dependency!"); |
27 | soliveira | 229 | |
26 | soliveira | 230 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
231 | |||
232 | c.autowire("conn" /* the property name that will receive the dependency */, |
||
233 | Connection.class /* the type of the property that will receive the dependency */, |
||
234 | "connection" /* the source inside the container */); |
||
235 | |||
27 | soliveira | 236 | c.autowire("myDep" /* notice this is the setter of the Connection class */, |
237 | SomeDependency.class /* the type - it could be an interface for better decoupling */, |
||
238 | "aDependency" /* again this is the name of the bean/component inside the container */); |
||
239 | |||
28 | soliveira | 240 | return c; |
241 | } |
||
242 | |||
243 | @Test |
||
244 | public void testAutoWiring() { |
||
245 | |||
246 | Container c = getConfiguredContainer(); |
||
247 | |||
41 | soliveira | 248 | MyDAO myDAO = c.get("myDAO"); |
26 | soliveira | 249 | |
250 | // check implementation... |
||
251 | |||
252 | Assert.assertEquals(JdbcMyDAO.class, myDAO.getClass()); |
||
253 | |||
254 | // check dependency... |
||
255 | |||
256 | Connection conn = ((JdbcMyDAO) myDAO).getConn(); |
||
257 | |||
258 | Assert.assertNotNull(conn); |
||
259 | |||
260 | Assert.assertEquals("A super JDBC connection!", conn.getName()); |
||
261 | |||
27 | soliveira | 262 | // check dependency of dependency... |
263 | |||
264 | Assert.assertEquals(c.get("aDependency"), conn.getMyDep()); |
||
265 | |||
266 | Assert.assertTrue(c.get("aDependency") == conn.getMyDep()); // singleton! |
||
267 | |||
268 | // check DAO can do its job... |
||
269 | |||
26 | soliveira | 270 | Assert.assertNotNull(myDAO.getSomething()); |
271 | } |
||
28 | soliveira | 272 | |
273 | public static class SomeAction { |
||
274 | |||
275 | private MyDAO myDAO = null; |
||
276 | |||
277 | public void setMyDAO(MyDAO myDAO) { |
||
278 | |||
279 | this.myDAO = myDAO; |
||
280 | } |
||
281 | |||
282 | public MyDAO getMyDAO() { return myDAO; } |
||
283 | } |
||
284 | |||
40 | soliveira | 285 | public void testPopulate() { |
28 | soliveira | 286 | |
287 | Container c = getConfiguredContainer(); |
||
288 | |||
289 | SomeAction a = new SomeAction(); |
||
290 | |||
291 | c.populate(a); // great... properties of SomeAction were populated by container... |
||
292 | |||
293 | // let's check if myDAO was injected... |
||
294 | |||
295 | Assert.assertNotNull(a.getMyDAO()); |
||
296 | |||
297 | // also check if myDAO was corrected wired... |
||
298 | |||
299 | Connection conn = ((JdbcMyDAO) a.getMyDAO()).getConn(); |
||
300 | |||
301 | Assert.assertNotNull(conn); |
||
302 | |||
303 | // check if conn was also wired... |
||
304 | |||
305 | Assert.assertNotNull(conn.getMyDep()); |
||
306 | |||
307 | } |
||
4 | soliveira | 308 | } |