MentaContainer

Rev

Rev 38 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
32 soliveira 1
package org.mentacontainer.impl;
2
 
3
import junit.framework.Assert;
4
 
5
import org.junit.Test;
6
import org.mentacontainer.Component;
7
import org.mentacontainer.Container;
8
 
9
public class GenericComponentTest {
10
 
11
        public static class SomeFactory {
12
 
13
                public String giveMeSomething() {
14
 
15
                        return String.valueOf(System.nanoTime());
16
                }
17
        }
18
 
19
        @Test
20
        public void testGenericComponent() throws Exception {
21
 
38 soliveira 22
                // in real life this will be a SessionFactory, a ConnectionPool or any factory...
32 soliveira 23
                SomeFactory factory = new SomeFactory();
24
 
25
                Container c = new MentaContainer();
26
 
38 soliveira 27
                // giveMeSomething => method that will be called to return object
39 soliveira 28
                Component generic = new GenericComponent(factory, "giveMeSomething");
32 soliveira 29
 
39 soliveira 30
                c.ioc("myFactory", generic);
32 soliveira 31
 
32
                String s1 = (String) c.get("myFactory");
33
 
38 soliveira 34
                Thread.sleep(5); // so strings are different... my cpu is fast! :-)
32 soliveira 35
 
36
                String s2 = (String) c.get("myFactory");
37
 
38
                Assert.assertNotNull(s1);
39
                Assert.assertNotNull(s2);
40
                Assert.assertTrue(s1 != s2);
41
                Assert.assertTrue(!s1.equals(s2));
42
        }
43
}