use of org.eclipse.jdt.ui.actions.OrganizeImportsAction in project evosuite by EvoSuite.
the class TestGenerationJob method run.
@Override
protected IStatus run(final IProgressMonitor monitor) {
// && System.getProperty("evosuite.disable").equals("1")
Boolean disabled = System.getProperty("evosuite.disable") != null;
if (disabled) {
System.out.println("TestGenerationJob: The EvoSuite plugin is disabled :(");
return Status.OK_STATUS;
}
final String suiteFileName = getSuiteFileName(suiteClass);
final IFile fileSuite = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(suiteFileName));
if (fileSuite != null && fileSuite.exists()) {
// Open test suite in editor
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
IWorkbenchWindow iw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = iw.getActivePage();
try {
IDE.openEditor(page, fileSuite, true);
} catch (PartInitException e1) {
System.out.println("Could not open test suite");
e1.printStackTrace();
}
}
});
// Generated tests should be checked by tester?
Boolean checkMarkers = System.getProperty("evosuite.markers.enforce") != null;
if (checkMarkers) {
String fileContents = readFileToString(suiteFileName);
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setStatementsRecovery(true);
@SuppressWarnings("unchecked") Map<String, String> COMPILER_OPTIONS = new HashMap<String, String>(JavaCore.getOptions());
COMPILER_OPTIONS.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
COMPILER_OPTIONS.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
COMPILER_OPTIONS.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
parser.setUnitName(suiteClass);
String[] encodings = { ENCODING };
String[] classpaths = { classPath };
String[] sources = { new File(suiteFileName).getParent() };
parser.setEnvironment(classpaths, sources, encodings, true);
parser.setSource(fileContents.toCharArray());
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
final List<String> uncheckedMethods = new ArrayList<String>();
compilationUnit.accept(new ASTVisitor() {
@Override
public boolean visit(MemberValuePair node) {
if (node.getName().toString().equals("checked") && !((BooleanLiteral) node.getValue()).booleanValue()) {
NormalAnnotation ann = (NormalAnnotation) node.getParent();
MethodDeclaration method = (MethodDeclaration) ann.getParent();
uncheckedMethods.add(method.getName().toString());
return false;
}
return true;
}
});
if (uncheckedMethods.size() > 0) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog dialog = new MessageDialog(shell, "JUnit test suite contains unit tests that need to be checked", // image
null, "The JUnit test suite " + suiteClass + " contains test cases that need to be checked:\n" + uncheckedMethods.toString(), MessageDialog.OK, new String[] { "Ok" }, 0);
dialog.open();
}
});
return Status.OK_STATUS;
}
} else
System.out.println("Not checking markers.");
} else {
System.out.println("File " + suiteFileName + " does not exist");
// TODO: Dialog
// Display.getDefault().syncExec(new Runnable() {
// @Override
// public void run() {
// MessageDialog dialog = new MessageDialog(
// shell,
// "Error during test generation",
// null, // image
// "EvoSuite failed to generate tests for class"
// + suiteClass,
// MessageDialog.OK, new String[] { "Ok" }, 0);
// dialog.open();
// }
// });
// return Status.CANCEL_STATUS;
}
setThread(new Thread());
running = true;
clearMarkersTarget();
String oldTgr = getOldTestGenerationResult();
lastTest = oldTgr;
ArrayList<TestGenerationResult> results = runEvoSuite(monitor);
writeMarkersTarget(results);
// uncomment after experiment
if (writeAllMarkers)
writeMarkersTestSuite();
try {
target.getProject().refreshLocal(IProject.DEPTH_INFINITE, null);
// if ("true".equals(target.getProject().getPersistentProperty(
// EvoSuitePropertyPage.REPORT_PROP_KEY))) {
// syncWithUi(target);
// };
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
final IFile generatedSuite = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(suiteFileName));
ICompilationUnit cu = JavaCore.createCompilationUnitFrom(generatedSuite);
IWorkbenchWindow iw = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = iw.getActivePage();
IEditorPart part = IDE.openEditor(page, generatedSuite, true);
if (Activator.organizeImports()) {
OrganizeImportsAction a = new OrganizeImportsAction(part.getSite());
a.run(cu);
cu.commitWorkingCopy(true, null);
cu.save(null, true);
}
} catch (PartInitException e1) {
System.out.println("Could not open test suite to organize imports");
e1.printStackTrace();
} catch (JavaModelException e) {
System.out.println("Something went wrong while saving test suite after organizing imports");
e.printStackTrace();
}
;
}
});
} catch (CoreException e) {
System.out.println("Dear me");
e.printStackTrace();
}
Activator.CURRENT_WRITING_FILE = null;
running = false;
monitor.done();
done(ASYNC_FINISH);
Activator.FILE_QUEUE.update();
return Status.OK_STATUS;
}
Aggregations