/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.java.codeInspection;

import com.intellij.analysis.AnalysisScope;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.reference.RefElement;
import com.intellij.codeInspection.reference.RefEntity;
import com.intellij.codeInspection.reference.RefManagerImpl;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;

import java.util.ArrayList;
import java.util.List;

public class BatchModeInspectionTest extends LightCodeInsightFixtureTestCase {
  public void testEnsureReferencesAreRemoved() {
    PsiClass aClass = myFixture.addClass("class Foo {public void bar(int i){}}");
    Project project = myFixture.getProject();
    RefManagerImpl refManager = new RefManagerImpl(project, new AnalysisScope(aClass.getContainingFile()), InspectionManager.getInstance(
      project).createNewGlobalContext(false));
    refManager.findAllDeclarations();
    List<RefElement> sortedElements = refManager.getSortedElements();

    RefElement refMethod = refManager.getReference(aClass.getMethods()[0]);
    List<RefEntity> children = refMethod.getChildren();
    ArrayList<RefElement> deletedRefs = new ArrayList<>();
    refManager.removeRefElement(refMethod, deletedRefs);
    assertTrue(deletedRefs.containsAll(children));
    assertTrue(deletedRefs.contains(refMethod));

    //check that table was not reinitialized due to full table traversal
    assertTrue(sortedElements == refManager.getSortedElements());
  }
}

@@ -39,560 +39,107 @@
 r.o.
-%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the Licens
+ Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE fil
 e.%0A 
@@ -395,16 +395,70 @@
 Entity;%0A
+import com.intellij.codeInspection.reference.RefFile;%0A
 import c
@@ -711,20 +711,85 @@
 va.util.
-List
+Collection;%0Aimport java.util.List;%0Aimport java.util.stream.Collectors
 ;%0A%0Apubli
@@ -1769,30 +1769,596 @@
 sert
-True(sortedElement
+Same(sortedElements, refManager.getSortedElements());%0A  %7D%0A%0A  public void testPsiClassOwnerReferencesCollectedWhileGraphBuilding() %7B%0A    PsiClass aClass = myFixture.addClass(%22class Foo %7B%7D%22);%0A    myFixture.addFileToProject(%22Bar.groovy%22, %22class Bar %7B void m() %7B new Foo(); %7D%7D%22);%0A    Project project = myFixture.getProject();%0A    RefManagerImpl refManager =%0A      new RefManagerImpl(project, new AnalysisScope(project), InspectionManager.getInstance(project).createNewGlobalContext(false));    refManager.findAllDeclarations();%0A    refManager.findAllDeclarations();%0A%0A    RefElement refClas
 s =
-=
  ref
@@ -2364,37 +2364,363 @@
 fManager.get
-SortedElements
+Reference(aClass);%0A    Collection%3CRefElement%3E fileReferences = refClass.getInReferences().stream().filter(x -%3E x instanceof RefFile).collect(Collectors.toList());%0A    RefElement referent = assertOneElement(fileReferences);%0A    RefFile groovyFile = assertInstanceOf(referent, RefFile.class);%0A    assertEquals(%22Bar.groovy%22, groovyFile.getName
 ());%0A  %7D%0A%7D%0A

add a test that checks that RefJavaManager collects references from external PsiClassOwner-s (eg: groovy)