public void testRemoveExcludeRoot() throws Exception {
    ProjectRootManagerEx rootManager =
        (ProjectRootManagerEx) ProjectRootManager.getInstance(myProject);
    final VirtualFile root = rootManager.getContentRoots()[0];

    final VirtualFile dir = root.findChild("aDir");

    PsiTestUtil.addExcludedRoot(myModule, dir);

    PsiTodoSearchHelper.SERVICE
        .getInstance(myProject)
        .findFilesWithTodoItems(); // to initialize caches

    new WriteCommandAction.Simple(getProject()) {
      @Override
      protected void run() throws Throwable {
        VirtualFile newFile = createChildData(dir, "New.java");
        setFileText(newFile, "class A{ Exception e;} //todo");
      }
    }.execute().throwException();

    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiTodoSearchHelper.SERVICE.getInstance(myProject).findFilesWithTodoItems(); // to update caches

    PsiTestUtil.removeExcludedRoot(myModule, dir);

    PsiClass exceptionClass =
        myJavaFacade.findClass("java.lang.Exception", GlobalSearchScope.allScope(getProject()));
    assertNotNull(exceptionClass);
    checkUsages(exceptionClass, new String[] {"1.java", "2.java", "New.java"});
    checkTodos(new String[] {"2.java", "New.java"});
  }
Пример #2
0
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    String root = JavaTestUtil.getJavaTestDataPath() + "/psi/repositoryUse/modifyAnnotations";
    PsiTestUtil.removeAllRoots(myModule, IdeaTestUtil.getMockJdk17("mock 1.5"));
    PsiTestUtil.createTestProjectStructure(myProject, myModule, root, myFilesToDelete);
  }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    LanguageLevelProjectExtension.getInstance(myProject).setLanguageLevel(LanguageLevel.JDK_1_5);
    String root = PathManagerEx.getTestDataPath() + "/psi/repositoryUse/src15";
    PsiTestUtil.removeAllRoots(myModule, JavaSdkImpl.getMockJdk17("mock 1.5"));
    PsiTestUtil.createTestProjectStructure(myProject, myModule, root, myFilesToDelete);
  }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    String root =
        JavaTestUtil.getJavaTestDataPath() + "/psi/search/findUsages/" + getTestName(true);
    PsiTestUtil.removeAllRoots(myModule, JavaSdkImpl.getMockJdk17());
    PsiTestUtil.createTestProjectStructure(myProject, myModule, root, myFilesToDelete);
  }
 private static String[] addOutputs(Module module, int index) {
   String[] outputs = new String[2];
   String prefix = "outputs" + File.separatorChar;
   VirtualFile generalOutput = findFile(prefix + "general " + index);
   VirtualFile testOutput = findFile(prefix + "tests" + index);
   outputs[0] = generalOutput.getPresentableUrl();
   outputs[1] = testOutput.getPresentableUrl();
   PsiTestUtil.setCompilerOutputPath(module, generalOutput.getUrl(), false);
   PsiTestUtil.setCompilerOutputPath(module, testOutput.getUrl(), true);
   return outputs;
 }
 private static String setCompilerOutput(Module module, String path, boolean testOutput) {
   VirtualFile output =
       ModuleRootManager.getInstance(module).getContentEntries()[0].getFile().findChild(path);
   assertNotNull(output);
   PsiTestUtil.setCompilerOutputPath(module, output.getUrl(), testOutput);
   return output.getPath().replace('/', File.separatorChar);
 }
  public void testRemoveSourceRoot() {
    final VirtualFile root = ModuleRootManager.getInstance(myModule).getContentRoots()[0];

    PsiTodoSearchHelper.SERVICE
        .getInstance(myProject)
        .findFilesWithTodoItems(); // to initialize caches

    new WriteCommandAction.Simple(getProject()) {
      @Override
      protected void run() throws Throwable {
        VirtualFile newFile = createChildData(root, "New.java");
        setFileText(newFile, "class A{ Exception e;} //todo");
      }
    }.execute().throwException();

    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiTodoSearchHelper.SERVICE.getInstance(myProject).findFilesWithTodoItems(); // to update caches

    VirtualFile[] sourceRoots = ModuleRootManager.getInstance(myModule).getSourceRoots();
    LOG.assertTrue(sourceRoots.length == 1);
    PsiTestUtil.removeSourceRoot(myModule, sourceRoots[0]);

    PsiClass exceptionClass =
        myJavaFacade.findClass("java.lang.Exception", GlobalSearchScope.allScope(getProject()));
    assertNotNull(exceptionClass);
    // currently it actually finds usages by FQN due to Java PSI enabled for out-of-source java
    // files
    // so the following check is disabled
    // checkUsages(exceptionClass, new String[]{});
    checkTodos(new String[] {"2.java", "New.java"});
  }
  protected void doTest(final PerformAction performAction, final String testName) throws Exception {
    String path = getTestDataPath() + getTestRoot() + testName;

    String pathBefore = path + "/before";
    final VirtualFile rootDir =
        PsiTestUtil.createTestProjectStructure(
            myProject, myModule, pathBefore, myFilesToDelete, false);
    prepareProject(rootDir);
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    String pathAfter = path + "/after";
    final VirtualFile rootAfter =
        LocalFileSystem.getInstance().findFileByPath(pathAfter.replace(File.separatorChar, '/'));

    performAction.performAction(rootDir, rootAfter);
    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                myProject.getComponent(PostprocessReformattingAspect.class).doPostponedFormatting();
              }
            });

    FileDocumentManager.getInstance().saveAllDocuments();

    if (myDoCompare) {
      PlatformTestUtil.assertDirectoriesEqual(rootAfter, rootDir);
    }
  }
Пример #9
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 testAddSourceRoot() throws Exception {
    File dir = createTempDirectory();

    final VirtualFile root =
        LocalFileSystem.getInstance()
            .refreshAndFindFileByPath(dir.getCanonicalPath().replace(File.separatorChar, '/'));

    new WriteCommandAction.Simple(getProject()) {
      @Override
      protected void run() throws Throwable {
        PsiTestUtil.addContentRoot(myModule, root);

        VirtualFile newFile = createChildData(root, "New.java");
        setFileText(newFile, "class A{ Exception e;} //todo");
      }
    }.execute().throwException();

    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    PsiTodoSearchHelper.SERVICE
        .getInstance(myProject)
        .findFilesWithTodoItems(); // to initialize caches

    PsiTestUtil.addSourceRoot(myModule, root);

    PsiClass exceptionClass =
        myJavaFacade.findClass("java.lang.Exception", GlobalSearchScope.allScope(getProject()));
    assertNotNull(exceptionClass);
    checkUsages(exceptionClass, new String[] {"1.java", "2.java", "New.java"});
    checkTodos(new String[] {"2.java", "New.java"});
  }
 public void testAar() throws Throwable {
   PsiTestUtil.addLibrary(
       myModule,
       "maven_aar_dependency",
       getTestDataPath() + "/" + testFolder + "/myaar",
       "classes.jar",
       "res");
   doTestCompletion();
 }
        @Override
        public void configureModule(
            @NotNull Module module,
            @NotNull ModifiableRootModel model,
            @NotNull ContentEntry contentEntry) {
          super.configureModule(module, model, contentEntry);

          String libPath = PluginPathManager.getPluginHomePath("osmorc") + "/lib";
          PsiTestUtil.addLibrary(
              module, model, "osgi.core", libPath, "org.apache.felix.framework-4.2.1.jar");
          PsiTestUtil.addLibrary(module, model, "plexus", libPath, "plexus-utils-3.0.10.jar");

          String annotationsPath = PathManager.getJarPathForClass(NotNull.class);
          assertNotNull(annotationsPath);
          File annotations = new File(annotationsPath);
          PsiTestUtil.addLibrary(
              module, model, "annotations", annotations.getParent(), annotations.getName());

          FacetManager.getInstance(module).addFacet(OsmorcFacetType.getInstance(), "OSGi", null);
        }
  public void testAnnotated() throws Throwable {
    PsiFile psiFile = myFixture.configureByFile("lib/p/TestAnnotated.java");
    PsiTestUtil.addSourceRoot(myModule, psiFile.getVirtualFile().getParent());
    final PsiFile file = myFixture.getFile();
    final Editor editor = myFixture.getEditor();
    assertNotAvailable("Annotate method 'get' as @NotNull");
    assertNotAvailable("Annotate method 'get' as @Nullable");

    final DeannotateIntentionAction deannotateFix = new DeannotateIntentionAction();
    assertFalse(deannotateFix.isAvailable(myProject, editor, file));
  }
  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"});
  }
Пример #15
0
  private void configure() {
    try {
      String path = getTestDataPath() + getTestRoot() + getTestName(true);

      rootDir =
          PsiTestUtil.createTestProjectStructure(myProject, myModule, path, myFilesToDelete, false);
      prepareProject(rootDir);
      PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    } catch (Exception e) {
      throw UtilsPackage.rethrow(e);
    }
  }
Пример #16
0
 private void setUpDataClasses() throws IOException {
   File classesDir =
       new File(
           Test01.class
               .getResource("/" + Test01.class.getPackage().getName().replace('.', '/'))
               .getFile());
   File destDir =
       new File(myModule.getProject().getBaseDir().getPath() + myClassesProjectRelativePath);
   FileUtil.copyDir(classesDir, destDir);
   VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(destDir);
   assertNotNull(vFile);
   PsiTestUtil.addLibrary(
       myModule, "dataClasses", vFile.getPath(), new String[] {""}, ArrayUtil.EMPTY_STRING_ARRAY);
 }
Пример #17
0
  public void testModuleConflicts() throws Exception {
    final Module module2 = doCreateRealModule("module2");
    final VirtualFile contentRoot =
        LocalFileSystem.getInstance()
            .findFileByPath(getTestDataPath() + getTestRoot() + getTestName(false) + "/module2");
    PsiTestUtil.addSourceRoot(module2, contentRoot);
    FlexTestUtils.addFlexModuleDependency(myModule, module2);

    String[] conflicts =
        new String[] {
          "Class A, referenced in method Sub.foo(A), will not be accessible in module module2"
        };
    doTestConflicts("Sub", "ISuper", DocCommentPolicy.ASIS, conflicts, "foo");
  }
  private void loadAndSetupProject(String path) throws Exception {
    LocalFileSystem.getInstance().refreshIoFiles(myFilesToDelete);

    myProject = ProjectManager.getInstance().loadAndOpenProject(path);

    setUpModule();

    final String root = JavaTestUtil.getJavaTestDataPath() + "/psi/search/updateCache";
    PsiTestUtil.createTestProjectStructure(myProject, myModule, root, myFilesToDelete);

    setUpJdk();

    myProjectManager.openTestProject(myProject);
    runStartupActivities();
  }
 public void testPackageInfo() throws Exception {
   final String path = JavaTestUtil.getJavaTestDataPath() + "/codeInsight/javadocIG/";
   final String packageInfo = path + getTestName(true);
   PsiTestUtil.createTestProjectStructure(myProject, myModule, path, myFilesToDelete);
   final String info =
       new JavaDocInfoGenerator(
               getProject(),
               JavaPsiFacade.getInstance(getProject()).findPackage(getTestName(true)))
           .generateDocInfo(null);
   String htmlText =
       FileUtil.loadFile(new File(packageInfo + File.separator + "packageInfo.html"));
   assertNotNull(info);
   assertEquals(
       StringUtil.convertLineSeparators(htmlText.trim()),
       StringUtil.convertLineSeparators(info.trim()));
 }
  public void testContentWrite() throws Exception {
    File content = getTestRoot();
    File source = new File(content, "source");
    File testSource = new File(content, "testSource");
    File exclude = new File(content, "exclude");
    File classes = new File(content, "classes");
    File testClasses = new File(content, "testClasses");
    final VirtualFile contentFile = LocalFileSystem.getInstance().findFileByIoFile(content);
    assertNotNull(contentFile);
    final VirtualFile sourceFile = LocalFileSystem.getInstance().findFileByIoFile(source);
    assertNotNull(sourceFile);
    final VirtualFile testSourceFile = LocalFileSystem.getInstance().findFileByIoFile(testSource);
    assertNotNull(testSourceFile);
    final VirtualFile excludeFile = LocalFileSystem.getInstance().findFileByIoFile(exclude);

    assertNotNull(excludeFile);
    final VirtualFile classesFile = LocalFileSystem.getInstance().findFileByIoFile(classes);

    assertNotNull(classesFile);
    final VirtualFile testClassesFile = LocalFileSystem.getInstance().findFileByIoFile(testClasses);

    assertNotNull(testClassesFile);

    final File moduleFile = new File(content, "test.iml");
    final Module module = createModule(moduleFile);
    final ModuleRootManagerImpl moduleRootManager =
        (ModuleRootManagerImpl) ModuleRootManager.getInstance(module);

    PsiTestUtil.addContentRoot(module, contentFile);
    PsiTestUtil.addSourceRoot(module, sourceFile);
    PsiTestUtil.addSourceRoot(module, testSourceFile, true);
    ModuleRootModificationUtil.setModuleSdk(module, JavaSdkImpl.getMockJdk17());
    PsiTestUtil.addExcludedRoot(module, excludeFile);
    PsiTestUtil.setCompilerOutputPath(module, classesFile.getUrl(), false);
    PsiTestUtil.setCompilerOutputPath(module, testClassesFile.getUrl(), true);

    final Element element = new Element("root");
    moduleRootManager.getState().writeExternal(element);
    assertElementEquals(
        element,
        "<root inherit-compiler-output=\"false\">"
            + "<output url=\"file://$MODULE_DIR$/classes\" />"
            + "<output-test url=\"file://$MODULE_DIR$/testClasses\" />"
            + "<exclude-output />"
            + "<content url=\"file://$MODULE_DIR$\">"
            + "<sourceFolder url=\"file://$MODULE_DIR$/source\" isTestSource=\"false\" />"
            + "<sourceFolder url=\"file://$MODULE_DIR$/testSource\" isTestSource=\"true\" />"
            + "<excludeFolder url=\"file://$MODULE_DIR$/exclude\" />"
            + "</content>"
            + "<orderEntry type=\"jdk\" jdkName=\"java 1.7\" jdkType=\"JavaSDK\" />"
            + "<orderEntry type=\"sourceFolder\" forTests=\"false\" />"
            + "</root>",
        module);
  }
Пример #21
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());
          }
        });
  }
Пример #22
0
 private void doTestConflicts(
     final String from,
     final String to,
     final int docCommentPolicy,
     String[] conflicts,
     final String... toPullUp)
     throws Exception {
   String filePath = getTestDataPath() + getTestRoot() + getTestName(false);
   if (new File(filePath + ".as").exists()) {
     configureByFile(getTestRoot() + getTestName(false) + ".as");
   } else {
     String rootBefore = filePath + "/before";
     VirtualFile rootDir =
         PsiTestUtil.createTestProjectStructure(
             myProject, myModule, rootBefore, myFilesToDelete, false);
     prepareProject(rootDir);
     PsiDocumentManager.getInstance(myProject).commitAllDocuments();
   }
   performAction(from, to, docCommentPolicy, conflicts, toPullUp);
 }
 private void doSetUpSourcePath(final String sourcePath, boolean isTestSource) throws IOException {
   final String sourceDirectoryName = isTestSource ? "test" : "src";
   final VirtualFile moduleFile = myModule.getModuleFile();
   assertNotNull(moduleFile);
   VirtualFile moduleSourceDir =
       ApplicationManager.getApplication()
           .runWriteAction(
               new ThrowableComputable<VirtualFile, IOException>() {
                 @Override
                 public VirtualFile compute() throws IOException {
                   VirtualFile moduleSourceDir =
                       VfsUtil.createDirectoryIfMissing(
                           moduleFile.getParent(), sourceDirectoryName);
                   FileUtil.copyDirContent(
                       new File(sourcePath), new File(moduleSourceDir.getPath()));
                   VfsUtil.markDirtyAndRefresh(false, true, true, moduleSourceDir);
                   return moduleSourceDir;
                 }
               });
   PsiTestUtil.addSourceRoot(myModule, moduleSourceDir, isTestSource);
 }
Пример #24
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));
          }
        });
  }
 protected Module addDependentModule() {
   Module module = addModule("dependent");
   PsiTestUtil.addDependency(module, myModule);
   return module;
 }
  public void testPrimitive() throws Throwable {
    PsiFile psiFile = myFixture.configureByFile("lib/p/TestPrimitive.java");
    PsiTestUtil.addSourceRoot(myModule, psiFile.getVirtualFile().getParent());

    assertNotAvailable("Annotate method 'get' as @NotNull");
  }
 protected void prepareProject(VirtualFile rootDir) {
   PsiTestUtil.addSourceContentToRoots(myModule, rootDir);
 }
 private static void addSourcePath(Module module, String path, boolean testSource) {
   final ContentEntry entry = ModuleRootManager.getInstance(module).getContentEntries()[0];
   VirtualFile child = entry.getFile().findChild(path);
   assertNotNull(child);
   PsiTestUtil.addSourceRoot(module, child, testSource);
 }
 protected static void addGroovyLibrary(final Module to) {
   File jar = GroovyUtils.getBundledGroovyJar();
   PsiTestUtil.addLibrary(to, "groovy", jar.getParent(), jar.getName());
 }