Polish KotlinConstructorParametersBinderTests
See gh-25532
This commit is contained in:
parent
e1ad2cdab3
commit
bd9dd9d5eb
|
|
@ -46,6 +46,10 @@ public class MockConfigurationPropertySource implements IterableConfigurationPro
|
||||||
OriginTrackedValue.of(value, MockOrigin.of(origin)));
|
OriginTrackedValue.of(value, MockOrigin.of(origin)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MockConfigurationPropertySource(Map<String, String> configs) {
|
||||||
|
configs.forEach(this::put);
|
||||||
|
}
|
||||||
|
|
||||||
public void put(String name, String value) {
|
public void put(String name, String value) {
|
||||||
put(ConfigurationPropertyName.of(name), value);
|
put(ConfigurationPropertyName.of(name), value);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,107 +16,101 @@ class KotlinConstructorParametersBinderTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class should create bound bean`() {
|
fun `Bind to class should create bound bean`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource(mapOf("foo.int-value" to "12",
|
||||||
source.put("foo.int-value", "12")
|
"foo.int-value" to "12",
|
||||||
source.put("foo.long-value", "34")
|
"foo.long-value" to "34",
|
||||||
source.put("foo.boolean-value", "true")
|
"foo.boolean-value" to "true",
|
||||||
source.put("foo.string-value", "foo")
|
"foo.string-value" to "foo",
|
||||||
source.put("foo.enum-value", "foo-bar")
|
"foo.enum-value" to "foo-bar"))
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bind("foo", Bindable.of(ExampleValueBean::class.java)).get()
|
val bean = binder.bind("foo", Bindable.of(ExampleValueBean::class.java)).get()
|
||||||
assertThat(bean.intValue).isEqualTo(12)
|
assertThat(bean.intValue).isEqualTo(12)
|
||||||
assertThat(bean.longValue).isEqualTo(34)
|
assertThat(bean.longValue).isEqualTo(34)
|
||||||
assertThat(bean.booleanValue).isTrue()
|
assertThat(bean.booleanValue).isTrue
|
||||||
assertThat(bean.stringValue).isEqualTo("foo")
|
assertThat(bean.stringValue).isEqualTo("foo")
|
||||||
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
|
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class when has no prefix should create bound bean`() {
|
fun `Bind to class when has no prefix should create bound bean`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource(mapOf("int-value" to "12",
|
||||||
source.put("int-value", "12")
|
"long-value" to "34",
|
||||||
source.put("long-value", "34")
|
"boolean-value" to "true",
|
||||||
source.put("boolean-value", "true")
|
"string-value" to "foo",
|
||||||
source.put("string-value", "foo")
|
"enum-value" to "foo-bar"))
|
||||||
source.put("enum-value", "foo-bar")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bind(ConfigurationPropertyName.of(""),
|
val bean = binder.bind(ConfigurationPropertyName.of(""),
|
||||||
Bindable.of(ExampleValueBean::class.java)).get()
|
Bindable.of(ExampleValueBean::class.java)).get()
|
||||||
assertThat(bean.intValue).isEqualTo(12)
|
assertThat(bean.intValue).isEqualTo(12)
|
||||||
assertThat(bean.longValue).isEqualTo(34)
|
assertThat(bean.longValue).isEqualTo(34)
|
||||||
assertThat(bean.booleanValue).isTrue()
|
assertThat(bean.booleanValue).isTrue
|
||||||
assertThat(bean.stringValue).isEqualTo("foo")
|
assertThat(bean.stringValue).isEqualTo("foo")
|
||||||
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
|
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to data class should create bound bean`() {
|
fun `Bind to data class should create bound bean`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource(mapOf("foo.int-value" to "12",
|
||||||
source.put("foo.int-value", "12")
|
"foo.long-value" to "34",
|
||||||
source.put("foo.long-value", "34")
|
"foo.boolean-value" to "true",
|
||||||
source.put("foo.boolean-value", "true")
|
"foo.string-value" to "foo",
|
||||||
source.put("foo.string-value", "foo")
|
"foo.enum-value" to "foo-bar"))
|
||||||
source.put("foo.enum-value", "foo-bar")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bind("foo", Bindable.of(ExampleDataClassBean::class.java)).get()
|
val bean = binder.bind("foo", Bindable.of(ExampleDataClassBean::class.java)).get()
|
||||||
assertThat(bean.intValue).isEqualTo(12)
|
val expectedBean = ExampleDataClassBean(intValue = 12,
|
||||||
assertThat(bean.longValue).isEqualTo(34)
|
longValue = 34,
|
||||||
assertThat(bean.booleanValue).isTrue()
|
booleanValue = true,
|
||||||
assertThat(bean.stringValue).isEqualTo("foo")
|
stringValue = "foo",
|
||||||
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
|
enumValue = ExampleEnum.FOO_BAR)
|
||||||
|
assertThat(bean).isEqualTo(expectedBean)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class with multiple constructors and primary constructor should bind`() {
|
fun `Bind to class with multiple constructors and primary constructor should bind`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.int-value", "12")
|
||||||
source.put("foo.int-value", "12")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bindable = binder.bind("foo", Bindable.of(
|
val bindable = binder.bind("foo", Bindable.of(
|
||||||
MultipleConstructorsWithPrimaryConstructorBean::class.java))
|
MultipleConstructorsWithPrimaryConstructorBean::class.java))
|
||||||
assertThat(bindable.isBound).isTrue()
|
assertThat(bindable.isBound).isTrue
|
||||||
assertThat(bindable.get().intValue).isEqualTo(12)
|
assertThat(bindable.get().intValue).isEqualTo(12)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class with multiple constructors should not bind`() {
|
fun `Bind to class with multiple constructors should not bind`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.int-value", "12")
|
||||||
source.put("foo.int-value", "12")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bindable = binder.bind("foo", Bindable.of(
|
val bindable = binder.bind("foo", Bindable.of(
|
||||||
MultipleConstructorsBean::class.java))
|
MultipleConstructorsBean::class.java))
|
||||||
assertThat(bindable.isBound).isFalse()
|
assertThat(bindable.isBound).isFalse
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class with only default constructor should not bind`() {
|
fun `Bind to class with only default constructor should not bind`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.int-value", "12")
|
||||||
source.put("foo.int-value", "12")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bindable = binder.bind("foo", Bindable.of(
|
val bindable = binder.bind("foo", Bindable.of(
|
||||||
DefaultConstructorBean::class.java))
|
DefaultConstructorBean::class.java))
|
||||||
assertThat(bindable.isBound).isFalse()
|
assertThat(bindable.isBound).isFalse
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class should bind nested`() {
|
fun `Bind to class should bind nested`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource(mapOf("foo.value-bean.int-value" to "123",
|
||||||
source.put("foo.value-bean.int-value", "123")
|
"foo.value-bean.long-value" to "34",
|
||||||
source.put("foo.value-bean.long-value", "34")
|
"foo.value-bean.boolean-value" to "true",
|
||||||
source.put("foo.value-bean.boolean-value", "true")
|
"foo.value-bean.string-value" to "foo"))
|
||||||
source.put("foo.value-bean.string-value", "foo")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bind("foo", Bindable.of(ExampleNestedBean::class.java)).get()
|
val bean = binder.bind("foo", Bindable.of(ExampleNestedBean::class.java)).get()
|
||||||
assertThat(bean.valueBean.intValue).isEqualTo(123)
|
assertThat(bean.valueBean.intValue).isEqualTo(123)
|
||||||
assertThat(bean.valueBean.longValue).isEqualTo(34)
|
assertThat(bean.valueBean.longValue).isEqualTo(34)
|
||||||
assertThat(bean.valueBean.booleanValue).isTrue()
|
assertThat(bean.valueBean.booleanValue).isTrue
|
||||||
assertThat(bean.valueBean.stringValue).isEqualTo("foo")
|
assertThat(bean.valueBean.stringValue).isEqualTo("foo")
|
||||||
assertThat(bean.valueBean.enumValue).isNull()
|
assertThat(bean.valueBean.enumValue).isNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class with no value for optional should use null`() {
|
fun `Bind to class with no value for optional should use null`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.string-value", "foo")
|
||||||
source.put("foo.string-value", "foo")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bind("foo", Bindable.of(
|
val bean = binder.bind("foo", Bindable.of(
|
||||||
ExampleValueBean::class.java)).get()
|
ExampleValueBean::class.java)).get()
|
||||||
|
|
@ -129,31 +123,28 @@ class KotlinConstructorParametersBinderTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class with no value for primitive should use default value`() {
|
fun `Bind to class with no value for primitive should use default value`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.string-value", "foo")
|
||||||
source.put("foo.string-value", "foo")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bind("foo", Bindable.of(
|
val bean = binder.bind("foo", Bindable.of(
|
||||||
ExamplePrimitiveDefaultBean::class.java)).get()
|
ExamplePrimitiveDefaultBean::class.java)).get()
|
||||||
assertThat(bean.intValue).isEqualTo(0)
|
assertThat(bean.intValue).isEqualTo(0)
|
||||||
assertThat(bean.longValue).isEqualTo(0)
|
assertThat(bean.longValue).isEqualTo(0)
|
||||||
assertThat(bean.booleanValue).isFalse()
|
assertThat(bean.booleanValue).isFalse
|
||||||
assertThat(bean.stringValue).isEqualTo("foo")
|
assertThat(bean.stringValue).isEqualTo("foo")
|
||||||
assertThat(bean.enumValue).isNull()
|
assertThat(bean.enumValue).isNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to class with no value and default value should return unbound`() {
|
fun `Bind to class with no value and default value should return unbound`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.string-value", "foo")
|
||||||
source.put("foo.string-value", "foo")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
assertThat(binder.bind("foo", Bindable.of(
|
assertThat(binder.bind("foo", Bindable.of(
|
||||||
ExampleDefaultValueBean::class.java)).isBound()).isFalse();
|
ExampleDefaultValueBean::class.java)).isBound).isFalse
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind or create to class with no value and default value should return default value`() {
|
fun `Bind or create to class with no value and default value should return default value`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.string-value", "foo")
|
||||||
source.put("foo.string-value", "foo")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bindOrCreate("foo", Bindable.of(
|
val bean = binder.bindOrCreate("foo", Bindable.of(
|
||||||
ExampleDefaultValueBean::class.java))
|
ExampleDefaultValueBean::class.java))
|
||||||
|
|
@ -164,39 +155,45 @@ class KotlinConstructorParametersBinderTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to data class with no value should use default value`() {
|
fun `Bind to data class with no value should use default value`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.enum-value", "foo-bar")
|
||||||
source.put("foo.enum-value", "foo-bar")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bind("foo", Bindable.of(ExampleDataClassBean::class.java)).get()
|
val bean = binder.bind("foo", Bindable.of(ExampleDataClassBean::class.java)).get()
|
||||||
assertThat(bean.intValue).isEqualTo(5)
|
val expectedBean = ExampleDataClassBean(intValue = 5,
|
||||||
assertThat(bean.longValue).isEqualTo(42)
|
longValue = 42,
|
||||||
assertThat(bean.booleanValue).isFalse()
|
booleanValue = false,
|
||||||
assertThat(bean.stringValue).isEqualTo("my data")
|
stringValue = "my data",
|
||||||
assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR)
|
enumValue = ExampleEnum.FOO_BAR)
|
||||||
|
assertThat(bean).isEqualTo(expectedBean)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to data class with generics`() {
|
fun `Bind to data class with generics`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.value.bar", "baz")
|
||||||
source.put("foo.value.bar", "baz")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val type = ResolvableType.forClassWithGenerics(Map::class.java, String::class.java,
|
val type = ResolvableType.forClassWithGenerics(Map::class.java, String::class.java,
|
||||||
String::class.java)
|
String::class.java)
|
||||||
val bean = binder.bind("foo", Bindable
|
val bean = binder.bind("foo", Bindable
|
||||||
.of<GenericValue<Map<String, String>>>(ResolvableType.forClassWithGenerics(GenericValue::class.java, type)))
|
.of<GenericValue<Map<String, String>>>(ResolvableType.forClassWithGenerics(GenericValue::class.java, type)))
|
||||||
.get()
|
.get()
|
||||||
assertThat(bean.value.get("bar")).isEqualTo("baz");
|
assertThat(bean.value["bar"]).isEqualTo("baz")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Bind to named constructor parameter`() {
|
fun `Bind to named constructor parameter`() {
|
||||||
val source = MockConfigurationPropertySource()
|
val source = MockConfigurationPropertySource("foo.string-value", "test")
|
||||||
source.put("foo.string-value", "test")
|
|
||||||
val binder = Binder(source)
|
val binder = Binder(source)
|
||||||
val bean = binder.bind("foo", Bindable.of(ExampleNamedParameterBean::class.java)).get()
|
val bean = binder.bind("foo", Bindable.of(ExampleNamedParameterBean::class.java)).get()
|
||||||
assertThat(bean.stringDataValue).isEqualTo("test")
|
assertThat(bean.stringDataValue).isEqualTo("test")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Bind to singleton object`() {
|
||||||
|
val source = MockConfigurationPropertySource("foo.string-value", "test")
|
||||||
|
val binder = Binder(source)
|
||||||
|
val bean = binder.bind("foo", Bindable.of(ExampleSingletonBean::class.java)).get()
|
||||||
|
assertThat(bean.stringValue).isEqualTo("test")
|
||||||
|
}
|
||||||
|
|
||||||
class ExampleValueBean(val intValue: Int?, val longValue: Long?,
|
class ExampleValueBean(val intValue: Int?, val longValue: Long?,
|
||||||
val booleanValue: Boolean?, val stringValue: String?,
|
val booleanValue: Boolean?, val stringValue: String?,
|
||||||
val enumValue: ExampleEnum?)
|
val enumValue: ExampleEnum?)
|
||||||
|
|
@ -244,4 +241,7 @@ class KotlinConstructorParametersBinderTests {
|
||||||
val value: T
|
val value: T
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
object ExampleSingletonBean {
|
||||||
|
var stringValue: String? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue