From afca38b44ef8d01824e4ffc26ab6656142e8a556 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 11 Apr 2012 13:10:40 +0200 Subject: [PATCH] Reset-ability for roots allowed in tests; cleanup --- .../vfs/newvfs/impl/VirtualDirectoryImpl.java | 71 ++++++++++--------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/VirtualDirectoryImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/VirtualDirectoryImpl.java index c7b2b88a308c..6f8f8bf192fb 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/VirtualDirectoryImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/VirtualDirectoryImpl.java @@ -195,26 +195,32 @@ public class VirtualDirectoryImpl extends VirtualFileSystemEntry { private static final boolean IS_UNDER_TEAMCITY = System.getProperty("bootstrap.testcases") != null; - private static final boolean SHOULD_PERFORM_ACCESS_CHECK = System.getenv("NO_FS_ROOTS_ACCESS_CHECK") == null; - private static final boolean IS_UNIT_TESTS = ApplicationManager.getApplication().isUnitTestMode(); - - - private static final Collection additionalRoots = new THashSet(); + private static final Collection ourAdditionalRoots = new THashSet(); @TestOnly - public static void allowToAccess(@NotNull String root) { - additionalRoots.add(FileUtil.toSystemIndependentName(root)); + public static void allowRootAccess(@NotNull String... roots) { + for (String root : roots) { + ourAdditionalRoots.add(FileUtil.toSystemIndependentName(root)); + } + } + + @TestOnly + public static void disallowRootAccess(@NotNull String... roots) { + for (String root : roots) { + ourAdditionalRoots.remove(FileUtil.toSystemIndependentName(root)); + } } @TestOnly private static void assertAccessInTests(VirtualFileSystemEntry child) { - if (IS_UNIT_TESTS && - IS_UNDER_TEAMCITY && - ApplicationManager.getApplication() instanceof ApplicationImpl && - ((ApplicationImpl)ApplicationManager.getApplication()).isComponentsCreated() && - SHOULD_PERFORM_ACCESS_CHECK) { + final Application application = ApplicationManager.getApplication(); + if (IS_UNDER_TEAMCITY && + SHOULD_PERFORM_ACCESS_CHECK && + application.isUnitTestMode() && + application instanceof ApplicationImpl && + ((ApplicationImpl)application).isComponentsCreated()) { NewVirtualFileSystem fileSystem = child.getFileSystem(); if (fileSystem != LocalFileSystem.getInstance() && fileSystem != JarFileSystem.getInstance()) { return; @@ -256,25 +262,25 @@ public class VirtualDirectoryImpl extends VirtualFileSystemEntry { @Nullable private static Set allowedRoots() { if (insideGettingRoots) return null; + Project[] openProjects = ProjectManager.getInstance().getOpenProjects(); if (openProjects.length == 0) return null; + final Set allowed = new THashSet(); - String homePath = PathManager.getHomePath(); - allowed.add(FileUtil.toSystemIndependentName(homePath)); + allowed.add(FileUtil.toSystemIndependentName(PathManager.getHomePath())); + try { URL outUrl = Application.class.getResource("/"); String output = new File(outUrl.toURI()).getParentFile().getParentFile().getPath(); allowed.add(FileUtil.toSystemIndependentName(output)); } catch (URISyntaxException ignored) { } - String javaHome = SystemProperties.getJavaHome(); - allowed.add(FileUtil.toSystemIndependentName(javaHome)); - String tempDirectorySpecific = new File(FileUtil.getTempDirectory()).getParent(); - allowed.add(FileUtil.toSystemIndependentName(tempDirectorySpecific)); - String tempDirectory = System.getProperty("java.io.tmpdir"); - allowed.add(FileUtil.toSystemIndependentName(tempDirectory)); - String userHome = SystemProperties.getUserHome(); - allowed.add(FileUtil.toSystemIndependentName(userHome)); + + allowed.add(FileUtil.toSystemIndependentName(SystemProperties.getJavaHome())); + allowed.add(FileUtil.toSystemIndependentName(new File(FileUtil.getTempDirectory()).getParent())); + allowed.add(FileUtil.toSystemIndependentName(System.getProperty("java.io.tmpdir"))); + allowed.add(FileUtil.toSystemIndependentName(SystemProperties.getUserHome())); + for (Project project : openProjects) { if (!project.isInitialized()) { return null; // all is allowed @@ -290,12 +296,8 @@ public class VirtualDirectoryImpl extends VirtualFileSystemEntry { allowed.add(FileUtil.toSystemIndependentName(location)); } - //for (Sdk sdk : ProjectJdkTable.getInstance().getAllJdks()) { - // allowed.add(FileUtil.toSystemIndependentName(sdk.getHomePath())); - //} - for (String root : additionalRoots) { - allowed.add(root); - } + allowed.addAll(ourAdditionalRoots); + return allowed; } @@ -303,20 +305,19 @@ public class VirtualDirectoryImpl extends VirtualFileSystemEntry { private static VirtualFile[] getAllRoots(Project project) { insideGettingRoots = true; - Set roots = new THashSet(); + final Set roots = new THashSet(); + final Module[] modules = ModuleManager.getInstance(project).getModules(); for (Module module : modules) { final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); final OrderEntry[] orderEntries = moduleRootManager.getOrderEntries(); for (OrderEntry entry : orderEntries) { - VirtualFile[] files = entry.getFiles(OrderRootType.CLASSES); - ContainerUtil.addAll(roots, files); - files = entry.getFiles(OrderRootType.SOURCES); - ContainerUtil.addAll(roots, files); - files = entry.getFiles(OrderRootType.CLASSES_AND_OUTPUT); - ContainerUtil.addAll(roots, files); + ContainerUtil.addAll(roots, entry.getFiles(OrderRootType.CLASSES)); + ContainerUtil.addAll(roots, entry.getFiles(OrderRootType.SOURCES)); + ContainerUtil.addAll(roots, entry.getFiles(OrderRootType.CLASSES_AND_OUTPUT)); } } + insideGettingRoots = false; return VfsUtil.toVirtualFileArray(roots); }