Home / Class/ KotlinDefaultBindConstructorProviderTests Class — spring-boot Architecture

KotlinDefaultBindConstructorProviderTests Class — spring-boot Architecture

Architecture documentation for the KotlinDefaultBindConstructorProviderTests class in KotlinDefaultBindConstructorProviderTests.kt from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinDefaultBindConstructorProviderTests.kt lines 29–247

@Suppress("unused")
class KotlinDefaultBindConstructorProviderTests {

	private val constructorProvider = DefaultBindConstructorProvider()

	@Test
	fun `type with default constructor should register java bean`() {
		val bindConstructor = this.constructorProvider.getBindConstructor(FooProperties::class.java, false)
		assertThat(bindConstructor).isNull()
	}

	@Test
	fun `type with no primary constructor should register java bean`() {
		val bindConstructor = this.constructorProvider.getBindConstructor(MultipleAmbiguousConstructors::class.java, false)
		assertThat(bindConstructor).isNull()
	}

	@Test
	fun `type with primary and secondary annotated constructor should use secondary constructor for binding`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(ConstructorBindingOnSecondaryWithPrimaryConstructor::class.java, false)
		assertThat(bindConstructor).isNotNull()
	}

	@Test
	fun `type with primary constructor with autowired should not use constructor binding`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(AutowiredPrimaryProperties::class.java, false)
		assertThat(bindConstructor).isNull()
	}

	@Test
	fun `type with primary and secondary constructor with autowired should not use constructor binding`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(PrimaryWithAutowiredSecondaryProperties::class.java, false)
		assertThat(bindConstructor).isNull()
	}

	@Test
	fun `type with no param primary constructor and params secondary constructor should not use constructor binding`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(NoParamPrimaryWithParamsSecondaryProperties::class.java, false)
		assertThat(bindConstructor).isNull()
	}

	@Test
	fun `type with params primary constructor and no param secondary constructor should use constructor binding`() {
		val bindConstructor = this.constructorProvider.
				getBindConstructor(ParamsPrimaryWithNoParamSecondaryProperties::class.java, false)
		assertThat(bindConstructor).isNotNull()
	}

	@Test
	fun `type with autowired secondary constructor should not use constructor binding`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(AutowiredSecondaryProperties::class.java, false)
		assertThat(bindConstructor).isNull()
	}

	@Test
	fun `type with autowired primary and constructor binding on secondary constructor should throw exception`() {
		assertThatIllegalStateException().isThrownBy {
			this.constructorProvider
					.getBindConstructor(ConstructorBindingOnSecondaryAndAutowiredPrimaryProperties::class.java, false)
		}
	}

	@Test
	fun `type with autowired secondary and constructor binding on primary constructor should throw exception`() {
		assertThatIllegalStateException().isThrownBy {
			this.constructorProvider
					.getBindConstructor(ConstructorBindingOnPrimaryAndAutowiredSecondaryProperties::class.java, false)
		}
	}

	@Test
	fun `type with primary constructor and no annotation should use constructor binding`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(ConstructorBindingPrimaryConstructorNoAnnotation::class.java, false)
		assertThat(bindConstructor).isNotNull()
	}

	@Test
	fun `type with secondary constructor and no annotation should use constructor binding`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(ConstructorBindingSecondaryConstructorNoAnnotation::class.java, false)
		assertThat(bindConstructor).isNotNull()
	}

	@Test
	fun `type with multiple constructors`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(ConstructorBindingMultipleConstructors::class.java, false)
		assertThat(bindConstructor).isNotNull()
	}

	@Test
	fun `type with multiple annotated constructors should throw exception`() {
		assertThatIllegalStateException().isThrownBy {
			this.constructorProvider
					.getBindConstructor(ConstructorBindingMultipleAnnotatedConstructors::class.java, false)
		}
	}

	@Test
	fun `type with secondary and primary annotated constructors should throw exception`() {
		assertThatIllegalStateException().isThrownBy {
			this.constructorProvider
					.getBindConstructor(ConstructorBindingSecondaryAndPrimaryAnnotatedConstructors::class.java, false)
		}
	}

	@Test
	fun `data class with default values should use constructor binding`() {
		val bindConstructor = this.constructorProvider
				.getBindConstructor(ConstructorBindingDataClassWithDefaultValues::class.java, false)
		assertThat(bindConstructor).isNotNull()
	}

	class FooProperties

	class PrimaryWithAutowiredSecondaryProperties(val name: String?, val counter: Int = 42) {

		@Autowired
		constructor(@Suppress("UNUSED_PARAMETER") foo: String) : this(foo, 21)
	}

	class NoParamPrimaryWithParamsSecondaryProperties() {

		constructor(@Suppress("UNUSED_PARAMETER") name: String) : this()

		var name: String? = null
	}

	class ParamsPrimaryWithNoParamSecondaryProperties(var name: String?) {

		constructor() : this(null)
	}

	class AutowiredSecondaryProperties {

		@Autowired
		constructor(@Suppress("UNUSED_PARAMETER") foo: String)
	}

	class AutowiredPrimaryProperties @Autowired constructor(val name: String?, val counter: Int = 42)

	class ConstructorBindingOnSecondaryAndAutowiredPrimaryProperties @Autowired constructor(
			val name: String?, val counter: Int = 42) {

		@ConstructorBinding
		constructor(@Suppress("UNUSED_PARAMETER") foo: String) : this(foo, 21)
	}

	class ConstructorBindingOnPrimaryAndAutowiredSecondaryProperties @ConstructorBinding constructor(
			val name: String?, val counter: Int = 42) {

		@Autowired
		constructor(@Suppress("UNUSED_PARAMETER") foo: String) : this(foo, 21)
	}

	class ConstructorBindingOnSecondaryWithPrimaryConstructor(val name: String?, val counter: Int = 42) {

		@ConstructorBinding
		constructor(@Suppress("UNUSED_PARAMETER") foo: String) : this(foo, 21)
	}

	class ConstructorBindingOnPrimaryWithSecondaryConstructor @ConstructorBinding constructor(
			val name: String?, val counter: Int = 42) {

		constructor(@Suppress("UNUSED_PARAMETER") foo: String) : this(foo, 21)
	}

	class ConstructorBindingPrimaryConstructorNoAnnotation(val name: String?, val counter: Int = 42)

	class ConstructorBindingSecondaryConstructorNoAnnotation {

		constructor(@Suppress("UNUSED_PARAMETER") foo: String)

	}

	class MultipleAmbiguousConstructors {

		constructor()

		constructor(@Suppress("UNUSED_PARAMETER") foo: String)

	}

	class ConstructorBindingMultipleConstructors {

		constructor(@Suppress("UNUSED_PARAMETER") bar: Int)

		@ConstructorBinding
		constructor(@Suppress("UNUSED_PARAMETER") foo: String)

	}

	class ConstructorBindingMultipleAnnotatedConstructors {

		@ConstructorBinding
		constructor(@Suppress("UNUSED_PARAMETER") bar: Int)

		@ConstructorBinding
		constructor(@Suppress("UNUSED_PARAMETER") foo: String)

	}

	class ConstructorBindingSecondaryAndPrimaryAnnotatedConstructors @ConstructorBinding constructor(
			val name: String?, val counter: Int = 42) {

		@ConstructorBinding
		constructor(@Suppress("UNUSED_PARAMETER") foo: String) : this(foo, 21)

	}

	data class ConstructorBindingDataClassWithDefaultValues(val name: String = "Joan", val counter: Int = 42)

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free