001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.assembler.classic;
017:
018: import junit.framework.TestCase;
019: import org.apache.openejb.OpenEJBException;
020: import org.apache.openejb.spi.ContainerSystem;
021: import org.apache.openejb.loader.SystemInstance;
022: import org.apache.openejb.config.AppModule;
023: import org.apache.openejb.config.ConfigurationFactory;
024: import org.apache.openejb.config.EjbModule;
025: import org.apache.openejb.core.ivm.naming.InitContextFactory;
026: import org.apache.openejb.jee.EjbJar;
027: import org.apache.openejb.jee.StatelessBean;
028:
029: import javax.ejb.EJB;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import java.io.IOException;
033: import java.util.Arrays;
034:
035: /**
036: * A potential issue with this feature is that the stateless bean is created
037: * and the object it references does not exist at the time it is instantiated
038: * and put into the pool. Later the bean is deployed yet the instances in the
039: * pool remain with null references, only new instances will be able to reference
040: * the newly deployed bean.
041: *
042: * @version $Rev$ $Date$
043: */
044: public class EjbRefTest extends TestCase {
045: private InitialContext context;
046: private Assembler assembler;
047: private ConfigurationFactory config;
048:
049: public void asList(Object... objects) {
050: Arrays.asList(objects);
051: }
052:
053: protected void setUp() throws Exception {
054: config = new ConfigurationFactory();
055: assembler = new Assembler();
056:
057: assembler.createProxyFactory(config
058: .configureService(ProxyFactoryInfo.class));
059: assembler.createTransactionManager(config
060: .configureService(TransactionServiceInfo.class));
061: assembler.createSecurityService(config
062: .configureService(SecurityServiceInfo.class));
063: assembler.createContainer(config
064: .configureService(StatelessSessionContainerInfo.class));
065:
066: System.setProperty(
067: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
068: InitContextFactory.class.getName());
069: context = new InitialContext();
070: }
071:
072: protected void tearDown() throws Exception {
073: for (AppInfo appInfo : assembler.getDeployedApplications()) {
074: assembler.destroyApplication(appInfo.jarPath);
075: }
076: SystemInstance.get().setComponent(Assembler.class, null);
077: SystemInstance.get().setComponent(ContainerSystem.class, null);
078: super .tearDown();
079: }
080:
081: public void testInterfaceOnlyRefs() throws Exception {
082: ear(ejbjar(Apple.class, AmbiguousFruitRef.class));
083:
084: Fruit apple = get(Apple.class, Fruit.class);
085: assertNotNull(apple);
086:
087: FruitRef fruitRef = get(AmbiguousFruitRef.class, FruitRef.class);
088: assertNotNull(fruitRef);
089:
090: assertEquals(fruitRef.getFruit(), apple);
091: }
092:
093: public void testBeanNameRef() throws Exception {
094: ear(ejbjar(Apple.class, OrangeFruitRef.class, Orange.class));
095:
096: Fruit apple = get(Apple.class, Fruit.class);
097: assertNotNull(apple);
098:
099: Fruit orange = get(Orange.class, Fruit.class);
100: assertNotNull(orange);
101:
102: FruitRef fruitRef = get(OrangeFruitRef.class, FruitRef.class);
103: assertNotNull(fruitRef);
104:
105: assertEquals(fruitRef.getFruit(), orange);
106: }
107:
108: public void testMappedNameRef() throws Exception {
109: ear(ejbjar(Apple.class, AppleFruitRef.class, Orange.class));
110:
111: Fruit apple = get(Apple.class, Fruit.class);
112: assertNotNull(apple);
113:
114: Fruit orange = get(Orange.class, Fruit.class);
115: assertNotNull(orange);
116:
117: FruitRef fruitRef = get(AppleFruitRef.class, FruitRef.class);
118: assertNotNull(fruitRef);
119:
120: assertEquals(fruitRef.getFruit(), apple);
121: }
122:
123: public void testIntraEarInterfaceRef1() throws Exception {
124: ear(ejbjar(Apple.class), ejbjar(AmbiguousFruitRef.class));
125:
126: Fruit apple = get(Apple.class, Fruit.class);
127: assertNotNull(apple);
128:
129: FruitRef fruitRef = get(AmbiguousFruitRef.class, FruitRef.class);
130: assertNotNull(fruitRef);
131:
132: assertEquals(fruitRef.getFruit(), apple);
133: }
134:
135: public void testIntraEarInterfaceRef() throws Exception {
136: ear(ejbjar(Apple.class, AmbiguousFruitRef.class),
137: ejbjar(Orange.class));
138:
139: Fruit apple = get(Apple.class, Fruit.class);
140: assertNotNull(apple);
141:
142: Fruit orange = get(Orange.class, Fruit.class);
143: assertNotNull(orange);
144:
145: FruitRef fruitRef = get(AmbiguousFruitRef.class, FruitRef.class);
146: assertNotNull(fruitRef);
147:
148: assertEquals(fruitRef.getFruit(), apple);
149: }
150:
151: public void testIntraEarBeanNameRef1() throws Exception {
152: ear(ejbjar(Apple.class), ejbjar(OrangeFruitRef.class),
153: ejbjar(Orange.class));
154:
155: Fruit apple = get(Apple.class, Fruit.class);
156: assertNotNull(apple);
157:
158: Fruit orange = get(Orange.class, Fruit.class);
159: assertNotNull(orange);
160:
161: FruitRef fruitRef = get(OrangeFruitRef.class, FruitRef.class);
162: assertNotNull(fruitRef);
163:
164: assertEquals(fruitRef.getFruit(), orange);
165: }
166:
167: public void testIntraEarBeanNameRef2() throws Exception {
168: ear(ejbjar(Apple.class, OrangeFruitRef.class),
169: ejbjar(Orange.class));
170:
171: Fruit apple = get(Apple.class, Fruit.class);
172: assertNotNull(apple);
173:
174: Fruit orange = get(Orange.class, Fruit.class);
175: assertNotNull(orange);
176:
177: FruitRef fruitRef = get(OrangeFruitRef.class, FruitRef.class);
178: assertNotNull(fruitRef);
179:
180: assertEquals(fruitRef.getFruit(), orange);
181: }
182:
183: public void testInterEarInterfaceRef1() throws Exception {
184: ear(ejbjar(Apple.class));
185: ear(ejbjar(AmbiguousFruitRef.class));
186:
187: Fruit apple = get(Apple.class, Fruit.class);
188: assertNotNull(apple);
189:
190: FruitRef fruitRef = get(AmbiguousFruitRef.class, FruitRef.class);
191: assertNotNull(fruitRef);
192:
193: assertEquals(fruitRef.getFruit(), apple);
194: }
195:
196: public void testInterEarInterfaceRef2() throws Exception {
197: ear(ejbjar(Apple.class), ejbjar(AmbiguousFruitRef.class));
198: ear(ejbjar(Orange.class));
199:
200: Fruit apple = get(Apple.class, Fruit.class);
201: assertNotNull(apple);
202:
203: Fruit orange = get(Orange.class, Fruit.class);
204: assertNotNull(orange);
205:
206: FruitRef fruitRef = get(AmbiguousFruitRef.class, FruitRef.class);
207: assertNotNull(fruitRef);
208:
209: assertEquals(fruitRef.getFruit(), apple);
210: }
211:
212: public void testInterEarBeanNameRef1() throws Exception {
213: ear(ejbjar(Orange.class));
214: ear(ejbjar(Apple.class, OrangeFruitRef.class));
215:
216: Fruit apple = get(Apple.class, Fruit.class);
217: assertNotNull(apple);
218:
219: Fruit orange = get(Orange.class, Fruit.class);
220: assertNotNull(orange);
221:
222: FruitRef fruitRef = get(OrangeFruitRef.class, FruitRef.class);
223: assertNotNull(fruitRef);
224:
225: assertEquals(fruitRef.getFruit(), orange);
226: }
227:
228: public void testInterEarBeanNameRef2() throws Exception {
229: ear(ejbjar(Orange.class));
230: ear(ejbjar(Apple.class));
231: ear(ejbjar(OrangeFruitRef.class));
232:
233: Fruit apple = get(Apple.class, Fruit.class);
234: assertNotNull(apple);
235:
236: Fruit orange = get(Orange.class, Fruit.class);
237: assertNotNull(orange);
238:
239: FruitRef fruitRef = get(OrangeFruitRef.class, FruitRef.class);
240: assertNotNull(fruitRef);
241:
242: assertEquals(fruitRef.getFruit(), orange);
243: }
244:
245: public void testInterEarLazyInterfaceRef1() throws Exception {
246: ear(ejbjar(AmbiguousFruitRef.class));
247: ear(ejbjar(Apple.class));
248:
249: Fruit apple = get(Apple.class, Fruit.class);
250: assertNotNull(apple);
251:
252: FruitRef fruitRef = get(AmbiguousFruitRef.class, FruitRef.class);
253: assertNotNull(fruitRef);
254:
255: assertEquals(fruitRef.getFruit(), apple);
256: }
257:
258: public void testInterEarLazyBeanNameRef1() throws Exception {
259: ear(ejbjar(Apple.class, OrangeFruitRef.class));
260: ear(ejbjar(Orange.class));
261:
262: Fruit apple = get(Apple.class, Fruit.class);
263: assertNotNull(apple);
264:
265: Fruit orange = get(Orange.class, Fruit.class);
266: assertNotNull(orange);
267:
268: FruitRef fruitRef = get(OrangeFruitRef.class, FruitRef.class);
269: assertNotNull(fruitRef);
270:
271: assertEquals(fruitRef.getFruit(), orange);
272: }
273:
274: public void testInterEarLazyBeanNameRef2() throws Exception {
275: ear(ejbjar(OrangeFruitRef.class));
276: ear(ejbjar(Apple.class));
277: ear(ejbjar(Orange.class));
278:
279: Fruit apple = get(Apple.class, Fruit.class);
280: assertNotNull(apple);
281:
282: Fruit orange = get(Orange.class, Fruit.class);
283: assertNotNull(orange);
284:
285: FruitRef fruitRef = get(OrangeFruitRef.class, FruitRef.class);
286: assertNotNull(fruitRef);
287:
288: assertEquals(fruitRef.getFruit(), orange);
289: }
290:
291: public void testInterEarCircularBeanNameRef() throws Exception {
292: ear(ejbjar(BlueBean.class));
293: ear(ejbjar(WhiteBean.class));
294:
295: Blue blue = get(BlueBean.class, Blue.class);
296: White white = get(WhiteBean.class, White.class);
297:
298: assertNotNull(blue);
299: assertNotNull(blue.getWhite());
300:
301: assertNotNull(white);
302: assertNotNull(white.getBlue());
303:
304: assertEquals(blue, white.getBlue());
305: assertEquals(white, blue.getWhite());
306: }
307:
308: public void testInterEarCircularInterfaceRef() throws Exception {
309: ear(ejbjar(RedBean.class));
310: ear(ejbjar(BlackBean.class));
311:
312: Red red = get(RedBean.class, Red.class);
313: Black black = get(BlackBean.class, Black.class);
314:
315: assertNotNull(red);
316: assertNotNull(red.getBlack());
317:
318: assertNotNull(black);
319: assertNotNull(black.getRed());
320:
321: assertEquals(red, black.getRed());
322: assertEquals(black, red.getBlack());
323: }
324:
325: public void ear(Class... beans) throws Exception {
326: EjbJar ejbJar = ejbjar(beans);
327: ear(ejbJar);
328: }
329:
330: private void ear(EjbJar... ejbJars) throws OpenEJBException,
331: NamingException, IOException {
332: AppModule app = new AppModule(this .getClass().getClassLoader(),
333: "classpath-" + ejbJars.hashCode());
334: for (EjbJar ejbJar : ejbJars) {
335: app.getEjbModules().add(new EjbModule(ejbJar));
336: }
337: assembler.createApplication(config.configureApplication(app));
338: }
339:
340: private EjbJar ejbjar(Class... beans) {
341: EjbJar ejbJar = new EjbJar();
342: for (Class bean : beans) {
343: ejbJar.addEnterpriseBean(new StatelessBean(bean));
344: }
345: return ejbJar;
346: }
347:
348: public <T> T get(Class bean, Class<T> intrface) {
349: try {
350: return (T) context.lookup(bean.getSimpleName() + "Local");
351: } catch (NamingException e) {
352: throw new IllegalStateException(e);
353: }
354: }
355:
356: // ------------------------------------------------------- //
357: // Ref not unique by interface alone
358: // ------------------------------------------------------- //
359: public static class Apple implements Fruit {
360: }
361:
362: public static class Orange implements Fruit {
363: }
364:
365: public static interface Fruit {
366: }
367:
368: public static class AmbiguousFruitRef implements FruitRef {
369: @EJB
370: Fruit fruit;
371:
372: public Fruit getFruit() {
373: return fruit;
374: }
375: }
376:
377: public static class OrangeFruitRef implements FruitRef {
378: @EJB(beanName="Orange")
379: Fruit fruit;
380:
381: public Fruit getFruit() {
382: return fruit;
383: }
384: }
385:
386: public static class AppleFruitRef implements FruitRef {
387: @EJB(mappedName="Apple")
388: Fruit fruit;
389:
390: public Fruit getFruit() {
391: return fruit;
392: }
393: }
394:
395: public static interface FruitRef {
396: Fruit getFruit();
397: }
398:
399: // ------------------------------------------------------- //
400: // bi-directional relationship
401: // ------------------------------------------------------- //
402:
403: public static class RedBean implements Red {
404: @EJB
405: Black black;
406:
407: public Black getBlack() {
408: return black;
409: }
410: }
411:
412: public static interface Red {
413: public Black getBlack();
414: }
415:
416: public static class BlackBean implements Black {
417: @EJB
418: Red red;
419:
420: public Red getRed() {
421: return red;
422: }
423: }
424:
425: public static interface Black {
426: public Red getRed();
427: }
428:
429: public static class BlueBean implements Blue {
430: @EJB(beanName="WhiteBean")
431: White white;
432:
433: public White getWhite() {
434: return white;
435: }
436: }
437:
438: public static interface Blue {
439: public White getWhite();
440: }
441:
442: public static class WhiteBean implements White {
443: @EJB(beanName="BlueBean")
444: Blue blue;
445:
446: public Blue getBlue() {
447: return blue;
448: }
449: }
450:
451: public static interface White {
452: public Blue getBlue();
453: }
454: }
|