Search in sources :

Example 41 with ISchedulingRule

use of org.eclipse.core.runtime.jobs.ISchedulingRule in project evosuite by EvoSuite.

the class ExtendSuiteAction method addTestJob.

// @Override
// public void selectionChanged(IAction action, ISelection selection) {
// currentSelection.clear();
// 
// if (selection instanceof IStructuredSelection) {
// IStructuredSelection sel = (IStructuredSelection) selection;
// 
// for (Object o : sel.toList()) {
// if (o instanceof IJavaElement) {
// IJavaElement jEl = (IJavaElement) o;
// try {
// IResource jRes = jEl.getCorrespondingResource();
// if (jRes != null) {
// jRes.accept(new IResourceVisitor() {
// @Override
// public boolean visit(IResource resource)
// throws CoreException {
// if ("java".equals(resource.getFileExtension()))
// currentSelection.add(resource);
// return true;
// }
// });
// }
// } catch (JavaModelException e) {
// System.err.println("Error while traversing resources!" + e);
// } catch (CoreException e) {
// System.err.println("Error while traversing resources!" + e);
// }
// }
// }
// }
// }
// 
// @Override
// public void run(IAction action) {
// if (currentSelection.isEmpty()) {
// MessageDialog.openError(shell, "Evosuite",
// "Unable to generate test cases for selection: Cannot find .java files.");
// } else if (currentSelection.size() > 1) {
// MessageDialog.openError(shell, "Evosuite",
// "Please only select one class at a time.");
// } else {
// 
// for (IResource res : currentSelection) {
// IProject proj = res.getProject();
// fixJUnitClassPath(JavaCore.create(proj));
// generateTests(res);
// }
// }
// }
/**
 * Add a new test generation job to the job queue
 *
 * @param target
 */
@Override
protected void addTestJob(final IResource target) {
    IJavaElement element = JavaCore.create(target);
    IJavaElement packageElement = element.getParent();
    String packageName = packageElement.getElementName();
    final String suiteClass = (!packageName.equals("") ? packageName + "." : "") + target.getName().replace(".java", "").replace(File.separator, ".");
    System.out.println("Building new job for " + suiteClass);
    DetermineSUT det = new DetermineSUT();
    IJavaProject jProject = JavaCore.create(target.getProject());
    try {
        String classPath = target.getWorkspace().getRoot().findMember(jProject.getOutputLocation()).getLocation().toOSString();
        String SUT = det.getSUTName(suiteClass, classPath);
        // choose
        SelectionDialog typeDialog = JavaUI.createTypeDialog(shell, new ProgressMonitorDialog(shell), target.getProject(), IJavaElementSearchConstants.CONSIDER_CLASSES, false);
        Object[] sutDefault = new Object[1];
        sutDefault[0] = SUT;
        typeDialog.setInitialSelections(sutDefault);
        typeDialog.setTitle("Please select the class under test");
        typeDialog.open();
        // Type selected by the user
        Object[] result = typeDialog.getResult();
        if (result.length > 0) {
            SourceType sourceType = (SourceType) result[0];
            SUT = sourceType.getFullyQualifiedName();
        } else {
            return;
        }
        Job job = new TestExtensionJob(shell, target, SUT, suiteClass);
        job.setPriority(Job.SHORT);
        IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
        ISchedulingRule rule = ruleFactory.createRule(target.getProject());
        // IFolder folder = proj.getFolder(ResourceUtil.EVOSUITE_FILES);
        job.setRule(rule);
        job.setUser(true);
        // start as soon as possible
        job.schedule();
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (NoJUnitClassException e) {
        MessageDialog.openError(shell, "Evosuite", "Cannot find JUnit tests in " + suiteClass);
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) SourceType(org.eclipse.jdt.internal.core.SourceType) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IJavaProject(org.eclipse.jdt.core.IJavaProject) NoJUnitClassException(org.evosuite.junit.DetermineSUT.NoJUnitClassException) DetermineSUT(org.evosuite.junit.DetermineSUT) IResourceRuleFactory(org.eclipse.core.resources.IResourceRuleFactory) Job(org.eclipse.core.runtime.jobs.Job)

Example 42 with ISchedulingRule

use of org.eclipse.core.runtime.jobs.ISchedulingRule in project evosuite by EvoSuite.

the class TestGenerationAction method addTestJob.

/**
 * Add a new test generation job to the job queue
 *
 * @param target
 */
protected void addTestJob(final IResource target) {
    IJavaElement element = JavaCore.create(target);
    if (element == null) {
        return;
    }
    IJavaElement packageElement = element.getParent();
    String packageName = packageElement.getElementName();
    final String targetClass = (!packageName.isEmpty() ? packageName + "." : "") + target.getName().replace(".java", "").replace(File.separator, ".");
    System.out.println("* Scheduling new automated job for " + targetClass);
    final String targetClassWithoutPackage = target.getName().replace(".java", "");
    final String suiteClassName = targetClass + Properties.JUNIT_SUFFIX;
    final String suiteFileName = target.getProject().getLocation() + "/evosuite-tests/" + suiteClassName.replace('.', File.separatorChar) + ".java";
    System.out.println("Checking for " + suiteFileName);
    File suiteFile = new File(suiteFileName);
    Job job = null;
    if (suiteFile.exists()) {
        MessageDialog dialog = new MessageDialog(shell, "Existing test suite found", // image
        null, "A test suite for class \"" + targetClass + "\" already exists. EvoSuite will overwrite this test suite. Do you really want to proceed?", MessageDialog.QUESTION_WITH_CANCEL, new String[] { "Overwrite", "Extend", "Rename Original", "Cancel" }, 0);
        int returnCode = dialog.open();
        // 1 == extend
        if (returnCode == 1) {
            IWorkspaceRoot wroot = target.getWorkspace().getRoot();
            IResource suiteResource = wroot.getFileForLocation(new Path(suiteFileName));
            job = new TestExtensionJob(shell, suiteResource, targetClass, suiteClassName);
        } else if (returnCode == 2) {
            // 2 == Rename
            renameSuite(target, packageName, targetClassWithoutPackage + Properties.JUNIT_SUFFIX + ".java");
        } else if (returnCode > 2) {
            // Cancel
            return;
        }
    }
    if (job == null)
        job = new TestGenerationJob(shell, target, targetClass, suiteClassName);
    job.setPriority(Job.SHORT);
    IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
    ISchedulingRule rule = ruleFactory.createRule(target.getProject());
    // IFolder folder = proj.getFolder(ResourceUtil.EVOSUITE_FILES);
    job.setRule(rule);
    job.setUser(true);
    // start as soon as possible
    job.schedule();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IResourceRuleFactory(org.eclipse.core.resources.IResourceRuleFactory) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) Job(org.eclipse.core.runtime.jobs.Job) File(java.io.File) IResource(org.eclipse.core.resources.IResource)

Example 43 with ISchedulingRule

use of org.eclipse.core.runtime.jobs.ISchedulingRule in project ecf by eclipse.

the class ResourcesShare method handleResourceChangeMessage.

private void handleResourceChangeMessage(byte[] data) throws Exception {
    IModelChange remoteChange = ResourcesSynchronizationStrategy.getInstance().deserializeRemoteChange(data);
    final IModelChange[] remoteChanges = ResourcesSynchronizationStrategy.getInstance().transformRemoteChange(remoteChange);
    // create a scheduling rule to lock the projects
    ISchedulingRule[] rules = new ISchedulingRule[sharedProjects.size()];
    int index = 0;
    for (Iterator it = sharedProjects.iterator(); it.hasNext(); ) {
        String projectName = (String) it.next();
        rules[index] = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        index++;
    }
    try {
        // lock to prevent resource changes from being propagated
        lock(remoteChanges);
        applyRemoteChanges(remoteChanges, new MultiRule(rules));
    } finally {
        // unlock now that we've applied the remote changes to our
        // own workspace
        unlock(remoteChanges);
    }
    if (remoteChange instanceof BatchModelChange) {
        BatchModelChange batchChange = (BatchModelChange) remoteChange;
        batchChange.setOutgoing(false);
        batchChange.setTime(System.currentTimeMillis());
        SyncResourcesCore.add(batchChange);
    }
}
Also used : MultiRule(org.eclipse.core.runtime.jobs.MultiRule) Iterator(java.util.Iterator) IModelChange(org.eclipse.ecf.sync.IModelChange) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule)

Example 44 with ISchedulingRule

use of org.eclipse.core.runtime.jobs.ISchedulingRule in project titan.EclipsePlug-ins by eclipse.

the class FormatLog method createRuleFromResources.

private ISchedulingRule createRuleFromResources(final IFile file, final IFile[] targetFiles) {
    final IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
    final ISchedulingRule rule1 = ruleFactory.createRule(file);
    ISchedulingRule combinedRule = MultiRule.combine(rule1, null);
    for (final IFile targetFile : targetFiles) {
        combinedRule = MultiRule.combine(ruleFactory.createRule(targetFile), combinedRule);
    }
    return combinedRule;
}
Also used : IFile(org.eclipse.core.resources.IFile) IResourceRuleFactory(org.eclipse.core.resources.IResourceRuleFactory) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule)

Example 45 with ISchedulingRule

use of org.eclipse.core.runtime.jobs.ISchedulingRule in project titan.EclipsePlug-ins by eclipse.

the class ProjectSourceParser method makefileCreatingAnalyzeAll.

/**
 * This function behaves just like the {@link #analyzeAll(IFile)}
 * function. With the only difference being that it does not have a
 * scheduling rule and does not can not run in parallel with the calling
 * party..
 * <p>
 * This function should only be called if no on-the-fly check was run
 * previously, it is required to have one run, but the scheduling rules
 * of the actual job does not allow it to run with its own scheduling
 * rules. In such cases the external scheduling rules must provide the
 * protection, that is usually provided by its own rules.
 */
// FIXME this function is only temporary, it should be removed once its
// functionality is available on any other way
public void makefileCreatingAnalyzeAll() {
    fullAnalyzersRunning.incrementAndGet();
    ISchedulingRule rule = getSchedulingRule();
    Job.getJobManager().beginRule(rule, new NullProgressMonitor());
    try {
        internalDoAnalyzeWithReferences(null);
    } catch (Exception e) {
        ErrorReporter.logExceptionStackTrace(e);
    } finally {
        Job.getJobManager().endRule(rule);
        fullAnalyzersRunning.decrementAndGet();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule)

Aggregations

ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)124 CoreException (org.eclipse.core.runtime.CoreException)46 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)46 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)37 IWorkspace (org.eclipse.core.resources.IWorkspace)31 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)29 PersistenceException (org.talend.commons.exception.PersistenceException)19 IStatus (org.eclipse.core.runtime.IStatus)18 InvocationTargetException (java.lang.reflect.InvocationTargetException)17 Status (org.eclipse.core.runtime.Status)16 IResource (org.eclipse.core.resources.IResource)14 MultiRule (org.eclipse.core.runtime.jobs.MultiRule)14 ArrayList (java.util.ArrayList)13 IResourceRuleFactory (org.eclipse.core.resources.IResourceRuleFactory)13 Job (org.eclipse.core.runtime.jobs.Job)12 IFile (org.eclipse.core.resources.IFile)11 HashSet (java.util.HashSet)10 IPath (org.eclipse.core.runtime.IPath)10 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)10 RepositoryWorkUnit (org.talend.repository.RepositoryWorkUnit)10