How to unit test a PsiElementFinder implementation

Hello,

I'm working on a plugin to add support in IDEA for a framework of my own which is based on a AnnotationProcessor generating classes from a class annoted with a specific annotation.

To let IDEA know about the generated classes, searching the web told me I had to provide an implementation of PsiElementFinder.

I wrote my PsiElementFinder and as far I have tested it by launching a Plugin test, it seams to work.

But I would like to add unit tests to my project to make sure the resolution work in various cases.

After Searching the web, existing plugin projects and trying various ways (in vain), I believe the best approach would be to use "PsiFile.findReferenceAt". But it doesn't seem to work.

Here is are two samples of the trials I made. (notes : this is no real code, just showing here the spirit and the API elements I'm using).

1. with PsiFile creating from String source  :

String fileSource = "public class UseGeneratedClass {\n" +
  "  private final GeneratedClass generatedClass;\n" +
  "}\n";
String fileName = "UseGeneratedClass.java";
FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(fileName);
PsiFile psiFile = PsiFileFactory.getInstance(myFixture.getProject())
                                   .createFileFromText(fileName, type, fileSource);

int offset = fileSource.indexOf("generatedClass");
PsiReference ref = psiFile.findReferenceAt(offset);

assertThat(ref).isNotNUll();


2. with PsiFile created from a file on disk added the fixture's project (a very simple class, empty but for a property of the generated class type) :

String filePath = "[...]/UseGeneratedClass.java";
String relativePath = "UseGeneratedClass.java";
PsiFile psiFile = loadToPsiFile(fileName);
VirtualFile virtualFile = myFixture.copyFileToProject(filePath, relativePath);
myFixture.configureFromExistingVirtualFile(virtualFile);
PsiFile file = myFixture.getFile();
int offset = 15; /*find the right offset of the property name*/
PsiReference ref = psiFile.findReferenceAt(offset); assertThat(ref).isNotNUll();


I have been spending quite a lot of time on the subject, so any help will be very much appreciated.

Seb'

Please sign in to leave a comment.