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
This commit is contained in:
parent
f4ef057e9e
commit
2f78b42133
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.assertj.core.api.Assertions.assertThat
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
class KotlinDefaultParameterNameDiscovererTests {
|
/**
|
||||||
|
* Tests for Kotlin support in [DefaultParameterNameDiscoverer].
|
||||||
private val parameterNameDiscoverer = DefaultParameterNameDiscoverer()
|
*
|
||||||
|
* @author Sebastien Deleuze
|
||||||
|
*/
|
||||||
|
class KotlinDefaultParameterNameDiscovererTests :
|
||||||
|
AbstractKotlinReflectionParameterNameDiscovererTests(DefaultParameterNameDiscoverer()){
|
||||||
|
|
||||||
enum class MyEnum {
|
enum class MyEnum {
|
||||||
ONE, TWO
|
ONE, TWO
|
||||||
|
|
@ -31,6 +35,7 @@ class KotlinDefaultParameterNameDiscovererTests {
|
||||||
fun getParameterNamesOnEnum() {
|
fun getParameterNamesOnEnum() {
|
||||||
val constructor = MyEnum::class.java.declaredConstructors[0]
|
val constructor = MyEnum::class.java.declaredConstructors[0]
|
||||||
val actualParams = parameterNameDiscoverer.getParameterNames(constructor)
|
val actualParams = parameterNameDiscoverer.getParameterNames(constructor)
|
||||||
assertThat(actualParams!!.size).isEqualTo(2)
|
assertThat(actualParams).containsExactly("\$enum\$name", "\$enum\$ordinal")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,48 +16,10 @@
|
||||||
|
|
||||||
package org.springframework.core
|
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 {
|
class KotlinReflectionParameterNameDiscovererTests :
|
||||||
|
AbstractKotlinReflectionParameterNameDiscovererTests(KotlinReflectionParameterNameDiscoverer())
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue