Search in sources :

Example 1 with OrganizeImportsAction

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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TestGenerationResult(org.evosuite.result.TestGenerationResult) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) PartInitException(org.eclipse.ui.PartInitException) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) OrganizeImportsAction(org.eclipse.jdt.ui.actions.OrganizeImportsAction) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IEditorPart(org.eclipse.ui.IEditorPart) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 ASTParser (org.eclipse.jdt.core.dom.ASTParser)1 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)1 OrganizeImportsAction (org.eclipse.jdt.ui.actions.OrganizeImportsAction)1 MessageDialog (org.eclipse.jface.dialogs.MessageDialog)1 IEditorPart (org.eclipse.ui.IEditorPart)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1