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
23 import org.apache.onami.factoryannotation.annotations.User;
24 import org.apache.onami.factoryannotation.impl.InjectedUserValueInjector;
25 import org.apache.onami.factoryannotation.impl.UserEntity;
26 import org.junit.Test;
27
28 import com.google.inject.AbstractModule;
29 import com.google.inject.Guice;
30 import com.google.inject.Injector;
31 import com.google.inject.name.Names;
32
33 public class InjectedValueProviderTestCase
34 {
35
36 @Test
37 public void testInjectedValueProvider()
38 {
39 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
40 {
41 @Override
42 protected void configure()
43 {
44 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new InjectedUserValueInjector() );
45 }
46 }, new AbstractModule()
47 {
48 @Override
49 protected void configure()
50 {
51 bind( UserEntity.class ).annotatedWith( Names.named( "test1" ) ).toInstance( UserEntity.build( "User1",
52 1 ) );
53
54 bind( UserEntity.class ).annotatedWith( Names.named( "test2" ) ).toInstance( UserEntity.build( "User2",
55 2 ) );
56 }
57 } );
58
59 final UserInjectable injectable = new UserInjectable();
60 injector.injectMembers( injectable );
61
62 assertEquals( "User1", injectable.user1.getName() );
63 assertEquals( 1, injectable.user1.getId() );
64 assertEquals( "User2", injectable.user2.getName() );
65 assertEquals( 2, injectable.user2.getId() );
66 }
67
68 public class UserInjectable
69 {
70 @User( byId = 1 )
71 private UserEntity user1;
72
73 @User( byId = 2 )
74 private UserEntity user2;
75 }
76
77 }