Correct way to setup and teardown libraries for integration tests

I've written a small plugin that adds javax persistence annotations to code. For now I'm making the javax persistence jar available to the test code like this:

hibernateJPALocation = "/Users/proctorh/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar";

File testLib = new File(hibernateJPALocation);
System.out.println("Does library exist? " + testLib.exists());
PsiTestUtil.addLibrary(myFixture.getModule(), hibernateJPALocation);

The test is able to run and the output is correct. The plugin code uses the java code style manager to shorten the references to the annotations from fully qualified to the short format and adds the imports. However after this the test still fails with an error on disposal of resources:

com.intellij.openapi.util.TraceableDisposable$DisposalException: Virtual pointer 'jar:///Users/proctorh/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar!/' hasn't been disposed: --------------Creation trace:
java.lang.Throwable: 1569010411792
at com.intellij.openapi.util.TraceableDisposable.<init>(TraceableDisposable.java:44)
at com.intellij.openapi.vfs.impl.VirtualFilePointerImpl.<init>(VirtualFilePointerImpl.java:41)
at com.intellij.openapi.vfs.impl.VirtualFilePointerManagerImpl.getOrCreate(VirtualFilePointerManagerImpl.java:262)
at com.intellij.openapi.vfs.impl.VirtualFilePointerManagerImpl.create(VirtualFilePointerManagerImpl.java:197)
at com.intellij.openapi.vfs.impl.VirtualFilePointerManagerImpl.create(VirtualFilePointerManagerImpl.java:132)
at com.intellij.openapi.vfs.impl.VirtualFilePointerContainerImpl.create(VirtualFilePointerContainerImpl.java:331)
at com.intellij.openapi.vfs.impl.VirtualFilePointerContainerImpl.add(VirtualFilePointerContainerImpl.java:139)
at com.intellij.openapi.roots.impl.libraries.LibraryImpl.addRoot(LibraryImpl.java:515)
at com.intellij.testFramework.PsiTestUtil.lambda$addProjectLibrary$21(PsiTestUtil.java:359)

I searched on this forum and found another question that talked about using a different method to set up the test resources so they will be cleared down correctly:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000648324-Virtual-pointer-hasn-t-been-disposed-on-LATEST-EAP-SNAPSHOT

This answer there being:

You can also use com.intellij.testFramework.PsiTestUtil#addLibrary(com.intellij.openapi.Disposable, com.intellij.openapi.module.Module, java.lang.String, java.lang.String, java.lang.String...) for adding the library instead of com.intellij.testFramework.PsiTestUtil#addLibrary(com.intellij.openapi.module.Module, java.lang.String, java.lang.String, java.lang.String...). The former accepts Disposable which will remove the library on disposal. You can pass com.intellij.testFramework.UsefulTestCase#getTestRootDisposable there for that purpose. 

However when I did this, the shorten class references in my code stopped working!!! 

What is the correct way to ensure the resources are disposed of but also ensure the code style manager / shorten class references code can still run correctly?

 

 

4 comments
Comment actions Permalink

Please post the full source code of your test class

0
Comment actions Permalink

 

public class BasicEntityTest extends LightCodeInsightFixtureTestCase {

@Override
protected void setUp() throws Exception {
super.setUp();

String hibernateJPALocation = System.getProperty("hibernate.jpa.location");
System.out.println("hibernate.jpa.location for hibernate JPA jar file: " + hibernateJPALocation);

if (hibernateJPALocation == null) {
hibernateJPALocation = "/Users/proctorh/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar";
System.out.println("Hibernate JPA location set to default: " + hibernateJPALocation);
}

File testLib = new File(hibernateJPALocation);
System.out.println("Does library exist? " + testLib.exists());
PsiTestUtil.addLibrary(myFixture.getModule(), hibernateJPALocation);

}

@NotNull
@Override
protected String getTestDataPath() {
return "src/test/resources";
}

/** Basic test to test adding:
*
* @Entity
* @Table
* @Id
*/
public void testBasicEntity() {

// configureByFile both copies the files into the temporary test directory, and opens the file in the (in-memory) editor.
myFixture.configureByFile("hbm-files/BasicEntity.hbm.xml");
// put the caret on the <class> line
myFixture.getEditor().getCaretModel().moveToLogicalPosition(new LogicalPosition(5,8));

// Java classes we want to be rewritten
myFixture.copyFileToProject("input-code/BasicEntity.java", "BasicEntity.java");

MigrateHbmToAnnotationsAction migrateHbmToAnnotationsAction = new MigrateHbmToAnnotationsAction();
myFixture.testAction(migrateHbmToAnnotationsAction);

myFixture.checkResultByFile("BasicEntity.java","expected-output-code/BasicEntity.java", true);

}
}
0
Comment actions Permalink

Any update on this? Thanks.

0
Comment actions Permalink

If it helps, the other method I tried was this:

PsiTestUtil.addLibrary(myFixture.getProjectDisposable(), myFixture.getModule(), "HibernateJPA", hibernateJPALocation);
0

Please sign in to leave a comment.