JAVA-101 Split Core Kotlin

This commit is contained in:
mikr
2020-06-08 00:22:04 +02:00
parent 0130ae560f
commit 73e169d1eb
48 changed files with 136 additions and 27 deletions

View File

@@ -0,0 +1,22 @@
package com.baeldung.builder
class FoodOrder private constructor(
val bread: String?,
val condiments: String?,
val meat: String?,
val fish: String?
) {
data class Builder(
var bread: String? = null,
var condiments: String? = null,
var meat: String? = null,
var fish: String? = null) {
fun bread(bread: String) = apply { this.bread = bread }
fun condiments(condiments: String) = apply { this.condiments = condiments }
fun meat(meat: String) = apply { this.meat = meat }
fun fish(fish: String) = apply { this.fish = fish }
fun build() = FoodOrder(bread, condiments, meat, fish)
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.builder
class FoodOrderApply {
var bread: String? = null
var condiments: String? = null
var meat: String? = null
var fish: String? = null
}

View File

@@ -0,0 +1,7 @@
package com.baeldung.builder
data class FoodOrderNamed(
val bread: String? = null,
val condiments: String? = null,
val meat: String? = null,
val fish: String? = null)

View File

@@ -0,0 +1,9 @@
package com.baeldung.builder
fun main(args: Array<String>) {
FoodOrder.Builder()
.bread("bread")
.condiments("condiments")
.meat("meat")
.fish("bread").let { println(it) }
}

View File

@@ -0,0 +1,140 @@
package com.baeldung.builder
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
internal class BuilderPatternUnitTest {
@Test
fun whenBuildingFoodOrderSettingValues_thenFieldsNotNull() {
val foodOrder = FoodOrder.Builder()
.bread("white bread")
.meat("bacon")
.fish("salmon")
.condiments("olive oil")
.build()
Assertions.assertNotNull(foodOrder.bread)
Assertions.assertNotNull(foodOrder.meat)
Assertions.assertNotNull(foodOrder.condiments)
Assertions.assertNotNull(foodOrder.fish)
}
@Test
fun whenBuildingFoodOrderSettingValues_thenFieldsContainsValues() {
val foodOrder = FoodOrder.Builder()
.bread("white bread")
.meat("bacon")
.fish("salmon")
.condiments("olive oil")
.build()
Assertions.assertEquals("white bread", foodOrder.bread)
Assertions.assertEquals("bacon", foodOrder.meat)
Assertions.assertEquals("olive oil", foodOrder.condiments)
Assertions.assertEquals("salmon", foodOrder.fish)
}
@Test
fun whenBuildingFoodOrderWithoutSettingValues_thenFieldsNull() {
val foodOrder = FoodOrder.Builder()
.build()
Assertions.assertNull(foodOrder.bread)
Assertions.assertNull(foodOrder.meat)
Assertions.assertNull(foodOrder.condiments)
Assertions.assertNull(foodOrder.fish)
}
@Test
fun whenBuildingFoodOrderNamedSettingValues_thenFieldsNotNull() {
val foodOrder = FoodOrderNamed(
meat = "bacon",
fish = "salmon",
condiments = "olive oil",
bread = "white bread"
)
Assertions.assertNotNull(foodOrder.bread)
Assertions.assertNotNull(foodOrder.meat)
Assertions.assertNotNull(foodOrder.condiments)
Assertions.assertNotNull(foodOrder.fish)
}
@Test
fun whenBuildingFoodOrderNamedSettingValues_thenFieldsContainsValues() {
val foodOrder = FoodOrderNamed(
meat = "bacon",
fish = "salmon",
condiments = "olive oil",
bread = "white bread"
)
Assertions.assertEquals("white bread", foodOrder.bread)
Assertions.assertEquals("bacon", foodOrder.meat)
Assertions.assertEquals("olive oil", foodOrder.condiments)
Assertions.assertEquals("salmon", foodOrder.fish)
}
@Test
fun whenBuildingFoodOrderNamedWithoutSettingValues_thenFieldsNull() {
val foodOrder = FoodOrderNamed()
Assertions.assertNull(foodOrder.bread)
Assertions.assertNull(foodOrder.meat)
Assertions.assertNull(foodOrder.condiments)
Assertions.assertNull(foodOrder.fish)
}
@Test
fun whenBuildingFoodOrderApplySettingValues_thenFieldsNotNull() {
val foodOrder = FoodOrderApply().apply {
meat = "bacon"
fish = "salmon"
condiments = "olive oil"
bread = "white bread"
}
Assertions.assertNotNull(foodOrder.bread)
Assertions.assertNotNull(foodOrder.meat)
Assertions.assertNotNull(foodOrder.condiments)
Assertions.assertNotNull(foodOrder.fish)
}
@Test
fun whenBuildingFoodOrderApplySettingValues_thenFieldsContainsValues() {
val foodOrder = FoodOrderApply().apply {
meat = "bacon"
fish = "salmon"
condiments = "olive oil"
bread = "white bread"
}
Assertions.assertEquals("white bread", foodOrder.bread)
Assertions.assertEquals("bacon", foodOrder.meat)
Assertions.assertEquals("olive oil", foodOrder.condiments)
Assertions.assertEquals("salmon", foodOrder.fish)
}
@Test
fun whenBuildingFoodOrderApplyWithoutSettingValues_thenFieldsNull() {
val foodOrder = FoodOrderApply()
Assertions.assertNull(foodOrder.bread)
Assertions.assertNull(foodOrder.meat)
Assertions.assertNull(foodOrder.condiments)
Assertions.assertNull(foodOrder.fish)
}
}