001: package org.objectweb.celtix.common.injection;
002:
003: import javax.annotation.PostConstruct;
004: import javax.annotation.Resource;
005: import javax.annotation.Resources;
006: import junit.framework.TestCase;
007:
008: import org.easymock.classextension.EasyMock;
009: import org.objectweb.celtix.resource.ResourceManager;
010:
011: public class ResourceInjectorTest extends TestCase {
012: private static final String RESOURCE_ONE = "resource one";
013: private static final String RESOURCE_TWO = "resource one";
014:
015: private ResourceInjector injector;
016:
017: public void setUp() {
018:
019: ResourceManager resMgr = EasyMock
020: .createMock(ResourceManager.class);
021:
022: resMgr.resolveResource("resource1", String.class);
023: EasyMock.expectLastCall().andReturn(RESOURCE_ONE);
024: resMgr.resolveResource("resource2", String.class);
025: EasyMock.expectLastCall().andReturn(RESOURCE_TWO);
026: EasyMock.replay(resMgr);
027:
028: injector = new ResourceInjector(resMgr);
029: }
030:
031: public void testFieldInjection() {
032:
033: doInjectTest(new FieldTarget());
034: }
035:
036: public void testSetterInjection() {
037:
038: doInjectTest(new SetterTarget());
039: }
040:
041: public void testClassLevelInjection() {
042: doInjectTest(new ClassTarget());
043: }
044:
045: public void testResourcesContainer() {
046: doInjectTest(new ResourcesContainerTarget());
047: }
048:
049: public void testPostConstruct() {
050:
051: SetterTarget target = new SetterTarget();
052: doInjectTest(target);
053: assertTrue(target.injectionCompleteCalled());
054: }
055:
056: protected void doInjectTest(Target target) {
057:
058: injector.inject(target);
059:
060: assertNotNull(target.getResource1());
061: assertEquals(RESOURCE_ONE, target.getResource1());
062:
063: assertNotNull(target.getResource2());
064: assertEquals(RESOURCE_TWO, target.getResource2());
065: }
066:
067: }
068:
069: interface Target {
070: String getResource1();
071:
072: String getResource2();
073: }
074:
075: class FieldTarget implements Target {
076:
077: @Resource
078: private String resource1;
079:
080: @Resource(name="resource2")
081: private String resource2foo;
082:
083: public String getResource1() {
084: return resource1;
085: }
086:
087: public String getResource2() {
088: return resource2foo;
089: }
090:
091: public String toString() {
092: return "[" + resource1 + ":" + resource2foo + "]";
093: }
094:
095: }
096:
097: class SetterTarget implements Target {
098:
099: private String resource1;
100: private String resource2;
101: private boolean injectionCompletePublic;
102: private boolean injectionCompletePrivate;
103:
104: public final String getResource1() {
105: return this .resource1;
106: }
107:
108: @Resource
109: public final void setResource1(final String argResource1) {
110: this .resource1 = argResource1;
111: }
112:
113: public final String getResource2() {
114: return this .resource2;
115: }
116:
117: @Resource(name="resource2")
118: public final void setResource2(final String argResource2) {
119: this .resource2 = argResource2;
120: }
121:
122: @PostConstruct
123: public void injectionIsAllFinishedNowThankYouVeryMuch() {
124: injectionCompletePublic = true;
125:
126: // stick this here to keep PMD happy...
127: injectionIsAllFinishedNowThankYouVeryMuchPrivate();
128: }
129:
130: @PostConstruct
131: private void injectionIsAllFinishedNowThankYouVeryMuchPrivate() {
132: injectionCompletePrivate = true;
133: }
134:
135: public boolean injectionCompleteCalled() {
136: return injectionCompletePrivate && injectionCompletePublic;
137: }
138: }
139:
140: @Resource(name="resource1")
141: class ClassTarget implements Target {
142:
143: @Resource(name="resource2")
144: public String resource2foo;
145: private String res1;
146:
147: public final void setResource1(String res) {
148: res1 = res;
149: }
150:
151: public final String getResource1() {
152: return res1;
153: }
154:
155: public final String getResource2() {
156: return resource2foo;
157: }
158: }
159:
160: @Resources({@Resource(name="resource1"),@Resource(name="resource2")})
161: class ResourcesContainerTarget implements Target {
162:
163: private String res1;
164: private String resource2;
165:
166: public final void setResource1(String res) {
167: res1 = res;
168: }
169:
170: public final String getResource1() {
171: return res1;
172: }
173:
174: public final String getResource2() {
175: return resource2;
176: }
177: }
|