//
// Remove some JUnit imports.
//
List<ImportDeclaration> oldImports = visitor.getJUnitImports();
for (int i = 0; i < oldImports.size(); i++) {
Name importName = oldImports.get(i).getName();
String fqn = importName.getFullyQualifiedName();
if (IMPORTS_TO_REMOVE.contains(fqn)) {
result.remove(oldImports.get(i), null);
}
for (String s : STATIC_IMPORTS_TO_REMOVE) {
if (fqn.contains(s)) {
result.remove(oldImports.get(i), null);
}
}
}
//
// Add imports as needed
//
maybeAddImport(ast, result, astRoot, visitor.hasAsserts(), "org.testng.AssertJUnit");
maybeAddImport(ast, result, astRoot, visitor.hasFail(), "org.testng.Assert");
maybeAddImport(ast, result, astRoot, !visitor.getBeforeClasses().isEmpty(),
"org.testng.annotations.BeforeClass");
maybeAddImport(ast, result, astRoot, !visitor.getBeforeMethods().isEmpty(),
"org.testng.annotations.BeforeMethod");
maybeAddImport(ast, result, astRoot, visitor.hasTestMethods(), "org.testng.annotations.Test");
maybeAddImport(ast, result, astRoot, !visitor.getAfterMethods().isEmpty(),
"org.testng.annotations.AfterMethod");
maybeAddImport(ast, result, astRoot, !visitor.getAfterClasses().isEmpty(),
"org.testng.annotations.AfterClass");
//
// Add static imports
//
Set<String> staticImports = visitor.getStaticImports();
for (String si : staticImports) {
addImport(ast, result, astRoot, "org.testng.AssertJUnit." + si, true /* static import */);
}
//
// Remove "extends TestCase"
//
SimpleType td = visitor.getTestCase();
if (null != td) {
result.remove(td, null);
}
//
// Addd the annotations as needed
//
maybeAddAnnotations(ast, visitor, result, visitor.getTestMethods(), "Test", null, null);
maybeAddAnnotations(ast, visitor, result, visitor.getDisabledTestMethods(), "Test", null,
createDisabledAttribute(ast));
maybeAddAnnotations(ast, visitor, result, visitor.getBeforeMethods(), "BeforeMethod",
"@Before" /* annotation to remove */);
maybeAddAnnotations(ast, visitor, result, visitor.getAfterMethods(), "AfterMethod",
"@After" /* annotation to remove */);
//
// suite() method: remove, comment out or leave untouched, depending on the setting
//
SuiteMethodTreatment smt = TestNGPlugin.getPluginPreferenceStore().getSuiteMethodTreatement();
MethodDeclaration suiteMethod = visitor.getSuite();
if (smt != SuiteMethodTreatment.DONT_TOUCH && suiteMethod != null) {
if (smt == SuiteMethodTreatment.REMOVE) {
// 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);
}