Browse Source

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
pull/30624/head
Sébastien Deleuze 1 year ago
parent
commit
2f78b42133
  1. 77
      spring-core/src/test/kotlin/org/springframework/core/AbstractKotlinReflectionParameterNameDiscovererTests.kt
  2. 15
      spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt
  3. 50
      spring-core/src/test/kotlin/org/springframework/core/KotlinReflectionParameterNameDiscovererTests.kt

77
spring-core/src/test/kotlin/org/springframework/core/AbstractKotlinReflectionParameterNameDiscovererTests.kt

@ -0,0 +1,77 @@ @@ -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)
}
}

15
spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt

@ -1,5 +1,5 @@ @@ -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 @@ -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 { @@ -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")
}
}

50
spring-core/src/test/kotlin/org/springframework/core/KotlinReflectionParameterNameDiscovererTests.kt

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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())

Loading…
Cancel
Save