/**
  * Tests that making Javadoc changes to the source file TestClass2 cause the workspace baseline to
  * be updated.
  *
  * <p>This test adds a @noinstantiate tag to the source file TestClass2
  */
 public void testWPUpdateSourceTypeChanged() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   NullProgressMonitor monitor = new NullProgressMonitor();
   IPackageFragment fragment = root.getPackageFragment("a.b.c");
   FileUtils.importFileFromDirectory(
       SRC_LOC.append("TestClass2.java").toFile(), fragment.getPath(), monitor);
   ICompilationUnit element =
       (ICompilationUnit) project.findElement(new Path("a/b/c/TestClass2.java"));
   assertNotNull("TestClass2 must exist in the test project", element);
   updateTagInSource(element, "TestClass2", null, "@noinstantiate", false);
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot = desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass2"));
   assertNotNull("the annotations for a.b.c.TestClass2 cannot be null", annot);
   assertTrue(
       "there must be a noinstantiate setting for TestClass2",
       (annot.getRestrictions() & RestrictionModifiers.NO_INSTANTIATE) != 0);
   assertTrue(
       "there must be a noextend setting for TestClass2",
       (annot.getRestrictions() & RestrictionModifiers.NO_EXTEND) != 0);
 }
  /**
   * Blocks until FindBugs has finished scanning
   *
   * @param className file in this project to scan
   * @throws CoreException
   */
  private void scanForBugs(String className) throws CoreException {
    IJavaElement element = testProject.findElement(new Path(className));
    if (element == null) {
      fail("Could not find java class " + className);
      return;
    }
    final AtomicBoolean isWorking = new AtomicBoolean(true);
    FindBugsWorker worker =
        new FindBugsWorker(
            testProject.getProject(),
            new NullProgressMonitor() {
              @Override
              public void done() {
                isWorking.set(false);
              }
            });

    worker.work(Collections.singletonList(new WorkItem(element)));
    // wait for the findBugsWorker to finish
    // 500ms reduces the chance that the IMarkers haven't loaded yet and the tests will fail
    // unpredictably
    // (see JavaProjectHelper discussion about performDummySearch for more info)
    TestingUtils.waitForUiEvents(800);
    while (isWorking.get()) {
      TestingUtils.waitForUiEvents(100);
    }
  }
  private void validatePropertyPackage() {

    IPackageFragmentRoot root = fResourceBundlePackage.getSelectedFragmentRoot();
    if ((root == null) || !root.exists()) {
      setInvalid(
          IDX_BUNDLE_PACKAGE,
          NLSUIMessages.NLSAccessorConfigurationDialog_property_package_root_invalid);
      return;
    }

    IPackageFragment fragment = fResourceBundlePackage.getSelected();
    if ((fragment == null) || !fragment.exists()) {
      setInvalid(
          IDX_BUNDLE_PACKAGE,
          NLSUIMessages.NLSAccessorConfigurationDialog_property_package_invalid);
      return;
    }

    String pkgName = fragment.getElementName();

    IStatus status = JavaConventionsUtil.validatePackageName(pkgName, root);
    if ((pkgName.length() > 0) && (status.getSeverity() == IStatus.ERROR)) {
      setInvalid(IDX_BUNDLE_PACKAGE, status.getMessage());
      return;
    }

    IPath pkgPath = new Path(pkgName.replace('.', IPath.SEPARATOR)).makeRelative();

    IJavaProject project = fRefactoring.getCu().getJavaProject();
    try {
      IJavaElement element = project.findElement(pkgPath);
      if (element == null || !element.exists()) {
        setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_must_exist);
        return;
      }
      IPackageFragment fPkgFragment = (IPackageFragment) element;
      if (!PackageBrowseAdapter.canAddPackage(fPkgFragment)) {
        setInvalid(
            IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_incorrect_package);
        return;
      }
      if (!PackageBrowseAdapter.canAddPackageRoot(
          (IPackageFragmentRoot) fPkgFragment.getParent())) {
        setInvalid(
            IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_incorrect_package);
        return;
      }
    } catch (JavaModelException e) {
      setInvalid(IDX_BUNDLE_PACKAGE, e.getStatus().getMessage());
      return;
    }

    setValid(IDX_BUNDLE_PACKAGE);
  }
Example #4
0
  /**
   * Finds a compilation unit by looking in all the java project of the supplied name.
   *
   * @param project The name of the project to locate the file in.
   * @param file The src dir relative file path to find.
   * @return The compilation unit or null if not found.
   */
  public static ICompilationUnit findCompilationUnit(String project, String file) throws Exception {
    IPath path = Path.fromOSString(file);

    IJavaProject javaProject = getJavaProject(project);
    javaProject.open(null);
    // javaProject.getResource().refreshLocal(IResource.DEPTH_INFINITE, null);

    ICompilationUnit src = (ICompilationUnit) javaProject.findElement(path);

    return src;
  }
 /**
  * Tests that removing a source file from an API aware project causes the workspace description to
  * be updated
  */
 public void testWPUpdateSourceRemoved() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestClass1");
   IJavaElement element = project.findElement(new Path("a/b/c/TestClass1.java"));
   assertNotNull("the class a.b.c.TestClass1 must exist in the project", element);
   element.getResource().delete(true, new NullProgressMonitor());
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot = desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass1"));
   assertNull("the annotations for a.b.c.TestClass1 should no longer be present", annot);
 }
Example #6
0
  /**
   * Finds a compilation unit by looking in all the available java projects.
   *
   * @param file The src directory relative file to find.
   * @return The compilation unit or null if not found.
   */
  public static ICompilationUnit findCompilationUnit(String file) throws Exception {
    IPath path = Path.fromOSString(file);
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for (IProject project : projects) {
      if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject javaProject = getJavaProject(project);
        javaProject.open(null);
        // javaProject.getResource().refreshLocal(IResource.DEPTH_INFINITE, null);

        ICompilationUnit src = (ICompilationUnit) javaProject.findElement(path);
        if (src != null) {
          return src;
        }
      }
    }
    return null;
  }
 /**
  * Returns the class file or compilation unit containing the given fully qualified name in the
  * specified project. All registered java like file extensions are considered.
  *
  * @param qualifiedTypeName fully qualified type name
  * @param project project to search in
  * @return class file or compilation unit or <code>null</code>
  * @throws CoreException if an exception occurs
  */
 public static IJavaElement findElement(String qualifiedTypeName, IJavaProject project)
     throws CoreException {
   String[] javaLikeExtensions = JavaCore.getJavaLikeExtensions();
   String path = qualifiedTypeName;
   int pos = path.indexOf('$');
   if (pos != -1) {
     path = path.substring(0, pos);
   }
   path = path.replace('.', IPath.SEPARATOR);
   path += "."; // $NON-NLS-1$
   for (String ext : javaLikeExtensions) {
     IJavaElement element = project.findElement(new Path(path + ext));
     if (element != null) {
       return element;
     }
   }
   return null;
 }
 /**
  * Tests that removing a tag from a field updates the workspace baseline
  *
  * <p>This test adds a @noextend tag to the field 'field' in TestField9
  */
 public void testWPUpdateSourceFieldRemoveTag() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestField9");
   ICompilationUnit element =
       (ICompilationUnit) project.findElement(new Path("a/b/c/TestField9.java"));
   assertNotNull("TestField9 must exist in the test project", element);
   updateTagInSource(element, "field1", null, "@noreference", true);
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot =
       desc.resolveAnnotations(Factory.fieldDescriptor("a.b.c.TestField9", "field"));
   assertNotNull("the annotations for 'field' cannot be null", annot);
   assertTrue("there must be a no restrictions for 'field'", annot.getRestrictions() == 0);
 }
 /**
  * Tests that removing a tag from a type updates the workspace baseline
  *
  * <p>This test removes a @noinstantiate tag to an inner class in TestClass3
  */
 public void testWPUpdateSourceTypeRemoveTag() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestClass3");
   ICompilationUnit element =
       (ICompilationUnit) project.findElement(new Path("a/b/c/TestClass3.java"));
   assertNotNull("TestClass3 must exist in the test project", element);
   updateTagInSource(element, "InnerTestClass3", null, "@noextend", true);
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot =
       desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass3$InnerTestClass3"));
   assertNotNull("the annotations for 'InnerTestClass3' cannot be null", annot);
   assertTrue(
       "there must be a no restrictions for 'InnerTestClass3'",
       (annot.getRestrictions() & RestrictionModifiers.NO_INSTANTIATE) == 0);
 }
 /**
  * Tests that changing the javadoc for a method updates the workspace baseline
  *
  * <p>This test adds a @noextend tag to the method foo() in TestClass1
  */
 public void testWPUpdateSourceMethodChanged() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestClass1");
   ICompilationUnit element =
       (ICompilationUnit) project.findElement(new Path("a/b/c/TestClass1.java"));
   assertNotNull("TestClass1 must exist in the test project", element);
   updateTagInSource(element, "foo", "()V", "@nooverride", false);
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot =
       desc.resolveAnnotations(Factory.methodDescriptor("a.b.c.TestClass1", "foo", "()V"));
   assertNotNull("the annotations for foo() cannot be null", annot);
   assertTrue(
       "there must be a nooverride setting for foo()",
       (annot.getRestrictions() & RestrictionModifiers.NO_OVERRIDE) != 0);
 }