Skip to content

Fix filtering of UtMockAssumptionViolatedException #1592

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 4 commits into from
Dec 28, 2022
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 @@ -248,6 +248,11 @@ class UtBotSymbolicEngine(
val concreteExecutionResult =
concreteExecutor.executeConcretely(methodUnderTest, stateBefore, instrumentation)

if (concreteExecutionResult.violatesUtMockAssumption()) {
logger.debug { "Generated test case violates the UtMock assumption: $concreteExecutionResult" }
return@bracket
}

val concreteUtExecution = UtSymbolicExecution(
stateBefore,
concreteExecutionResult.stateAfter,
Expand Down Expand Up @@ -368,8 +373,8 @@ class UtBotSymbolicEngine(
// in case an exception occurred from the concrete execution
concreteExecutionResult ?: return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.PASS)

if (concreteExecutionResult.result.exceptionOrNull() is UtMockAssumptionViolatedException) {
logger.debug { "Generated test case by fuzzer violates the UtMock assumption" }
if (concreteExecutionResult.violatesUtMockAssumption()) {
logger.debug { "Generated test case by fuzzer violates the UtMock assumption: $concreteExecutionResult" }
return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.PASS)
}

Expand Down Expand Up @@ -497,6 +502,11 @@ class UtBotSymbolicEngine(
instrumentation
)

if (concreteExecutionResult.violatesUtMockAssumption()) {
logger.debug { "Generated test case violates the UtMock assumption: $concreteExecutionResult" }
return
}

val concolicUtExecution = symbolicUtExecution.copy(
stateAfter = concreteExecutionResult.stateAfter,
result = concreteExecutionResult.result,
Expand Down Expand Up @@ -593,3 +603,11 @@ private fun makeWrapperConsistencyCheck(
val visitedSelectExpression = memory.isVisited(symbolicValue.addr)
visitedConstraints += mkEq(visitedSelectExpression, mkInt(1))
}

private fun UtConcreteExecutionResult.violatesUtMockAssumption(): Boolean {
// We should compare FQNs instead of `if (... is UtMockAssumptionViolatedException)`
// because the exception from the `concreteExecutionResult` is loaded by user's ClassLoader,
// but the `UtMockAssumptionViolatedException` is loaded by the current ClassLoader,
// so we can't cast them to each other.
return result.exceptionOrNull()?.javaClass?.name == UtMockAssumptionViolatedException::class.java.name
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.esotericsoftware.kryo.kryo5.serializers.JavaSerializer
import com.esotericsoftware.kryo.kryo5.util.DefaultInstantiatorStrategy
import com.jetbrains.rd.util.lifetime.Lifetime
import com.jetbrains.rd.util.lifetime.throwIfNotAlive
import org.utbot.api.exception.UtMockAssumptionViolatedException
import org.utbot.framework.plugin.api.TimeoutException
import java.io.ByteArrayOutputStream

Expand Down Expand Up @@ -123,6 +124,7 @@ internal class TunedKryo : Kryo() {

this.setOptimizedGenerics(false)
register(TimeoutException::class.java, TimeoutExceptionSerializer())
register(UtMockAssumptionViolatedException::class.java, UtMockAssumptionViolatedExceptionSerializer())

// TODO: JIRA:1492
addDefaultSerializer(java.lang.Throwable::class.java, JavaSerializer())
Expand All @@ -149,4 +151,19 @@ internal class TunedKryo : Kryo() {
override fun read(kryo: Kryo?, input: Input, type: Class<out TimeoutException>?): TimeoutException =
TimeoutException(input.readString())
}

/**
* Specific serializer for [UtMockAssumptionViolatedException] - [JavaSerializer] is not applicable
* because [UtMockAssumptionViolatedException] is not in class loader.
*/
private class UtMockAssumptionViolatedExceptionSerializer : Serializer<UtMockAssumptionViolatedException>() {
override fun write(kryo: Kryo, output: Output, value: UtMockAssumptionViolatedException) {
output.writeString(value.message)
}

override fun read(kryo: Kryo?, input: Input, type: Class<out UtMockAssumptionViolatedException>?): UtMockAssumptionViolatedException {
input.readString() // shift the reading position
return UtMockAssumptionViolatedException()
}
}
}