Correct way to setup and teardown libraries for integration tests Follow
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:
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?
Please sign in to leave a comment.
Please post the full source code of your test class
Any update on this? Thanks.
If it helps, the other method I tried was this: