-
Notifications
You must be signed in to change notification settings - Fork 46
Introduce and test UtBotSpringApi
#2632
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
Changes from all commits
ec220e7
15774aa
15c8b65
4058766
3a9cd46
c269a2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.utbot.external.api | ||
|
||
import org.utbot.framework.context.ApplicationContext | ||
import org.utbot.framework.context.simple.SimpleApplicationContext | ||
import org.utbot.framework.context.spring.SpringApplicationContext | ||
import org.utbot.framework.context.spring.SpringApplicationContextImpl | ||
import org.utbot.framework.plugin.api.SpringConfiguration | ||
import org.utbot.framework.plugin.api.SpringSettings | ||
import org.utbot.framework.plugin.api.SpringTestType | ||
import org.utbot.framework.process.SpringAnalyzerTask | ||
import java.io.File | ||
|
||
object UtBotSpringApi { | ||
private val springBootConfigAnnotations = setOf( | ||
"org.springframework.boot.autoconfigure.SpringBootApplication", | ||
"org.springframework.boot.SpringBootConfiguration" | ||
) | ||
|
||
/** | ||
* NOTE: [classpath] should include project under test classpath (with all dependencies) as well as | ||
* `spring-test`, `spring-boot-test`, and `spring-security-test` if respectively `spring-beans`, | ||
* `spring-boot`, and `spring-security-core` are dependencies of project under test. | ||
* | ||
* UtBot doesn't add Spring test modules to classpath automatically to let API users control their versions. | ||
*/ | ||
@JvmOverloads | ||
@JvmStatic | ||
fun createSpringApplicationContext( | ||
springSettings: SpringSettings, | ||
springTestType: SpringTestType, | ||
classpath: List<String>, | ||
delegateContext: ApplicationContext = SimpleApplicationContext() | ||
): SpringApplicationContext { | ||
if (springTestType == SpringTestType.INTEGRATION_TEST) { | ||
require(springSettings is SpringSettings.PresentSpringSettings) { | ||
"Integration tests can't be generated without Spring settings" | ||
} | ||
val configuration = springSettings.configuration | ||
require(configuration !is SpringConfiguration.XMLConfiguration) { | ||
"Integration tests aren't supported for XML configurations, consider using Java " + | ||
"configuration that imports your XML configuration with @ImportResource" | ||
} | ||
} | ||
return SpringApplicationContextImpl.internalCreate( | ||
delegateContext = delegateContext, | ||
beanDefinitions = when (springSettings) { | ||
SpringSettings.AbsentSpringSettings -> listOf() | ||
is SpringSettings.PresentSpringSettings -> SpringAnalyzerTask(classpath, springSettings).perform() | ||
}, | ||
springTestType = springTestType, | ||
springSettings = springSettings, | ||
) | ||
} | ||
|
||
@JvmStatic | ||
fun createXmlSpringConfiguration(xmlConfig: File): SpringConfiguration.XMLConfiguration = | ||
SpringConfiguration.XMLConfiguration(xmlConfig.absolutePath) | ||
|
||
@JvmStatic | ||
fun createJavaSpringConfiguration(javaConfig: Class<*>): SpringConfiguration.JavaBasedConfiguration = | ||
if (javaConfig.annotations.any { it.annotationClass.java.name in springBootConfigAnnotations }) { | ||
SpringConfiguration.SpringBootConfiguration(javaConfig.name, isDefinitelyUnique = false) | ||
} else { | ||
SpringConfiguration.JavaConfiguration(javaConfig.name) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer | ||
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer | ||
|
||
plugins { | ||
id("com.github.johnrengelman.shadow") version "7.1.2" | ||
id("java") | ||
} | ||
|
||
val springBootVersion: String by rootProject | ||
|
||
dependencies { | ||
implementation("org.projectlombok:lombok:1.18.20") | ||
annotationProcessor("org.projectlombok:lombok:1.18.20") | ||
|
||
implementation(group = "org.springframework.boot", name = "spring-boot-starter-web", version = springBootVersion) | ||
implementation(group = "org.springframework.boot", name = "spring-boot-starter-data-jpa", version = springBootVersion) | ||
implementation(group = "org.springframework.boot", name = "spring-boot-starter-test", version = springBootVersion) | ||
} | ||
|
||
tasks.shadowJar { | ||
isZip64 = true | ||
|
||
transform(Log4j2PluginsCacheFileTransformer::class.java) | ||
archiveFileName.set("utbot-spring-sample-shadow.jar") | ||
|
||
// Required for Spring to run properly when using shadowJar | ||
// More details: https://github.com/spring-projects/spring-boot/issues/1828 | ||
mergeServiceFiles() | ||
append("META-INF/spring.handlers") | ||
append("META-INF/spring.schemas") | ||
append("META-INF/spring.tooling") | ||
transform(PropertiesFileTransformer().apply { | ||
paths = listOf("META-INF/spring.factories") | ||
mergeStrategy = "append" | ||
}) | ||
} | ||
|
||
val springSampleJar: Configuration by configurations.creating { | ||
isCanBeResolved = false | ||
isCanBeConsumed = true | ||
} | ||
|
||
artifacts { | ||
add(springSampleJar.name, tasks.shadowJar) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.utbot.examples.spring.config.boot; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class ExampleSpringBootConfig { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.utbot.examples.spring.config.boot; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.utbot.examples.spring.config.utils.SafetyUtils; | ||
|
||
@Service | ||
public class ExampleSpringBootService { | ||
public ExampleSpringBootService() { | ||
SafetyUtils.shouldNeverBeCalled(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.utbot.examples.spring.config.pure; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Profile; | ||
import org.utbot.examples.spring.config.utils.SafetyUtils; | ||
|
||
public class ExamplePureSpringConfig { | ||
@Bean(name = "exampleService0") | ||
public ExamplePureSpringService exampleService() { | ||
SafetyUtils.shouldNeverBeCalled(); | ||
return null; | ||
} | ||
|
||
@Bean(name = "exampleServiceTest1") | ||
@Profile("test1") | ||
public ExamplePureSpringService exampleServiceTest1() { | ||
SafetyUtils.shouldNeverBeCalled(); | ||
return null; | ||
} | ||
|
||
@Bean(name = "exampleServiceTest2") | ||
@Profile("test2") | ||
public ExamplePureSpringService exampleServiceTest2() { | ||
SafetyUtils.shouldNeverBeCalled(); | ||
return null; | ||
} | ||
|
||
@Bean(name = "exampleServiceTest3") | ||
@Profile("test3") | ||
public ExamplePureSpringService exampleServiceTest3() { | ||
SafetyUtils.shouldNeverBeCalled(); | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.utbot.examples.spring.config.pure; | ||
|
||
public class ExamplePureSpringService { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.utbot.examples.spring.config.utils; | ||
|
||
public class SafetyUtils { | ||
|
||
private static final int UNEXPECTED_CALL_EXIT_STATUS = -1182; | ||
|
||
/** | ||
* Bean constructors and factory methods should never be executed during bean analysis, | ||
* hence call to this method is added into them to ensure they are actually never called. | ||
*/ | ||
public static void shouldNeverBeCalled() { | ||
System.err.println("shouldNeverBeCalled() is unexpectedly called"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will we see this message in log on CI? If yes, maybe stack trace would be more useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I doubt we will see it on CI. Spring analyzer runs in a separate process and similarly to instrumented process it uses RD logger to send logs to engine process which can actually print them. However, RD logger isn't configured in the sample Spring project. So if these tests start failing due to Spring analyzer initializing beans it may take some time to debug them and find the problem, but it's unlikely, so I think it's not worth configuring RD logger in sample Spring project now. |
||
System.exit(UNEXPECTED_CALL_EXIT_STATUS); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.utbot.examples.spring.config.xml; | ||
|
||
import org.utbot.examples.spring.config.utils.SafetyUtils; | ||
|
||
public class ExampleXmlService { | ||
public ExampleXmlService() { | ||
SafetyUtils.shouldNeverBeCalled(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" | ||
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"> | ||
|
||
<beans> | ||
|
||
<bean id="xmlService" class="org.utbot.examples.spring.config.xml.ExampleXmlService"/> | ||
|
||
</beans> |
Uh oh!
There was an error while loading. Please reload this page.