From 2525e0f2bb25d27659a6a862d8bddd26829c87ae Mon Sep 17 00:00:00 2001 From: az Date: Sun, 21 Aug 2022 13:23:04 +0200 Subject: [PATCH] refactor DesktopType --- .../desktop/application/CliArgumentsParser.kt | 2 +- .../provs/desktop/domain/DesktopType.kt | 12 +++-- .../provs/desktop/domain/DesktopTypeTest.kt | 51 +++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopTypeTest.kt diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt index 4277c59..b2a3605 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt @@ -24,7 +24,7 @@ open class CliArgumentsParser(name: String) : CliTargetParser(name) { val module = modules.first { it.parsed } return DesktopCliCommand( - DesktopType.returnIfExists(module.name.uppercase()), + DesktopType.valueOf(module.name.uppercase()), TargetCliCommand( target, passwordInteractive diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopType.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopType.kt index 2f1b72c..13edb9c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopType.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopType.kt @@ -4,9 +4,8 @@ package org.domaindrivenarchitecture.provs.desktop.domain /** * Provides desktop types. For each type a different set of software and packages is installed, see README.md. */ -open class DesktopType(val name: String) { - - // A regular class is used rather than enum class in order to allow extending DesktopType by subclassing. +// Uses a regular class instead of an enum class in order to allow subclasses which can add new DesktopTypes +open class DesktopType protected constructor(val name: String) { companion object { @@ -17,7 +16,11 @@ open class DesktopType(val name: String) { @JvmStatic protected val values = listOf(BASIC, OFFICE, IDE) - fun returnIfExists(value: String, valueList: List = values): DesktopType { + @JvmStatic + fun valueOf(value: String): DesktopType = valueOf(value, values) + + @JvmStatic + protected fun valueOf(value: String, valueList: List): DesktopType { for (type in valueList) { if (value.uppercase().equals(type.name)) { return type @@ -31,4 +34,3 @@ open class DesktopType(val name: String) { return name } } - diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopTypeTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopTypeTest.kt new file mode 100644 index 0000000..715ede7 --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopTypeTest.kt @@ -0,0 +1,51 @@ +package org.domaindrivenarchitecture.provs.desktop.domain + +import org.domaindrivenarchitecture.provs.desktop.domain.DesktopType.Companion.BASIC +import org.domaindrivenarchitecture.provs.desktop.domain.DesktopType.Companion.IDE +import org.domaindrivenarchitecture.provs.desktop.domain.SubDesktopType.Companion.SUBTYPE +import org.junit.jupiter.api.Test + +import org.junit.jupiter.api.Assertions.* + +// tests subclassing of DesktopType +internal open class SubDesktopType protected constructor(name: String) : DesktopType(name) { + + companion object { + + // defines a new DesktopType + val SUBTYPE = SubDesktopType("SUBTYPE") + + private val values = DesktopType.values + SUBTYPE + + fun valueOf(value: String): DesktopType { + return valueOf(value, values) + } + } +} + +internal class DesktopTypeTest { + + @Test + fun test_valueOf() { + assertEquals(BASIC, DesktopType.valueOf("basic")) + assertEquals(BASIC, DesktopType.valueOf("Basic")) + assertEquals(IDE, DesktopType.valueOf("IDE")) + + val exception = assertThrows(RuntimeException::class.java) { + DesktopType.valueOf("subtype") + } + assertEquals("No DesktopType found for value: subtype", exception.message) + } + + @Test + fun test_valueOf_in_subclass() { + assertEquals(SUBTYPE, SubDesktopType.valueOf("subtype")) + assertEquals(BASIC, SubDesktopType.valueOf("basic")) + assertNotEquals(SUBTYPE, DesktopType.valueOf("basic")) + + val exception = assertThrows(RuntimeException::class.java) { + DesktopType.valueOf("subtype2") + } + assertEquals("No DesktopType found for value: subtype2", exception.message) + } +}