-
Notifications
You must be signed in to change notification settings - Fork 45
Tests for the utbot maven plugin #241
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
utbot-maven/src/test/kotlin/org/utbot/maven/plugin/GenerateTestsAndSarifReportMojoTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package org.utbot.maven.plugin | ||
|
|
||
| import org.apache.maven.plugin.testing.AbstractMojoTestCase | ||
| import org.apache.maven.project.MavenProject | ||
| import org.junit.jupiter.api.* | ||
| import org.utbot.common.PathUtil.toPath | ||
| import org.utbot.framework.util.GeneratedSarif | ||
| import java.io.File | ||
|
|
||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| class GenerateTestsAndSarifReportMojoTest : AbstractMojoTestCase() { | ||
|
|
||
| @BeforeAll | ||
| override fun setUp() { | ||
| super.setUp() | ||
| } | ||
|
|
||
| @AfterAll | ||
| override fun tearDown() { | ||
| super.tearDown() | ||
| } | ||
|
|
||
| @Test | ||
| fun `test directory exists and not empty`() { | ||
| val testsRelativePath = sarifReportMojo.sarifProperties.generatedTestsRelativeRoot | ||
| val testDirectory = testMavenProject.projectBaseDir.resolve(testsRelativePath) | ||
| assert(directoryExistsAndNotEmpty(testDirectory)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sarif directory exists and not empty`() { | ||
| val reportsRelativePath = sarifReportMojo.sarifProperties.sarifReportsRelativeRoot | ||
| val sarifDirectory = testMavenProject.projectBaseDir.resolve(reportsRelativePath) | ||
| assert(directoryExistsAndNotEmpty(sarifDirectory)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sarif report contains all required results`() { | ||
| val sarifReportFile = sarifReportMojo.rootMavenProjectWrapper.sarifReportFile | ||
| val sarifReportText = sarifReportFile.readText() | ||
| GeneratedSarif(sarifReportText).apply { | ||
| assert(hasSchema()) | ||
| assert(hasVersion()) | ||
| assert(hasRules()) | ||
| assert(hasResults()) | ||
| assert(hasCodeFlows()) | ||
| assert(codeFlowsIsNotEmpty()) | ||
| assert(contains("ArithmeticException")) | ||
| } | ||
| } | ||
|
|
||
| // internal | ||
|
|
||
| private val testMavenProject: TestMavenProject = | ||
| TestMavenProject("src/test/resources/project-to-test".toPath()) | ||
|
|
||
| private val sarifReportMojo by lazy { | ||
| configureSarifReportMojo(testMavenProject.mavenProject).apply { | ||
| this.execute() | ||
| } | ||
| } | ||
|
|
||
| private fun configureSarifReportMojo(mavenProject: MavenProject): GenerateTestsAndSarifReportMojo { | ||
| val generateTestsAndSarifReportMojo = configureMojo( | ||
| GenerateTestsAndSarifReportMojo(), "utbot-maven", mavenProject.file | ||
| ) as GenerateTestsAndSarifReportMojo | ||
| generateTestsAndSarifReportMojo.mavenProject = mavenProject | ||
| return generateTestsAndSarifReportMojo | ||
| } | ||
|
|
||
| private fun directoryExistsAndNotEmpty(directory: File): Boolean = | ||
| directory.exists() && directory.isDirectory && directory.list()?.isNotEmpty() == true | ||
| } |
38 changes: 38 additions & 0 deletions
38
utbot-maven/src/test/kotlin/org/utbot/maven/plugin/TestMavenProject.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package org.utbot.maven.plugin | ||
|
|
||
| import org.apache.commons.lang3.reflect.FieldUtils | ||
| import org.apache.maven.model.io.xpp3.MavenXpp3Reader | ||
| import org.apache.maven.project.MavenProject | ||
| import org.codehaus.plexus.util.FileUtils | ||
| import java.io.File | ||
| import java.io.FileReader | ||
| import java.nio.file.Path | ||
|
|
||
| /** | ||
| * Wrapper for the maven project stored in the test resources. | ||
| */ | ||
| class TestMavenProject(pathToProject: Path) { | ||
|
|
||
| /** | ||
| * Path to the copied maven project. | ||
| */ | ||
| val projectBaseDir = | ||
| File("build/resources/${pathToProject.fileName}") | ||
|
|
||
| init { | ||
| projectBaseDir.deleteRecursively() | ||
| // copying the project to the build directory to change it there | ||
| FileUtils.copyDirectoryStructure(pathToProject.toFile(), projectBaseDir) | ||
| } | ||
|
|
||
| val mavenProject: MavenProject = run { | ||
| val pomFile = File(projectBaseDir, "pom.xml") | ||
| val model = MavenXpp3Reader().read(FileReader(pomFile)) | ||
| val mavenProject = MavenProject(model) | ||
| mavenProject.setPomFile(pomFile) | ||
| mavenProject.collectedProjects = listOf() // no child modules | ||
| mavenProject.addCompileSourceRoot(mavenProject.build.sourceDirectory) | ||
| FieldUtils.writeField(mavenProject, "basedir", projectBaseDir, true) | ||
| mavenProject | ||
| } | ||
| } |
119 changes: 119 additions & 0 deletions
119
...n/src/test/kotlin/org/utbot/maven/plugin/extension/SarifMavenConfigurationProviderTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package org.utbot.maven.plugin.extension | ||
|
|
||
| import org.apache.maven.plugin.testing.AbstractMojoTestCase | ||
| import org.apache.maven.project.MavenProject | ||
| import org.junit.jupiter.api.* | ||
| import org.utbot.common.PathUtil.toPath | ||
| import org.utbot.engine.Mocker | ||
| import org.utbot.framework.codegen.* | ||
| import org.utbot.framework.plugin.api.ClassId | ||
| import org.utbot.framework.plugin.api.CodegenLanguage | ||
| import org.utbot.framework.plugin.api.MockFramework | ||
| import org.utbot.framework.plugin.api.MockStrategyApi | ||
| import org.utbot.maven.plugin.GenerateTestsAndSarifReportMojo | ||
| import org.utbot.maven.plugin.TestMavenProject | ||
| import java.io.File | ||
|
|
||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| class SarifMavenConfigurationProviderTest : AbstractMojoTestCase() { | ||
|
|
||
| @BeforeAll | ||
| override fun setUp() { | ||
| super.setUp() | ||
| } | ||
|
|
||
| @AfterAll | ||
| override fun tearDown() { | ||
| super.tearDown() | ||
| } | ||
|
|
||
| @Test | ||
| fun `targetClasses should be provided from the configuration`() { | ||
| Assertions.assertEquals(listOf("Main"), configurationProvider.targetClasses) | ||
| } | ||
|
|
||
| @Test | ||
| fun `projectRoot should be provided from the configuration`() { | ||
| Assertions.assertEquals(File("build/resources/project-to-test"), configurationProvider.projectRoot) | ||
| } | ||
|
|
||
| @Test | ||
| fun `generatedTestsRelativeRoot should be provided from the configuration`() { | ||
| Assertions.assertEquals("target/generated/test", configurationProvider.generatedTestsRelativeRoot) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sarifReportsRelativeRoot should be provided from the configuration`() { | ||
| Assertions.assertEquals("target/generated/sarif", configurationProvider.sarifReportsRelativeRoot) | ||
| } | ||
|
|
||
| @Test | ||
| fun `markGeneratedTestsDirectoryAsTestSourcesRoot should be provided from the configuration`() { | ||
| Assertions.assertEquals(true, configurationProvider.markGeneratedTestsDirectoryAsTestSourcesRoot) | ||
| } | ||
|
|
||
| @Test | ||
| fun `testFramework should be provided from the configuration`() { | ||
| Assertions.assertEquals(Junit5, configurationProvider.testFramework) | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| fun `mockFramework should be provided from the configuration`() { | ||
| Assertions.assertEquals(MockFramework.MOCKITO, configurationProvider.mockFramework) | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| fun `generationTimeout should be provided from the configuration`() { | ||
| Assertions.assertEquals(10000, configurationProvider.generationTimeout) | ||
| } | ||
|
|
||
| @Test | ||
| fun `codegenLanguage should be provided from the configuration`() { | ||
| Assertions.assertEquals(CodegenLanguage.JAVA, configurationProvider.codegenLanguage) | ||
| } | ||
|
|
||
| @Test | ||
| fun `mockStrategy should be provided from the configuration`() { | ||
| Assertions.assertEquals(MockStrategyApi.OTHER_PACKAGES, configurationProvider.mockStrategy) | ||
| } | ||
|
|
||
| @Test | ||
| fun `staticsMocking should be provided from the configuration`() { | ||
| Assertions.assertEquals(MockitoStaticMocking, configurationProvider.staticsMocking) | ||
| } | ||
|
|
||
| @Test | ||
| fun `forceStaticMocking should be provided from the configuration`() { | ||
| Assertions.assertEquals(ForceStaticMocking.FORCE, configurationProvider.forceStaticMocking) | ||
| } | ||
|
|
||
| @Test | ||
| fun `classesToMockAlways should be provided from the configuration`() { | ||
| val expectedClassesToMockAlways = | ||
| (Mocker.defaultSuperClassesToMockAlwaysNames + "java.io.File").map(::ClassId).toSet() | ||
| Assertions.assertEquals(expectedClassesToMockAlways, configurationProvider.classesToMockAlways) | ||
| } | ||
|
|
||
| // internal | ||
|
|
||
| private val testMavenProject: TestMavenProject = | ||
| TestMavenProject("src/test/resources/project-to-test".toPath()) | ||
|
|
||
| private val sarifReportMojo by lazy { | ||
| configureSarifReportMojo(testMavenProject.mavenProject) | ||
| } | ||
|
|
||
| private val configurationProvider by lazy { | ||
| sarifReportMojo.sarifProperties | ||
| } | ||
|
|
||
| private fun configureSarifReportMojo(mavenProject: MavenProject): GenerateTestsAndSarifReportMojo { | ||
| val generateTestsAndSarifReportMojo = configureMojo( | ||
| GenerateTestsAndSarifReportMojo(), "utbot-maven", mavenProject.file | ||
| ) as GenerateTestsAndSarifReportMojo | ||
| generateTestsAndSarifReportMojo.mavenProject = mavenProject | ||
| return generateTestsAndSarifReportMojo | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.