Package org.apache.tools.ant.taskdefs.optional.junit

Examples of org.apache.tools.ant.taskdefs.optional.junit.JUnitTask$ForkedTestConfiguration


    IProject project = ProjectUtils.getProject(projectName);
    project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null);

    IJavaProject javaProject = JavaUtils.getJavaProject(project);
    JUnitTask junit = createJUnitTask(javaProject, debug, halt);

    String[] vmargs =
      getPreferences().getArrayValue(project, "org.eclim.java.junit.jvmargs");
    for(String vmarg : vmargs){
      if (!vmarg.startsWith("-")){
        continue;
      }
      Argument a = junit.createJvmarg();
      a.setValue(vmarg);
    }

    String[] props =
      getPreferences().getArrayValue(project, "org.eclim.java.junit.sysprops");
    for(String prop : props){
      String[] sysprop = StringUtils.split(prop, "=", 2);
      if (sysprop.length != 2){
        continue;
      }
      if (sysprop[0].startsWith("-D")){
        sysprop[0] = sysprop[0].substring(2);
      }
      Variable var = new Variable();
      var.setKey(sysprop[0]);
      var.setValue(sysprop[1]);
      junit.addConfiguredSysproperty(var);
    }

    String[] envs =
      getPreferences().getArrayValue(project, "org.eclim.java.junit.envvars");
    for(String env : envs){
      String[] envvar = StringUtils.split(env, "=", 2);
      if (envvar.length != 2){
        continue;
      }
      Variable var = new Variable();
      var.setKey(envvar[0]);
      var.setValue(envvar[1]);
      junit.addEnv(var);
    }

    if (file != null){
      ICompilationUnit src = JavaUtils.getCompilationUnit(javaProject, file);
      IMethod method = null;
      if (offset != -1){
        IJavaElement element = src.getElementAt(offset);
        if(element != null && element.getElementType() == IJavaElement.METHOD){
          method = (IMethod)element;
        }
      }

      JUnit4TestFinder finder = new JUnit4TestFinder();
      IType type = src.getTypes()[0];
      if (!finder.isTest(type)){
        src = JUnitUtils.findTest(javaProject, type);
        if (src == null){
          println(Services.getMessage("junit.testing.test.not.found"));
          return null;
        }

        if (method != null){
          method = JUnitUtils.findTestMethod(src, method);
          if (method == null){
            println(Services.getMessage("junit.testing.test.method.not.found"));
            return null;
          }
        }
      }

      JUnitTest test = new JUnitTest();
      test.setName(JavaUtils.getFullyQualifiedName(src));
      if (method != null){
        IAnnotation testAnnotation = method.getAnnotation("Test");
        if (testAnnotation == null || !testAnnotation.exists()){
          println(Services.getMessage(
                "junit.testing.test.method.not.annotated",
                method.getElementName()));
          return null;
        }
        test.setMethods(method.getElementName());
      }
      junit.addTest(test);

    }else if (testName != null){
      if (testName.indexOf('*') != -1){
        createBatchTest(javaProject, junit, testName);
      }else{
        JUnitTest test = new JUnitTest();
        test.setName(testName);
        junit.addTest(test);
      }

    }else{
      ArrayList<String> names = new ArrayList<String>();
      IType[] types = JUnitCore.findTestTypes(javaProject, null);
      for (IType type : types) {
        names.add(type.getFullyQualifiedName());
      }
      Collections.sort(names);

      for (String name : names){
        JUnitTest test = new JUnitTest();
        test.setName(name);
        junit.addTest(test);
      }
    }

    try{
      junit.init();
      junit.execute();
    }catch(BuildException be){
      if(debug){
        be.printStackTrace(getContext().err);
      }
    }
View Full Code Here


    bundle = Platform.getBundle("org.eclim.jdt");
    pathName = FileLocator.getBundleFile(bundle).getPath();
    classpath.createPathElement().setPath(pathName + "/eclim.jdt.jar");

    JUnitTask junit = new JUnitTask();
    junit.setProject(antProject);
    junit.setTaskName("junit");
    junit.setFork(true);

    IProject project = javaProject.getProject();
    String cwd = getPreferences().getValue(project, "org.eclim.java.junit.cwd");
    junit.setDir(new File(
          cwd != null && cwd.trim().length() > 0 ? cwd :
          ProjectUtils.getPath(project)));

    junit.setHaltonerror(halt);
    junit.setHaltonfailure(halt);
    junit.createClasspath().append(classpath);

    // we need to add ant.jar to the classpath for the ant test runner to work,
    // but then JUnitTask will complain about multiple ant jars, so prevent
    // that.
    Field forkedPathChecked =
      JUnitTask.class.getDeclaredField("forkedPathChecked");
    forkedPathChecked.setAccessible(true);
    forkedPathChecked.set(junit, true);

    FormatterElement formatter = new FormatterElement();
    junit.addFormatter(formatter);
    formatter.setClassname("org.eclim.plugin.jdt.command.junit.ResultFormatter");
    formatter.setUseFile(false);

    return junit;
  }
View Full Code Here

* Utility class to setup JUnit test execution.
*/
public class JUnit {

  public static void test(MxTest mxtest, String jvmarg) {
    JUnitTask junit = null;
    try {
      // constructor throws Exception. Really??
      junit = new JUnitTask();
    } catch (Exception e) {
    }
    junit.setTaskName("test");
    junit.setProject(mxtest.getProject());
    junit.init();
   
    if (!StringUtils.isEmpty(jvmarg)) {
      junit.createJvmarg().setValue(jvmarg);
    }
 
    junit.setFailureProperty(mxtest.getFailureProperty());
    junit.createClasspath().add(mxtest.getUnitTestClasspath());
   
    junit.addConfiguredSysproperty(mxtest.getCoberturaFileProperty());
    junit.addConfiguredSysproperty(mxtest.getEmmaFileProperty());
    junit.addConfiguredSysproperty(mxtest.getEmmaMergeProperty());
   
    BatchTest batchTest = junit.createBatchTest();
    batchTest.setFork(true);
    batchTest.setTodir(mxtest.getUnitTestOutputDir());
    batchTest.addFileSet(mxtest.getUnitTests());
   
    TypeAttribute xml = (TypeAttribute) TypeAttribute.getInstance(TypeAttribute.class, "xml");
    FormatterElement formatter = new FormatterElement();
    formatter.setProject(mxtest.getProject());
    formatter.setType(xml);
    junit.addFormatter(formatter);

    // configure properties from Moxie file
    MaxmlMap testAttributes = mxtest.getBuild().getConfig().getTaskAttributes("junit");
    if (testAttributes != null) {
      AttributeReflector.setAttributes(mxtest.getProject(), junit, testAttributes);
    }

    junit.execute();

    XMLResultAggregator junitReport = new XMLResultAggregator();
    junitReport.setTaskName("test");
    junitReport.setProject(mxtest.getProject());
    junitReport.init();
View Full Code Here

TOP

Related Classes of org.apache.tools.ant.taskdefs.optional.junit.JUnitTask$ForkedTestConfiguration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.