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.test;
017:
018: import java.lang.reflect.Field;
019: import java.lang.reflect.Method;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.ejb.EJB;
025: import javax.ejb.EJBMetaData;
026: import javax.ejb.Handle;
027: import javax.ejb.HomeHandle;
028: import javax.naming.InitialContext;
029: import javax.rmi.PortableRemoteObject;
030:
031: import org.apache.xbean.finder.ClassFinder;
032:
033: /**
034: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
035: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
036: */
037: public abstract class TestClient extends NamedTestCase {
038:
039: protected InitialContext initialContext;
040: protected EJBMetaData ejbMetaData;
041: protected HomeHandle ejbHomeHandle;
042: protected Handle ejbHandle;
043: protected Integer ejbPrimaryKey;
044:
045: public TestClient(String name) {
046: super (name);
047: }
048:
049: /**
050: * Sets up the fixture, for example, open a network connection.
051: * This method is called before a test is executed.
052: */
053: protected abstract void setUp() throws Exception;
054:
055: protected Object cast(Object object, Class type) {
056: return PortableRemoteObject.narrow(object, type);
057: }
058:
059: protected final void processFieldInjections() {
060: Object home = null;
061: ClassFinder finder = null;
062: List<Field> fieldList = null;
063:
064: finder = new ClassFinder(getClassPath());
065: fieldList = finder.findAnnotatedFields(EJB.class);
066: for (Iterator fields = fieldList.iterator(); fields.hasNext();) {
067: Field field = (Field) fields.next();
068: EJB ejbAnnotation = field.getAnnotation(EJB.class);
069: if ((ejbAnnotation.name() != null)
070: && (ejbAnnotation.name() != "")
071: && (ejbAnnotation.beanInterface() != null)) {
072: try {
073: home = initialContext.lookup(ejbAnnotation.name());
074: // home = ejbAnnotation.beanInterface().cast(PortableRemoteObject.narrow(home, ejbAnnotation.beanInterface()));
075: home = cast(home, ejbAnnotation.beanInterface());
076: field.setAccessible(true);
077: field.set(this , home);
078: } catch (Exception ex) {
079: // TODO - MNour : Needs better exception handling
080: ex.printStackTrace();
081: }
082: }
083: }
084: }
085:
086: protected final void processSetterInjections() {
087: Object home = null;
088: ClassFinder finder = null;
089: List<Method> methodList = null;
090:
091: finder = new ClassFinder(getClassPath());
092: methodList = finder.findAnnotatedMethods(EJB.class);
093: for (Iterator methods = methodList.iterator(); methods
094: .hasNext();) {
095: Method method = (Method) methods.next();
096: EJB ejbAnnotation = method.getAnnotation(EJB.class);
097: if ((ejbAnnotation.name() != null)
098: && (ejbAnnotation.name() != "")
099: && (ejbAnnotation.beanInterface() != null)) {
100: try {
101: home = initialContext.lookup(ejbAnnotation.name());
102: // home = ejbAnnotation.beanInterface().cast(PortableRemoteObject.narrow(home, ejbAnnotation.beanInterface()));
103: home = cast(home, ejbAnnotation.beanInterface());
104: method.setAccessible(true);
105: method.invoke(this , new Object[] { home });
106: } catch (Exception ex) {
107: // TODO - MNour : Needs better exception handling
108: ex.printStackTrace();
109: }
110: }
111: }
112: }
113:
114: private List<Class> getClassPath() {
115: Class super Class = null;
116: List<Class> classPath = new ArrayList<Class>();
117:
118: classPath.add(getClass());
119: super Class = getClass().getSuperclass();
120: while (!super Class.equals(Object.class)) {
121: classPath.add(superClass);
122: superClass = superClass.getSuperclass();
123: }
124: return classPath;
125: }
126:
127: }
|