I have Java Enterprise application consisting of more than ten projects compiled into one ear archive. I need to do some integration testing and I have to use the Arquillian platform with ShrinkWrap. In the @Deployment
annotated method I have to explicitly include each class in the dependency tree of the class I need to test.
My question: is there a tool that can build the class tree for classes referenced via EJB annotation (CDI) starting from a single EJB?
For example: I have such tree (omitting some 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);
}
}
My @Deployment
annotated method would look like this:
@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;
}
And my test:
@Test
public void testLoadCategories(A alpha) {
System.out.println("DoingTest");
alpha.doSomethingOnB();
}
I would like my createDeployment to look more like this:
@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;
}
Where buildClassTree
is the method I am looking for.
I've just created some utility methods which can help in getting the EJB injection tree given a class.
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;
}
}
The class list can then be used in conjunction with ShrinkWrap in the jar creation process
@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;
}