refactor DesktopType

merge-requests/2/head
az 2 years ago
parent 4cc1ce756a
commit 2525e0f2bb

@ -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

@ -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<DesktopType> = values): DesktopType {
@JvmStatic
fun valueOf(value: String): DesktopType = valueOf(value, values)
@JvmStatic
protected fun valueOf(value: String, valueList: List<DesktopType>): 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
}
}

@ -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)
}
}
Loading…
Cancel
Save