From bd9dd9d5ebb28dbd011c652ca7eea25a8e1479c1 Mon Sep 17 00:00:00 2001 From: katielevy1 Date: Sun, 7 Mar 2021 13:33:10 -0500 Subject: [PATCH 1/2] Polish KotlinConstructorParametersBinderTests See gh-25532 --- .../MockConfigurationPropertySource.java | 4 + .../KotlinConstructorParametersBinderTests.kt | 126 +++++++++--------- 2 files changed, 67 insertions(+), 63 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/MockConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/MockConfigurationPropertySource.java index 5ef62bf66e..95d2a30382 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/MockConfigurationPropertySource.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/MockConfigurationPropertySource.java @@ -46,6 +46,10 @@ public class MockConfigurationPropertySource implements IterableConfigurationPro OriginTrackedValue.of(value, MockOrigin.of(origin))); } + public MockConfigurationPropertySource(Map configs) { + configs.forEach(this::put); + } + public void put(String name, String value) { put(ConfigurationPropertyName.of(name), value); } diff --git a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt index 71326c7c95..561dbb720b 100644 --- a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt +++ b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt @@ -16,107 +16,101 @@ 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 source = MockConfigurationPropertySource(mapOf("foo.int-value" to "12", + "foo.int-value" to "12", + "foo.long-value" to "34", + "foo.boolean-value" to "true", + "foo.string-value" to "foo", + "foo.enum-value" to "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.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 source = MockConfigurationPropertySource(mapOf("int-value" to "12", + "long-value" to "34", + "boolean-value" to "true", + "string-value" to "foo", + "enum-value" to "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.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 source = MockConfigurationPropertySource(mapOf("foo.int-value" to "12", + "foo.long-value" to "34", + "foo.boolean-value" to "true", + "foo.string-value" to "foo", + "foo.enum-value" to "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) + val expectedBean = ExampleDataClassBean(intValue = 12, + longValue = 34, + booleanValue = true, + stringValue = "foo", + enumValue = ExampleEnum.FOO_BAR) + assertThat(bean).isEqualTo(expectedBean) } @Test fun `Bind to class with multiple constructors and primary constructor should bind`() { - val source = MockConfigurationPropertySource() - source.put("foo.int-value", "12") + val source = MockConfigurationPropertySource("foo.int-value", "12") val binder = Binder(source) val bindable = binder.bind("foo", Bindable.of( MultipleConstructorsWithPrimaryConstructorBean::class.java)) - assertThat(bindable.isBound).isTrue() + 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 source = MockConfigurationPropertySource("foo.int-value", "12") val binder = Binder(source) val bindable = binder.bind("foo", Bindable.of( MultipleConstructorsBean::class.java)) - assertThat(bindable.isBound).isFalse() + 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 source = MockConfigurationPropertySource("foo.int-value", "12") val binder = Binder(source) val bindable = binder.bind("foo", Bindable.of( DefaultConstructorBean::class.java)) - assertThat(bindable.isBound).isFalse() + 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 source = MockConfigurationPropertySource(mapOf("foo.value-bean.int-value" to "123", + "foo.value-bean.long-value" to "34", + "foo.value-bean.boolean-value" to "true", + "foo.value-bean.string-value" to "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.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 source = MockConfigurationPropertySource("foo.string-value", "foo") val binder = Binder(source) val bean = binder.bind("foo", Bindable.of( ExampleValueBean::class.java)).get() @@ -129,31 +123,28 @@ class KotlinConstructorParametersBinderTests { @Test fun `Bind to class with no value for primitive should use default value`() { - val source = MockConfigurationPropertySource() - source.put("foo.string-value", "foo") + val source = MockConfigurationPropertySource("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.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 source = MockConfigurationPropertySource("foo.string-value", "foo") val binder = Binder(source) assertThat(binder.bind("foo", Bindable.of( - ExampleDefaultValueBean::class.java)).isBound()).isFalse(); + 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 source = MockConfigurationPropertySource("foo.string-value", "foo") val binder = Binder(source) val bean = binder.bindOrCreate("foo", Bindable.of( ExampleDefaultValueBean::class.java)) @@ -164,39 +155,45 @@ class KotlinConstructorParametersBinderTests { @Test fun `Bind to data class with no value should use default value`() { - val source = MockConfigurationPropertySource() - source.put("foo.enum-value", "foo-bar") + val source = MockConfigurationPropertySource("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) + val expectedBean = ExampleDataClassBean(intValue = 5, + longValue = 42, + booleanValue = false, + stringValue = "my data", + enumValue = ExampleEnum.FOO_BAR) + assertThat(bean).isEqualTo(expectedBean) } @Test fun `Bind to data class with generics`() { - val source = MockConfigurationPropertySource() - source.put("foo.value.bar", "baz") + val source = MockConfigurationPropertySource("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>>(ResolvableType.forClassWithGenerics(GenericValue::class.java, type))) .get() - assertThat(bean.value.get("bar")).isEqualTo("baz"); + assertThat(bean.value["bar"]).isEqualTo("baz") } @Test fun `Bind to named constructor parameter`() { - val source = MockConfigurationPropertySource() - source.put("foo.string-value", "test") + val source = MockConfigurationPropertySource("foo.string-value", "test") val binder = Binder(source) val bean = binder.bind("foo", Bindable.of(ExampleNamedParameterBean::class.java)).get() 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?, val booleanValue: Boolean?, val stringValue: String?, val enumValue: ExampleEnum?) @@ -244,4 +241,7 @@ class KotlinConstructorParametersBinderTests { val value: T ) -} + object ExampleSingletonBean { + var stringValue: String? = null + } +} \ No newline at end of file From bac101647a8ebbd1e8ea8952e5716e9f8a76c907 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 16 Apr 2021 09:29:06 +0200 Subject: [PATCH 2/2] Polish contribution See gh-25532 --- .../MockConfigurationPropertySource.java | 2 +- .../KotlinConstructorParametersBinderTests.kt | 65 ++++++++++--------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/MockConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/MockConfigurationPropertySource.java index 95d2a30382..976630b3f6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/MockConfigurationPropertySource.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/MockConfigurationPropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt index 561dbb720b..6b908843b6 100644 --- a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt +++ b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt @@ -17,16 +17,16 @@ class KotlinConstructorParametersBinderTests { @Test fun `Bind to class should create bound bean`() { val source = MockConfigurationPropertySource(mapOf("foo.int-value" to "12", - "foo.int-value" to "12", - "foo.long-value" to "34", - "foo.boolean-value" to "true", - "foo.string-value" to "foo", - "foo.enum-value" to "foo-bar")) + "foo.int-value" to "12", + "foo.long-value" to "34", + "foo.boolean-value" to "true", + "foo.string-value" to "foo", + "foo.enum-value" to "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.booleanValue).isTrue() assertThat(bean.stringValue).isEqualTo("foo") assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR) } @@ -34,16 +34,16 @@ class KotlinConstructorParametersBinderTests { @Test fun `Bind to class when has no prefix should create bound bean`() { val source = MockConfigurationPropertySource(mapOf("int-value" to "12", - "long-value" to "34", - "boolean-value" to "true", - "string-value" to "foo", - "enum-value" to "foo-bar")) + "long-value" to "34", + "boolean-value" to "true", + "string-value" to "foo", + "enum-value" to "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.booleanValue).isTrue() assertThat(bean.stringValue).isEqualTo("foo") assertThat(bean.enumValue).isEqualTo(ExampleEnum.FOO_BAR) } @@ -51,17 +51,17 @@ class KotlinConstructorParametersBinderTests { @Test fun `Bind to data class should create bound bean`() { val source = MockConfigurationPropertySource(mapOf("foo.int-value" to "12", - "foo.long-value" to "34", - "foo.boolean-value" to "true", - "foo.string-value" to "foo", - "foo.enum-value" to "foo-bar")) + "foo.long-value" to "34", + "foo.boolean-value" to "true", + "foo.string-value" to "foo", + "foo.enum-value" to "foo-bar")) val binder = Binder(source) val bean = binder.bind("foo", Bindable.of(ExampleDataClassBean::class.java)).get() val expectedBean = ExampleDataClassBean(intValue = 12, - longValue = 34, - booleanValue = true, - stringValue = "foo", - enumValue = ExampleEnum.FOO_BAR) + longValue = 34, + booleanValue = true, + stringValue = "foo", + enumValue = ExampleEnum.FOO_BAR) assertThat(bean).isEqualTo(expectedBean) } @@ -71,7 +71,7 @@ class KotlinConstructorParametersBinderTests { val binder = Binder(source) val bindable = binder.bind("foo", Bindable.of( MultipleConstructorsWithPrimaryConstructorBean::class.java)) - assertThat(bindable.isBound).isTrue + assertThat(bindable.isBound).isTrue() assertThat(bindable.get().intValue).isEqualTo(12) } @@ -81,7 +81,7 @@ class KotlinConstructorParametersBinderTests { val binder = Binder(source) val bindable = binder.bind("foo", Bindable.of( MultipleConstructorsBean::class.java)) - assertThat(bindable.isBound).isFalse + assertThat(bindable.isBound).isFalse() } @Test @@ -90,20 +90,20 @@ class KotlinConstructorParametersBinderTests { val binder = Binder(source) val bindable = binder.bind("foo", Bindable.of( DefaultConstructorBean::class.java)) - assertThat(bindable.isBound).isFalse + assertThat(bindable.isBound).isFalse() } @Test fun `Bind to class should bind nested`() { val source = MockConfigurationPropertySource(mapOf("foo.value-bean.int-value" to "123", - "foo.value-bean.long-value" to "34", - "foo.value-bean.boolean-value" to "true", - "foo.value-bean.string-value" to "foo")) + "foo.value-bean.long-value" to "34", + "foo.value-bean.boolean-value" to "true", + "foo.value-bean.string-value" to "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.booleanValue).isTrue() assertThat(bean.valueBean.stringValue).isEqualTo("foo") assertThat(bean.valueBean.enumValue).isNull() } @@ -129,7 +129,7 @@ class KotlinConstructorParametersBinderTests { ExamplePrimitiveDefaultBean::class.java)).get() assertThat(bean.intValue).isEqualTo(0) assertThat(bean.longValue).isEqualTo(0) - assertThat(bean.booleanValue).isFalse + assertThat(bean.booleanValue).isFalse() assertThat(bean.stringValue).isEqualTo("foo") assertThat(bean.enumValue).isNull() } @@ -139,7 +139,7 @@ class KotlinConstructorParametersBinderTests { val source = MockConfigurationPropertySource("foo.string-value", "foo") val binder = Binder(source) assertThat(binder.bind("foo", Bindable.of( - ExampleDefaultValueBean::class.java)).isBound).isFalse + ExampleDefaultValueBean::class.java)).isBound).isFalse() } @Test @@ -159,10 +159,10 @@ class KotlinConstructorParametersBinderTests { val binder = Binder(source) val bean = binder.bind("foo", Bindable.of(ExampleDataClassBean::class.java)).get() val expectedBean = ExampleDataClassBean(intValue = 5, - longValue = 42, - booleanValue = false, - stringValue = "my data", - enumValue = ExampleEnum.FOO_BAR) + longValue = 42, + booleanValue = false, + stringValue = "my data", + enumValue = ExampleEnum.FOO_BAR) assertThat(bean).isEqualTo(expectedBean) } @@ -244,4 +244,5 @@ class KotlinConstructorParametersBinderTests { object ExampleSingletonBean { var stringValue: String? = null } + } \ No newline at end of file