Skip to content

Commit

Permalink
Fix issue with base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Lucka committed Sep 16, 2022
1 parent ef2fc84 commit db96233
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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<PactBrokerConsumerVersionSelectors>()
}

if (method != null) {
return method.javaMethod
}
}

klass = klass.superclass
}

return null
}

}
}

0 comments on commit db96233

Please sign in to comment.