1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht.util;
21
22 import org.grouplens.grapht.InjectionException;
23 import org.grouplens.grapht.Injector;
24 import org.grouplens.grapht.InjectorBuilder;
25 import org.junit.Ignore;
26 import org.junit.Test;
27
28 import javax.annotation.PostConstruct;
29 import javax.annotation.PreDestroy;
30 import javax.inject.Inject;
31
32 import static org.hamcrest.Matchers.*;
33 import static org.junit.Assert.*;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class ComponentLifecycleTest {
55
56 @Test
57 public void testClosesAutoCloseable() throws Exception {
58 InjectorBuilder bld = InjectorBuilder.create();
59 Flag closed
60 = new Flag();
61 bld.bind(Flag.class).to(closed);
62 Injector injector = bld.build();
63 try {
64 CloseableComponent comp = injector.getInstance(CloseableComponent.class);
65 assertThat(comp, notNullValue());
66 } finally {
67 injector.close();
68 }
69
70 assertTrue("close was called", closed.isSet());
71 }
72
73
74
75
76 @Test
77 public void testDoesNotCloseAutoCloseableInstance() throws Exception {
78 InjectorBuilder bld = InjectorBuilder.create();
79 Flag closed = new Flag();
80 bld.bind(Flag.class).to(closed);
81 bld.bind(CloseableComponent.class).to(new CloseableComponent(closed));
82 Injector injector = bld.build();
83 try {
84 CloseableComponent comp = injector.getInstance(CloseableComponent.class);
85 assertThat(comp, notNullValue());
86 } finally {
87 injector.close();
88 }
89
90 assertFalse("close was called", closed.isSet());
91 }
92
93
94
95
96 @Test
97 public void testCallsPreDestroy() throws Exception {
98 InjectorBuilder bld = InjectorBuilder.create();
99 Flag closed = new Flag();
100 bld.bind(Flag.class).to(closed);
101 Injector injector = bld.build();
102 try {
103 LifecycleShutdownComponent comp = injector.getInstance(LifecycleShutdownComponent.class);
104 assertThat(comp, notNullValue());
105 } finally {
106 injector.close();
107 }
108
109 assertTrue("close was called", closed.isSet());
110 }
111
112
113
114
115 @Test
116 public void testDoesNotCallPreDestroyOnInstance() throws Exception {
117 InjectorBuilder bld = InjectorBuilder.create();
118 Flag closed = new Flag();
119 bld.bind(LifecycleShutdownComponent.class).to(new LifecycleShutdownComponent(closed));
120 Injector injector = bld.build();
121 try {
122 LifecycleShutdownComponent comp = injector.getInstance(LifecycleShutdownComponent.class);
123 assertThat(comp, notNullValue());
124 } finally {
125 injector.close();
126 }
127
128 assertFalse("close was called", closed.isSet());
129 }
130
131
132
133
134 @Test
135 public void testCallsPostConstruct() throws Exception {
136 InjectorBuilder bld = InjectorBuilder.create();
137 Flag setup = new Flag();
138 bld.bind(Flag.class).to(setup);
139 Injector injector = bld.build();
140 try {
141 PostConstructComponent comp = injector.getInstance(PostConstructComponent.class);
142 assertThat(comp, notNullValue());
143 assertTrue("setup was called", setup.isSet());
144 } finally {
145 injector.close();
146 }
147
148 assertTrue("close was called", setup.isSet());
149 }
150
151
152
153
154 public static class Flag {
155 private boolean value = false;
156
157 public void set() {
158 value = true;
159 }
160
161 public boolean isSet() {
162 return value;
163 }
164 }
165
166
167
168
169 public static class CloseableComponent implements AutoCloseable {
170 private final Flag flag;
171
172 @Inject
173 public CloseableComponent(Flag f) {
174 flag = f;
175 }
176
177 @Override
178 public void close() throws Exception {
179 flag.set();
180 }
181 }
182
183
184
185
186 public static class LifecycleShutdownComponent {
187 private final Flag flag;
188
189 @Inject
190 public LifecycleShutdownComponent(Flag f) {
191 flag = f;
192 }
193
194 @PreDestroy
195 public void shutdown() {
196 flag.set();
197 }
198 }
199
200
201
202
203 public static class PostConstructComponent {
204 private Flag setup;
205
206
207 @Inject
208 public void setFlag(Flag f) {
209 setup = f;
210 }
211
212 @PostConstruct
213 public void setup() {
214 assertThat(setup, notNullValue());
215 setup.set();
216 }
217 }
218 }