Skip to content

Commit ed78e13

Browse files
committed
Introduced jUnit5, migrated Abstract Factory and Adapter to jUnit, updated Adapter
1 parent e19cab0 commit ed78e13

22 files changed

+110
-66
lines changed

build.gradle

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
group 'pl.dariuszbacinski.designpatterns'
21
version '1.0-SNAPSHOT'
32

43
buildscript {
@@ -21,4 +20,20 @@ repositories {
2120
dependencies {
2221
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
2322
implementation("org.jetbrains.kotlin:kotlin-reflect")
23+
testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.2"
24+
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.3.2"
25+
testImplementation("org.assertj:assertj-core:3.11.1")
26+
}
27+
28+
test {
29+
// Enable JUnit 5 (Gradle 4.6+).
30+
useJUnitPlatform()
31+
32+
// Always run tests, even when nothing changed.
33+
dependsOn 'cleanTest'
34+
35+
// Show test results.
36+
testLogging {
37+
events "passed", "skipped", "failed"
38+
}
2439
}
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Fri Jan 04 10:36:24 CET 2019
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip

settings.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
rootProject.name = 'design-patterns-kotlin'
2-

src/main/kotlin/AbstractFactory.kt

-33
This file was deleted.

src/main/kotlin/Adapter.kt

-29
This file was deleted.

src/test/kotlin/AbstractFactory.kt

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import org.assertj.core.api.Assertions.assertThat
2+
import org.junit.jupiter.api.Test
3+
4+
//Based on: http://stackoverflow.com/a/13030163/361832
5+
6+
interface Plant
7+
8+
class OrangePlant : Plant
9+
10+
class ApplePlant : Plant
11+
12+
abstract class PlantFactory {
13+
14+
abstract fun makePlant(): Plant
15+
16+
companion object {
17+
inline fun <reified T : Plant> createFactory(): PlantFactory =
18+
when (T::class) {
19+
OrangePlant::class -> OrangeFactory()
20+
ApplePlant::class -> AppleFactory()
21+
else -> throw IllegalArgumentException()
22+
}
23+
}
24+
}
25+
26+
class AppleFactory : PlantFactory() {
27+
override fun makePlant(): Plant = ApplePlant()
28+
}
29+
30+
class OrangeFactory : PlantFactory() {
31+
override fun makePlant(): Plant = OrangePlant()
32+
}
33+
34+
35+
class AbstractFactoryTest {
36+
37+
@Test
38+
fun `Abstract Factory`() {
39+
val plantFactory = PlantFactory.createFactory<OrangePlant>()
40+
val plant = plantFactory.makePlant()
41+
println("Created plant: $plant")
42+
43+
assertThat(plant).isInstanceOf(OrangePlant::class.java)
44+
}
45+
}

src/test/kotlin/Adapter.kt

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import org.assertj.core.api.Assertions.assertThat
2+
import org.junit.jupiter.api.Test
3+
import java.math.BigDecimal
4+
5+
interface Temperature {
6+
var temperature: Double
7+
}
8+
9+
class CelsiusTemperature(override var temperature: Double) : Temperature
10+
11+
class FahrenheitTemperature(var celsiusTemperature: CelsiusTemperature) : Temperature {
12+
13+
override var temperature: Double
14+
get() = convertCelsiusToFahrenheit(celsiusTemperature.temperature)
15+
set(temperatureInF) {
16+
celsiusTemperature.temperature = convertFahrenheitToCelsius(temperatureInF)
17+
}
18+
19+
private fun convertFahrenheitToCelsius(f: Double): Double =
20+
((BigDecimal.valueOf(f).setScale(2) - BigDecimal(32)) * BigDecimal(5) / BigDecimal(9))
21+
.toDouble()
22+
23+
24+
private fun convertCelsiusToFahrenheit(c: Double): Double =
25+
((BigDecimal.valueOf(c).setScale(2) * BigDecimal(9) / BigDecimal(5)) + BigDecimal(32))
26+
.toDouble()
27+
}
28+
29+
class AdapterTest {
30+
31+
@Test
32+
fun `Adapter`() {
33+
val celsiusTemperature = CelsiusTemperature(0.0)
34+
val fahrenheitTemperature = FahrenheitTemperature(celsiusTemperature)
35+
36+
celsiusTemperature.temperature = 36.6
37+
println("${celsiusTemperature.temperature} C -> ${fahrenheitTemperature.temperature} F")
38+
39+
assertThat(fahrenheitTemperature.temperature).isEqualTo(97.88)
40+
41+
fahrenheitTemperature.temperature = 100.0
42+
println("${fahrenheitTemperature.temperature} F -> ${celsiusTemperature.temperature} C")
43+
44+
assertThat(celsiusTemperature.temperature).isEqualTo(37.78)
45+
}
46+
}

src/main/kotlin/Builder.kt renamed to src/test/kotlin/Builder.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import java.io.File
33
// Let's assume that Dialog class is provided by external library.
44
// We have only access to Dialog public interface which cannot be changed.
55

6-
class Dialog() {
6+
class Dialog {
77

88
fun showTitle() = println("showing title")
99

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)