JDK setup in unit tests produce "File accessed outside allowed roots" error
已回答
Hello,
I'm making a unit test with Gradle functionality for our project. For this, I used IntelliJ Community sources as an example, but getting an error.
File accessed outside allowed roots: file:///Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/src.zip;
Allowed roots: [/Users/aleksandr/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/2018.2.2/bb30662f57b73e341655b76260d13d8c0fe6abbd/ideaIC-2018.2.2, /Users/aleksandr/.m2, /Users/aleksandr/Projects/java/snyk/snyk-intellij-plugin-gradle-support-2/build/classes, /private/etc, /private/var/folders/b9/3r78768n6sg6sflgdxjqqb780000gn/T, /Users/aleksandr, /Users/aleksandr/.gradle, /private/var/folders/b9/3r78768n6sg6sflgdxjqqb780000gn/T/unitTest_getVulnerabilities/testGetVulnerabilities, /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre, /etc, /var/folders/b9/3r78768n6sg6sflgdxjqqb780000gn/T/]
java.lang.AssertionError: File accessed outside allowed roots: file:///Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/src.zip;
Allowed roots: [/Users/aleksandr/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/2018.2.2/bb30662f57b73e341655b76260d13d8c0fe6abbd/ideaIC-2018.2.2, /Users/aleksandr/.m2, /Users/aleksandr/Projects/java/snyk/snyk-intellij-plugin-gradle-support-2/build/classes, /private/etc, /private/var/folders/b9/3r78768n6sg6sflgdxjqqb780000gn/T, /Users/aleksandr, /Users/aleksandr/.gradle, /private/var/folders/b9/3r78768n6sg6sflgdxjqqb780000gn/T/unitTest_getVulnerabilities/testGetVulnerabilities, /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre, /etc, /var/folders/b9/3r78768n6sg6sflgdxjqqb780000gn/T/]
at com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess.assertAccessInTests(VfsRootAccess.java:86)
at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.doFindChild(VirtualDirectoryImpl.java:189)
at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.findChild(VirtualDirectoryImpl.java:95)
at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.findChild(VirtualDirectoryImpl.java:430)
at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.findChild(VirtualDirectoryImpl.java:57)
at com.intellij.openapi.vfs.newvfs.VfsImplUtil.findFileByPath(VfsImplUtil.java:54)
at com.intellij.openapi.vfs.impl.local.LocalFileSystemBase.findFileByPath(LocalFileSystemBase.java:44)
at com.intellij.openapi.vfs.newvfs.ArchiveFileSystem.findLocalByRootPath(ArchiveFileSystem.java:246)
at com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl.findRoot(PersistentFSImpl.java:979)
at com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl.findRoot(PersistentFSImpl.java:56)
at com.intellij.openapi.vfs.newvfs.VfsImplUtil.prepare(VfsImplUtil.java:139)
at com.intellij.openapi.vfs.newvfs.VfsImplUtil.findFileByPath(VfsImplUtil.java:38)
at com.intellij.openapi.vfs.impl.jar.JarFileSystemImpl.findFileByPath(JarFileSystemImpl.java:128)
at com.intellij.openapi.vfs.impl.VirtualFileManagerImpl.findFileByUrl(VirtualFileManagerImpl.java:136)
at com.intellij.openapi.projectRoots.impl.JavaSdkImpl.findInJar(JavaSdkImpl.java:542)
at com.intellij.openapi.projectRoots.impl.JavaSdkImpl.findSources(JavaSdkImpl.java:479)
at com.intellij.openapi.projectRoots.impl.JavaSdkImpl.addSources(JavaSdkImpl.java:455)
at com.intellij.openapi.projectRoots.impl.JavaSdkImpl.createJdk(JavaSdkImpl.java:341)
at io.snyk.plugin.AbstractGradleTestCase.$anonfun$setUp$1(AbstractGradleTestCase.scala:87)
at com.intellij.openapi.application.WriteAction.lambda$runAndWait$1(WriteAction.java:142)
at com.intellij.openapi.application.WriteAction.computeAndWait(WriteAction.java:158)
at com.intellij.openapi.application.WriteAction.runAndWait(WriteAction.java:141)
at io.snyk.plugin.AbstractGradleTestCase.setUp(AbstractGradleTestCase.scala:77)
at com.intellij.testFramework.UsefulTestCase.defaultRunBare(UsefulTestCase.java:358)
at com.intellij.testFramework.EdtTestUtil$Companion$runInEdtAndWait$1.invoke(EdtTestUtil.kt:19)
at com.intellij.testFramework.EdtTestUtil$Companion$runInEdtAndWait$1.invoke(EdtTestUtil.kt:14)
at com.intellij.testFramework.EdtTestUtilKt$runInEdtAndWait$2.run(EdtTestUtil.kt:59)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:361)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
This error produces with this source code (last line of source code):
val jdkHomeDirVirtualFile = LocalFileSystem.getInstance.refreshAndFindFileByIoFile(new File(myJdkHome))
val javaSdk = JavaSdk.getInstance
val javaSdkType = if (javaSdk == null) {
SimpleJavaSdkType.getInstance
} else {
javaSdk
}
println(jdkHomeDirVirtualFile.getCanonicalPath)
val jdk = SdkConfigurationUtil
.setupSdk(new Array[Sdk](0), jdkHomeDirVirtualFile, javaSdkType, true, null, GRADLE_JDK_NAME)
Could someone tells me what I'm doing wrong?
Thanks!
Alex.
请先登录再写评论。
Found the next solution - https://gist.github.com/aldanchenko/882c79afa2065bf376ff927018a351a4