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.UserEntity;
25 import org.apache.onami.factoryannotation.impl.UserValueInjector;
26 import org.junit.Test;
27
28 import com.google.inject.Guice;
29 import com.google.inject.Inject;
30 import com.google.inject.Injector;
31
32 public class MethodInjectionTestCase
33 {
34
35 @Test
36 public void testMethodInjectionValueProvider1()
37 {
38 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
39 {
40 @Override
41 protected void configure()
42 {
43 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
44 }
45 } );
46
47 final UserInjectable1 injectable = new UserInjectable1();
48 injector.injectMembers( injectable );
49
50 assertEquals( "User1", injectable.user1.getName() );
51 assertEquals( 1, injectable.user1.getId() );
52 }
53
54 @Test
55 public void testMethodInjectionValueProvider2()
56 {
57 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
58 {
59 @Override
60 protected void configure()
61 {
62 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
63 }
64 } );
65
66 final UserInjectable2 injectable = new UserInjectable2();
67 injector.injectMembers( injectable );
68
69 assertEquals( "User1", injectable.user1.getName() );
70 assertEquals( 1, injectable.user1.getId() );
71 assertEquals( "User2", injectable.user2.getName() );
72 assertEquals( 2, injectable.user2.getId() );
73 }
74
75 @Test
76 public void testMethodInjectionValueProvider3()
77 {
78 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
79 {
80 @Override
81 protected void configure()
82 {
83 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
84 }
85 } );
86
87 final UserInjectable3 injectable = new UserInjectable3();
88 injector.injectMembers( injectable );
89
90 assertEquals( "User1", injectable.user1.getName() );
91 assertEquals( 1, injectable.user1.getId() );
92 assertEquals( "User2", injectable.user2.getName() );
93 assertEquals( 2, injectable.user2.getId() );
94 }
95
96 public static class UserInjectable1
97 {
98 private UserEntity user1;
99
100 @Inject
101 public void user( @User( byId = 1 )
102 final UserEntity user1 )
103 {
104 this.user1 = user1;
105 }
106 }
107
108 public static class UserInjectable2
109 {
110 private UserEntity user1;
111
112 private UserEntity user2;
113
114 @Inject
115 public void user( @User( byId = 1 )
116 final UserEntity user1, @User( byId = 2 )
117 final UserEntity user2 )
118 {
119
120 this.user1 = user1;
121 this.user2 = user2;
122 }
123 }
124
125 public static class UserInjectable3
126 {
127 private UserEntity user1;
128
129 private UserEntity user2;
130
131 @Inject
132 public void user1( @User( byId = 1 )
133 final UserEntity user1 )
134 {
135 this.user1 = user1;
136 }
137
138 @Inject
139 public void user2( @User( byId = 2 )
140 final UserEntity user2 )
141 {
142 this.user2 = user2;
143 }
144 }
145
146 }