Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Transactional and AutoConfigureTestDatabase annotations
  • Loading branch information
EgorkaKulikov committed Jun 26, 2023
commit be19ce0470599ea2a82a91e09fb77b532b6a4b47
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.utbot.framework.plugin.api.util

import org.utbot.framework.plugin.api.BuiltinClassId
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.MethodId
import org.utbot.framework.plugin.api.SpringRepositoryId
Expand All @@ -18,7 +17,9 @@ object SpringModelUtils {

val springBootTestClassId = ClassId("org.springframework.boot.test.context.SpringBootTest")
val dirtiesContextClassId = ClassId("org.springframework.test.annotation.DirtiesContext")
val dirtiesContextClassModeClassId = ClassId("org.springframework.test.annotation.DirtiesContext.ClassMode")
val dirtiesContextClassModeClassId = ClassId("org.springframework.test.annotation.DirtiesContext\$ClassMode")
val transactionalClassId = ClassId("org.springframework.transaction.annotation.Transactional")
val autoConfigureTestDbClassId = ClassId("org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase")

// most likely only one persistent library is on the classpath, but we need to be able to work with either of them
private val persistentLibraries = listOf("javax.persistence", "jakarta.persistence")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.utbot.framework.codegen.tree

import org.utbot.common.tryLoadClass
import org.utbot.framework.codegen.domain.context.CgContext
import org.utbot.framework.codegen.domain.models.CgClass
import org.utbot.framework.codegen.domain.models.CgEnumConstantAccess
import org.utbot.framework.codegen.domain.models.CgFieldDeclaration
import org.utbot.framework.codegen.domain.models.CgMethodsCluster
import org.utbot.framework.codegen.domain.models.SpringTestClassModel
import org.utbot.framework.plugin.api.UtSpringContextModel
import org.utbot.framework.codegen.domain.models.*
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.util.SpringModelUtils.autoConfigureTestDbClassId
import org.utbot.framework.plugin.api.util.SpringModelUtils.autowiredClassId
import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassId
import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassModeClassId
import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestClassId
import org.utbot.framework.plugin.api.util.SpringModelUtils.transactionalClassId
import org.utbot.framework.plugin.api.util.utContext

class CgSpringIntegrationTestClassConstructor(context: CgContext) : CgAbstractSpringTestClassConstructor(context) {
override fun constructTestClass(testClassModel: SpringTestClassModel): CgClass {
Expand All @@ -20,13 +20,7 @@ class CgSpringIntegrationTestClassConstructor(context: CgContext) : CgAbstractSp
body = constructTestClassBody(testClassModel)

with (currentTestClassContext) {
annotations += statementConstructor.annotation(springBootTestClassId)
annotations += statementConstructor.annotation(
classId = dirtiesContextClassId,
namedArguments = listOf(
"classMode" to CgEnumConstantAccess(dirtiesContextClassModeClassId, "BEFORE_EACH_TEST_METHOD")
)
)
annotations += collectSpringSpecificAnnotations()
annotations += collectedTestClassAnnotations

superclass = testClassSuperclass
Expand All @@ -40,8 +34,26 @@ class CgSpringIntegrationTestClassConstructor(context: CgContext) : CgAbstractSp
return constructFieldsWithAnnotation(autowiredClassId, autowiredFromContextModels)
}

override fun constructAdditionalMethods() = CgMethodsCluster(
header = null,
content = emptyList(),
)
override fun constructAdditionalMethods() = CgMethodsCluster(header = null, content = emptyList())

private fun collectSpringSpecificAnnotations(): List<CgAnnotation> {
val annotations = mutableListOf<CgAnnotation>()

annotations += statementConstructor.annotation(springBootTestClassId)
annotations += statementConstructor.annotation(
classId = dirtiesContextClassId,
namedArguments = listOf(
"classMode" to CgEnumConstantAccess(dirtiesContextClassModeClassId, "BEFORE_EACH_TEST_METHOD")
)
)

listOf(transactionalClassId, autoConfigureTestDbClassId)
.filter { annotationTypeIsAccessible(it) }
.forEach { annotations += statementConstructor.annotation(it) }

return annotations
}

private fun annotationTypeIsAccessible(annotationType: ClassId): Boolean =
utContext.classLoader.tryLoadClass(annotationType.name) != null
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.utbot.framework.codegen.tree

import org.utbot.framework.codegen.domain.UtModelWrapper
import org.utbot.framework.codegen.domain.builtin.autowiredClassId
import org.utbot.framework.codegen.domain.builtin.injectMocksClassId
import org.utbot.framework.codegen.domain.builtin.mockClassId
import org.utbot.framework.codegen.domain.context.CgContext
Expand All @@ -14,6 +13,7 @@ import org.utbot.framework.plugin.api.UtCompositeModel
import org.utbot.framework.plugin.api.UtModel
import org.utbot.framework.plugin.api.UtSpringContextModel
import org.utbot.framework.plugin.api.isMockModel
import org.utbot.framework.plugin.api.util.SpringModelUtils.autowiredClassId
import org.utbot.framework.plugin.api.util.SpringModelUtils.isAutowiredFromContext
import org.utbot.framework.plugin.api.util.stringClassId

Expand Down