Appendix B. The TestProject Fixture

This appendix lists the full source code of the TestProject fixture class that we used to implement the tests.

Example . org.eclipse.contribution.junit.test/TestProject

public class TestProject {
  public IProject project;
  public IJavaProject javaProject;
  private IPackageFragmentRoot sourceFolder;

  public TestProject() throws CoreException {
    IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
    project= root.getProject("Project-1");
    project.create(null);
    project.open(null);

    javaProject= JavaCore.create(project);
    IFolder binFolder= createBinFolder();
    setJavaNature();
    javaProject.setRawClasspath(new IClasspathEntry[0], null);
    createOutputFolder(binFolder);
    addSystemLibraries();
  }
  public IProject getProject() {
    return project;
  }

  public IJavaProject getJavaProject() {
    return javaProject;
  }

  public void addJar(String plugin, String jar)
    throws MalformedURLException, IOException, JavaModelException {
    Path result= findFileInPlugin(plugin, jar);
    IClasspathEntry[] oldEntries= javaProject.getRawClasspath();
    IClasspathEntry[] newEntries=
      new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0,
      oldEntries.length);
    newEntries[oldEntries.length]=
      JavaCore.newLibraryEntry(result, null, null);
    javaProject.setRawClasspath(newEntries, null);
  }

  public IPackageFragment createPackage(String name)
      throws CoreException {
    if (sourceFolder == null)
      sourceFolder= createSourceFolder();
    return sourceFolder.createPackageFragment(name, false, null);
  }

  public IType createType(IPackageFragment pack,
      String cuName, String source) throws JavaModelException {
    StringBuffer buf= new StringBuffer();
    buf.append("package " + pack.getElementName() + ";
");
    buf.append("
");
    buf.append(source);
    ICompilationUnit cu= pack.createCompilationUnit(cuName,
      buf.toString(), false, null);
    return cu.getTypes()[0];
  }

  public void dispose() throws CoreException {
    waitForIndexer();
    project.delete(true, true, null);
  }

  private IFolder createBinFolder() throws CoreException {
    IFolder binFolder= project.getFolder("bin");
    binFolder.create(false, true, null);
    return binFolder;
  }

  private void setJavaNature() throws CoreException {
    IProjectDescription description= project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
  }

  private void createOutputFolder(IFolder binFolder)
     throws JavaModelException {
    IPath outputLocation= binFolder.getFullPath();
    javaProject.setOutputLocation(outputLocation, null);
  }

  private IPackageFragmentRoot createSourceFolder()
      throws CoreException {
    IFolder folder= project.getFolder("src");
    folder.create(false, true, null);
    IPackageFragmentRoot root=
      javaProject.getPackageFragmentRoot(folder);

    IClasspathEntry[] oldEntries= javaProject.getRawClasspath();
    IClasspathEntry[] newEntries=
      new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0,
      oldEntries.length);
    newEntries[oldEntries.length]=
      JavaCore.newSourceEntry(root.getPath());
    javaProject.setRawClasspath(newEntries, null);
    return root;
  }

  private void addSystemLibraries() throws JavaModelException {
    IClasspathEntry[] oldEntries= javaProject.getRawClasspath();
    IClasspathEntry[] newEntries=
      new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0,
      oldEntries.length);
    newEntries[oldEntries.length]=
      JavaRuntime.getDefaultJREContainerEntry();
    javaProject.setRawClasspath(newEntries, null);
  }

  private Path findFileInPlugin(String plugin, String file)
      throws MalformedURLException, IOException {
    IPluginRegistry registry= Platform.getPluginRegistry();
    IPluginDescriptor descriptor=
      registry.getPluginDescriptor(plugin);
    URL pluginURL= descriptor.getInstallURL();
    URL jarURL= new URL(pluginURL, file);
    URL localJarURL= Platform.asLocalURL(jarURL);
    return new Path(localJarURL.getPath());
  }

  private void waitForIndexer() throws JavaModelException {
    new SearchEngine()
      .searchAllTypeNames(
        ResourcesPlugin.getWorkspace(),
        null,
        null,
        IJavaSearchConstants.EXACT_MATCH,
        IJavaSearchConstants.CASE_SENSITIVE,
        IJavaSearchConstants.CLASS,
        SearchEngine.createJavaSearchScope(new IJavaElement[0]),
        new ITypeNameRequestor() {
      public void acceptClass(char[] packageName,
        char[] simpleTypeName, char[][] enclosingTypeNames,
        String path) {
      }

      public void acceptInterface(char[] packageName,
        char[] simpleTypeName, char[][] enclosingTypeNames,
        String path) {
      }
    }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
  }
}

The method waitForIndexer() deserves some additional explanation. Deleting a project requires a non-obvious step. The Java search infrastructure uses an index to perform precise and efficient declaration and reference searches. The indexer is updated in a background thread whenever the contents of a project change. To avoid the indexer interfering with deleting (for example, trying to delete a file the indexer just opened), we wait until the indexer is finished. This is done by performing a dummy search query. The query specifies that it should wait until the indexer is up-to-date and ready. Before deleting a project we call waitForIndexer().

..................Content has been hidden....................

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