View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.onami.factoryannotation;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import org.apache.onami.factoryannotation.annotations.Message;
24  import org.apache.onami.factoryannotation.annotations.User;
25  import org.apache.onami.factoryannotation.impl.MessageEntity;
26  import org.apache.onami.factoryannotation.impl.MessageValueInjector;
27  import org.apache.onami.factoryannotation.impl.UserEntity;
28  import org.apache.onami.factoryannotation.impl.UserValueInjector;
29  import org.junit.Test;
30  
31  import com.google.inject.AbstractModule;
32  import com.google.inject.Guice;
33  import com.google.inject.Inject;
34  import com.google.inject.Injector;
35  import com.google.inject.ProvisionException;
36  
37  public class FactoryAnnotationProviderTestCase
38  {
39  
40      @Test
41      public void testInjectAndUserAnnotationInjection()
42      {
43          final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
44          {
45              @Override
46              protected void configure()
47              {
48                  bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
49              }
50          } );
51  
52          final InjectAnnotationUserInjectable injectable = new InjectAnnotationUserInjectable();
53          injector.injectMembers( injectable );
54  
55          assertEquals( "User1", injectable.user1.getName() );
56          assertEquals( 1, injectable.user1.getId() );
57          assertEquals( "User2", injectable.user2.getName() );
58          assertEquals( 2, injectable.user2.getId() );
59      }
60  
61      @Test
62      public void testUserAnnotationInjection1()
63      {
64          final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
65          {
66              @Override
67              protected void configure()
68              {
69                  bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
70              }
71          } );
72  
73          final UserInjectable injectable = new UserInjectable();
74          injector.injectMembers( injectable );
75  
76          assertEquals( "User1", injectable.user1.getName() );
77          assertEquals( 1, injectable.user1.getId() );
78          assertEquals( "User2", injectable.user2.getName() );
79          assertEquals( 2, injectable.user2.getId() );
80      }
81  
82      @Test
83      public void testUserAnnotationInjection2()
84      {
85          final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
86          {
87              @Override
88              protected void configure()
89              {
90                  bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
91              }
92          }, new AbstractModule()
93          {
94              @Override
95              protected void configure()
96              {
97                  bind( UserInjectable.class );
98              }
99          } );
100 
101         final UserInjectable injectable = injector.getInstance( UserInjectable.class );
102 
103         assertEquals( "User1", injectable.user1.getName() );
104         assertEquals( 1, injectable.user1.getId() );
105         assertEquals( "User2", injectable.user2.getName() );
106         assertEquals( 2, injectable.user2.getId() );
107     }
108 
109     @Test
110     public void testMessageAnnotationInjection1()
111     {
112         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
113         {
114             @Override
115             protected void configure()
116             {
117                 bindType( MessageEntity.class ).annotatedWith( Message.class ).toAnnotationFactory( new MessageValueInjector() );
118             }
119         } );
120 
121         final MessageInjectable injectable = new MessageInjectable();
122         injector.injectMembers( injectable );
123 
124         assertEquals( "MessageTest100", injectable.message100.getMessage() );
125         assertEquals( 100, injectable.message100.getId() );
126         assertEquals( "MessageTest200", injectable.message200.getMessage() );
127         assertEquals( 200, injectable.message200.getId() );
128     }
129 
130     @Test
131     public void testMessageAnnotationInjection2()
132     {
133         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
134         {
135             @Override
136             protected void configure()
137             {
138                 bindType( MessageEntity.class ).annotatedWith( Message.class ).toAnnotationFactory( new MessageValueInjector() );
139             }
140         }, new AbstractModule()
141         {
142             @Override
143             protected void configure()
144             {
145                 bind( MessageInjectable.class );
146             }
147         } );
148 
149         final MessageInjectable injectable = injector.getInstance( MessageInjectable.class );
150 
151         assertEquals( "MessageTest100", injectable.message100.getMessage() );
152         assertEquals( 100, injectable.message100.getId() );
153         assertEquals( "MessageTest200", injectable.message200.getMessage() );
154         assertEquals( 200, injectable.message200.getId() );
155     }
156 
157     @Test
158     public void testCombinedAnnotationInjection()
159     {
160         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
161         {
162             @Override
163             protected void configure()
164             {
165                 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
166 
167                 bindType( MessageEntity.class ).annotatedWith( Message.class ).toAnnotationFactory( new MessageValueInjector() );
168             }
169         } );
170 
171         final MessageInjectable injectable = new MessageInjectable();
172         injector.injectMembers( injectable );
173 
174         final UserInjectable injectable2 = new UserInjectable();
175         injector.injectMembers( injectable2 );
176 
177         assertEquals( "MessageTest100", injectable.message100.getMessage() );
178         assertEquals( 100, injectable.message100.getId() );
179         assertEquals( "MessageTest200", injectable.message200.getMessage() );
180         assertEquals( 200, injectable.message200.getId() );
181 
182         assertEquals( "User1", injectable2.user1.getName() );
183         assertEquals( 1, injectable2.user1.getId() );
184         assertEquals( "User2", injectable2.user2.getName() );
185         assertEquals( 2, injectable2.user2.getId() );
186     }
187 
188     @Test( expected = ProvisionException.class )
189     public void testFailedInjection1()
190     {
191         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
192         {
193             @Override
194             protected void configure()
195             {
196                 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
197             }
198         } );
199 
200         injector.injectMembers( new UserInjectableFailing() );
201     }
202 
203     @Test( expected = ProvisionException.class )
204     public void testFailedInjection2()
205     {
206         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
207         {
208             @Override
209             protected void configure()
210             {
211                 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
212             }
213         }, new AbstractModule()
214         {
215             @Override
216             protected void configure()
217             {
218                 bind( UserInjectableFailing.class );
219             }
220         } );
221 
222         injector.getInstance( UserInjectableFailing.class );
223     }
224 
225     @Test
226     public void testConstructorInjection1()
227     {
228         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
229         {
230             @Override
231             protected void configure()
232             {
233                 bindType( MessageEntity.class ).annotatedWith( Message.class ).toAnnotationFactory( new MessageValueInjector() );
234             }
235         }, new AbstractModule()
236         {
237             @Override
238             protected void configure()
239             {
240                 bind( ConstructorInjectable1.class );
241             }
242         } );
243 
244         final ConstructorInjectable1 injectable = injector.getInstance( ConstructorInjectable1.class );
245 
246         assertEquals( "MessageTest100", injectable.message1.getMessage() );
247         assertEquals( 100, injectable.message1.getId() );
248     }
249 
250     @Test
251     public void testConstructorInjection2()
252     {
253         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
254         {
255             @Override
256             protected void configure()
257             {
258                 bindType( MessageEntity.class ).annotatedWith( Message.class ).toAnnotationFactory( new MessageValueInjector() );
259             }
260         }, new AbstractModule()
261         {
262             @Override
263             protected void configure()
264             {
265                 bind( ConstructorInjectable2.class );
266             }
267         } );
268 
269         final ConstructorInjectable2 injectable = injector.getInstance( ConstructorInjectable2.class );
270 
271         assertEquals( "MessageTest100", injectable.message1.getMessage() );
272         assertEquals( 100, injectable.message1.getId() );
273         assertEquals( "MessageTest200", injectable.message2.getMessage() );
274         assertEquals( 200, injectable.message2.getId() );
275     }
276 
277     @Test
278     public void testConstructorInjection3()
279     {
280         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
281         {
282             @Override
283             protected void configure()
284             {
285                 bindType( MessageEntity.class ).annotatedWith( Message.class ).toAnnotationFactory( new MessageValueInjector() );
286 
287                 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
288             }
289         }, new AbstractModule()
290         {
291             @Override
292             protected void configure()
293             {
294                 bind( ConstructorInjectable3.class );
295             }
296         } );
297 
298         final ConstructorInjectable3 injectable = injector.getInstance( ConstructorInjectable3.class );
299 
300         assertEquals( "MessageTest100", injectable.message1.getMessage() );
301         assertEquals( 100, injectable.message1.getId() );
302         assertEquals( "MessageTest200", injectable.message2.getMessage() );
303         assertEquals( 200, injectable.message2.getId() );
304         assertEquals( "User1", injectable.user1.getName() );
305         assertEquals( 1, injectable.user1.getId() );
306     }
307 
308     @Test
309     public void testConstructorInjection4()
310     {
311         final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
312         {
313             @Override
314             protected void configure()
315             {
316                 bindType( MessageEntity.class ).annotatedWith( Message.class ).toAnnotationFactory( new MessageValueInjector() );
317 
318                 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
319             }
320         }, new AbstractModule()
321         {
322             @Override
323             protected void configure()
324             {
325                 bind( ConstructorInjectable4.class );
326             }
327         } );
328 
329         final ConstructorInjectable4 injectable = injector.getInstance( ConstructorInjectable4.class );
330 
331         assertEquals( "MessageTest100", injectable.message1.getMessage() );
332         assertEquals( 100, injectable.message1.getId() );
333         assertEquals( "MessageTest200", injectable.message2.getMessage() );
334         assertEquals( 200, injectable.message2.getId() );
335         assertEquals( "User1", injectable.user1.getName() );
336         assertEquals( 1, injectable.user1.getId() );
337         assertEquals( "User2", injectable.user2.getName() );
338         assertEquals( 2, injectable.user2.getId() );
339     }
340 
341     public static class UserInjectable
342     {
343         @User( byId = 1 )
344         private UserEntity user1;
345 
346         @User( byId = 2 )
347         private UserEntity user2;
348     }
349 
350     public static class InjectAnnotationUserInjectable
351     {
352         @Inject
353         @User( byId = 1 )
354         private UserEntity user1;
355 
356         @Inject
357         @User( byId = 2 )
358         private UserEntity user2;
359     }
360 
361     public static class UserInjectableFailing
362     {
363         @User( byId = 3 )
364         private UserEntity user1;
365     }
366 
367     public static class MessageInjectable
368     {
369         @Message( byId = 100 )
370         private MessageEntity message100;
371 
372         @Message( byId = 200 )
373         private MessageEntity message200;
374     }
375 
376     public static class ConstructorInjectable1
377     {
378         private final MessageEntity message1;
379 
380         @Inject
381         public ConstructorInjectable1( @Message( byId = 100 )
382         final MessageEntity message1 )
383         {
384 
385             this.message1 = message1;
386         }
387     }
388 
389     public static class ConstructorInjectable2
390     {
391         private final MessageEntity message1;
392 
393         private final MessageEntity message2;
394 
395         @Inject
396         public ConstructorInjectable2( @Message( byId = 100 )
397         final MessageEntity message1, @Message( byId = 200 )
398         final MessageEntity message2 )
399         {
400 
401             this.message1 = message1;
402             this.message2 = message2;
403         }
404     }
405 
406     public static class ConstructorInjectable3
407     {
408         private final MessageEntity message1;
409 
410         private final MessageEntity message2;
411 
412         private final UserEntity user1;
413 
414         @Inject
415         public ConstructorInjectable3( @Message( byId = 100 )
416         final MessageEntity message1, @Message( byId = 200 )
417         final MessageEntity message2, @User( byId = 1 )
418         final UserEntity user1 )
419         {
420 
421             this.message1 = message1;
422             this.message2 = message2;
423             this.user1 = user1;
424         }
425     }
426 
427     public static class ConstructorInjectable4
428     {
429         private final MessageEntity message1;
430 
431         private final MessageEntity message2;
432 
433         private final UserEntity user1;
434 
435         private final UserEntity user2;
436 
437         @Inject
438         public ConstructorInjectable4( @Message( byId = 100 )
439         final MessageEntity message1, @Message( byId = 200 )
440         final MessageEntity message2, @User( byId = 1 )
441         final UserEntity user1, @User( byId = 2 )
442         final UserEntity user2 )
443         {
444 
445             this.message1 = message1;
446             this.message2 = message2;
447             this.user1 = user1;
448             this.user2 = user2;
449         }
450     }
451 
452 }