Skip to content

Add utbot-spring-commons-shadow.jar to engine process utContext #2543

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
Aug 23, 2023
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
6 changes: 6 additions & 0 deletions utbot-framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

configurations {
fetchSpringAnalyzerJar
fetchSpringCommonsJar
fetchInstrumentationJar
}

Expand Down Expand Up @@ -51,6 +52,7 @@ dependencies {
implementation project(':utbot-spring-commons-api')
implementation project(':utbot-spring-analyzer')
fetchSpringAnalyzerJar project(path: ':utbot-spring-analyzer', configuration: 'springAnalyzerJar')
fetchSpringCommonsJar project(path: ':utbot-spring-commons', configuration: 'springCommonsJar')
}

processResources {
Expand All @@ -61,4 +63,8 @@ processResources {
from(configurations.fetchSpringAnalyzerJar) {
into "lib"
}

from(configurations.fetchSpringCommonsJar) {
into "lib"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.utbot.instrumentation.instrumentation.execution.context.Instrumentati
import org.utbot.instrumentation.instrumentation.execution.phases.ExecutionPhaseFailingOnAnyException
import org.utbot.instrumentation.instrumentation.execution.phases.PhasesController
import org.utbot.spring.api.SpringApi
import java.io.File
import java.net.URL
import java.net.URLClassLoader
import java.security.ProtectionDomain
Expand Down Expand Up @@ -50,6 +51,14 @@ class SpringUtExecutionInstrumentation(
companion object {
private val logger = getLogger<SpringUtExecutionInstrumentation>()
private const val SPRING_COMMONS_JAR_FILENAME = "utbot-spring-commons-shadow.jar"

val springCommonsJar: File by lazy {
JarUtils.extractJarFileFromResources(
jarFileName = SPRING_COMMONS_JAR_FILENAME,
jarResourcePath = "lib/$SPRING_COMMONS_JAR_FILENAME",
targetDirectoryName = "spring-commons"
)
}
}

fun tryLoadingSpringContext(): ConcreteContextLoadingResult {
Expand Down Expand Up @@ -147,11 +156,7 @@ class SpringUtExecutionInstrumentation(
private val buildDirs: Array<URL>,
) : UtExecutionInstrumentation.Factory<SpringUtExecutionInstrumentation> {
override val additionalRuntimeClasspath: Set<String>
get() = super.additionalRuntimeClasspath + JarUtils.extractJarFileFromResources(
jarFileName = SPRING_COMMONS_JAR_FILENAME,
jarResourcePath = "lib/$SPRING_COMMONS_JAR_FILENAME",
targetDirectoryName = "spring-commons"
).path
get() = super.additionalRuntimeClasspath + springCommonsJar.path

// TODO may be we can use some alternative sandbox that has more permissions
// (at the very least we need `ReflectPermission("suppressAccessChecks")`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import org.utbot.framework.plugin.api.util.LockFile
import org.utbot.framework.plugin.api.util.withStaticsSubstitutionRequired
import org.utbot.framework.plugin.services.JdkInfoService
import org.utbot.framework.plugin.services.WorkingDirService
import org.utbot.instrumentation.instrumentation.spring.SpringUtExecutionInstrumentation
import org.utbot.intellij.plugin.generator.CodeGenerationController.generateTests
import org.utbot.intellij.plugin.models.GenerateTestsModel
import org.utbot.intellij.plugin.process.EngineProcess
Expand Down Expand Up @@ -261,7 +262,11 @@ object UtTestsDialogProcessor {
val process = EngineProcess.createBlocking(project, classNameToPath)

process.terminateOnException { _ ->
val classpathForClassLoader = buildDirs + classpathList
val classpathForClassLoader = buildDirs + classpathList + when (model.projectType) {
Spring -> listOf(SpringUtExecutionInstrumentation.springCommonsJar.path)
else -> emptyList<String>()
}

process.setupUtContext(classpathForClassLoader)
val simpleApplicationContext = SimpleApplicationContext(
SimpleMockerContext(mockFrameworkInstalled, staticMockingConfigured)
Expand Down
13 changes: 9 additions & 4 deletions utbot-spring-commons/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ java {

dependencies {
implementation(project(":utbot-spring-commons-api"))
implementation(project(":utbot-core"))

// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot

// these dependencies are compilieOnly, because they should
// already be present on the classpath in all utbot processes
compileOnly(project(":utbot-core"))
compileOnly("com.jetbrains.rd:rd-core:$rdVersion") { exclude(group = "org.slf4j", module = "slf4j-api") }


// these dependencies are compilieOnly, because they should
// be picked up from user classpath if they are present there
compileOnly("org.springframework.boot:spring-boot:$springBootVersion")
compileOnly("org.springframework.boot:spring-boot-test-autoconfigure:$springBootVersion")
compileOnly("org.springframework:spring-test:$springVersion")
Expand All @@ -32,8 +39,6 @@ dependencies {

compileOnly("javax.persistence:javax.persistence-api:$javaxVersion")
compileOnly("jakarta.persistence:jakarta.persistence-api:$jakartaVersion")

implementation("com.jetbrains.rd:rd-core:$rdVersion") { exclude(group = "org.slf4j", module = "slf4j-api") }
}

tasks.shadowJar {
Expand Down