From bc4893474d2ad6a0abe6bc419fc49d023722ab36 Mon Sep 17 00:00:00 2001 From: nik Date: Thu, 25 May 2017 14:15:49 +0300 Subject: [PATCH] tests refactoring: extracted base class from DirectoryIndexTest DirectoryIndexTest is too big now and it's better to write new tests in separate classes. Also initializing the whole directory structure in 'setUp' method looks overcomplicated, because many test methods use only some of these directories. --- .../roots/impl/DirectoryIndexTest.java | 63 ++---------- .../roots/impl/DirectoryIndexTestCase.java | 97 +++++++++++++++++++ 2 files changed, 103 insertions(+), 57 deletions(-) create mode 100644 java/java-tests/testSrc/com/intellij/openapi/roots/impl/DirectoryIndexTestCase.java diff --git a/java/java-tests/testSrc/com/intellij/java/openapi/roots/impl/DirectoryIndexTest.java b/java/java-tests/testSrc/com/intellij/java/openapi/roots/impl/DirectoryIndexTest.java index bb1f314ae3b0..e26dedf3b40a 100644 --- a/java/java-tests/testSrc/com/intellij/java/openapi/roots/impl/DirectoryIndexTest.java +++ b/java/java-tests/testSrc/com/intellij/java/openapi/roots/impl/DirectoryIndexTest.java @@ -26,17 +26,18 @@ import com.intellij.openapi.module.StdModuleTypes; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.*; import com.intellij.openapi.roots.ex.ProjectRootManagerEx; -import com.intellij.openapi.roots.impl.DirectoryIndex; -import com.intellij.openapi.roots.impl.DirectoryIndexImpl; +import com.intellij.openapi.roots.impl.DirectoryIndexTestCase; import com.intellij.openapi.roots.impl.DirectoryInfo; import com.intellij.openapi.roots.impl.ModuleFileIndexImpl; import com.intellij.openapi.util.EmptyRunnable; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.*; import com.intellij.openapi.vfs.ex.http.HttpFileSystem; -import com.intellij.testFramework.*; +import com.intellij.testFramework.PlatformTestCase; +import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.testFramework.PsiTestUtil; +import com.intellij.testFramework.VfsTestUtil; import com.intellij.util.containers.ContainerUtil; -import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jps.model.java.JavaResourceRootType; @@ -47,8 +48,7 @@ import java.io.File; import java.util.*; @PlatformTestCase.WrapInCommand -public class DirectoryIndexTest extends IdeaTestCase { - private DirectoryIndexImpl myIndex; +public class DirectoryIndexTest extends DirectoryIndexTestCase { private Module myModule2, myModule3; private VirtualFile myRootVFile; @@ -65,7 +65,6 @@ public class DirectoryIndexTest extends IdeaTestCase { private VirtualFile myModule1OutputDir; private VirtualFile myResDir, myTestResDir; private VirtualFile myExcludedLibSrcDir, myExcludedLibClsDir; - private ProjectFileIndex myFileIndex; @Override protected void setUp() throws Exception { @@ -192,16 +191,12 @@ public class DirectoryIndexTest extends IdeaTestCase { } }); - myIndex = (DirectoryIndexImpl)DirectoryIndex.getInstance(myProject); - myFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex(); // to not interfere with previous test firing vfs events VirtualFileManager.getInstance().syncRefresh(); } @Override protected void tearDown() throws Exception { - myFileIndex = null; - myIndex = null; myModule2 = null; myModule3 = null; super.tearDown(); @@ -747,25 +742,6 @@ public class DirectoryIndexTest extends IdeaTestCase { assertIteratedContent(myFileIndex, Arrays.asList(fileSourceRoot, fileTestSourceRoot), Collections.singletonList(fileRoot)); } - private void assertIteratedContent(Module module, @Nullable List contains, @Nullable List doesntContain) { - assertIteratedContent(ModuleRootManager.getInstance(module).getFileIndex(), contains, doesntContain); - assertIteratedContent(myFileIndex, contains, doesntContain); - } - - private static void assertIteratedContent(FileIndex fileIndex, - @Nullable List contains, - @Nullable List doesntContain) { - final Set collected = new THashSet<>(); - fileIndex.iterateContent(fileOrDir -> { - if (!collected.add(fileOrDir)) { - fail(fileOrDir + " visited twice"); - } - return true; - }); - if (contains != null) assertContainsElements(collected, contains); - if (doesntContain != null) assertDoesntContain(collected, doesntContain); - } - public void testFileSourceRootsUnderDirContentRoot() { VirtualFile fileSourceRoot = createChildData(myModule1Dir, "fileSourceRoot.txt"); assertTrue(myFileIndex.isInContent(fileSourceRoot)); @@ -989,33 +965,6 @@ public class DirectoryIndexTest extends IdeaTestCase { } } - private void assertNotInProject(VirtualFile file) { - DirectoryInfo info = myIndex.getInfoForFile(file); - assertFalse(info.toString(), info.isInProject()); - assertFalse(info.toString(), info.isExcluded()); - } - - private void assertExcluded(VirtualFile file, Module module) { - DirectoryInfo info = myIndex.getInfoForFile(file); - assertTrue(info.toString(), info.isExcluded()); - assertEquals(module, info.getModule()); - } - - private DirectoryInfo assertInProject(VirtualFile file) { - DirectoryInfo info = myIndex.getInfoForFile(file); - assertTrue(file.toString(), info.isInProject()); - myIndex.assertConsistency(info); - return info; - } - - private void assertNotExcluded(VirtualFile file) { - assertFalse(myIndex.getInfoForFile(file).isExcluded()); - } - - private void assertExcludedFromProject(VirtualFile file) { - assertExcluded(file, null); - } - private void checkPackage(String packageName, boolean includeLibrarySources, VirtualFile... expectedDirs) { VirtualFile[] actualDirs = myIndex.getDirectoriesByPackageName(packageName, includeLibrarySources).toArray(VirtualFile.EMPTY_ARRAY); assertNotNull(actualDirs); diff --git a/java/java-tests/testSrc/com/intellij/openapi/roots/impl/DirectoryIndexTestCase.java b/java/java-tests/testSrc/com/intellij/openapi/roots/impl/DirectoryIndexTestCase.java new file mode 100644 index 000000000000..ea14c41a4e73 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/openapi/roots/impl/DirectoryIndexTestCase.java @@ -0,0 +1,97 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.roots.impl; + +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.FileIndex; +import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.roots.ProjectFileIndex; +import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.testFramework.IdeaTestCase; +import gnu.trove.THashSet; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.Set; + +/** + * @author nik + */ +public abstract class DirectoryIndexTestCase extends IdeaTestCase { + protected DirectoryIndexImpl myIndex; + protected ProjectFileIndex myFileIndex; + + @Override + protected void setUp() throws Exception { + super.setUp(); + myIndex = (DirectoryIndexImpl)DirectoryIndex.getInstance(myProject); + myFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex(); + } + + @Override + protected void tearDown() throws Exception { + myFileIndex = null; + myIndex = null; + super.tearDown(); + } + + protected void assertNotInProject(VirtualFile file) { + DirectoryInfo info = myIndex.getInfoForFile(file); + assertFalse(info.toString(), info.isInProject(file)); + assertFalse(info.toString(), info.isExcluded(file)); + } + + protected void assertExcluded(VirtualFile file, Module module) { + DirectoryInfo info = myIndex.getInfoForFile(file); + assertTrue(info.toString(), info.isExcluded(file)); + assertEquals(module, info.getModule()); + } + + protected DirectoryInfo assertInProject(VirtualFile file) { + DirectoryInfo info = myIndex.getInfoForFile(file); + assertTrue(file.toString(), info.isInProject(file)); + myIndex.assertConsistency(info); + return info; + } + + protected void assertNotExcluded(VirtualFile file) { + assertFalse(myIndex.getInfoForFile(file).isExcluded(file)); + } + + protected void assertExcludedFromProject(VirtualFile file) { + assertExcluded(file, null); + } + + protected void assertIteratedContent(Module module, @Nullable List contains, @Nullable List doesntContain) { + assertIteratedContent(ModuleRootManager.getInstance(module).getFileIndex(), contains, doesntContain); + assertIteratedContent(myFileIndex, contains, doesntContain); + } + + protected static void assertIteratedContent(FileIndex fileIndex, + @Nullable List contains, + @Nullable List doesntContain) { + final Set collected = new THashSet<>(); + fileIndex.iterateContent(fileOrDir -> { + if (!collected.add(fileOrDir)) { + fail(fileOrDir + " visited twice"); + } + return true; + }); + if (contains != null) assertContainsElements(collected, contains); + if (doesntContain != null) assertDoesntContain(collected, doesntContain); + } +}