Ejemplo n.º 1
0
  public void _testCollectedPsiWithChangedDocument() throws IOException {
    VirtualFile dir = getVirtualFile(createTempDirectory());
    PsiTestUtil.addSourceContentToRoots(myModule, dir);

    final VirtualFile vFile = createChildData(dir, "Foo.java");
    VfsUtil.saveText(vFile, "class Foo {}");

    final GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    assertNotNull(facade.findClass("Foo", scope));
    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(vFile);
                assertNotNull(psiFile);

                Document document = FileDocumentManager.getInstance().getDocument(vFile);
                document.deleteString(0, document.getTextLength());
                assertNotNull(facade.findClass("Foo", scope));

                psiFile = null;
                PlatformTestUtil.tryGcSoftlyReachableObjects();
                assertNull(
                    ((PsiManagerEx) PsiManager.getInstance(getProject()))
                        .getFileManager()
                        .getCachedPsiFile(vFile));

                // should be assertNull(facade.findClass("Foo", scope));
                // or the file should not be allowed to be gc'ed
                facade.findClass("Foo", scope).getText();
              }
            });
  }
  public void testExternalFileModificationWhileProjectClosed() throws Exception {
    VirtualFile root = ProjectRootManager.getInstance(myProject).getContentRoots()[0];

    PsiClass objectClass =
        myJavaFacade.findClass(
            CommonClassNames.JAVA_LANG_OBJECT, GlobalSearchScope.allScope(getProject()));
    assertNotNull(objectClass);
    checkUsages(objectClass, new String[] {});
    FileBasedIndex.getInstance()
        .getContainingFiles(
            TodoIndex.NAME,
            new TodoIndexEntry("todo", true),
            GlobalSearchScope.allScope(getProject()));

    final String projectLocation = myProject.getPresentableUrl();
    assert projectLocation != null : myProject;
    PlatformTestUtil.saveProject(myProject);
    final VirtualFile content = ModuleRootManager.getInstance(getModule()).getContentRoots()[0];
    Project project = myProject;
    ProjectUtil.closeAndDispose(project);
    InjectedLanguageManagerImpl.checkInjectorsAreDisposed(project);

    assertTrue("Project was not disposed", myProject.isDisposed());
    myModule = null;

    final File file = new File(root.getPath(), "1.java");
    assertTrue(file.exists());

    FileUtil.writeToFile(file, "class A{ Object o;}".getBytes(CharsetToolkit.UTF8_CHARSET));
    root.refresh(false, true);

    LocalFileSystem.getInstance().refresh(false);

    myProject = ProjectManager.getInstance().loadAndOpenProject(projectLocation);
    InjectedLanguageManagerImpl.pushInjectors(getProject());

    setUpModule();
    setUpJdk();
    ProjectManagerEx.getInstanceEx().openTestProject(myProject);
    UIUtil.dispatchAllInvocationEvents(); // startup activities

    runStartupActivities();
    PsiTestUtil.addSourceContentToRoots(getModule(), content);

    assertNotNull(myProject);
    myPsiManager = (PsiManagerImpl) PsiManager.getInstance(myProject);
    myJavaFacade = JavaPsiFacadeEx.getInstanceEx(myProject);

    objectClass =
        myJavaFacade.findClass(
            CommonClassNames.JAVA_LANG_OBJECT, GlobalSearchScope.allScope(getProject()));
    assertNotNull(objectClass);
    checkUsages(objectClass, new String[] {"1.java"});
  }
Ejemplo n.º 3
0
  public void testSavedUncommittedDocument() throws IOException {
    VirtualFile dir = getVirtualFile(createTempDirectory());
    PsiTestUtil.addSourceContentToRoots(myModule, dir);

    final VirtualFile vFile = createChildData(dir, "Foo.java");
    VfsUtil.saveText(vFile, "");

    final GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    assertNull(facade.findClass("Foo", scope));
    WriteCommandAction.runWriteCommandAction(
        null,
        new Runnable() {
          @Override
          public void run() {
            PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(vFile);
            assertNotNull(psiFile);

            long count =
                PsiManager.getInstance(myProject).getModificationTracker().getModificationCount();

            Document document = FileDocumentManager.getInstance().getDocument(vFile);
            document.insertString(0, "class Foo {}");
            FileDocumentManager.getInstance().saveDocument(document);

            assertTrue(
                count
                    == PsiManager.getInstance(myProject)
                        .getModificationTracker()
                        .getModificationCount());
            assertNull(facade.findClass("Foo", scope));

            PsiDocumentManager.getInstance(myProject).commitAllDocuments();
            assertNotNull(facade.findClass("Foo", scope));
            assertNotNull(facade.findClass("Foo", scope).getText());
            // if Foo exists now, mod count should be different
            assertTrue(
                count
                    != PsiManager.getInstance(myProject)
                        .getModificationTracker()
                        .getModificationCount());
          }
        });
  }
Ejemplo n.º 4
0
  public void testCollectedPsiWithChangedDocument() throws IOException {
    VirtualFile dir = getVirtualFile(createTempDirectory());
    PsiTestUtil.addSourceContentToRoots(myModule, dir);

    final VirtualFile vFile = createChildData(dir, "Foo.java");
    VfsUtil.saveText(vFile, "class Foo {}");

    final GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    assertNotNull(facade.findClass("Foo", scope));
    WriteCommandAction.runWriteCommandAction(
        null,
        new Runnable() {
          @Override
          public void run() {
            PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(vFile);
            assertNotNull(psiFile);

            Document document = FileDocumentManager.getInstance().getDocument(vFile);
            document.deleteString(0, document.getTextLength());
            assertNotNull(facade.findClass("Foo", scope));

            psiFile = null;
            PlatformTestUtil.tryGcSoftlyReachableObjects();
            assertNull(
                ((PsiManagerEx) PsiManager.getInstance(getProject()))
                    .getFileManager()
                    .getCachedPsiFile(vFile));

            PsiClass foo = facade.findClass("Foo", scope);
            assertNotNull(foo);
            assertTrue(foo.isValid());
            assertEquals("class Foo {}", foo.getText());
            assertTrue(foo.isValid());

            PsiDocumentManager.getInstance(myProject).commitAllDocuments();
            assertNull(facade.findClass("Foo", scope));
          }
        });
  }
Ejemplo n.º 5
0
  public static VirtualFile createTestProjectStructure(
      String tempName,
      Module module,
      String rootPath,
      Collection<File> filesToDelete,
      boolean addProjectRoots)
      throws IOException {
    File dir = FileUtil.createTempDirectory(tempName, null, false);
    filesToDelete.add(dir);

    VirtualFile vDir =
        LocalFileSystem.getInstance()
            .refreshAndFindFileByPath(dir.getCanonicalPath().replace(File.separatorChar, '/'));
    assert vDir != null && vDir.isDirectory() : dir;
    PlatformTestCase.synchronizeTempDirVfs(vDir);

    EdtTestUtil.runInEdtAndWait(
        () -> {
          AccessToken token = WriteAction.start();
          try {
            if (rootPath != null) {
              VirtualFile vDir1 =
                  LocalFileSystem.getInstance()
                      .findFileByPath(rootPath.replace(File.separatorChar, '/'));
              if (vDir1 == null) {
                throw new Exception(rootPath + " not found");
              }
              VfsUtil.copyDirectory(null, vDir1, vDir, null);
            }

            if (addProjectRoots) {
              addSourceContentToRoots(module, vDir);
            }
          } finally {
            token.finish();
          }
        });
    return vDir;
  }
 public static void addSourceContentToRoots(Module module, @NotNull VirtualFile vDir) {
   addSourceContentToRoots(module, vDir, false);
 }
 protected void prepareProject(VirtualFile rootDir) {
   PsiTestUtil.addSourceContentToRoots(myModule, rootDir);
 }