Skip to content

Commit

Permalink
chore: correct static code issues and failing tests from PR #1605
Browse files Browse the repository at this point in the history
  • Loading branch information
uglyog authored and rholshausen committed Nov 24, 2022
1 parent 6f3c422 commit 02ee591
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static SelectorBuilder cvs() {
}
}

class Test4 extends Test4Super {}
static class Test4 extends Test4Super {}

static class Test4Super {
@PactBrokerConsumerVersionSelectors
Expand All @@ -65,7 +65,7 @@ protected static SelectorBuilder cvs() {
}
}

class Test5 extends Test5Super {}
static class Test5 extends Test5Super {}

static class Test5Super {
@PactBrokerConsumerVersionSelectors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test

@Suppress("UnnecessaryAbstractClass", "UnusedPrivateMember")
class PactBrokerLoaderKtTest {

@Test
Expand Down Expand Up @@ -68,7 +69,6 @@ class PactBrokerLoaderKtTest {

class Test4 : Test4Super()

@Suppress("UnnecessaryAbstractClass")
abstract class Test4Super {

@PactBrokerConsumerVersionSelectors
Expand All @@ -79,7 +79,6 @@ class PactBrokerLoaderKtTest {

class Test5 : Test5Super()

@Suppress("UnnecessaryAbstractClass")
abstract class Test5Super {
@PactBrokerConsumerVersionSelectors
fun cvs(): SelectorBuilder {
Expand All @@ -90,12 +89,12 @@ class PactBrokerLoaderKtTest {
class Test6 : Test6Super()

@Suppress("UnnecessaryAbstractClass")
abstract class Test6Super() {
abstract class Test6Super {
companion object {
@PactBrokerConsumerVersionSelectors
fun cvs(): SelectorBuilder {
return SelectorBuilder()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import java.net.URISyntaxException
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.full.companionObject
import kotlin.reflect.full.companionObjectInstance
import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.full.hasAnnotation
import kotlin.reflect.full.isSubtypeOf
import kotlin.reflect.full.memberFunctions
import kotlin.reflect.full.starProjectedType
import kotlin.reflect.jvm.javaMethod
import kotlin.reflect.jvm.kotlinFunction
Expand Down Expand Up @@ -139,7 +141,10 @@ open class PactBrokerLoader(
val tags = pactBrokerTags.orEmpty().flatMap { parseListExpression(it, resolver) }
val selectorsMethod = testClassHasSelectorsMethod(this.testClass)
return if (selectorsMethod != null) {
invokeSelectorsMethod(this.testInstance, selectorsMethod)
val (method, methodClass) = selectorsMethod
val instance = if (methodClass.isCompanion) methodClass.objectInstance
else this.testInstance
invokeSelectorsMethod(instance, methodClass.java, method)
} else if (shouldFallBackToTags(tags, pactBrokerConsumerVersionSelectors, resolver)) {
permutations(tags, pactBrokerConsumers.flatMap { parseListExpression(it, resolver) })
.map { ConsumerVersionSelectors.Selector(it.first, true, it.second) }
Expand Down Expand Up @@ -353,32 +358,52 @@ open class PactBrokerLoader(

companion object : KLogging() {
@JvmStatic
fun invokeSelectorsMethod(testInstance: Any?, selectorsMethod: KFunction<*>): List<ConsumerVersionSelectors> {
fun invokeSelectorsMethod(
testInstance: Any?,
testClass: Class<*>?,
method: Method
): List<ConsumerVersionSelectors> {
val projectedType = SelectorBuilder::class.starProjectedType
val selectorsMethod = method.kotlinFunction!!
return when (selectorsMethod.parameters.size) {
0 -> if (selectorsMethod.returnType.isSubtypeOf(projectedType)) {
val builder = selectorsMethod.call() as SelectorBuilder
val builder = method.invoke(null) as SelectorBuilder
builder.build()
} else {
selectorsMethod.call() as List<ConsumerVersionSelectors>
method.invoke(null) as List<ConsumerVersionSelectors>
}
1 -> if (selectorsMethod.returnType.isSubtypeOf(projectedType)) {
val builder = selectorsMethod.call(testInstance) as SelectorBuilder
builder.build()
} else {
selectorsMethod.call(testInstance) as List<ConsumerVersionSelectors>
1 -> {
val instance = instanceForMethod(testInstance, testClass, method)
if (selectorsMethod.returnType.isSubtypeOf(projectedType)) {
val builder = method.invoke(instance) as SelectorBuilder
builder.build()
} else {
method.invoke(instance) as List<ConsumerVersionSelectors>
}
}
else -> throw java.lang.IllegalArgumentException(
"Consumer version selector method should not take any parameters and return an instance of SelectorBuilder")
}
}

private fun instanceForMethod(testInstance: Any?, testClass: Class<*>?, selectorsMethod: Method): Any? {
return if (testInstance == null) {
val declaringClass = testClass?.kotlin ?: selectorsMethod.declaringClass.kotlin
if (declaringClass.isCompanion) {
declaringClass.companionObjectInstance
} else {
declaringClass.java.newInstance()
}
} else testInstance
}

@JvmStatic
@Suppress("ThrowsCount")
fun testClassHasSelectorsMethod(testClass: Class<*>?): KFunction<*>? {
val method = findConsumerVersionSelectorAnnotatedMethod(testClass)
fun testClassHasSelectorsMethod(testClass: Class<*>?): Pair<Method, KClass<*>>? {
val result = findConsumerVersionSelectorAnnotatedMethod(testClass)

if (method != null) {
if (result != null) {
val (method, _) = result;
if (method.parameterCount > 0) {
throw IllegalAccessException("Consumer version selector methods must not have any parameters. " +
"Method ${method.name} has ${method.parameterCount}.")
Expand All @@ -402,11 +427,11 @@ open class PactBrokerLoader(
}
}

return method?.kotlinFunction
return result
}

@OptIn(ExperimentalStdlibApi::class)
private fun findConsumerVersionSelectorAnnotatedMethod(testClass: Class<*>?) : Method? {
private fun findConsumerVersionSelectorAnnotatedMethod(testClass: Class<*>?) : Pair<Method, KClass<*>>? {
if (testClass == null) {
return null
}
Expand All @@ -416,16 +441,16 @@ open class PactBrokerLoader(

for (declaredMethod in klass.declaredMethods) {
if (declaredMethod.isAnnotationPresent(PactBrokerConsumerVersionSelectors::class.java)) {
return declaredMethod
return declaredMethod to testClass.kotlin
}
}

val method = klass.kotlin.companionObject?.declaredFunctions?.firstOrNull {
it.hasAnnotation<PactBrokerConsumerVersionSelectors>()
}
val method = klass.kotlin.companionObject?.declaredFunctions?.firstOrNull {
it.hasAnnotation<PactBrokerConsumerVersionSelectors>()
}

if (method != null) {
return method.javaMethod
}
if (method != null) {
return method.javaMethod!! to klass.kotlin.companionObject!!
}

klass = klass.superclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,7 @@ class PactBrokerLoaderSpec extends Specification {
@Unroll
def 'Invoke Selectors Method'() {
expect:
PactBrokerLoader.invokeSelectorsMethod(instance, PactBrokerLoader.testClassHasSelectorsMethod(clazz)) == result
PactBrokerLoader.invokeSelectorsMethod(instance, clazz, PactBrokerLoader.testClassHasSelectorsMethod(clazz).first) == result

where:

Expand Down

0 comments on commit 02ee591

Please sign in to comment.