/*
   * Ensures that searching takes the owner's working copies into account.
   */
  public void testSearch2() throws CoreException {
    ICompilationUnit cu = getCompilationUnit("P/X.java");
    TestWorkingCopyOwner owner = new TestWorkingCopyOwner();
    this.workingCopy = cu.getWorkingCopy(owner, null);

    // remove type X
    this.workingCopy.getBuffer().setContents("");
    this.workingCopy.makeConsistent(null);

    SearchPattern pattern =
        SearchPattern.createPattern(
            "X",
            IJavaSearchConstants.TYPE,
            IJavaSearchConstants.DECLARATIONS,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    JavaSearchTests.JavaSearchResultCollector resultCollector =
        new JavaSearchTests.JavaSearchResultCollector();
    new SearchEngine(owner)
        .search(
            pattern,
            new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
            SearchEngine.createWorkspaceScope(),
            resultCollector,
            null);
    assertEquals(
        "", // should not find any in the owner's context
        resultCollector.toString());
  }
  /*
   * Ensures that searching takes the primary owner's working copies into account only if the working copy
   * is not saved.
   */
  public void testSearch3() throws CoreException {
    try {
      createFile("/P/Y.java", "");
      this.workingCopy = getCompilationUnit("P/Y.java");
      this.workingCopy.becomeWorkingCopy(null);

      // create type Y in working copy
      this.workingCopy.getBuffer().setContents("public class Y {}");
      this.workingCopy.makeConsistent(null);

      JavaSearchTests.JavaSearchResultCollector resultCollector =
          new JavaSearchTests.JavaSearchResultCollector();
      search(
          "Y",
          IJavaSearchConstants.TYPE,
          IJavaSearchConstants.DECLARATIONS,
          SearchEngine.createWorkspaceScope(),
          resultCollector);
      assertEquals("Y.java Y [Y]", resultCollector.toString());

      //	commit new type
      this.workingCopy.commitWorkingCopy(false, null);
      resultCollector = new JavaSearchTests.JavaSearchResultCollector();
      search(
          "Y",
          IJavaSearchConstants.TYPE,
          IJavaSearchConstants.DECLARATIONS,
          SearchEngine.createWorkspaceScope(),
          resultCollector);
      assertEquals("Y.java Y [Y]", resultCollector.toString());
    } finally {
      deleteFile("/P/Y.java");
    }
  }
  /*
   * Ensures that searching takes the primary owner's working copies and the given working copies into account.
   * (regression test for bug 43300 SearchEngine(IWorkingCopy[] workingCopies) not backward compatible)
   */
  public void testSearch4() throws CoreException {
    ICompilationUnit primaryWorkingCopy = null;
    try {
      createFolder("P/p");
      createFile("/P/p/Y.java", "");
      primaryWorkingCopy = getCompilationUnit("P/p/Y.java");
      primaryWorkingCopy.becomeWorkingCopy(null);

      // create type Y in working copy
      primaryWorkingCopy.getBuffer().setContents("package p;\n" + "public class Y {\n" + "}");
      primaryWorkingCopy.makeConsistent(null);

      // create new working copy on X.java and add type X
      this.workingCopy = getCompilationUnit("P/p/X.java").getWorkingCopy(null);
      this.workingCopy.getBuffer().setContents("package p;\n" + "public class X {\n" + "}");
      this.workingCopy.makeConsistent(null);

      JavaSearchTests.JavaSearchResultCollector resultCollector =
          new JavaSearchTests.JavaSearchResultCollector();
      IJavaSearchScope scope =
          SearchEngine.createJavaSearchScope(new IJavaElement[] {primaryWorkingCopy.getParent()});
      SearchPattern pattern =
          SearchPattern.createPattern(
              "*",
              IJavaSearchConstants.TYPE,
              IJavaSearchConstants.DECLARATIONS,
              SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
      new SearchEngine(new ICompilationUnit[] {this.workingCopy})
          .search(
              pattern,
              new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
              scope,
              resultCollector,
              null);
      assertEquals("p/X.java p.X [X]\n" + "p/Y.java p.Y [Y]", resultCollector.toString());

    } finally {
      if (primaryWorkingCopy != null) {
        primaryWorkingCopy.discardWorkingCopy();
      }
      deleteFile("/P/p/Y.java");
    }
  }
  /*
   * Ensures that searching takes the owner's working copies into account.
   */
  public void testSearch1() throws CoreException {
    ICompilationUnit cu = getCompilationUnit("P/Y.java");
    TestWorkingCopyOwner owner = new TestWorkingCopyOwner();
    this.workingCopy = cu.getWorkingCopy(owner, null);
    this.workingCopy.getBuffer().setContents("public class Y {\n" + "  X field;\n" + "}");
    this.workingCopy.makeConsistent(null);

    SearchPattern pattern =
        SearchPattern.createPattern(
            "X",
            IJavaSearchConstants.TYPE,
            IJavaSearchConstants.REFERENCES,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    JavaSearchTests.JavaSearchResultCollector resultCollector =
        new JavaSearchTests.JavaSearchResultCollector();
    new SearchEngine(owner)
        .search(
            pattern,
            new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
            SearchEngine.createWorkspaceScope(),
            resultCollector,
            null);
    assertEquals("Y.java Y.field [X]", resultCollector.toString());
  }