Rev 20 | Rev 27 | 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 | |||
98 | public static class Connection { |
||
99 | |||
100 | private final String name; |
||
101 | |||
102 | public Connection(String name) { |
||
103 | this.name = name; |
||
104 | } |
||
105 | |||
106 | public String toString() { |
||
107 | return getClass().getName() + ": " + name; |
||
108 | } |
||
109 | |||
110 | public String getName() { return name; } |
||
111 | } |
||
112 | |||
113 | public static class JdbcMyDAO implements MyDAO { |
||
114 | |||
115 | private Connection conn; |
||
116 | |||
117 | public void setConn(Connection conn) { this.conn = conn; } |
||
118 | |||
119 | public Connection getConn() { return conn; } |
||
120 | |||
121 | public Object getSomething() { |
||
122 | |||
123 | // use the connection to get something... |
||
124 | |||
125 | return conn.toString(); // it cannot be null! |
||
126 | } |
||
127 | } |
||
128 | |||
129 | @Test |
||
130 | public void testAutoWiring() { |
||
131 | |||
132 | Container c = new MentaContainer(); |
||
133 | |||
134 | c.ioc("myDAO", JdbcMyDAO.class); |
||
135 | |||
136 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
||
137 | |||
138 | c.autowire("conn" /* the property name that will receive the dependency */, |
||
139 | Connection.class /* the type of the property that will receive the dependency */, |
||
140 | "connection" /* the source inside the container */); |
||
141 | |||
142 | MyDAO myDAO = (MyDAO) c.get("myDAO"); |
||
143 | |||
144 | // check implementation... |
||
145 | |||
146 | Assert.assertEquals(JdbcMyDAO.class, myDAO.getClass()); |
||
147 | |||
148 | // check dependency... |
||
149 | |||
150 | Connection conn = ((JdbcMyDAO) myDAO).getConn(); |
||
151 | |||
152 | Assert.assertNotNull(conn); |
||
153 | |||
154 | Assert.assertEquals("A super JDBC connection!", conn.getName()); |
||
155 | |||
156 | Assert.assertNotNull(myDAO.getSomething()); |
||
157 | } |
||
4 | soliveira | 158 | } |