test fix
authorAlexey Kudravtsev <cdr@intellij.com>
Tue, 16 Jun 2015 10:39:33 +0000 (13:39 +0300)
committerAlexey Kudravtsev <cdr@intellij.com>
Tue, 16 Jun 2015 10:52:37 +0000 (13:52 +0300)
java/java-tests/testSrc/com/intellij/psi/impl/smartPointers/SmartPsiElementPointersTest.java
platform/core-impl/src/com/intellij/psi/impl/smartPointers/SmartPointerManagerImpl.java

index eea4d02d66ba376c61c159821a09ae0146f10566..ce44031e3e711fe88a01a1052c53e0649e031c49 100644 (file)
@@ -28,6 +28,8 @@ import com.intellij.openapi.editor.event.DocumentEvent;
 import com.intellij.openapi.editor.event.DocumentListener;
 import com.intellij.openapi.editor.event.EditorEventMulticaster;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
+import com.intellij.openapi.fileEditor.FileEditor;
+import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.fileTypes.StdFileTypes;
 import com.intellij.openapi.util.Segment;
 import com.intellij.openapi.util.TextRange;
@@ -588,23 +590,49 @@ public class SmartPsiElementPointersTest extends CodeInsightTestCase {
   }
   
   public void testEqualPointersWhenCreatedFromStubAndAST() {
-    final PsiFile file = configureByText(JavaFileType.INSTANCE,
-                                         "class S {\n" +
-                                         "}");
-
+    PsiJavaFile file = (PsiJavaFile)myJavaFacade.findClass("AClass", GlobalSearchScope.allScope(getProject())).getContainingFile();
 
     final SmartPointerManager manager = SmartPointerManager.getInstance(myProject);
-    int hash1 = ((PsiJavaFile)file).getClasses()[0].hashCode();
-    final SmartPsiElementPointer<PsiClass> pointer1 = manager.createSmartPsiElementPointer(((PsiJavaFile)file).getClasses()[0]);
+    int hash1 = file.getClasses()[0].hashCode();
+    final SmartPsiElementPointer<PsiClass> pointer1 = manager.createSmartPsiElementPointer(file.getClasses()[0]);
     assertNotNull(((PsiFileImpl)file).getStubTree());
     
     PlatformTestUtil.tryGcSoftlyReachableObjects();
 
     final FileASTNode node = file.getNode();
-    final SmartPsiElementPointer<PsiClass> pointer2 = manager.createSmartPsiElementPointer(((PsiJavaFile)file).getClasses()[0]);
-    assertFalse(hash1 == ((PsiJavaFile)file).getClasses()[0].hashCode());
+    final SmartPsiElementPointer<PsiClass> pointer2 = manager.createSmartPsiElementPointer(file.getClasses()[0]);
+    assertFalse(hash1 == file.getClasses()[0].hashCode());
     assertEquals(pointer1, pointer2);
     assertEquals(pointer1.getRange(), pointer2.getRange());
     assertNotNull(node);
   }
+
+  public void testSmartPointersForOpenFilesAreFastened() {
+    PsiJavaFile file = (PsiJavaFile)myJavaFacade.findClass("AClass", GlobalSearchScope.allScope(getProject())).getContainingFile();
+
+    SmartPointerManagerImpl manager = (SmartPointerManagerImpl)SmartPointerManager.getInstance(myProject);
+    VirtualFile virtualFile = file.getVirtualFile();
+
+    assertFalse(manager.areBeltsFastened(virtualFile));
+    FileEditor[] editors = FileEditorManager.getInstance(myProject).openFile(virtualFile, true);
+    assertTrue(editors.length != 0);
+
+    assertTrue(manager.areBeltsFastened(virtualFile));
+
+    FileEditorManager.getInstance(myProject).closeFile(virtualFile);
+    assertFalse(manager.areBeltsFastened(virtualFile));
+
+    SmartPsiElementPointer<PsiClass> pointer = manager.createSmartPsiElementPointer(file.getClasses()[0]);
+
+    assertFalse(manager.areBeltsFastened(virtualFile));
+    editors = FileEditorManager.getInstance(myProject).openFile(virtualFile, true);
+    assertTrue(editors.length != 0);
+
+    assertTrue(manager.areBeltsFastened(virtualFile));
+
+    FileEditorManager.getInstance(myProject).closeFile(virtualFile);
+    assertFalse(manager.areBeltsFastened(virtualFile));
+
+    assertEquals(file.getClasses()[0], pointer.getElement()); // retain pointer from gc
+  }
 }
index eea7b5c6e356749b98a6cc760c78a553a84720b1..7bb5ae397dfc1a1e3f39e2248ca32ea4cdee0c50 100644 (file)
@@ -122,15 +122,14 @@ public class SmartPointerManagerImpl extends SmartPointerManager {
     ApplicationManager.getApplication().assertIsDispatchThread();
     processQueue();
     synchronized (lock) {
-      FilePointersList pointers = getPointers(file);
-      if (pointers == null || pointers.isEmpty()) return;
-
       if (!getAndUnfasten(file)) return;
-
-      for (PointerReference ref : pointers.references) {
-        SmartPointerEx pointer = SoftReference.dereference(ref);
-        if (pointer != null) {
-          pointer.unfastenBelt(offset);
+      FilePointersList pointers = getPointers(file);
+      if (pointers != null && !pointers.isEmpty()) {
+        for (PointerReference ref : pointers.references) {
+          SmartPointerEx pointer = SoftReference.dereference(ref);
+          if (pointer != null) {
+            pointer.unfastenBelt(offset);
+          }
         }
       }
 
@@ -281,7 +280,7 @@ public class SmartPointerManagerImpl extends SmartPointerManager {
     file.putUserData(POINTERS_ARE_FASTENED_KEY, null);
     return fastened;
   }
-  private boolean areBeltsFastened(@NotNull VirtualFile file) {
+  boolean areBeltsFastened(@NotNull VirtualFile file) {
     return file.getUserData(POINTERS_ARE_FASTENED_KEY) == Boolean.TRUE;
   }