1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.onami.factoryannotation;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import org.apache.onami.factoryannotation.annotations.User;
25 import org.apache.onami.factoryannotation.impl.UserEntity;
26 import org.apache.onami.factoryannotation.impl.UserValueInjector;
27 import org.junit.Test;
28
29 import com.google.inject.AbstractModule;
30 import com.google.inject.Guice;
31 import com.google.inject.Inject;
32 import com.google.inject.Injector;
33
34 public class ListenerMultiInjectionTestCase
35 {
36
37 @Test
38 public void testInjectionListenerValueProvider1()
39 {
40 final Listener listener = new Listener();
41
42 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
43 {
44 @Override
45 protected void configure()
46 {
47 bindType( UserEntity.class ).annotatedWith( User.class ).applyListener( listener ).toAnnotationFactory( new UserValueInjector() );
48 }
49 } );
50
51 final UserInjectable1 injectable = new UserInjectable1();
52 injector.injectMembers( injectable );
53
54 assertEquals( "User1", injectable.user1.getName() );
55 assertEquals( 1, injectable.user1.getId() );
56 assertEquals( "User2", injectable.user2.getName() );
57 assertEquals( 2, injectable.user2.getId() );
58
59 assertTrue( listener.executed[0] );
60 assertTrue( listener.executed[1] );
61 assertTrue( listener.executed[2] );
62 assertTrue( listener.executed[3] );
63
64 assertEquals( "before0", listener.orderOfExecution[0] );
65 assertEquals( "after0", listener.orderOfExecution[1] );
66 assertEquals( "before1", listener.orderOfExecution[2] );
67 assertEquals( "after1", listener.orderOfExecution[3] );
68 }
69
70 @Test
71 public void testInjectionListenerValueProvider2()
72 {
73 final Listener listener = new Listener();
74
75 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
76 {
77 @Override
78 protected void configure()
79 {
80 bindType( UserEntity.class ).annotatedWith( User.class ).applyListener( listener ).toAnnotationFactory( new UserValueInjector() );
81 }
82 }, new AbstractModule()
83 {
84 @Override
85 protected void configure()
86 {
87 bind( UserInjectable2.class );
88 }
89 } );
90
91 final UserInjectable2 injectable = injector.getInstance( UserInjectable2.class );
92
93 assertEquals( "User1", injectable.user1.getName() );
94 assertEquals( 1, injectable.user1.getId() );
95 assertEquals( "User2", injectable.user2.getName() );
96 assertEquals( 2, injectable.user2.getId() );
97
98 assertTrue( listener.executed[0] );
99 assertTrue( listener.executed[1] );
100 assertTrue( listener.executed[2] );
101 assertTrue( listener.executed[3] );
102
103 assertEquals( "before0", listener.orderOfExecution[0] );
104 assertEquals( "after0", listener.orderOfExecution[1] );
105 assertEquals( "before1", listener.orderOfExecution[2] );
106 assertEquals( "after1", listener.orderOfExecution[3] );
107 }
108
109 @Test
110 public void testInjectionListenerValueProvider3()
111 {
112 final Listener listener = new Listener();
113
114 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
115 {
116 @Override
117 protected void configure()
118 {
119 bindType( UserEntity.class ).annotatedWith( User.class ).applyListener( listener ).toAnnotationFactory( new UserValueInjector() );
120 }
121 }, new AbstractModule()
122 {
123 @Override
124 protected void configure()
125 {
126 bind( UserInjectable3.class );
127 }
128 } );
129
130 final UserInjectable3 injectable = injector.getInstance( UserInjectable3.class );
131
132 assertEquals( "User1", injectable.user1.getName() );
133 assertEquals( 1, injectable.user1.getId() );
134 assertEquals( "User2", injectable.user2.getName() );
135 assertEquals( 2, injectable.user2.getId() );
136
137 assertTrue( listener.executed[0] );
138 assertTrue( listener.executed[1] );
139 assertTrue( listener.executed[2] );
140 assertTrue( listener.executed[3] );
141
142 assertEquals( "before1", listener.orderOfExecution[0] );
143 assertEquals( "after2", listener.orderOfExecution[1] );
144 assertEquals( "before3", listener.orderOfExecution[2] );
145 assertEquals( "after4", listener.orderOfExecution[3] );
146 }
147
148 @Test
149 public void testInjectionListenerValueProvider4()
150 {
151 final Listener listener = new Listener();
152
153 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
154 {
155 @Override
156 protected void configure()
157 {
158 bindType( UserEntity.class ).annotatedWith( User.class ).applyListener( listener ).toAnnotationFactory( new UserValueInjector() );
159 }
160 } );
161
162 final UserInjectable4 injectable = new UserInjectable4();
163 injector.injectMembers( injectable );
164
165 assertEquals( "User1", injectable.user1.getName() );
166 assertEquals( 1, injectable.user1.getId() );
167 assertEquals( "User2", injectable.user2.getName() );
168 assertEquals( 2, injectable.user2.getId() );
169
170 assertTrue( listener.executed[0] );
171 assertTrue( listener.executed[1] );
172 assertTrue( listener.executed[2] );
173 assertTrue( listener.executed[3] );
174
175 assertEquals( "before1", listener.orderOfExecution[0] );
176 assertEquals( "after2", listener.orderOfExecution[1] );
177 assertEquals( "before3", listener.orderOfExecution[2] );
178 assertEquals( "after4", listener.orderOfExecution[3] );
179 }
180
181 public static class UserInjectable1
182 {
183 private UserEntity user1;
184
185 private UserEntity user2;
186
187 @Inject
188 public void user( @User( byId = 1 )
189 final UserEntity user1, @User( byId = 2 )
190 final UserEntity user2 )
191 {
192
193 this.user1 = user1;
194 this.user2 = user2;
195 }
196 }
197
198 public static class UserInjectable2
199 {
200 private final UserEntity user1;
201
202 private final UserEntity user2;
203
204 @Inject
205 public UserInjectable2( @User( byId = 1 )
206 final UserEntity user1, @User( byId = 2 )
207 final UserEntity user2 )
208 {
209
210 this.user1 = user1;
211 this.user2 = user2;
212 }
213 }
214
215 public static class UserInjectable3
216 {
217 @Inject
218 @User( byId = 1 )
219 private UserEntity user1;
220
221 @Inject
222 @User( byId = 2 )
223 private UserEntity user2;
224 }
225
226 public static class UserInjectable4
227 {
228 @User( byId = 1 )
229 private UserEntity user1;
230
231 @User( byId = 2 )
232 private UserEntity user2;
233 }
234
235 public static class Listener
236 implements FactoryAnnotationProvisionListener<UserEntity, User>
237 {
238
239 private final boolean[] executed = new boolean[4];
240
241 private final String[] orderOfExecution = new String[4];
242
243 private int count = 0;
244
245 public void beforeInjection( final FactoryAnnotationProvision<UserEntity, User> identityProvision,
246 final User provisionAnnotation )
247 {
248
249 final int position = identityProvision.getParameterPosition();
250
251 executed[count++] = true;
252
253 for ( int i = 0; i < 4; i++ )
254 {
255 if ( orderOfExecution[i] == null )
256 {
257 if ( position == -1 )
258 {
259 orderOfExecution[i] = "before" + count;
260
261 }
262 else
263 {
264 orderOfExecution[i] = "before" + position;
265 }
266 break;
267 }
268 }
269 }
270
271 public void afterInjection( final FactoryAnnotationProvision<UserEntity, User> identityProvision,
272 final User provisionAnnotation, final UserEntity value )
273 {
274
275 final int position = identityProvision.getParameterPosition();
276
277 executed[count++] = true;
278
279 for ( int i = 0; i < 4; i++ )
280 {
281 if ( orderOfExecution[i] == null )
282 {
283 if ( position == -1 )
284 {
285 orderOfExecution[i] = "after" + count;
286
287 }
288 else
289 {
290 orderOfExecution[i] = "after" + position;
291 }
292 break;
293 }
294 }
295 }
296 }
297
298 }