Appendix C. AutoTestBuilder with Exclusion Support

This is the version of the AutoTestBuilder with exclusion support. Before each test run, the exclusion file test.exclusions is read and applied to the list of available types.

Example . org.eclipse.contribution.junit/AutoTestBuilder

public class AutoTestBuilder extends IncrementalProjectBuilder {
  private static boolean enabled= true;
  private static boolean trace= false;

  public AutoTestBuilder() {
  }

  protected IProject[] build(int kind, Map args,
     IProgressMonitor pm) throws CoreException {
    if (hasBuildErrors() || !AutoTestBuilder.enabled)
     return null;
    IJavaProject javaProject= JavaCore.create(getProject());
    IType[] types= new TestSearcher().findAll(javaProject, pm);
    types= exclude(types);
    if (trace)
     printTestTypes(types);
    JUnitPlugin.getPlugin().run(types, javaProject);
    return null;
  }

  public static void setEnabled(boolean isEnabled) {
    AutoTestBuilder.enabled= isEnabled;
  }

  private boolean hasBuildErrors() throws CoreException {
    IMarker[] markers= getProject().findMarkers(
     IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, false,
    IResource.DEPTH_INFINITE);
    for (int i= 0; i < markers.length; i++) {
      IMarker marker= markers[i];
      if (marker.getAttribute(
         IMarker.SEVERITY, 0) == IMarker.SEVERITY_ERROR)
       return true;
    }
    return false;
  }

  static {
    String value= Platform.getDebugOption(
      "org.eclipse.contribution.junit/trace/testfinding");
    if (value != null && value.equalsIgnoreCase("true"))
      trace= true;
  }

  private static void printTestTypes(IType[] tests) {
    System.out.println("Auto Test: "); //$NON-NLS-1$
    for (int i= 0; i < tests.length; i++) {
      System.out.println("	"+tests[i].getFullyQualifiedName());
    }
  }

 private IType[] exclude(IType[] types) {
    try {
      Set exclusions= readExclusions(getProject().getFile(
        new Path("test.exclusions")));
      List result= new ArrayList(types.length);
      for (int i= 0; i < types.length; i++) {
        IType type= types[i];
        String typeName= type.getFullyQualifiedName();
        if (!exclusions.contains(typeName))
          result.add(type);
        return (IType[])(result.toArray(
          new IType[result.size()]));
      }
    } catch (Exception e) {
      // fall through
    }
    return types;
  }

 private Set readExclusions(IFile file)
     throws IOException, CoreException {
    Set result= new HashSet();
    BufferedReader br= new BufferedReader(
      new InputStreamReader(file.getContents()));
    try {
      String line;
      while ((line= br.readLine()) != null) {
        line= line.trim();
        if (line.length() > 0)
          result.add(line);
      }
    } finally {
      br.close();
    }
    return result;
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset