// Remove suite()
result.remove(suiteMethod, null);
} else {
// Comment out suite()
TypeDeclaration type = visitor.getType();
ListRewrite lr = result.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
lr.insertBefore(result.createStringPlaceholder("/*", ASTNode.METHOD_DECLARATION),
suiteMethod, null);
lr.insertAfter(result.createStringPlaceholder("*/", ASTNode.METHOD_DECLARATION),
suiteMethod, null);
}
}
//
// Remove all the nodes that need to be removed
//
for (ASTNode n : visitor.getNodesToRemove()) {
result.remove(n, null);
}
//
// Replace @Ignore with @Test(enabled = false)
//
for (Map.Entry<MethodDeclaration, Annotation> e : visitor.getIgnoredMethods().entrySet()) {
MethodDeclaration md = e.getKey();
Annotation ignored = e.getValue();
// Add the @Test(enabled = false)
NormalAnnotation test = ast.newNormalAnnotation();
test.setTypeName(ast.newName("Test"));
MemberValuePair mvp = ast.newMemberValuePair();
mvp.setName(ast.newSimpleName("enabled"));
mvp.setValue(ast.newBooleanLiteral(false));
test.values().add(mvp);
result.remove(ignored, null);
ListRewrite lr = result.getListRewrite(md, MethodDeclaration.MODIFIERS2_PROPERTY);
lr.insertFirst(test, null);
}
//
// Replace "Assert" with "AssertJUnit", unless the method is already imported statically.
//
Set<MethodInvocation> asserts = visitor.getAsserts();
for (MethodInvocation m : asserts) {
if (! staticImports.contains(m.getName().toString())) {
Expression exp = m.getExpression();
Name name = ast.newName("AssertJUnit");
if (exp != null) {
result.replace(exp, name, null);
} else {
result.set(m, MethodInvocation.EXPRESSION_PROPERTY, name, null);
}
}
}
//
// Replace "fail()" with "Assert.fail()"
//
for (MethodInvocation fail : visitor.getFails()) {
SimpleName exp = ast.newSimpleName("Assert");
result.set(fail, MethodInvocation.EXPRESSION_PROPERTY, exp, null);
}
//
// Replace @Test(expected) with @Test(expectedExceptions)
// and @Test(timeout) with @Test(timeOut)
//
for (Map.Entry<MemberValuePair, String> pair : visitor.getTestsWithExpected().entrySet()) {
result.replace(pair.getKey().getName(), ast.newSimpleName(pair.getValue()), null);
}
//
// Remove super invocation in the constructor
//
SuperConstructorInvocation sci = visitor.getSuperConstructorInvocation();
if (sci != null) {
result.remove(sci, null);
}
//
// Convert @RunWith(Parameterized.class)
//
SingleMemberAnnotation runWith = visitor.getRunWithParameterized();
if (runWith != null) {
// Remove @RunWith
result.remove(runWith, null);
// Add imports
addImport(ast, result, astRoot, "org.testng.ConversionUtils.wrapDataProvider",
true /* static import */);
addImport(ast, result, astRoot, "org.testng.annotations.Factory", false /* not static */);
// Add the factory method
MethodDeclaration parameterMethod = visitor.getParametersMethod();
ListRewrite lr = result.getListRewrite(visitor.getType(),
TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
MethodDeclaration md = ast.newMethodDeclaration();
md.setName(ast.newSimpleName("factory" + capitalize(parameterMethod.getName().toString())));
// Add the "Factory" annotation
MarkerAnnotation factory = ast.newMarkerAnnotation();
factory.setTypeName(ast.newName("Factory"));
md.modifiers().add(factory);
// Make the method public
md.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC | Modifier.STATIC));
ArrayType returnType = ast.newArrayType(ast.newSimpleType(ast.newName("Object")));
md.setReturnType2(returnType);
// Create the method invocation "ConversionUtils.wrapDataProvider(Foo.class, data())"
MethodInvocation mi = ast.newMethodInvocation();
mi.setName(ast.newSimpleName("wrapDataProvider"));
// Add parameters to wrapDataProvider()
// 1) the current class
TypeLiteral tl = ast.newTypeLiteral();
tl.setType(ast.newSimpleType(ast.newSimpleName(visitor.getType().getName().toString())));
mi.arguments().add(tl);
// 2) the call to the @Parameters method
MethodInvocation pmi = ast.newMethodInvocation();
pmi.setName(ast.newSimpleName(parameterMethod.getName().getFullyQualifiedName()));
mi.arguments().add(pmi);
// Create the return statement
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(mi);
Block block = ast.newBlock();
block.statements().add(returnStatement);
md.setBody(block);
lr.insertFirst(md, null);
}
return result;
}