Revisit PropertyResolver Kotlin extensions
Issue: SPR-16883
This commit is contained in:
parent
ab9b575da6
commit
a7a29a8226
|
|
@ -82,7 +82,7 @@ class BeanDefinitionDslTests {
|
||||||
val beans = beans {
|
val beans = beans {
|
||||||
bean<Foo>()
|
bean<Foo>()
|
||||||
bean<Bar>("bar")
|
bean<Bar>("bar")
|
||||||
bean { FooFoo(env["name"]) }
|
bean { FooFoo(env["name"]!!) }
|
||||||
environment( { activeProfiles.contains("baz") } ) {
|
environment( { activeProfiles.contains("baz") } ) {
|
||||||
bean { Baz(ref()) }
|
bean { Baz(ref()) }
|
||||||
bean { Baz(ref("bar")) }
|
bean { Baz(ref("bar")) }
|
||||||
|
|
@ -109,7 +109,7 @@ class BeanDefinitionDslTests {
|
||||||
@Test // SPR-16412
|
@Test // SPR-16412
|
||||||
fun `Declare beans depending on environment properties`() {
|
fun `Declare beans depending on environment properties`() {
|
||||||
val beans = beans {
|
val beans = beans {
|
||||||
val n = env["number-of-beans"].toInt()
|
val n = env["number-of-beans"]!!.toInt()
|
||||||
for (i in 1..n) {
|
for (i in 1..n) {
|
||||||
bean("string$i") { Foo() }
|
bean("string$i") { Foo() }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2018 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.
|
||||||
|
|
@ -14,16 +14,40 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
||||||
|
|
||||||
package org.springframework.core.env
|
package org.springframework.core.env
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension for [PropertyResolver.getRequiredProperty] providing Array like getter.
|
* Extension for [PropertyResolver.getProperty] providing Array like getter returning a
|
||||||
|
* nullable [String].
|
||||||
*
|
*
|
||||||
* ```kotlin
|
* ```kotlin
|
||||||
* env["name"] = "Seb"
|
* val name = env["name"] ?: "Seb"
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
operator fun PropertyResolver.get(key: String) : String = getRequiredProperty(key)
|
operator fun PropertyResolver.get(key: String) : String? = getProperty(key)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension for [PropertyResolver.getProperty] providing a `getProperty<Foo>(...)`
|
||||||
|
* variant returning a nullable [String].
|
||||||
|
*
|
||||||
|
* @author Sebastien Deleuze
|
||||||
|
* @since 5.1
|
||||||
|
*/
|
||||||
|
inline fun <reified T: Any?> PropertyResolver.getProperty(key: String) : T? =
|
||||||
|
getProperty(key, T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension for [PropertyResolver.getRequiredProperty] providing a
|
||||||
|
* `getRequiredProperty<Foo>(...)` variant.
|
||||||
|
*
|
||||||
|
* @author Sebastien Deleuze
|
||||||
|
* @since 5.1
|
||||||
|
*/
|
||||||
|
inline fun <reified T: Any> PropertyResolver.getRequiredProperty(key: String) : T =
|
||||||
|
getRequiredProperty(key, T::class.java)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2018 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,9 +16,12 @@
|
||||||
|
|
||||||
package org.springframework.core.env
|
package org.springframework.core.env
|
||||||
|
|
||||||
|
import com.nhaarman.mockito_kotlin.any
|
||||||
|
import com.nhaarman.mockito_kotlin.whenever
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
import org.mockito.Answers
|
import org.mockito.Answers
|
||||||
|
import org.mockito.ArgumentMatchers.eq
|
||||||
import org.mockito.Mock
|
import org.mockito.Mock
|
||||||
import org.mockito.Mockito
|
import org.mockito.Mockito
|
||||||
import org.mockito.junit.MockitoJUnitRunner
|
import org.mockito.junit.MockitoJUnitRunner
|
||||||
|
|
@ -29,16 +32,30 @@ import org.mockito.junit.MockitoJUnitRunner
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
*/
|
*/
|
||||||
@RunWith(MockitoJUnitRunner::class)
|
@RunWith(MockitoJUnitRunner::class)
|
||||||
|
@Suppress("UNUSED_VARIABLE")
|
||||||
class PropertyResolverExtensionsTests {
|
class PropertyResolverExtensionsTests {
|
||||||
|
|
||||||
@Mock(answer = Answers.RETURNS_MOCKS)
|
@Mock(answer = Answers.RETURNS_MOCKS)
|
||||||
lateinit var propertyResolver: PropertyResolver
|
lateinit var propertyResolver: PropertyResolver
|
||||||
|
|
||||||
@Suppress("UNUSED_VARIABLE")
|
|
||||||
@Test
|
@Test
|
||||||
fun `get operator`() {
|
fun `get operator`() {
|
||||||
val name = propertyResolver["name"]
|
val name = propertyResolver["name"] ?: "foo"
|
||||||
Mockito.verify(propertyResolver, Mockito.times(1)).getRequiredProperty("name")
|
Mockito.verify(propertyResolver, Mockito.times(1)).getProperty("name")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `getProperty extension`() {
|
||||||
|
whenever(propertyResolver.getProperty(any(), eq(String::class.java))).thenReturn("foo")
|
||||||
|
val name = propertyResolver.getProperty<String>("name") ?: "foo"
|
||||||
|
Mockito.verify(propertyResolver, Mockito.times(1)).getProperty("name", String::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `getRequiredProperty extension`() {
|
||||||
|
whenever(propertyResolver.getRequiredProperty(any(), eq(String::class.java))).thenReturn("foo")
|
||||||
|
val name = propertyResolver.getRequiredProperty<String>("name")
|
||||||
|
Mockito.verify(propertyResolver, Mockito.times(1)).getRequiredProperty("name", String::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue