Skip to content

Commit a7a29a8

Browse files
committed
Revisit PropertyResolver Kotlin extensions
Issue: SPR-16883
1 parent ab9b575 commit a7a29a8

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class BeanDefinitionDslTests {
8282
val beans = beans {
8383
bean<Foo>()
8484
bean<Bar>("bar")
85-
bean { FooFoo(env["name"]) }
85+
bean { FooFoo(env["name"]!!) }
8686
environment( { activeProfiles.contains("baz") } ) {
8787
bean { Baz(ref()) }
8888
bean { Baz(ref("bar")) }
@@ -109,7 +109,7 @@ class BeanDefinitionDslTests {
109109
@Test // SPR-16412
110110
fun `Declare beans depending on environment properties`() {
111111
val beans = beans {
112-
val n = env["number-of-beans"].toInt()
112+
val n = env["number-of-beans"]!!.toInt()
113113
for (i in 1..n) {
114114
bean("string$i") { Foo() }
115115
}

spring-core/src/main/kotlin/org/springframework/core/env/PropertyResolverExtensions.kt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -14,16 +14,40 @@
1414
* limitations under the License.
1515
*/
1616

17+
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")
18+
1719
package org.springframework.core.env
1820

1921
/**
20-
* Extension for [PropertyResolver.getRequiredProperty] providing Array like getter.
22+
* Extension for [PropertyResolver.getProperty] providing Array like getter returning a
23+
* nullable [String].
2124
*
2225
* ```kotlin
23-
* env["name"] = "Seb"
26+
* val name = env["name"] ?: "Seb"
2427
* ```
2528
*
2629
* @author Sebastien Deleuze
2730
* @since 5.0
2831
*/
29-
operator fun PropertyResolver.get(key: String) : String = getRequiredProperty(key)
32+
operator fun PropertyResolver.get(key: String) : String? = getProperty(key)
33+
34+
35+
/**
36+
* Extension for [PropertyResolver.getProperty] providing a `getProperty<Foo>(...)`
37+
* variant returning a nullable [String].
38+
*
39+
* @author Sebastien Deleuze
40+
* @since 5.1
41+
*/
42+
inline fun <reified T: Any?> PropertyResolver.getProperty(key: String) : T? =
43+
getProperty(key, T::class.java)
44+
45+
/**
46+
* Extension for [PropertyResolver.getRequiredProperty] providing a
47+
* `getRequiredProperty<Foo>(...)` variant.
48+
*
49+
* @author Sebastien Deleuze
50+
* @since 5.1
51+
*/
52+
inline fun <reified T: Any> PropertyResolver.getRequiredProperty(key: String) : T =
53+
getRequiredProperty(key, T::class.java)

spring-core/src/test/kotlin/org/springframework/core/env/PropertyResolverExtensionsTests.kt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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,9 +16,12 @@
1616

1717
package org.springframework.core.env
1818

19+
import com.nhaarman.mockito_kotlin.any
20+
import com.nhaarman.mockito_kotlin.whenever
1921
import org.junit.Test
2022
import org.junit.runner.RunWith
2123
import org.mockito.Answers
24+
import org.mockito.ArgumentMatchers.eq
2225
import org.mockito.Mock
2326
import org.mockito.Mockito
2427
import org.mockito.junit.MockitoJUnitRunner
@@ -29,16 +32,30 @@ import org.mockito.junit.MockitoJUnitRunner
2932
* @author Sebastien Deleuze
3033
*/
3134
@RunWith(MockitoJUnitRunner::class)
35+
@Suppress("UNUSED_VARIABLE")
3236
class PropertyResolverExtensionsTests {
3337

3438
@Mock(answer = Answers.RETURNS_MOCKS)
3539
lateinit var propertyResolver: PropertyResolver
3640

37-
@Suppress("UNUSED_VARIABLE")
3841
@Test
3942
fun `get operator`() {
40-
val name = propertyResolver["name"]
41-
Mockito.verify(propertyResolver, Mockito.times(1)).getRequiredProperty("name")
43+
val name = propertyResolver["name"] ?: "foo"
44+
Mockito.verify(propertyResolver, Mockito.times(1)).getProperty("name")
45+
}
46+
47+
@Test
48+
fun `getProperty extension`() {
49+
whenever(propertyResolver.getProperty(any(), eq(String::class.java))).thenReturn("foo")
50+
val name = propertyResolver.getProperty<String>("name") ?: "foo"
51+
Mockito.verify(propertyResolver, Mockito.times(1)).getProperty("name", String::class.java)
52+
}
53+
54+
@Test
55+
fun `getRequiredProperty extension`() {
56+
whenever(propertyResolver.getRequiredProperty(any(), eq(String::class.java))).thenReturn("foo")
57+
val name = propertyResolver.getRequiredProperty<String>("name")
58+
Mockito.verify(propertyResolver, Mockito.times(1)).getRequiredProperty("name", String::class.java)
4259
}
4360

4461
}

0 commit comments

Comments
 (0)