Skip to content
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 @@ -254,10 +254,12 @@ class SarifReport(
return null

// searching needed method call
val publicMethodCallPattern = "$methodName("
val privateMethodCallPattern = Regex("""$methodName.*\.invoke\(""") // using reflection
val methodCallLineNumber = testsBodyLines
.drop(testMethodStartsAt + 1) // for search after it
.indexOfFirst { line ->
line.contains("$methodName(")
line.contains(publicMethodCallPattern) || line.contains(privateMethodCallPattern)
}
if (methodCallLineNumber == -1)
return null
Expand Down
146 changes: 98 additions & 48 deletions utbot-framework/src/test/kotlin/org/utbot/sarif/SarifReportTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SarifReportTest {
val actualReport = SarifReport(
testCases = listOf(),
generatedTestsCode = "",
SourceFindingStrategyDefault("", "", "", "")
sourceFindingEmpty
).createReport()

assert(actualReport.isNotEmpty())
Expand All @@ -28,7 +28,7 @@ class SarifReportTest {
val sarif = SarifReport(
testCases = listOf(testCase),
generatedTestsCode = "",
SourceFindingStrategyDefault("", "", "", "")
sourceFindingEmpty
).createReport().toSarif()

assert(sarif.runs.first().results.isEmpty())
Expand Down Expand Up @@ -58,7 +58,7 @@ class SarifReportTest {
val report = SarifReport(
testCases = testCases,
generatedTestsCode = "",
SourceFindingStrategyDefault("", "", "", "")
sourceFindingEmpty
).createReport().toSarif()

assert(report.runs.first().results[0].message.text.contains("NullPointerException"))
Expand All @@ -74,23 +74,9 @@ class SarifReportTest {
)
Mockito.`when`(mockUtExecution.stateBefore.parameters).thenReturn(listOf())
Mockito.`when`(mockUtExecution.path.lastOrNull()?.stmt?.javaSourceStartLineNumber).thenReturn(1337)
Mockito.`when`(mockUtExecution.testMethodName).thenReturn("testMain")
Mockito.`when`(mockUtExecution.testMethodName).thenReturn("testMain_ThrowArithmeticException")

val report = SarifReport(
testCases = listOf(testCase),
generatedTestsCode = """
// comment for `startLine == 2` in related location
public void testMain() throws Throwable {
Main.main();
}
""".trimIndent(),
SourceFindingStrategyDefault(
sourceClassFqn = "Main",
sourceFilePath = "src/Main.java",
testsFilePath = "test/MainTest.java",
projectRootPath = "."
)
).createReport().toSarif()
val report = sarifReportMain.createReport().toSarif()

val result = report.runs.first().results.first()
val location = result.locations.first().physicalLocation
Expand All @@ -99,7 +85,7 @@ class SarifReportTest {
assert(location.artifactLocation.uri.contains("Main.java"))
assert(location.region.startLine == 1337)
assert(relatedLocation.artifactLocation.uri.contains("MainTest.java"))
assert(relatedLocation.region.startLine == 2)
assert(relatedLocation.region.startLine == 1)
}

@Test
Expand All @@ -117,16 +103,7 @@ class SarifReportTest {
)
)

val report = SarifReport(
testCases = listOf(testCase),
generatedTestsCode = "",
SourceFindingStrategyDefault(
sourceClassFqn = "Main",
sourceFilePath = "src/Main.java",
testsFilePath = "test/MainTest.java",
projectRootPath = "."
)
).createReport().toSarif()
val report = sarifReportMain.createReport().toSarif()

val result = report.runs.first().results.first()
assert(result.message.text.contains("227"))
Expand All @@ -135,39 +112,75 @@ class SarifReportTest {
}

@Test
fun testCorrectCodeFlow() {
fun testCorrectCodeFlows() {
mockUtMethodNames()

val uncheckedException = Mockito.mock(NullPointerException::class.java)
val stackTraceElement = StackTraceElement("Main", "main", "Main.java", 17)
Mockito.`when`(uncheckedException.stackTrace).thenReturn(
Array(2) {
StackTraceElement("Main", "main", "Main.java", 17)
}
Array(2) { stackTraceElement }
)

Mockito.`when`(mockUtExecution.result).thenReturn(
UtImplicitlyThrownException(uncheckedException, false)
)
Mockito.`when`(mockUtExecution.stateBefore.parameters).thenReturn(listOf())

val report = SarifReport(
testCases = listOf(testCase),
generatedTestsCode = "",
SourceFindingStrategyDefault(
sourceClassFqn = "Main",
sourceFilePath = "src/Main.java",
testsFilePath = "test/MainTest.java",
projectRootPath = "."
)
).createReport().toSarif()
val report = sarifReportMain.createReport().toSarif()

val result = report.runs.first().results.first().codeFlows.first().threadFlows.first().locations.map {
it.location.physicalLocation
}
assert(result[0].artifactLocation.uri.contains("Main.java"))
assert(result[0].region.startLine == 17)
assert(result[1].artifactLocation.uri.contains("Main.java"))
assert(result[1].region.startLine == 17)
for (index in 0..1) {
assert(result[index].artifactLocation.uri.contains("Main.java"))
assert(result[index].region.startLine == 17)
}
}

@Test
fun testCodeFlowsStartsWithMethodCall() {
mockUtMethodNames()

val uncheckedException = Mockito.mock(NullPointerException::class.java)
val stackTraceElement = StackTraceElement("Main", "main", "Main.java", 3)
Mockito.`when`(uncheckedException.stackTrace).thenReturn(arrayOf(stackTraceElement))

Mockito.`when`(mockUtExecution.result).thenReturn(
UtImplicitlyThrownException(uncheckedException, false)
)
Mockito.`when`(mockUtExecution.stateBefore.parameters).thenReturn(listOf())
Mockito.`when`(mockUtExecution.testMethodName).thenReturn("testMain_ThrowArithmeticException")

val report = sarifReportMain.createReport().toSarif()

val codeFlowPhysicalLocations = report.runs[0].results[0].codeFlows[0].threadFlows[0].locations.map {
it.location.physicalLocation
}
assert(codeFlowPhysicalLocations[0].artifactLocation.uri.contains("MainTest.java"))
assert(codeFlowPhysicalLocations[0].region.startLine == 3)
}

@Test
fun testCodeFlowsStartsWithPrivateMethodCall() {
mockUtMethodNames()

val uncheckedException = Mockito.mock(NullPointerException::class.java)
val stackTraceElement = StackTraceElement("Main", "main", "Main.java", 3)
Mockito.`when`(uncheckedException.stackTrace).thenReturn(arrayOf(stackTraceElement))

Mockito.`when`(mockUtExecution.result).thenReturn(
UtImplicitlyThrownException(uncheckedException, false)
)
Mockito.`when`(mockUtExecution.stateBefore.parameters).thenReturn(listOf())
Mockito.`when`(mockUtExecution.testMethodName).thenReturn("testMain_ThrowArithmeticException")

val report = sarifReportPrivateMain.createReport().toSarif()

val codeFlowPhysicalLocations = report.runs[0].results[0].codeFlows[0].threadFlows[0].locations.map {
it.location.physicalLocation
}
assert(codeFlowPhysicalLocations[0].artifactLocation.uri.contains("MainTest.java"))
assert(codeFlowPhysicalLocations[0].region.startLine == 4)
}

// internal
Expand All @@ -184,4 +197,41 @@ class SarifReportTest {
}

private fun String.toSarif(): Sarif = jacksonObjectMapper().readValue(this)

// constants

private val sourceFindingEmpty = SourceFindingStrategyDefault(
sourceClassFqn = "",
sourceFilePath = "",
testsFilePath = "",
projectRootPath = ""
)

private val sourceFindingMain = SourceFindingStrategyDefault(
sourceClassFqn = "Main",
sourceFilePath = "src/Main.java",
testsFilePath = "test/MainTest.java",
projectRootPath = "."
)

private val generatedTestsCodeMain = """
public void testMain_ThrowArithmeticException() {
Main main = new Main();
main.main(0);
}
""".trimIndent()

private val generatedTestsCodePrivateMain = """
public void testMain_ThrowArithmeticException() {
Main main = new Main();
// ...
mainMethod.invoke(main, mainMethodArguments);
}
""".trimIndent()

private val sarifReportMain =
SarifReport(listOf(testCase), generatedTestsCodeMain, sourceFindingMain)

private val sarifReportPrivateMain =
SarifReport(listOf(testCase), generatedTestsCodePrivateMain, sourceFindingMain)
}