|
1 | 1 | package org.utbot.intellij.plugin.models
|
2 | 2 |
|
3 | 3 | import com.intellij.openapi.module.Module
|
| 4 | +import com.intellij.openapi.module.ModuleManager |
4 | 5 | import com.intellij.openapi.module.ModuleUtil
|
5 | 6 | import com.intellij.openapi.project.Project
|
| 7 | +import com.intellij.openapi.roots.TestModuleProperties |
6 | 8 | import com.intellij.openapi.vfs.VirtualFile
|
7 | 9 | import com.intellij.openapi.vfs.newvfs.impl.FakeVirtualFile
|
| 10 | +import com.intellij.psi.JavaPsiFacade |
8 | 11 | import com.intellij.psi.PsiClass
|
| 12 | +import com.intellij.psi.search.GlobalSearchScope |
| 13 | +import com.intellij.psi.search.ProjectScope |
| 14 | +import com.intellij.psi.search.searches.AnnotatedElementsSearch |
9 | 15 | import org.jetbrains.kotlin.idea.core.getPackage
|
| 16 | +import org.jetbrains.kotlin.idea.search.allScope |
10 | 17 | import org.jetbrains.kotlin.idea.util.projectStructure.allModules
|
| 18 | +import org.jetbrains.kotlin.idea.util.rootManager |
| 19 | +import org.jetbrains.kotlin.idea.util.sourceRoot |
11 | 20 | import org.utbot.framework.plugin.api.CodegenLanguage
|
12 | 21 | import org.utbot.intellij.plugin.ui.utils.ITestSourceRoot
|
| 22 | +import org.utbot.intellij.plugin.ui.utils.getSortedTestRoots |
13 | 23 | import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
|
14 | 24 | import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots
|
15 | 25 |
|
@@ -56,6 +66,51 @@ open class BaseTestsModel(
|
56 | 66 | }
|
57 | 67 | }
|
58 | 68 |
|
| 69 | + fun getSortedTestRoots(): MutableList<ITestSourceRoot> = getSortedTestRoots( |
| 70 | + getAllTestSourceRoots(), |
| 71 | + sourceRootHistory, |
| 72 | + srcModule.rootManager.sourceRoots.map { file: VirtualFile -> file.toNioPath().toString() }, |
| 73 | + codegenLanguage |
| 74 | + ) |
| 75 | + |
| 76 | + /** |
| 77 | + * Searches configuration classes in Spring application. |
| 78 | + * |
| 79 | + * Classes are selected and sorted in the following order: |
| 80 | + * - Classes marked with `@TestConfiguration` annotation |
| 81 | + * - Classes marked with `@Configuration` annotation |
| 82 | + * - firstly, from test source roots (in the order provided by [getSortedTestRoots]) |
| 83 | + * - after that, from source roots |
| 84 | + */ |
| 85 | + fun getSortedSpringConfigurationClasses(): List<PsiClass> { |
| 86 | + val testRootToIndex = getSortedTestRoots().withIndex().associate { (i, root) -> root.dir to i } |
| 87 | + |
| 88 | + // Not using `srcModule.testModules(project)` here because it returns |
| 89 | + // test modules for dependent modules if no test roots are found in the source module itself. |
| 90 | + // We don't want to search configurations there because they seem useless. |
| 91 | + val testModules = ModuleManager.getInstance(project) |
| 92 | + .modules |
| 93 | + .filter { module -> TestModuleProperties.getInstance(module).productionModule == srcModule } |
| 94 | + |
| 95 | + val searchScope = testModules.fold(GlobalSearchScope.moduleScope(srcModule)) { accScope, module -> |
| 96 | + accScope.union(GlobalSearchScope.moduleScope(module)) |
| 97 | + } |
| 98 | + |
| 99 | + val annotationClasses = listOf( |
| 100 | + "org.springframework.boot.test.context.TestConfiguration", |
| 101 | + "org.springframework.context.annotation.Configuration" |
| 102 | + ).mapNotNull { |
| 103 | + JavaPsiFacade.getInstance(project).findClass(it, project.allScope()) |
| 104 | + } |
| 105 | + |
| 106 | + return annotationClasses.flatMap { annotation -> |
| 107 | + AnnotatedElementsSearch |
| 108 | + .searchPsiClasses(annotation, searchScope) |
| 109 | + .findAll() |
| 110 | + .sortedBy { testRootToIndex[it.containingFile.sourceRoot] ?: Int.MAX_VALUE } |
| 111 | + } |
| 112 | + } |
| 113 | + |
59 | 114 | fun updateSourceRootHistory(path: String) {
|
60 | 115 | sourceRootHistory.apply {
|
61 | 116 | remove(path)//Remove existing entry if any
|
|
0 commit comments