Rev 40 | Rev 44 | 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 | |||
41 | soliveira | 19 | String s1 = c.get("myStr"); |
20 | soliveira | 20 | |
41 | soliveira | 21 | String s2 = c.get("myStr"); |
20 | soliveira | 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 | |||
41 | soliveira | 37 | String s1 = c.get("myStr"); |
20 | soliveira | 38 | |
41 | soliveira | 39 | String s2 = c.get("myStr"); |
20 | soliveira | 40 | |
41 | Assert.assertTrue(s1 != s2); |
||
42 | |||
43 | Assert.assertTrue(s1.equals(s2)); |
||
44 | |||
45 | c.ioc("anotherStr", String.class).addInitValue("hi"); |
||
46 | |||
41 | soliveira | 47 | String s3 = c.get("anotherStr"); |
20 | soliveira | 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 | |||
41 | soliveira | 63 | String s1 = c.get("myStr"); |
20 | soliveira | 64 | |
41 | soliveira | 65 | String s2 = c.get("myStr"); |
20 | soliveira | 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 | |||
28 | soliveira | 146 | // use the connection to do something... |
26 | soliveira | 147 | |
28 | soliveira | 148 | Assert.assertNotNull(conn); // it cannot be null! |
149 | |||
150 | // also test if the connection also received the myDep dependency... |
||
151 | |||
152 | Assert.assertNotNull(conn.getMyDep()); |
||
153 | |||
154 | return conn.toString(); |
||
26 | soliveira | 155 | } |
156 | } |
||
157 | |||
28 | soliveira | 158 | private Container getConfiguredContainer() { |
26 | soliveira | 159 | |
160 | Container c = new MentaContainer(); |
||
161 | |||
162 | c.ioc("myDAO", JdbcMyDAO.class); |
||
163 | |||
27 | soliveira | 164 | c.ioc("aDependency", SomeDependency.class, true /* singleton */).addProperty("name", "A super dependency!"); |
165 | |||
26 | soliveira | 166 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
167 | |||
168 | c.autowire("conn" /* the property name that will receive the dependency */, |
||
169 | Connection.class /* the type of the property that will receive the dependency */, |
||
170 | "connection" /* the source inside the container */); |
||
171 | |||
27 | soliveira | 172 | c.autowire("myDep" /* notice this is the setter of the Connection class */, |
173 | SomeDependency.class /* the type - it could be an interface for better decoupling */, |
||
174 | "aDependency" /* again this is the name of the bean/component inside the container */); |
||
175 | |||
28 | soliveira | 176 | return c; |
177 | } |
||
178 | |||
179 | @Test |
||
180 | public void testAutoWiring() { |
||
181 | |||
182 | Container c = getConfiguredContainer(); |
||
183 | |||
41 | soliveira | 184 | MyDAO myDAO = c.get("myDAO"); |
26 | soliveira | 185 | |
186 | // check implementation... |
||
187 | |||
188 | Assert.assertEquals(JdbcMyDAO.class, myDAO.getClass()); |
||
189 | |||
190 | // check dependency... |
||
191 | |||
192 | Connection conn = ((JdbcMyDAO) myDAO).getConn(); |
||
193 | |||
194 | Assert.assertNotNull(conn); |
||
195 | |||
196 | Assert.assertEquals("A super JDBC connection!", conn.getName()); |
||
197 | |||
27 | soliveira | 198 | // check dependency of dependency... |
199 | |||
200 | Assert.assertEquals(c.get("aDependency"), conn.getMyDep()); |
||
201 | |||
202 | Assert.assertTrue(c.get("aDependency") == conn.getMyDep()); // singleton! |
||
203 | |||
204 | // check DAO can do its job... |
||
205 | |||
26 | soliveira | 206 | Assert.assertNotNull(myDAO.getSomething()); |
207 | } |
||
28 | soliveira | 208 | |
209 | public static class SomeAction { |
||
210 | |||
211 | private MyDAO myDAO = null; |
||
212 | |||
213 | public void setMyDAO(MyDAO myDAO) { |
||
214 | |||
215 | this.myDAO = myDAO; |
||
216 | } |
||
217 | |||
218 | public MyDAO getMyDAO() { return myDAO; } |
||
219 | } |
||
220 | |||
40 | soliveira | 221 | public void testPopulate() { |
28 | soliveira | 222 | |
223 | Container c = getConfiguredContainer(); |
||
224 | |||
225 | SomeAction a = new SomeAction(); |
||
226 | |||
227 | c.populate(a); // great... properties of SomeAction were populated by container... |
||
228 | |||
229 | // let's check if myDAO was injected... |
||
230 | |||
231 | Assert.assertNotNull(a.getMyDAO()); |
||
232 | |||
233 | // also check if myDAO was corrected wired... |
||
234 | |||
235 | Connection conn = ((JdbcMyDAO) a.getMyDAO()).getConn(); |
||
236 | |||
237 | Assert.assertNotNull(conn); |
||
238 | |||
239 | // check if conn was also wired... |
||
240 | |||
241 | Assert.assertNotNull(conn.getMyDep()); |
||
242 | |||
243 | } |
||
4 | soliveira | 244 | } |