Search in sources :

Example 1 with DetermineSUT

use of org.evosuite.junit.DetermineSUT 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)

Aggregations

IResourceRuleFactory (org.eclipse.core.resources.IResourceRuleFactory)1 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)1 Job (org.eclipse.core.runtime.jobs.Job)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 SourceType (org.eclipse.jdt.internal.core.SourceType)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 SelectionDialog (org.eclipse.ui.dialogs.SelectionDialog)1 DetermineSUT (org.evosuite.junit.DetermineSUT)1 NoJUnitClassException (org.evosuite.junit.DetermineSUT.NoJUnitClassException)1