Rev 26 | Rev 28 | 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; |
||
7 | |||
8 | public class MentaContainerTest { |
||
20 | soliveira | 9 | |
10 | @Test |
||
11 | public void testSimpleGet() { |
||
12 | |||
13 | Container c = new MentaContainer(); |
||
14 | |||
15 | c.ioc("myStr", String.class); |
||
16 | |||
17 | Assert.assertEquals("", c.get("myStr")); |
||
18 | |||
19 | String s1 = (String) c.get("myStr"); |
||
20 | |||
21 | String s2 = (String) c.get("myStr"); |
||
22 | |||
23 | Assert.assertTrue(s1 != s2); |
||
24 | |||
25 | Assert.assertTrue(s1.equals(s2)); |
||
26 | } |
||
27 | |||
28 | @Test |
||
29 | public void testConstructorInit() { |
||
30 | |||
31 | Container c = new MentaContainer(); |
||
32 | |||
33 | c.ioc("myStr", String.class).addInitValue("hello"); |
||
34 | |||
35 | Assert.assertEquals("hello", c.get("myStr")); |
||
36 | |||
37 | String s1 = (String) c.get("myStr"); |
||
38 | |||
39 | String s2 = (String) c.get("myStr"); |
||
40 | |||
41 | Assert.assertTrue(s1 != s2); |
||
42 | |||
43 | Assert.assertTrue(s1.equals(s2)); |
||
44 | |||
45 | c.ioc("anotherStr", String.class).addInitValue("hi"); |
||
46 | |||
47 | String s3 = (String) c.get("anotherStr"); |
||
48 | |||
49 | Assert.assertTrue(s1 != s3); |
||
50 | |||
51 | Assert.assertFalse(s1.equals(s3)); |
||
52 | } |
||
53 | |||
54 | @Test |
||
55 | public void testSingleton() { |
||
56 | |||
57 | Container c = new MentaContainer(); |
||
58 | |||
59 | c.ioc("myStr", String.class, true).addInitValue("hello"); |
||
60 | |||
61 | Assert.assertEquals("hello", c.get("myStr")); |
||
62 | |||
63 | String s1 = (String) c.get("myStr"); |
||
64 | |||
65 | String s2 = (String) c.get("myStr"); |
||
66 | |||
67 | Assert.assertTrue(s1 == s2); |
||
68 | |||
69 | Assert.assertTrue(s1.equals(s2)); |
||
70 | } |
||
71 | |||
72 | @Test(expected = RuntimeException.class) |
||
73 | public void testAddingComponentTwice() { |
||
74 | |||
75 | Container c = new MentaContainer(); |
||
76 | |||
77 | c.ioc("myStr", String.class); |
||
78 | |||
79 | c.ioc("myStr", String.class); |
||
80 | } |
||
81 | |||
82 | @Test(expected = RuntimeException.class) |
||
83 | public void testSettingWiringTwice() { |
||
84 | |||
85 | Container c = new MentaContainer(); |
||
86 | |||
87 | c.autowire("myThread", Runnable.class); |
||
88 | |||
89 | c.autowire("myThread", Runnable.class); |
||
90 | } |
||
26 | soliveira | 91 | |
92 | public static interface MyDAO { |
||
93 | |||
94 | public Object getSomething(); |
||
95 | |||
96 | } |
||
97 | |||
27 | soliveira | 98 | public static class SomeDependency { |
99 | |||
100 | private String name; |
||
101 | |||
102 | public SomeDependency() { } |
||
103 | |||
104 | public void setName(String name) { |
||
105 | this.name = name; |
||
106 | } |
||
107 | |||
108 | public String getName() { |
||
109 | return name; |
||
110 | } |
||
111 | } |
||
112 | |||
26 | soliveira | 113 | public static class Connection { |
114 | |||
115 | private final String name; |
||
116 | |||
27 | soliveira | 117 | private SomeDependency dep; |
118 | |||
26 | soliveira | 119 | public Connection(String name) { |
120 | this.name = name; |
||
121 | } |
||
122 | |||
123 | public String toString() { |
||
124 | return getClass().getName() + ": " + name; |
||
125 | } |
||
126 | |||
127 | public String getName() { return name; } |
||
27 | soliveira | 128 | |
129 | public void setMyDep(SomeDependency dep) { this.dep = dep; } |
||
130 | |||
131 | public SomeDependency getMyDep() { return dep; } |
||
26 | soliveira | 132 | } |
133 | |||
27 | soliveira | 134 | |
135 | |||
26 | soliveira | 136 | public static class JdbcMyDAO implements MyDAO { |
137 | |||
138 | private Connection conn; |
||
139 | |||
140 | public void setConn(Connection conn) { this.conn = conn; } |
||
141 | |||
142 | public Connection getConn() { return conn; } |
||
143 | |||
144 | public Object getSomething() { |
||
145 | |||
146 | // use the connection to get something... |
||
147 | |||
148 | return conn.toString(); // it cannot be null! |
||
149 | } |
||
150 | } |
||
151 | |||
152 | @Test |
||
153 | public void testAutoWiring() { |
||
154 | |||
155 | Container c = new MentaContainer(); |
||
156 | |||
157 | c.ioc("myDAO", JdbcMyDAO.class); |
||
158 | |||
27 | soliveira | 159 | c.ioc("aDependency", SomeDependency.class, true /* singleton */).addProperty("name", "A super dependency!"); |
160 | |||
26 | soliveira | 161 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
162 | |||
163 | c.autowire("conn" /* the property name that will receive the dependency */, |
||
164 | Connection.class /* the type of the property that will receive the dependency */, |
||
165 | "connection" /* the source inside the container */); |
||
166 | |||
27 | soliveira | 167 | c.autowire("myDep" /* notice this is the setter of the Connection class */, |
168 | SomeDependency.class /* the type - it could be an interface for better decoupling */, |
||
169 | "aDependency" /* again this is the name of the bean/component inside the container */); |
||
170 | |||
26 | soliveira | 171 | MyDAO myDAO = (MyDAO) c.get("myDAO"); |
172 | |||
173 | // check implementation... |
||
174 | |||
175 | Assert.assertEquals(JdbcMyDAO.class, myDAO.getClass()); |
||
176 | |||
177 | // check dependency... |
||
178 | |||
179 | Connection conn = ((JdbcMyDAO) myDAO).getConn(); |
||
180 | |||
181 | Assert.assertNotNull(conn); |
||
182 | |||
183 | Assert.assertEquals("A super JDBC connection!", conn.getName()); |
||
184 | |||
27 | soliveira | 185 | // check dependency of dependency... |
186 | |||
187 | Assert.assertEquals(c.get("aDependency"), conn.getMyDep()); |
||
188 | |||
189 | Assert.assertTrue(c.get("aDependency") == conn.getMyDep()); // singleton! |
||
190 | |||
191 | // check DAO can do its job... |
||
192 | |||
26 | soliveira | 193 | Assert.assertNotNull(myDAO.getSomething()); |
194 | } |
||
4 | soliveira | 195 | } |