Skip to content

Commit 2f78b42

Browse files
committed
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
1 parent f4ef057 commit 2f78b42

File tree

3 files changed

+93
-49
lines changed

3 files changed

+93
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core
18+
19+
import org.assertj.core.api.Assertions.assertThat
20+
import org.junit.jupiter.api.Test
21+
22+
import org.springframework.util.ReflectionUtils
23+
24+
/**
25+
* Abstract tests for Kotlin [ParameterNameDiscoverer] aware implementations.
26+
*
27+
* @author Sebastien Deleuze
28+
*/
29+
@Suppress("UNUSED_PARAMETER")
30+
abstract class AbstractKotlinReflectionParameterNameDiscovererTests(protected val parameterNameDiscoverer: ParameterNameDiscoverer) {
31+
32+
@Test
33+
fun getParameterNamesOnInterface() {
34+
val method = ReflectionUtils.findMethod(MessageService::class.java,"sendMessage", String::class.java)!!
35+
val actualParams = parameterNameDiscoverer.getParameterNames(method)
36+
assertThat(actualParams).contains("message")
37+
}
38+
39+
@Test
40+
fun getParameterNamesOnClass() {
41+
val constructor = ReflectionUtils.accessibleConstructor(MessageServiceImpl::class.java,String::class.java)
42+
val actualConstructorParams = parameterNameDiscoverer.getParameterNames(constructor)
43+
assertThat(actualConstructorParams).contains("message")
44+
val method = ReflectionUtils.findMethod(MessageServiceImpl::class.java,"sendMessage", String::class.java)!!
45+
val actualMethodParams = parameterNameDiscoverer.getParameterNames(method)
46+
assertThat(actualMethodParams).contains("message")
47+
}
48+
49+
@Test
50+
fun getParameterNamesOnExtensionMethod() {
51+
val method = ReflectionUtils.findMethod(UtilityClass::class.java, "identity", String::class.java)!!
52+
val actualParams = parameterNameDiscoverer.getParameterNames(method)!!
53+
assertThat(actualParams).contains("\$receiver")
54+
}
55+
56+
interface MessageService {
57+
fun sendMessage(message: String)
58+
}
59+
60+
class MessageServiceImpl(message: String) {
61+
fun sendMessage(message: String) = message
62+
}
63+
64+
class UtilityClass {
65+
fun String.identity() = this
66+
}
67+
68+
class TestClass(name: String, age: Int) {
69+
fun test(name: String, age: Int) {
70+
}
71+
}
72+
73+
interface TestInterface {
74+
fun test(name: String, age: Int)
75+
}
76+
77+
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,9 +19,13 @@ package org.springframework.core
1919
import org.assertj.core.api.Assertions.assertThat
2020
import org.junit.jupiter.api.Test
2121

22-
class KotlinDefaultParameterNameDiscovererTests {
23-
24-
private val parameterNameDiscoverer = DefaultParameterNameDiscoverer()
22+
/**
23+
* Tests for Kotlin support in [DefaultParameterNameDiscoverer].
24+
*
25+
* @author Sebastien Deleuze
26+
*/
27+
class KotlinDefaultParameterNameDiscovererTests :
28+
AbstractKotlinReflectionParameterNameDiscovererTests(DefaultParameterNameDiscoverer()){
2529

2630
enum class MyEnum {
2731
ONE, TWO
@@ -31,6 +35,7 @@ class KotlinDefaultParameterNameDiscovererTests {
3135
fun getParameterNamesOnEnum() {
3236
val constructor = MyEnum::class.java.declaredConstructors[0]
3337
val actualParams = parameterNameDiscoverer.getParameterNames(constructor)
34-
assertThat(actualParams!!.size).isEqualTo(2)
38+
assertThat(actualParams).containsExactly("\$enum\$name", "\$enum\$ordinal")
3539
}
40+
3641
}
Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,48 +16,10 @@
1616

1717
package org.springframework.core
1818

19-
import org.assertj.core.api.Assertions.assertThat
20-
import org.junit.jupiter.api.Test
21-
22-
import org.springframework.util.ReflectionUtils
23-
2419
/**
25-
* Tests for KotlinReflectionParameterNameDiscoverer
20+
* Tests for [KotlinReflectionParameterNameDiscoverer].
21+
*
22+
* @author Sebastien Deleuze
2623
*/
27-
class KotlinReflectionParameterNameDiscovererTests {
28-
29-
private val parameterNameDiscoverer = KotlinReflectionParameterNameDiscoverer()
30-
31-
@Test
32-
fun getParameterNamesOnInterface() {
33-
val method = ReflectionUtils.findMethod(MessageService::class.java,"sendMessage", String::class.java)!!
34-
val actualParams = parameterNameDiscoverer.getParameterNames(method)
35-
assertThat(actualParams).contains("message")
36-
}
37-
38-
@Test
39-
fun getParameterNamesOnClass() {
40-
val method = ReflectionUtils.findMethod(MessageServiceImpl::class.java,"sendMessage", String::class.java)!!
41-
val actualParams = parameterNameDiscoverer.getParameterNames(method)
42-
assertThat(actualParams).contains("message")
43-
}
44-
45-
@Test
46-
fun getParameterNamesOnExtensionMethod() {
47-
val method = ReflectionUtils.findMethod(UtilityClass::class.java, "identity", String::class.java)!!
48-
val actualParams = parameterNameDiscoverer.getParameterNames(method)!!
49-
assertThat(actualParams).contains("\$receiver")
50-
}
51-
52-
interface MessageService {
53-
fun sendMessage(message: String)
54-
}
55-
56-
class MessageServiceImpl {
57-
fun sendMessage(message: String) = message
58-
}
59-
60-
class UtilityClass {
61-
fun String.identity() = this
62-
}
63-
}
24+
class KotlinReflectionParameterNameDiscovererTests :
25+
AbstractKotlinReflectionParameterNameDiscovererTests(KotlinReflectionParameterNameDiscoverer())

0 commit comments

Comments
 (0)