Skip to content

Commit e2fd04d

Browse files
committed
Add a [] Kotlin extension for PropertyResolver#getRequiredProperty
1 parent f72e0da commit e2fd04d

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException
2222
import org.springframework.beans.factory.getBean
2323
import org.springframework.context.support.BeanDefinitionDsl.*
2424
import org.springframework.core.env.SimpleCommandLinePropertySource
25+
import org.springframework.core.env.get
2526

2627
class BeanDefinitionDslTests {
2728

@@ -76,7 +77,7 @@ class BeanDefinitionDslTests {
7677
val beans = beans {
7778
bean<Foo>()
7879
bean<Bar>("bar")
79-
bean { FooFoo(it.env.getProperty("name")) }
80+
bean { FooFoo(it.env["name"]) }
8081
environment({it.activeProfiles.contains("baz")}) {
8182
bean { Baz(it.ref()) }
8283
bean { Baz(it.ref("bar")) }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2002-2017 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+
* http://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.env
18+
19+
/**
20+
* Extension for [PropertyResolver.getRequiredProperty] providing Array like getter.
21+
*
22+
* ```kotlin
23+
* env["name"] = "Seb"
24+
* ```
25+
*
26+
* @author Sebastien Deleuze
27+
* @since 5.0
28+
*/
29+
operator fun PropertyResolver.get(key: String) : String = getRequiredProperty(key)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2002-2017 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+
* http://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.env
18+
19+
import org.junit.Test
20+
import org.junit.runner.RunWith
21+
import org.mockito.Answers
22+
import org.mockito.Mock
23+
import org.mockito.Mockito
24+
import org.mockito.junit.MockitoJUnitRunner
25+
26+
/**
27+
* Mock object based tests for PropertyResolver Kotlin extensions
28+
*
29+
* @author Sebastien Deleuze
30+
*/
31+
@RunWith(MockitoJUnitRunner::class)
32+
class PropertyResolverExtensionsTests {
33+
34+
@Mock(answer = Answers.RETURNS_MOCKS)
35+
lateinit var propertyResolver: PropertyResolver
36+
37+
@Suppress("UNUSED_VARIABLE")
38+
@Test
39+
fun `get operator`() {
40+
val name = propertyResolver["name"]
41+
Mockito.verify(propertyResolver, Mockito.times(1)).getRequiredProperty("name")
42+
}
43+
44+
}

0 commit comments

Comments
 (0)