添加单个 Enterprise Bean 的 EJB 依赖注入所需的所有 类

Add all classes needed for EJB dependency injection of a single Enterprise Bean

我有Java个企业应用,由十多个项目编译成一个ear archive。我需要做一些集成测试,我必须使用带有 ShrinkWrap 的 Arquillian 平台。在 @Deployment 注释方法中,我必须明确地将每个 class 包含在我需要测试的 class 的依赖树中。

我的问题:是否有工具可以从单个 EJB 开始为通过 EJB 注释 (CDI) 引用的 classes 构建 class 树?

例如:我有这样一棵树(省略了一些classes)

@Singleton
public class A{
    @EJB 
    B b;

    public void doSomethingOnB(){
        b.doSomething();
    }
}

@Singleton
public class B{
    @EJB
    C c;

    @EJB
    D d;

    @EJB
    E e;

    public void doSomething(){
        d.toString();
        e.toString();
        c.fin();
    }

}

@Singleton
public class C{
    @EJB
    E e;

    @EJB
    F f;

    public void fin(){
        System.out.println(f.toString()+e.toString);
    }
}

我的 @Deployment 注释方法如下所示:

@RunWith(Arquillian.class) public class ClassAlphaTest {

    @Deployment(testable=true)
    public static JavaArchive createDeployment() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class) 
            .addClass(A.class) 
            .addClass(B.class) 
            .addClass(C.class) 
            .addClass(D.class) 
            .addClass(E.class) 
            .addClass(F.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        System.out.println(jar.toString(true));
        return jar;
    }

我的测试:

    @Test
    public void testLoadCategories(A alpha) {

        System.out.println("DoingTest");
        alpha.doSomethingOnB();

    }

我希望我的 createDeployment 看起来更像这样:

@Deployment(testable=true)
public static JavaArchive createDeployment() {
    List<Class> classes = buildClassTree(A.class);

    JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    foreach(Class c : classes){
        jar.addClass(c);
    }
    System.out.println(jar.toString(true));
    return jar;
}

其中 buildClassTree 是我正在寻找的方法。

我刚刚创建了一些实用方法,可以帮助在给定 class.

的情况下获取 EJB 注入树
public class TestJavaClassTreeConstruct {

    /**
     * To test everything works
     */
    @Test
    @SuppressWarnings("rawtypes")
    public void testLoadingClassList() {
        Class inputSingleClass = ExportTagsResource.class;
        Set<Class> list = getEjbAnnotatedClassTree(inputSingleClass);
        for (Class class1 : list) {
            System.err.println(class1.getName());
        }
    }

    /**
     * Returns the ejb classes sufficient to complete the EJB injection class
     * tree starting from the class "inputSingleClass"
     * 
     * @param inputSingleClass
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Set<Class> getEjbAnnotatedClassTree(Class inputSingleClass) {
        Set<Class> classes = new HashSet<Class>();
        classes.add(inputSingleClass);
        return getEjbAnnotatedClassTree(classes);
    }

    /**
     * Gets the ejb class tree starting from a list of classes.
     * 
     * @param classList
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Set<Class> getEjbAnnotatedClassTree(Set<Class> classList) {
        boolean found = false;
        List<Class> tempList = new ArrayList<Class>();

        for (Class innerClass : classList) {

            for (Field field : innerClass.getDeclaredFields()) {
                if (field.isAnnotationPresent(EJB.class)) {
                    if (!classList.contains(field.getType())) {
                        tempList.add(field.getType());
                        found = true;
                    }
                }
            }
        }
        classList.addAll(tempList);
        if (found) {
            classList.addAll(getEjbAnnotatedClassTree(classList));
        }
        return classList;
    }
}

然后 class 列表可以在 jar 创建过程中与 ShrinkWrap 结合使用

@Deployment(testable = true, managed = true)
public static JavaArchive createDeployment() {

    TestJavaClassTreeConstruct.getEjbAnnotatedClassTree(ExportTagsResource.class);
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class);


    for (Class c : TestJavaClassTreeConstruct.getEjbAnnotatedClassTree(MyMainClass.class)) {
        jar.addClass(c);
    }
    jar.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    System.out.println(jar.toString(true));

    return jar;
}