@Override
public Result execute(UIExecutionContext context) throws Exception
{
UIContext uiContext = context.getUIContext();
Project project = getSelectedProject(uiContext);
JavaClassSource javaClass = Roaster.create(JavaClassSource.class).setName(named.getValue())
.setPackage(packageName.getValue());
// Add imports
javaClass.addImport("org.jboss.arquillian.container.test.api.Deployment");
javaClass.addImport("org.jboss.arquillian.junit.Arquillian");
javaClass.addImport("org.junit.runner.RunWith");
javaClass.addImport("org.jboss.forge.arquillian.AddonDependency");
javaClass.addImport("org.jboss.forge.arquillian.Dependencies");
javaClass.addImport("org.jboss.forge.arquillian.archive.ForgeArchive");
javaClass.addImport("org.jboss.forge.furnace.repositories.AddonDependencyEntry");
javaClass.addImport("org.jboss.shrinkwrap.api.ShrinkWrap");
// Add Arquillian annotation
javaClass.addAnnotation("RunWith").setLiteralValue("Arquillian.class");
// Create getDeployment method
StringBuilder body = new StringBuilder(
"ForgeArchive archive = ShrinkWrap.create(ForgeArchive.class).addBeansXML()");
StringBuilder dependenciesAnnotationBody = new StringBuilder();
body.append(".addAsAddonDependencies(");
AddonId furnaceContainerId = furnaceContainer.getValue();
addAddonDependency(project, body, dependenciesAnnotationBody, furnaceContainerId);
Iterator<AddonId> it = addonDependencies.getValue().iterator();
if (it.hasNext())
{
body.append(",");
dependenciesAnnotationBody.append(",");
}
while (it.hasNext())
{
AddonId addonId = it.next();
addAddonDependency(project, body, dependenciesAnnotationBody, addonId);
if (it.hasNext())
{
body.append(",");
dependenciesAnnotationBody.append(",");
}
}
body.append(")");
body.append(";");
body.append("return archive;");
MethodSource<JavaClassSource> getDeployment = javaClass.addMethod().setName("getDeployment").setPublic()
.setStatic(true)
.setBody(body.toString()).setReturnType("ForgeArchive");
getDeployment.addAnnotation("Deployment");
String annotationBody = dependenciesAnnotationBody.toString();
if (annotationBody.length() > 0)
{
getDeployment.addAnnotation("Dependencies").setLiteralValue("{" + annotationBody + "}");
}
JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
JavaResource javaResource = facet.saveTestJavaSource(javaClass);
uiContext.setSelection(javaResource);
return Results.success("Test class " + javaClass.getQualifiedName() + " created");
}