1
+ package org.springframework.boot.context.properties
2
+
3
+ import org.assertj.core.api.Assertions.assertThat
4
+ import org.junit.Test
5
+ import org.springframework.beans.factory.annotation.Autowired
6
+ import org.springframework.beans.factory.support.DefaultListableBeanFactory
7
+ import org.springframework.beans.factory.support.GenericBeanDefinition
8
+ import org.springframework.core.type.AnnotationMetadata
9
+ import org.springframework.core.type.classreading.SimpleMetadataReaderFactory
10
+
11
+ /* *
12
+ * Tests for `EnableConfigurationPropertiesImportSelector`.
13
+ *
14
+ * @author Stephane Nicoll
15
+ */
16
+ @Suppress(" unused" )
17
+ class KotlinEnableConfigurationPropertiesImportSelectorTests {
18
+
19
+ private val registrar = EnableConfigurationPropertiesImportSelector .ConfigurationPropertiesBeanRegistrar ()
20
+
21
+ private val beanFactory = DefaultListableBeanFactory ()
22
+
23
+
24
+ @Test
25
+ fun `type with default constructor should register generic bean definition` () {
26
+ this .registrar.registerBeanDefinitions(
27
+ getAnnotationMetadata(TestConfiguration ::class .java), this .beanFactory)
28
+ val beanDefinition = this .beanFactory.getBeanDefinition(
29
+ " foo-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$ FooProperties" )
30
+ assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition ::class .java)
31
+ }
32
+
33
+ @Test
34
+ fun `type with autowired on constructor should register generic bean definition` () {
35
+ this .registrar.registerBeanDefinitions(
36
+ getAnnotationMetadata(TestConfiguration ::class .java), this .beanFactory)
37
+ val beanDefinition = this .beanFactory.getBeanDefinition(
38
+ " bar-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$ BarProperties" )
39
+ assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition ::class .java)
40
+ }
41
+
42
+ @Test
43
+ fun `type with primary constructor and no autowired should register configuration properties bean definition` () {
44
+ this .registrar.registerBeanDefinitions(
45
+ getAnnotationMetadata(TestConfiguration ::class .java), this .beanFactory)
46
+ val beanDefinition = this .beanFactory.getBeanDefinition(
47
+ " baz-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$ BazProperties" )
48
+ assertThat(beanDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition ::class .java)
49
+ }
50
+
51
+ @Test
52
+ fun `type with no primary constructor should register generic bean definition` () {
53
+ this .registrar.registerBeanDefinitions(
54
+ getAnnotationMetadata(TestConfiguration ::class .java), this .beanFactory)
55
+ val beanDefinition = this .beanFactory.getBeanDefinition(
56
+ " bing-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$ BingProperties" )
57
+ assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition ::class .java)
58
+ }
59
+
60
+ private fun getAnnotationMetadata (source : Class <* >): AnnotationMetadata {
61
+ return SimpleMetadataReaderFactory ().getMetadataReader(source.name)
62
+ .annotationMetadata
63
+ }
64
+
65
+
66
+ @EnableConfigurationProperties(FooProperties ::class , BarProperties ::class , BazProperties ::class , BingProperties ::class )
67
+ class TestConfiguration
68
+
69
+ @ConfigurationProperties(prefix = " foo" )
70
+ class FooProperties
71
+
72
+ @ConfigurationProperties(prefix = " bar" )
73
+ class BarProperties @Autowired constructor(val foo : String )
74
+
75
+ @ConfigurationProperties(prefix = " baz" )
76
+ class BazProperties (val name : String? , val counter : Int = 42 )
77
+
78
+ @ConfigurationProperties(prefix = " bing" )
79
+ class BingProperties {
80
+
81
+ constructor ()
82
+
83
+ constructor (@Suppress(" UNUSED_PARAMETER" ) foo: String )
84
+
85
+ }
86
+
87
+ }
0 commit comments