Skip to content
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

Fix static mocking #1142

Merged
merged 1 commit into from
Oct 12, 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
@@ -1,8 +1,8 @@
package org.utbot.framework.concrete

import org.utbot.framework.plugin.api.util.signature
import java.lang.reflect.Method
import java.util.IdentityHashMap
import org.utbot.instrumentation.instrumentation.mock.computeKeyForMethod

/**
* Some information, which is computed after classes instrumentation.
Expand Down Expand Up @@ -66,7 +66,7 @@ class InstrumentationContext {
}

fun updateMocks(obj: Any?, method: Method, values: List<*>) {
updateMocks(obj, method.signature, values)
updateMocks(obj, computeKeyForMethod(method), values)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.utbot.framework.concrete

import org.utbot.common.withAccessibility
import org.utbot.framework.plugin.api.util.signature
import org.utbot.instrumentation.instrumentation.mock.MockConfig
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import org.utbot.common.withAccessibility
import org.utbot.instrumentation.instrumentation.mock.MockConfig
import org.utbot.instrumentation.instrumentation.mock.computeKeyForMethod


/**
Expand All @@ -31,7 +31,8 @@ class MethodMockController(
error("$method is an instance method, but instance is null!")
}

val id = instrumentationContext.methodSignatureToId[method.signature]
val computedSignature = computeKeyForMethod(method)
val id = instrumentationContext.methodSignatureToId[computedSignature]

isMockField = clazz.declaredFields.firstOrNull { it.name == MockConfig.IS_MOCK_FIELD + id }
?: error("No field ${MockConfig.IS_MOCK_FIELD + id} in $clazz")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.commons.AdviceAdapter
import org.objectweb.asm.commons.Method.getMethod
import org.utbot.framework.plugin.api.util.signature

object MockConfig {
const val IS_MOCK_FIELD = "\$__is_mock_"
}

/**
* Computes key for method that is used for mocking.
*/
fun computeKeyForMethod(internalType: String, methodSignature: String) =
"$internalType@$methodSignature"

fun computeKeyForMethod(method: Method) =
computeKeyForMethod(Type.getInternalName(method.declaringClass), method.signature)

class MockClassVisitor(
classVisitor: ClassVisitor,
mockGetter: Method,
Expand Down Expand Up @@ -73,7 +83,7 @@ class MockClassVisitor(
val isStatic = access and Opcodes.ACC_STATIC != 0
val isVoidMethod = Type.getReturnType(descriptor) == Type.VOID_TYPE

val computedSignature = name + descriptor
val computedSignature = computeKeyForMethod(internalClassName, "$name$descriptor")
val id = signatureToId.size
signatureToId[computedSignature] = id

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.utbot.instrumentation.examples.mock

import org.utbot.common.withAccessibility
import org.utbot.framework.plugin.api.util.signature
import org.utbot.instrumentation.instrumentation.instrumenter.Instrumenter
import org.utbot.instrumentation.instrumentation.mock.MockClassVisitor
import org.utbot.instrumentation.instrumentation.mock.MockConfig
import java.lang.reflect.Method
import java.util.IdentityHashMap
import kotlin.reflect.jvm.javaMethod
import org.objectweb.asm.Type
import org.utbot.common.withAccessibility
import org.utbot.instrumentation.instrumentation.instrumenter.Instrumenter
import org.utbot.instrumentation.instrumentation.mock.MockClassVisitor
import org.utbot.instrumentation.instrumentation.mock.MockConfig
import org.utbot.instrumentation.instrumentation.mock.computeKeyForMethod

/**
* Helper for generating tests with methods mocks.
Expand Down Expand Up @@ -52,8 +52,8 @@ class MockHelper(
error("Can't mock function returning void!")
}

val sign = method.signature
val methodId = mockClassVisitor.signatureToId[sign]
val computedSignature = computeKeyForMethod(method)
val methodId = mockClassVisitor.signatureToId[computedSignature]

val isMockField = instrumentedClazz.getDeclaredField(MockConfig.IS_MOCK_FIELD + methodId)
MockGetter.updateMocks(instance, method, mockedValues)
Expand Down Expand Up @@ -129,7 +129,7 @@ class MockHelper(
}

fun updateMocks(obj: Any?, method: Method, values: List<*>) {
updateMocks(obj, method.signature, values)
updateMocks(obj, computeKeyForMethod(method), values)
}
}
}