Skip to content

Enabling action and TestNg version depends on the project SDK #1209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ val ExternalLibraryDescriptor.mavenCoordinates: String
get() = "$libraryGroupId:$libraryArtifactId:${preferredVersion ?: RepositoryLibraryDescription.ReleaseVersionId}"

val ExternalLibraryDescriptor.id: String
get() = "$libraryGroupId:$libraryArtifactId"
get() = "$libraryGroupId:$libraryArtifactId"

//TODO: think about using JUnitExternalLibraryDescriptor from intellij-community sources (difficult to install)
fun jUnit4LibraryDescriptor(versionInProject: String?) =
Expand All @@ -16,11 +16,20 @@ fun jUnit4LibraryDescriptor(versionInProject: String?) =
fun jUnit5LibraryDescriptor(versionInProject: String?) =
ExternalLibraryDescriptor("org.junit.jupiter", "junit-jupiter", "5.8.1", null, versionInProject ?: "5.8.1")

fun testNgLibraryDescriptor(versionInProject: String?) =
ExternalLibraryDescriptor("org.testng", "testng", "7.6.0", null, versionInProject ?: "7.6.0")

fun jUnit5ParametrizedTestsLibraryDescriptor(versionInProject: String?) =
ExternalLibraryDescriptor("org.junit.jupiter", "junit-jupiter-params", "5.8.1", null, versionInProject ?: "5.8.1")

fun mockitoCoreLibraryDescriptor(versionInProject: String?) =
ExternalLibraryDescriptor("org.mockito", "mockito-core", "3.5.0", "4.2.0", versionInProject ?: "4.2.0")
ExternalLibraryDescriptor("org.mockito", "mockito-core", "3.5.0", "4.2.0", versionInProject ?: "4.2.0")

/**
* TestNg requires JDK 11 since version 7.6.0
* For projects with JDK 8 version 7.5.0 should be installed.
* See https://groups.google.com/g/testng-users/c/BAFB1vk-kok?pli=1 for more details.
*/

fun testNgNewLibraryDescriptor(versionInProject: String?) =
ExternalLibraryDescriptor("org.testng", "testng", "7.6.0", null, versionInProject ?: "7.6.0")

fun testNgOldLibraryDescriptor() =
ExternalLibraryDescriptor("org.testng", "testng", "7.5.0", "7.5.0", "7.5.0")
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ data class GenerateTestsModel(
}
var runGeneratedTestsWithCoverage : Boolean = false
var enableSummariesGeneration : Boolean = true

val jdkVersion: JavaSdkVersion?
get() = try {
testModule.jdkVersion()
} catch (e: IllegalStateException) {
// Just ignore it here, notification will be shown in org.utbot.intellij.plugin.ui.utils.ModuleUtilsKt.jdkVersionBy
null
}
}

val PsiClass.packageName: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ import org.utbot.intellij.plugin.models.jUnit5LibraryDescriptor
import org.utbot.intellij.plugin.models.jUnit5ParametrizedTestsLibraryDescriptor
import org.utbot.intellij.plugin.models.mockitoCoreLibraryDescriptor
import org.utbot.intellij.plugin.models.packageName
import org.utbot.intellij.plugin.models.testNgLibraryDescriptor
import org.utbot.intellij.plugin.models.testNgNewLibraryDescriptor
import org.utbot.intellij.plugin.models.testNgOldLibraryDescriptor
import org.utbot.intellij.plugin.settings.Settings
import org.utbot.intellij.plugin.ui.components.CodeGenerationSettingItemRenderer
import org.utbot.intellij.plugin.ui.components.TestFolderComboWithBrowseButton
Expand All @@ -140,6 +141,7 @@ import org.utbot.intellij.plugin.ui.utils.testResourceRootTypes
import org.utbot.intellij.plugin.ui.utils.testRootType
import org.utbot.intellij.plugin.util.IntelliJApiHelper
import org.utbot.intellij.plugin.util.extractFirstLevelMembers
import org.utbot.intellij.plugin.util.findSdkVersion

private const val RECENTS_KEY = "org.utbot.recents"

Expand Down Expand Up @@ -314,15 +316,10 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
label?.let { add(it, BorderLayout.LINE_END) }
})

private fun findSdkVersion(): JavaVersion? {
val projectSdk = ModuleRootManager.getInstance(model.srcModule).sdk
return JavaVersion.tryParse(projectSdk?.versionString)
}

override fun createTitlePane(): JComponent? {
val sdkVersion = findSdkVersion()
val sdkVersion = findSdkVersion(model.srcModule)
//TODO:SAT-1571 investigate Android Studio specific sdk issues
if (sdkVersion?.feature in minSupportedSdkVersion..maxSupportedSdkVersion || IntelliJApiHelper.isAndroidStudio()) return null
if (sdkVersion.feature in minSupportedSdkVersion..maxSupportedSdkVersion || IntelliJApiHelper.isAndroidStudio()) return null
isOKActionEnabled = false
return SdkNotificationPanel(model, sdkVersion)
}
Expand Down Expand Up @@ -372,8 +369,8 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
val isEdited = ShowSettingsUtil.getInstance().editConfigurable(model.project, projectStructure)
{ projectStructure.select(model.srcModule.name, ClasspathEditor.getName(), true) }

val sdkVersion = findSdkVersion()
val sdkFixed = isEdited && sdkVersion?.feature in minSupportedSdkVersion..maxSupportedSdkVersion
val sdkVersion = findSdkVersion(model.srcModule)
val sdkFixed = isEdited && sdkVersion.feature in minSupportedSdkVersion..maxSupportedSdkVersion
if (sdkFixed) {
this@SdkNotificationPanel.isVisible = false
isOKActionEnabled = true
Expand Down Expand Up @@ -721,11 +718,15 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
val libraryInProject =
findFrameworkLibrary(model.project, model.testModule, selectedTestFramework, LibrarySearchScope.Project)
val versionInProject = libraryInProject?.libraryName?.parseVersion()
val sdkVersion = findSdkVersion(model.srcModule).feature

val libraryDescriptor = when (selectedTestFramework) {
Junit4 -> jUnit4LibraryDescriptor(versionInProject)
Junit5 -> jUnit5LibraryDescriptor(versionInProject)
TestNg -> testNgLibraryDescriptor(versionInProject)
TestNg -> when (sdkVersion) {
minSupportedSdkVersion -> testNgOldLibraryDescriptor()
else -> testNgNewLibraryDescriptor(versionInProject)
}
}

selectedTestFramework.isInstalled = true
Expand Down Expand Up @@ -956,12 +957,6 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
ParametrizedTestSource.PARAMETRIZE -> TestFramework.allItems.filterNot { it == Junit4 }
}

//Will be removed after gradle-intelij-plugin version update upper than 2020.2
//TestNg will be reverted after https://github.com/UnitTestBot/UTBotJava/issues/309
if (findSdkVersion()?.let { it.feature < 11 } == true) {
enabledTestFrameworks = enabledTestFrameworks.filterNot { it == TestNg }
}

var defaultItem = when (parametrizedTestSource) {
ParametrizedTestSource.DO_NOT_PARAMETRIZE -> TestFramework.defaultItem
ParametrizedTestSource.PARAMETRIZE -> TestFramework.parametrizedDefaultItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.intellij.refactoring.util.classMembers.MemberInfo
import org.jetbrains.kotlin.idea.core.getPackage
import org.jetbrains.kotlin.idea.core.util.toPsiDirectory
import org.jetbrains.kotlin.idea.core.util.toPsiFile
import org.jetbrains.kotlin.idea.util.module
import org.utbot.intellij.plugin.util.extractFirstLevelMembers
import org.utbot.intellij.plugin.util.isVisible
import java.util.*
Expand All @@ -29,6 +30,7 @@ import org.jetbrains.kotlin.utils.addIfNotNull
import org.utbot.framework.plugin.api.util.LockFile
import org.utbot.intellij.plugin.models.packageName
import org.utbot.intellij.plugin.ui.InvalidClassNotifier
import org.utbot.intellij.plugin.util.findSdkVersionOrNull
import org.utbot.intellij.plugin.util.isAbstract

class GenerateTestsAction : AnAction(), UpdateInBackground {
Expand Down Expand Up @@ -164,6 +166,11 @@ class GenerateTestsAction : AnAction(), UpdateInBackground {
}

private fun PsiClass.isInvalid(withWarnings: Boolean): Boolean {
if (this.module?.let { findSdkVersionOrNull(it) } == null) {
if (withWarnings) InvalidClassNotifier.notify("class out of module or with undefined SDK")
return true
}

val isAbstractOrInterface = this.isInterface || this.isAbstract
if (isAbstractOrInterface) {
if (withWarnings) InvalidClassNotifier.notify("abstract class or interface ${this.name}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.utbot.intellij.plugin.util

import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.util.lang.JavaVersion

fun findSdkVersion(module:Module): JavaVersion =
findSdkVersionOrNull(module) ?: error("Cannot define sdk version in module $module")

fun findSdkVersionOrNull(module: Module): JavaVersion? {
val moduleSdk = ModuleRootManager.getInstance(module).sdk
return JavaVersion.tryParse(moduleSdk?.versionString)
}