KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > common > injection > ResourceInjectorTest


1 package org.objectweb.celtix.common.injection;
2
3
4
5 import javax.annotation.PostConstruct;
6 import javax.annotation.Resource;
7 import javax.annotation.Resources;
8 import junit.framework.TestCase;
9
10 import org.easymock.classextension.EasyMock;
11 import org.objectweb.celtix.resource.ResourceManager;
12
13 public class ResourceInjectorTest extends TestCase {
14     private static final String JavaDoc RESOURCE_ONE = "resource one";
15     private static final String JavaDoc RESOURCE_TWO = "resource one";
16
17     private ResourceInjector injector;
18         
19     public void setUp() {
20
21         ResourceManager resMgr = EasyMock.createMock(ResourceManager.class);
22         
23         resMgr.resolveResource("resource1", String JavaDoc.class);
24         EasyMock.expectLastCall().andReturn(RESOURCE_ONE);
25         resMgr.resolveResource("resource2", String JavaDoc.class);
26         EasyMock.expectLastCall().andReturn(RESOURCE_TWO);
27         EasyMock.replay(resMgr);
28         
29         injector = new ResourceInjector(resMgr);
30     }
31
32     public void testFieldInjection() {
33
34         doInjectTest(new FieldTarget());
35     }
36
37     public void testSetterInjection() {
38
39         doInjectTest(new SetterTarget());
40     }
41
42     public void testClassLevelInjection() {
43         doInjectTest(new ClassTarget());
44     }
45
46     public void testResourcesContainer() {
47         doInjectTest(new ResourcesContainerTarget());
48     }
49
50     public void testPostConstruct() {
51
52         SetterTarget target = new SetterTarget();
53         doInjectTest(target);
54         assertTrue(target.injectionCompleteCalled());
55     }
56
57
58     protected void doInjectTest(Target target) {
59
60         injector.inject(target);
61
62         assertNotNull(target.getResource1());
63         assertEquals(RESOURCE_ONE, target.getResource1());
64
65         assertNotNull(target.getResource2());
66         assertEquals(RESOURCE_TWO, target.getResource2());
67     }
68
69 }
70
71
72 interface Target {
73     String JavaDoc getResource1();
74     String JavaDoc getResource2();
75 }
76
77 class FieldTarget implements Target {
78
79     @Resource private String JavaDoc resource1;
80
81     @Resource(name = "resource2") private String JavaDoc resource2foo;
82
83     public String JavaDoc getResource1() {
84         return resource1;
85     }
86
87     public String JavaDoc getResource2() {
88         return resource2foo;
89     }
90
91     public String JavaDoc toString() {
92         return "[" + resource1 + ":" + resource2foo + "]";
93     }
94
95 }
96
97 class SetterTarget implements Target {
98
99     private String JavaDoc resource1;
100     private String JavaDoc resource2;
101     private boolean injectionCompletePublic;
102     private boolean injectionCompletePrivate;
103
104     public final String JavaDoc getResource1() {
105         return this.resource1;
106     }
107
108     @Resource public final void setResource1(final String JavaDoc argResource1) {
109         this.resource1 = argResource1;
110     }
111     
112     public final String JavaDoc getResource2() {
113         return this.resource2;
114     }
115     
116     @Resource(name = "resource2") public final void setResource2(final String JavaDoc argResource2) {
117         this.resource2 = argResource2;
118     }
119
120     @PostConstruct public void injectionIsAllFinishedNowThankYouVeryMuch() {
121         injectionCompletePublic = true;
122
123         // stick this here to keep PMD happy...
124
injectionIsAllFinishedNowThankYouVeryMuchPrivate();
125     }
126     
127     @PostConstruct private void injectionIsAllFinishedNowThankYouVeryMuchPrivate() {
128         injectionCompletePrivate = true;
129     }
130     
131     public boolean injectionCompleteCalled() {
132         return injectionCompletePrivate && injectionCompletePublic;
133     }
134 }
135
136 @Resource(name = "resource1")
137 class ClassTarget implements Target {
138
139     @Resource(name = "resource2") public String JavaDoc resource2foo;
140     private String JavaDoc res1;
141
142     public final void setResource1(String JavaDoc res) {
143         res1 = res;
144     }
145
146     public final String JavaDoc getResource1() {
147         return res1;
148     }
149
150     public final String JavaDoc getResource2() {
151         return resource2foo;
152     }
153 }
154
155
156
157 @Resources({@Resource(name = "resource1"),
158             @Resource(name = "resource2") })
159 class ResourcesContainerTarget implements Target {
160
161     private String JavaDoc res1;
162     private String JavaDoc resource2;
163
164     public final void setResource1(String JavaDoc res) {
165         res1 = res;
166     }
167
168     public final String JavaDoc getResource1() {
169         return res1;
170     }
171
172     public final String JavaDoc getResource2() {
173         return resource2;
174     }
175 }
176
Popular Tags