/
KotlinConstructorParametersBinderTests.kt
235 lines (205 loc) · 8.19 KB
/
KotlinConstructorParametersBinderTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.springframework.boot.context.properties.bind
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.boot.context.properties.source.ConfigurationPropertyName
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource
import org.springframework.core.ResolvableType
/**
* Tests for `ConstructorParametersBinder`.
*
* @author Stephane Nicoll
*/
class KotlinConstructorParametersBinderTests {
@Test
fun `Bind to class should create bound bean`() {
val source = MockConfigurationPropertySource()
source.put("foo.int-value", "12")
source.put("foo.long-value", "34")
source.put("foo.boolean-value", "true")
source.put("foo.string-value", "foo")
source.put("foo.enum-value", "foo-bar")
val binder = Binder(source)
val bean = binder.bind("foo", Bindable.of(ExampleValueBean::class.java)).get()
assertThat(bean.intValue).isEqualTo(12)
assertThat(bean.longValue).isEqualTo(34)
assertThat(bean.booleanValue).isTrue()
assertThat(bean.stringValue).isEqualTo("foo")
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
}
@Test
fun `Bind to class when has no prefix should create bound bean`() {
val source = MockConfigurationPropertySource()
source.put("int-value", "12")
source.put("long-value", "34")
source.put("boolean-value", "true")
source.put("string-value", "foo")
source.put("enum-value", "foo-bar")
val binder = Binder(source)
val bean = binder.bind(ConfigurationPropertyName.of(""),
Bindable.of(ExampleValueBean::class.java)).get()
assertThat(bean.intValue).isEqualTo(12)
assertThat(bean.longValue).isEqualTo(34)
assertThat(bean.booleanValue).isTrue()
assertThat(bean.stringValue).isEqualTo("foo")
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
}
@Test
fun `Bind to data class should create bound bean`() {
val source = MockConfigurationPropertySource()
source.put("foo.int-value", "12")
source.put("foo.long-value", "34")
source.put("foo.boolean-value", "true")
source.put("foo.string-value", "foo")
source.put("foo.enum-value", "foo-bar")
val binder = Binder(source)
val bean = binder.bind("foo", Bindable.of(ExampleDataClassBean::class.java)).get()
assertThat(bean.intValue).isEqualTo(12)
assertThat(bean.longValue).isEqualTo(34)
assertThat(bean.booleanValue).isTrue()
assertThat(bean.stringValue).isEqualTo("foo")
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
}
@Test
fun `Bind to class with multiple constructors and primary constructor should bind`() {
val source = MockConfigurationPropertySource()
source.put("foo.int-value", "12")
val binder = Binder(source)
val bindable = binder.bind("foo", Bindable.of(
MultipleConstructorsWithPrimaryConstructorBean::class.java))
assertThat(bindable.isBound).isTrue()
assertThat(bindable.get().intValue).isEqualTo(12)
}
@Test
fun `Bind to class with multiple constructors should not bind`() {
val source = MockConfigurationPropertySource()
source.put("foo.int-value", "12")
val binder = Binder(source)
val bindable = binder.bind("foo", Bindable.of(
MultipleConstructorsBean::class.java))
assertThat(bindable.isBound).isFalse()
}
@Test
fun `Bind to class with only default constructor should not bind`() {
val source = MockConfigurationPropertySource()
source.put("foo.int-value", "12")
val binder = Binder(source)
val bindable = binder.bind("foo", Bindable.of(
DefaultConstructorBean::class.java))
assertThat(bindable.isBound).isFalse()
}
@Test
fun `Bind to class should bind nested`() {
val source = MockConfigurationPropertySource()
source.put("foo.value-bean.int-value", "123")
source.put("foo.value-bean.long-value", "34")
source.put("foo.value-bean.boolean-value", "true")
source.put("foo.value-bean.string-value", "foo")
val binder = Binder(source)
val bean = binder.bind("foo", Bindable.of(ExampleNestedBean::class.java)).get()
assertThat(bean.valueBean.intValue).isEqualTo(123)
assertThat(bean.valueBean.longValue).isEqualTo(34)
assertThat(bean.valueBean.booleanValue).isTrue()
assertThat(bean.valueBean.stringValue).isEqualTo("foo")
assertThat(bean.valueBean.enumValue).isNull()
}
@Test
fun `Bind to class with no value for optional should use null`() {
val source = MockConfigurationPropertySource()
source.put("foo.string-value", "foo")
val binder = Binder(source)
val bean = binder.bind("foo", Bindable.of(
ExampleValueBean::class.java)).get()
assertThat(bean.intValue).isNull()
assertThat(bean.longValue).isNull()
assertThat(bean.booleanValue).isNull()
assertThat(bean.stringValue).isEqualTo("foo")
assertThat(bean.enumValue).isNull()
}
@Test
fun `Bind to class with no value for primitive should use default value`() {
val source = MockConfigurationPropertySource()
source.put("foo.string-value", "foo")
val binder = Binder(source)
val bean = binder.bind("foo", Bindable.of(
ExamplePrimitiveDefaultBean::class.java)).get()
assertThat(bean.intValue).isEqualTo(0)
assertThat(bean.longValue).isEqualTo(0)
assertThat(bean.booleanValue).isFalse()
assertThat(bean.stringValue).isEqualTo("foo")
assertThat(bean.enumValue).isNull()
}
@Test
fun `Bind to class with no value and default value should return unbound`() {
val source = MockConfigurationPropertySource()
source.put("foo.string-value", "foo")
val binder = Binder(source)
assertThat(binder.bind("foo", Bindable.of(
ExampleDefaultValueBean::class.java)).isBound()).isFalse();
}
@Test
fun `Bind or create to class with no value and default value should return default value`() {
val source = MockConfigurationPropertySource()
source.put("foo.string-value", "foo")
val binder = Binder(source)
val bean = binder.bindOrCreate("foo", Bindable.of(
ExampleDefaultValueBean::class.java))
assertThat(bean.intValue).isEqualTo(5)
assertThat(bean.stringsList).containsOnly("a", "b", "c")
assertThat(bean.customList).containsOnly("x,y,z")
}
@Test
fun `Bind to data class with no value should use default value`() {
val source = MockConfigurationPropertySource()
source.put("foo.enum-value", "foo-bar")
val binder = Binder(source)
val bean = binder.bind("foo", Bindable.of(ExampleDataClassBean::class.java)).get()
assertThat(bean.intValue).isEqualTo(5)
assertThat(bean.longValue).isEqualTo(42)
assertThat(bean.booleanValue).isFalse()
assertThat(bean.stringValue).isEqualTo("my data")
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
}
@Test
fun `Bind to data class with generics`() {
val source = MockConfigurationPropertySource()
source.put("foo.value.bar", "baz")
val binder = Binder(source)
val type = ResolvableType.forClassWithGenerics(Map::class.java, String::class.java,
String::class.java)
val bean = binder.bind("foo", Bindable
.of<GenericValue<Map<String, String>>>(ResolvableType.forClassWithGenerics(GenericValue::class.java, type)))
.get()
assertThat(bean.value.get("bar")).isEqualTo("baz");
}
class ExampleValueBean(val intValue: Int?, val longValue: Long?,
val booleanValue: Boolean?, val stringValue: String?,
val enumValue: ExampleEnum?)
class ExamplePrimitiveDefaultBean(val intValue: Int = 0, val longValue: Long = 0,
val booleanValue: Boolean = false, val stringValue: String?,
val enumValue: ExampleEnum?)
enum class ExampleEnum {
FOO_BAR,
BAR_BAZ
}
@Suppress("unused", "UNUSED_PARAMETER")
class MultipleConstructorsWithPrimaryConstructorBean(val intValue: Int) {
constructor(intValue: Int, longValue: Long) : this(intValue)
}
@Suppress("unused", "UNUSED_PARAMETER")
class MultipleConstructorsBean {
constructor(intValue: Int)
constructor(intValue: Int, longValue: Long) : this(intValue)
}
class DefaultConstructorBean
class ExampleNestedBean(val valueBean: ExampleValueBean)
class ExampleDefaultValueBean(val intValue: Int = 5,
val stringsList: List<String> = listOf("a", "b", "c"),
val customList: List<String> = listOf("x,y,z"))
data class ExampleDataClassBean(val intValue: Int = 5, val longValue: Long = 42,
val booleanValue: Boolean = false,
val stringValue: String = "my data",
val enumValue: ExampleEnum = ExampleEnum.BAR_BAZ)
data class GenericValue<T>(
val value: T
)
}