Rev 146 | 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; |
||
146 | soliveira | 6 | import org.mentacontainer.Interceptor; |
7 | import org.mentacontainer.Factory; |
||
4 | soliveira | 8 | import org.mentacontainer.Container; |
57 | soliveira | 9 | import org.mentacontainer.Scope; |
4 | soliveira | 10 | |
11 | public class MentaContainerTest { |
||
20 | soliveira | 12 | |
13 | @Test |
||
14 | public void testSimpleGet() { |
||
15 | |||
16 | Container c = new MentaContainer(); |
||
17 | |||
18 | c.ioc("myStr", String.class); |
||
19 | |||
20 | Assert.assertEquals("", c.get("myStr")); |
||
21 | |||
41 | soliveira | 22 | String s1 = c.get("myStr"); |
20 | soliveira | 23 | |
41 | soliveira | 24 | String s2 = c.get("myStr"); |
20 | soliveira | 25 | |
26 | Assert.assertTrue(s1 != s2); |
||
27 | |||
28 | Assert.assertTrue(s1.equals(s2)); |
||
29 | } |
||
30 | |||
31 | @Test |
||
32 | public void testConstructorInit() { |
||
33 | |||
34 | Container c = new MentaContainer(); |
||
35 | |||
36 | c.ioc("myStr", String.class).addInitValue("hello"); |
||
37 | |||
38 | Assert.assertEquals("hello", c.get("myStr")); |
||
39 | |||
41 | soliveira | 40 | String s1 = c.get("myStr"); |
20 | soliveira | 41 | |
41 | soliveira | 42 | String s2 = c.get("myStr"); |
20 | soliveira | 43 | |
44 | Assert.assertTrue(s1 != s2); |
||
45 | |||
46 | Assert.assertTrue(s1.equals(s2)); |
||
47 | |||
48 | c.ioc("anotherStr", String.class).addInitValue("hi"); |
||
49 | |||
41 | soliveira | 50 | String s3 = c.get("anotherStr"); |
20 | soliveira | 51 | |
52 | Assert.assertTrue(s1 != s3); |
||
53 | |||
54 | Assert.assertFalse(s1.equals(s3)); |
||
55 | } |
||
56 | |||
57 | @Test |
||
58 | public void testSingleton() { |
||
59 | |||
60 | Container c = new MentaContainer(); |
||
61 | |||
57 | soliveira | 62 | c.ioc("myStr", String.class, Scope.SINGLETON).addInitValue("hello"); |
20 | soliveira | 63 | |
64 | Assert.assertEquals("hello", c.get("myStr")); |
||
65 | |||
41 | soliveira | 66 | String s1 = c.get("myStr"); |
20 | soliveira | 67 | |
41 | soliveira | 68 | String s2 = c.get("myStr"); |
20 | soliveira | 69 | |
70 | Assert.assertTrue(s1 == s2); |
||
71 | |||
72 | Assert.assertTrue(s1.equals(s2)); |
||
73 | } |
||
57 | soliveira | 74 | |
58 | soliveira | 75 | @Test |
76 | public void testCheckAndClear() { |
||
77 | |||
78 | Container c = new MentaContainer(); |
||
79 | |||
80 | c.ioc("myStr", String.class, Scope.SINGLETON).addInitValue("hello"); |
||
81 | |||
82 | Assert.assertEquals(false, c.check("myStr")); |
||
83 | |||
84 | String s1 = c.get("myStr"); |
||
85 | |||
86 | Assert.assertEquals(true, c.check("myStr")); |
||
87 | |||
88 | String s2 = c.get("myStr"); |
||
89 | |||
90 | Assert.assertTrue(s1 == s2); |
||
91 | |||
92 | c.clear(Scope.SINGLETON); |
||
93 | |||
94 | Assert.assertEquals(false, c.check("myStr")); |
||
95 | |||
96 | String s3 = c.get("myStr"); |
||
97 | |||
98 | Assert.assertTrue(s3 != s2); |
||
99 | } |
||
100 | |||
57 | soliveira | 101 | private static class MyThread extends Thread { |
102 | |||
103 | private final Container c; |
||
104 | private final String key; |
||
105 | private String value = null; |
||
106 | |||
107 | public MyThread(Container c, String key) { |
||
108 | super(); |
||
109 | this.c = c; |
||
110 | this.key = key; |
||
111 | } |
||
112 | |||
113 | @Override |
||
114 | public void run() { |
||
115 | |||
116 | for(int i = 0; i < 50; i++) { |
||
117 | |||
118 | String v = c.get(key); |
||
119 | |||
120 | if (this.value != null) { |
||
121 | |||
122 | Assert.assertTrue(this.value == v); |
||
123 | |||
124 | } |
||
125 | |||
126 | this.value = v; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public String getValue() { return value; } |
||
131 | } |
||
132 | |||
133 | @Test |
||
70 | soliveira | 134 | public void testScopes() { |
135 | |||
136 | Container c = new MentaContainer(); |
||
137 | |||
138 | // if you don't provide a scope the Scope is NONE |
||
139 | // meaning a new instance is created on every |
||
95 | soliveira | 140 | // request for the bean |
70 | soliveira | 141 | |
142 | c.ioc("myString", String.class).addInitValue("saoj"); |
||
143 | |||
144 | String s1 = c.get("myString"); |
||
145 | String s2 = c.get("myString"); |
||
146 | |||
147 | Assert.assertTrue(s1 != s2); |
||
148 | |||
149 | // then you can use SINGLETON |
||
150 | // always get the same instance no matter what |
||
151 | |||
152 | c.ioc("myString", String.class, Scope.SINGLETON).addInitValue("saoj"); |
||
153 | |||
154 | s1 = c.get("myString"); |
||
155 | s2 = c.get("myString"); |
||
156 | |||
157 | Assert.assertTrue(s1 == s2); |
||
158 | |||
159 | // then you can use THREAD |
||
160 | // each thread will get a different instance |
||
161 | |||
162 | c.ioc("myString", String.class, Scope.THREAD).addInitValue("saoj"); |
||
163 | |||
164 | s1 = c.get("myString"); |
||
165 | s2 = c.get("myString"); |
||
166 | |||
167 | Assert.assertTrue(s1 == s2); // same thread |
||
168 | } |
||
169 | |||
170 | @Test |
||
57 | soliveira | 171 | public void testThreadLocal() throws Exception { |
172 | |||
173 | final Container c = new MentaContainer(); |
||
174 | |||
175 | c.ioc("myStr", String.class, Scope.THREAD).addInitValue("saoj"); |
||
176 | |||
177 | String s1 = c.get("myStr"); |
||
178 | |||
179 | MyThread t1 = new MyThread(c, "myStr"); |
||
180 | MyThread t2 = new MyThread(c, "myStr"); |
||
181 | |||
182 | t1.start(); |
||
183 | t2.start(); |
||
184 | |||
185 | t1.join(); |
||
186 | t2.join(); |
||
187 | |||
188 | String s2 = t1.getValue(); |
||
189 | String s3 = t2.getValue(); |
||
190 | |||
191 | Assert.assertTrue(s1 != s2); |
||
192 | Assert.assertTrue(s2 != s3); |
||
193 | } |
||
20 | soliveira | 194 | |
26 | soliveira | 195 | public static interface MyDAO { |
196 | |||
197 | public Object getSomething(); |
||
198 | |||
199 | } |
||
200 | |||
27 | soliveira | 201 | public static class SomeDependency { |
202 | |||
203 | private String name; |
||
204 | |||
205 | public SomeDependency() { } |
||
206 | |||
207 | public void setName(String name) { |
||
208 | this.name = name; |
||
209 | } |
||
210 | |||
211 | public String getName() { |
||
212 | return name; |
||
213 | } |
||
214 | } |
||
215 | |||
26 | soliveira | 216 | public static class Connection { |
217 | |||
218 | private final String name; |
||
219 | |||
27 | soliveira | 220 | private SomeDependency dep; |
221 | |||
26 | soliveira | 222 | public Connection(String name) { |
223 | this.name = name; |
||
224 | } |
||
225 | |||
84 | soliveira | 226 | @Override |
26 | soliveira | 227 | public String toString() { |
228 | return getClass().getName() + ": " + name; |
||
229 | } |
||
230 | |||
231 | public String getName() { return name; } |
||
27 | soliveira | 232 | |
233 | public void setMyDep(SomeDependency dep) { this.dep = dep; } |
||
234 | |||
235 | public SomeDependency getMyDep() { return dep; } |
||
26 | soliveira | 236 | } |
237 | |||
27 | soliveira | 238 | |
239 | |||
26 | soliveira | 240 | public static class JdbcMyDAO implements MyDAO { |
241 | |||
242 | private Connection conn; |
||
243 | |||
101 | soliveira | 244 | public void setConnection(Connection conn) { this.conn = conn; } |
26 | soliveira | 245 | |
101 | soliveira | 246 | public Connection getConnection() { return conn; } |
26 | soliveira | 247 | |
84 | soliveira | 248 | @Override |
26 | soliveira | 249 | public Object getSomething() { |
250 | |||
28 | soliveira | 251 | // use the connection to do something... |
26 | soliveira | 252 | |
28 | soliveira | 253 | Assert.assertNotNull(conn); // it cannot be null! |
254 | |||
255 | // also test if the connection also received the myDep dependency... |
||
256 | |||
257 | Assert.assertNotNull(conn.getMyDep()); |
||
258 | |||
259 | return conn.toString(); |
||
26 | soliveira | 260 | } |
261 | } |
||
262 | |||
28 | soliveira | 263 | private Container getConfiguredContainer() { |
26 | soliveira | 264 | |
265 | Container c = new MentaContainer(); |
||
266 | |||
267 | c.ioc("myDAO", JdbcMyDAO.class); |
||
268 | |||
91 | soliveira | 269 | c.ioc("aDependency", SomeDependency.class, Scope.SINGLETON).addPropertyValue("name", "A super dependency!"); |
27 | soliveira | 270 | |
26 | soliveira | 271 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
272 | |||
101 | soliveira | 273 | c.autowire("connection"); |
26 | soliveira | 274 | |
101 | soliveira | 275 | c.autowire("aDependency", "myDep"); |
27 | soliveira | 276 | |
28 | soliveira | 277 | return c; |
278 | } |
||
279 | |||
280 | @Test |
||
281 | public void testAutoWiring() { |
||
282 | |||
283 | Container c = getConfiguredContainer(); |
||
284 | |||
41 | soliveira | 285 | MyDAO myDAO = c.get("myDAO"); |
26 | soliveira | 286 | |
287 | // check implementation... |
||
288 | |||
289 | Assert.assertEquals(JdbcMyDAO.class, myDAO.getClass()); |
||
290 | |||
291 | // check dependency... |
||
292 | |||
101 | soliveira | 293 | Connection conn = ((JdbcMyDAO) myDAO).getConnection(); |
26 | soliveira | 294 | |
295 | Assert.assertNotNull(conn); |
||
296 | |||
297 | Assert.assertEquals("A super JDBC connection!", conn.getName()); |
||
298 | |||
27 | soliveira | 299 | // check dependency of dependency... |
300 | |||
301 | Assert.assertEquals(c.get("aDependency"), conn.getMyDep()); |
||
302 | |||
303 | Assert.assertTrue(c.get("aDependency") == conn.getMyDep()); // singleton! |
||
304 | |||
305 | // check DAO can do its job... |
||
306 | |||
26 | soliveira | 307 | Assert.assertNotNull(myDAO.getSomething()); |
308 | } |
||
28 | soliveira | 309 | |
115 | soliveira | 310 | public static class MyObject1 { |
311 | |||
312 | } |
||
313 | |||
314 | public static class MyObject2 { |
||
315 | |||
316 | } |
||
317 | |||
318 | public static class MyObject3 { |
||
319 | |||
320 | public MyObject3(MyObject2 o2, MyObject1 o1) { |
||
321 | |||
322 | } |
||
323 | } |
||
324 | |||
325 | private Container getConfiguredContainer2() { |
||
326 | |||
327 | Container c = new MentaContainer(); |
||
328 | |||
329 | c.ioc("myObj1", MyObject1.class); |
||
330 | c.ioc("myObj2", MyObject2.class); |
||
331 | c.ioc("myObj3", MyObject3.class); |
||
332 | |||
333 | c.autowire("myObj1"); |
||
334 | c.autowire("myObj2"); |
||
335 | |||
336 | return c; |
||
337 | } |
||
338 | |||
339 | @Test |
||
340 | public void testAutoWiringWithTwoConstructors() { |
||
341 | |||
342 | Container c = getConfiguredContainer2(); |
||
343 | |||
344 | MyObject3 obj3 = c.get("myObj3"); |
||
345 | |||
346 | Assert.assertNotNull(obj3); |
||
347 | } |
||
348 | |||
28 | soliveira | 349 | public static class SomeAction { |
350 | |||
351 | private MyDAO myDAO = null; |
||
352 | |||
353 | public void setMyDAO(MyDAO myDAO) { |
||
354 | |||
355 | this.myDAO = myDAO; |
||
356 | } |
||
357 | |||
358 | public MyDAO getMyDAO() { return myDAO; } |
||
359 | } |
||
360 | |||
64 | soliveira | 361 | @Test |
40 | soliveira | 362 | public void testPopulate() { |
28 | soliveira | 363 | |
364 | Container c = getConfiguredContainer(); |
||
365 | |||
366 | SomeAction a = new SomeAction(); |
||
367 | |||
103 | soliveira | 368 | c.inject(a); // great... properties of SomeAction were populated by container... |
28 | soliveira | 369 | |
370 | // let's check if myDAO was injected... |
||
371 | |||
372 | Assert.assertNotNull(a.getMyDAO()); |
||
373 | |||
374 | // also check if myDAO was corrected wired... |
||
375 | |||
101 | soliveira | 376 | Connection conn = ((JdbcMyDAO) a.getMyDAO()).getConnection(); |
28 | soliveira | 377 | |
378 | Assert.assertNotNull(conn); |
||
379 | |||
380 | // check if conn was also wired... |
||
381 | |||
382 | Assert.assertNotNull(conn.getMyDep()); |
||
64 | soliveira | 383 | } |
384 | |||
103 | soliveira | 385 | public static class SomeAction2 { |
386 | |||
387 | private final MyDAO myDAO; |
||
388 | |||
389 | public SomeAction2(MyDAO myDAO) { |
||
390 | this.myDAO = myDAO; |
||
391 | } |
||
392 | |||
393 | public MyDAO getMyDAO() { return myDAO; } |
||
394 | |||
395 | } |
||
396 | |||
397 | @Test |
||
398 | public void testConstructorDependency() { |
||
399 | |||
400 | Container c = new MentaContainer(); |
||
401 | |||
402 | c.ioc("myDAO", JdbcMyDAO.class); |
||
403 | |||
404 | c.ioc("aDependency", SomeDependency.class, Scope.SINGLETON).addPropertyValue("name", "A super dependency!"); |
||
405 | |||
406 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
||
407 | |||
408 | c.autowire("connection"); |
||
409 | |||
410 | c.autowire("aDependency", "myDep"); |
||
411 | |||
139 | soliveira | 412 | c.ioc("someAction2", SomeAction2.class).addConstructorDependency("myDAO"); |
103 | soliveira | 413 | |
414 | SomeAction2 action = c.get("someAction2"); |
||
415 | |||
416 | Assert.assertNotNull(action.getMyDAO()); |
||
417 | } |
||
418 | |||
419 | @Test |
||
420 | public void testConstruct() { |
||
421 | |||
422 | Container c = getConfiguredContainer(); |
||
423 | |||
424 | SomeAction2 a = c.construct(SomeAction2.class); |
||
425 | // let's check if myDAO was injected... |
||
426 | |||
427 | Assert.assertNotNull(a.getMyDAO()); |
||
428 | |||
429 | // also check if myDAO was corrected wired... |
||
430 | |||
431 | Connection conn = ((JdbcMyDAO) a.getMyDAO()).getConnection(); |
||
432 | |||
433 | Assert.assertNotNull(conn); |
||
434 | |||
435 | // check if conn was also wired... |
||
436 | |||
437 | Assert.assertNotNull(conn.getMyDep()); |
||
438 | } |
||
146 | soliveira | 439 | |
440 | private static class SomeObject { |
||
441 | |||
442 | private boolean destroyed = false; |
||
443 | private boolean created = false; |
||
444 | |||
445 | public void destroyed() { this.destroyed = true; } |
||
446 | |||
447 | public boolean isDestroyed() { return destroyed; } |
||
448 | |||
449 | public void created() { this.created = true; } |
||
450 | |||
451 | public boolean isCreated() { return created; } |
||
452 | } |
||
453 | |||
454 | private static class SomeFactory implements Factory, Interceptor<SomeObject> { |
||
455 | |||
456 | @Override |
||
457 | public <T> T getInstance() { |
||
458 | |||
459 | return (T) new SomeObject(); |
||
460 | } |
||
461 | |||
462 | @Override |
||
148 | soliveira | 463 | public Class<?> getType() { |
146 | soliveira | 464 | |
465 | return SomeObject.class; |
||
466 | } |
||
467 | |||
468 | @Override |
||
469 | public void onCleared(SomeObject obj) { |
||
470 | |||
471 | obj.destroyed(); |
||
472 | } |
||
473 | |||
474 | @Override |
||
475 | public void onCreated(SomeObject obj) { |
||
476 | |||
477 | obj.created(); |
||
478 | } |
||
479 | } |
||
480 | |||
481 | @Test |
||
482 | public void testInterceptor() { |
||
483 | |||
484 | Container c = new MentaContainer(); |
||
485 | |||
486 | c.ioc("o", new SomeFactory(), Scope.SINGLETON); |
||
487 | |||
488 | SomeObject o = c.get("o"); |
||
489 | |||
490 | Assert.assertTrue(o.isCreated()); |
||
491 | |||
492 | c.clear(Scope.SINGLETON); |
||
493 | |||
494 | Assert.assertEquals(true, o.isDestroyed()); |
||
495 | |||
496 | c.ioc("o", new SomeFactory(), Scope.THREAD); |
||
497 | |||
498 | o = c.get("o"); |
||
499 | |||
500 | Assert.assertTrue(o.isCreated()); |
||
501 | |||
502 | c.clear(Scope.SINGLETON); |
||
503 | |||
504 | Assert.assertEquals(false, o.isDestroyed()); |
||
505 | |||
506 | c.clear(Scope.THREAD); |
||
507 | |||
508 | Assert.assertEquals(true, o.isDestroyed()); |
||
509 | |||
510 | c.ioc("o", new SomeFactory(), Scope.NONE); |
||
511 | |||
512 | o = c.get("o"); |
||
513 | |||
514 | Assert.assertTrue(o.isCreated()); |
||
515 | |||
516 | o = c.clear("o"); |
||
517 | |||
518 | Assert.assertNull(o); |
||
519 | |||
520 | c.ioc("o", new SomeFactory(), Scope.THREAD); |
||
521 | |||
522 | o = c.clear("o"); |
||
523 | |||
524 | Assert.assertNull(o); |
||
525 | |||
526 | o = c.get("o"); |
||
527 | |||
528 | Assert.assertTrue(o.isCreated()); |
||
529 | |||
530 | o = c.clear("o"); |
||
531 | |||
532 | Assert.assertEquals(true, o.isDestroyed()); |
||
533 | } |
||
534 | |||
4 | soliveira | 535 | } |