From 2f78b42133e0719da07a4bab8102f4498e2b3206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Thu, 8 Jun 2023 15:21:21 +0200 Subject: [PATCH] Refine Kotlin ParameterNameDiscoverer tests Related to gh-30052, we should improve ParameterNameDiscoverer Kotlin tests to make sure DefaultParameterNameDiscoverer behaves as expected and is consistent with KotlinReflectionParameterNameDiscoverer behavior. Closes gh-30618 --- ...nReflectionParameterNameDiscovererTests.kt | 77 +++++++++++++++++++ ...tlinDefaultParameterNameDiscovererTests.kt | 15 ++-- ...nReflectionParameterNameDiscovererTests.kt | 50 ++---------- 3 files changed, 93 insertions(+), 49 deletions(-) create mode 100644 spring-core/src/test/kotlin/org/springframework/core/AbstractKotlinReflectionParameterNameDiscovererTests.kt diff --git a/spring-core/src/test/kotlin/org/springframework/core/AbstractKotlinReflectionParameterNameDiscovererTests.kt b/spring-core/src/test/kotlin/org/springframework/core/AbstractKotlinReflectionParameterNameDiscovererTests.kt new file mode 100644 index 0000000000..e9b53f84b8 --- /dev/null +++ b/spring-core/src/test/kotlin/org/springframework/core/AbstractKotlinReflectionParameterNameDiscovererTests.kt @@ -0,0 +1,77 @@ +/* + * Copyright 2002-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +import org.springframework.util.ReflectionUtils + +/** + * Abstract tests for Kotlin [ParameterNameDiscoverer] aware implementations. + * + * @author Sebastien Deleuze + */ +@Suppress("UNUSED_PARAMETER") +abstract class AbstractKotlinReflectionParameterNameDiscovererTests(protected val parameterNameDiscoverer: ParameterNameDiscoverer) { + + @Test + fun getParameterNamesOnInterface() { + val method = ReflectionUtils.findMethod(MessageService::class.java,"sendMessage", String::class.java)!! + val actualParams = parameterNameDiscoverer.getParameterNames(method) + assertThat(actualParams).contains("message") + } + + @Test + fun getParameterNamesOnClass() { + val constructor = ReflectionUtils.accessibleConstructor(MessageServiceImpl::class.java,String::class.java) + val actualConstructorParams = parameterNameDiscoverer.getParameterNames(constructor) + assertThat(actualConstructorParams).contains("message") + val method = ReflectionUtils.findMethod(MessageServiceImpl::class.java,"sendMessage", String::class.java)!! + val actualMethodParams = parameterNameDiscoverer.getParameterNames(method) + assertThat(actualMethodParams).contains("message") + } + + @Test + fun getParameterNamesOnExtensionMethod() { + val method = ReflectionUtils.findMethod(UtilityClass::class.java, "identity", String::class.java)!! + val actualParams = parameterNameDiscoverer.getParameterNames(method)!! + assertThat(actualParams).contains("\$receiver") + } + + interface MessageService { + fun sendMessage(message: String) + } + + class MessageServiceImpl(message: String) { + fun sendMessage(message: String) = message + } + + class UtilityClass { + fun String.identity() = this + } + + class TestClass(name: String, age: Int) { + fun test(name: String, age: Int) { + } + } + + interface TestInterface { + fun test(name: String, age: Int) + } + +} diff --git a/spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt b/spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt index 7fe1992484..c51cfb7f1f 100644 --- a/spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt +++ b/spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,9 +19,13 @@ package org.springframework.core import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -class KotlinDefaultParameterNameDiscovererTests { - - private val parameterNameDiscoverer = DefaultParameterNameDiscoverer() +/** + * Tests for Kotlin support in [DefaultParameterNameDiscoverer]. + * + * @author Sebastien Deleuze + */ +class KotlinDefaultParameterNameDiscovererTests : + AbstractKotlinReflectionParameterNameDiscovererTests(DefaultParameterNameDiscoverer()){ enum class MyEnum { ONE, TWO @@ -31,6 +35,7 @@ class KotlinDefaultParameterNameDiscovererTests { fun getParameterNamesOnEnum() { val constructor = MyEnum::class.java.declaredConstructors[0] val actualParams = parameterNameDiscoverer.getParameterNames(constructor) - assertThat(actualParams!!.size).isEqualTo(2) + assertThat(actualParams).containsExactly("\$enum\$name", "\$enum\$ordinal") } + } diff --git a/spring-core/src/test/kotlin/org/springframework/core/KotlinReflectionParameterNameDiscovererTests.kt b/spring-core/src/test/kotlin/org/springframework/core/KotlinReflectionParameterNameDiscovererTests.kt index 55b5ed4772..a9e416e8e2 100644 --- a/spring-core/src/test/kotlin/org/springframework/core/KotlinReflectionParameterNameDiscovererTests.kt +++ b/spring-core/src/test/kotlin/org/springframework/core/KotlinReflectionParameterNameDiscovererTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,48 +16,10 @@ package org.springframework.core -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test - -import org.springframework.util.ReflectionUtils - /** - * Tests for KotlinReflectionParameterNameDiscoverer + * Tests for [KotlinReflectionParameterNameDiscoverer]. + * + * @author Sebastien Deleuze */ -class KotlinReflectionParameterNameDiscovererTests { - - private val parameterNameDiscoverer = KotlinReflectionParameterNameDiscoverer() - - @Test - fun getParameterNamesOnInterface() { - val method = ReflectionUtils.findMethod(MessageService::class.java,"sendMessage", String::class.java)!! - val actualParams = parameterNameDiscoverer.getParameterNames(method) - assertThat(actualParams).contains("message") - } - - @Test - fun getParameterNamesOnClass() { - val method = ReflectionUtils.findMethod(MessageServiceImpl::class.java,"sendMessage", String::class.java)!! - val actualParams = parameterNameDiscoverer.getParameterNames(method) - assertThat(actualParams).contains("message") - } - - @Test - fun getParameterNamesOnExtensionMethod() { - val method = ReflectionUtils.findMethod(UtilityClass::class.java, "identity", String::class.java)!! - val actualParams = parameterNameDiscoverer.getParameterNames(method)!! - assertThat(actualParams).contains("\$receiver") - } - - interface MessageService { - fun sendMessage(message: String) - } - - class MessageServiceImpl { - fun sendMessage(message: String) = message - } - - class UtilityClass { - fun String.identity() = this - } -} +class KotlinReflectionParameterNameDiscovererTests : + AbstractKotlinReflectionParameterNameDiscovererTests(KotlinReflectionParameterNameDiscoverer())