From db962337e7c3794651d335cdaf83e13d9a3b72cc Mon Sep 17 00:00:00 2001 From: Stefano Lucka Date: Fri, 16 Sep 2022 20:08:34 +0200 Subject: [PATCH] Fix issue with base classes --- .../spring/junit5/PactBrokerLoaderTest.java | 76 ++++++++++++++ .../spring/junit5/PactBrokerLoaderKtTest.kt | 98 +++++++++++++++++++ .../junitsupport/loader/PactBrokerLoader.kt | 38 ++++++- 3 files changed, 209 insertions(+), 3 deletions(-) create mode 100644 provider/junit5spring/src/test/java/au/com/dius/pact/provider/spring/junit5/PactBrokerLoaderTest.java create mode 100644 provider/junit5spring/src/test/kotlin/au/com/dius/pact/provider/spring/junit5/PactBrokerLoaderKtTest.kt diff --git a/provider/junit5spring/src/test/java/au/com/dius/pact/provider/spring/junit5/PactBrokerLoaderTest.java b/provider/junit5spring/src/test/java/au/com/dius/pact/provider/spring/junit5/PactBrokerLoaderTest.java new file mode 100644 index 0000000000..f9f1488a0d --- /dev/null +++ b/provider/junit5spring/src/test/java/au/com/dius/pact/provider/spring/junit5/PactBrokerLoaderTest.java @@ -0,0 +1,76 @@ +package au.com.dius.pact.provider.spring.junit5; + +import org.junit.jupiter.api.Test; + +import au.com.dius.pact.provider.junitsupport.loader.PactBrokerConsumerVersionSelectors; +import au.com.dius.pact.provider.junitsupport.loader.PactBrokerLoader; +import au.com.dius.pact.provider.junitsupport.loader.SelectorBuilder; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PactBrokerLoaderTest { + + @Test + void test1() { + assertNotNull(PactBrokerLoader.testClassHasSelectorsMethod(Test1.class)); + } + + @Test + void test2() { + assertThrows(IllegalAccessException.class, () -> PactBrokerLoader.testClassHasSelectorsMethod(Test2.class)); + } + + @Test + void test3() { + assertThrows(IllegalAccessException.class, () -> PactBrokerLoader.testClassHasSelectorsMethod(Test3.class)); + } + + @Test + void test4() { + assertThrows(IllegalAccessException.class, () -> PactBrokerLoader.testClassHasSelectorsMethod(Test4.class)); + } + + @Test + void test5() { + assertNotNull(PactBrokerLoader.testClassHasSelectorsMethod(Test5.class)); + } + + class Test1 { + @PactBrokerConsumerVersionSelectors + public static SelectorBuilder cvs() { + return new SelectorBuilder(); + } + } + class Test2 { + @PactBrokerConsumerVersionSelectors + static SelectorBuilder cvs() { + return new SelectorBuilder(); + } + } + + class Test3 { + @PactBrokerConsumerVersionSelectors + private static SelectorBuilder cvs() { + return new SelectorBuilder(); + } + } + + class Test4 extends Test4Super {} + + class Test4Super { + @PactBrokerConsumerVersionSelectors + protected static SelectorBuilder cvs() { + return new SelectorBuilder(); + } + } + + class Test5 extends Test5Super {} + + class Test5Super { + @PactBrokerConsumerVersionSelectors + public static SelectorBuilder cvs() { + return new SelectorBuilder(); + } + } +} diff --git a/provider/junit5spring/src/test/kotlin/au/com/dius/pact/provider/spring/junit5/PactBrokerLoaderKtTest.kt b/provider/junit5spring/src/test/kotlin/au/com/dius/pact/provider/spring/junit5/PactBrokerLoaderKtTest.kt new file mode 100644 index 0000000000..f2757a9461 --- /dev/null +++ b/provider/junit5spring/src/test/kotlin/au/com/dius/pact/provider/spring/junit5/PactBrokerLoaderKtTest.kt @@ -0,0 +1,98 @@ +package au.com.dius.pact.provider.spring.junit5 + +import au.com.dius.pact.provider.junitsupport.loader.PactBrokerConsumerVersionSelectors +import au.com.dius.pact.provider.junitsupport.loader.PactBrokerLoader.Companion.testClassHasSelectorsMethod +import au.com.dius.pact.provider.junitsupport.loader.SelectorBuilder +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test + +class PactBrokerLoaderKtTest { + + @Test + fun test1() { + assertNotNull(testClassHasSelectorsMethod(Test1::class.java)) + } + + @Test + fun test2() { + assertThrows(IllegalAccessException::class.java) { + testClassHasSelectorsMethod(Test2::class.java) + } + } + + @Test + fun test3() { + assertThrows(IllegalAccessException::class.java) { + testClassHasSelectorsMethod(Test3::class.java) + } + } + + @Test + fun test4() { + assertThrows(IllegalAccessException::class.java) { + testClassHasSelectorsMethod(Test4::class.java) + } + } + + @Test + fun test5() { + assertNotNull(testClassHasSelectorsMethod(Test5::class.java)) + } + + @Test + fun test6() { + assertNotNull(testClassHasSelectorsMethod(Test6::class.java)) + } + + class Test1 { + @PactBrokerConsumerVersionSelectors + fun cvs(): SelectorBuilder { + return SelectorBuilder() + } + } + + class Test2 { + @PactBrokerConsumerVersionSelectors + private fun cvs(): SelectorBuilder { + return SelectorBuilder() + } + } + + class Test3 { + @PactBrokerConsumerVersionSelectors + private fun cvs(): SelectorBuilder { + return SelectorBuilder() + } + } + + class Test4 : Test4Super() + + abstract class Test4Super { + + @PactBrokerConsumerVersionSelectors + protected fun cvs(): SelectorBuilder { + return SelectorBuilder() + } + } + + class Test5 : Test5Super() + + abstract class Test5Super { + @PactBrokerConsumerVersionSelectors + fun cvs(): SelectorBuilder { + return SelectorBuilder() + } + } + + class Test6 : Test6Super() + + abstract class Test6Super() { + companion object { + @PactBrokerConsumerVersionSelectors + fun cvs(): SelectorBuilder { + return SelectorBuilder() + } + } + } +} \ No newline at end of file diff --git a/provider/src/main/kotlin/au/com/dius/pact/provider/junitsupport/loader/PactBrokerLoader.kt b/provider/src/main/kotlin/au/com/dius/pact/provider/junitsupport/loader/PactBrokerLoader.kt index 76af35dac4..c83a80b351 100644 --- a/provider/src/main/kotlin/au/com/dius/pact/provider/junitsupport/loader/PactBrokerLoader.kt +++ b/provider/src/main/kotlin/au/com/dius/pact/provider/junitsupport/loader/PactBrokerLoader.kt @@ -28,9 +28,14 @@ import java.lang.reflect.Modifier import java.net.URI import java.net.URISyntaxException import kotlin.reflect.KClass +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 /** @@ -394,9 +399,7 @@ open class PactBrokerLoader( @JvmStatic @Suppress("ThrowsCount") fun testClassHasSelectorsMethod(testClass: Class<*>?): Method? { - val method = testClass?.methods?.firstOrNull { method -> - method.getAnnotation(PactBrokerConsumerVersionSelectors::class.java) != null - } + val method = findConsumerVersionSelectorAnnotatedMethod(testClass) if (method != null) { if (method.parameterCount > 0) { @@ -422,5 +425,34 @@ open class PactBrokerLoader( return method } + + private fun findConsumerVersionSelectorAnnotatedMethod(testClass: Class<*>?) : Method? { + if (testClass == null) { + return null + } + + var klass : Class<*> = testClass + while (klass != Object::class.java) { + + for (declaredMethod in klass.declaredMethods) { + if (declaredMethod.isAnnotationPresent(PactBrokerConsumerVersionSelectors::class.java)) { + return declaredMethod + } + + val method = klass.kotlin.companionObject?.declaredFunctions?.firstOrNull { + it.hasAnnotation() + } + + if (method != null) { + return method.javaMethod + } + } + + klass = klass.superclass + } + + return null + } + } }