Add Domain value class

This commit is contained in:
cketti 2023-05-19 17:53:58 +02:00
parent 2dbecb667c
commit 8b8bc409e8
2 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,8 @@
package app.k9mail.core.common.net
@JvmInline
value class Domain(val value: String) {
init {
requireNotNull(HostNameUtils.isLegalHostName(value)) { "Not a valid domain name: '$value'" }
}
}

View file

@ -0,0 +1,25 @@
package app.k9mail.core.common.net
import assertk.assertFailure
import assertk.assertThat
import assertk.assertions.hasMessage
import assertk.assertions.isEqualTo
import assertk.assertions.isInstanceOf
import kotlin.test.Test
class DomainTest {
@Test
fun `valid domain`() {
val domain = Domain("domain.example")
assertThat(domain.value).isEqualTo("domain.example")
}
@Test
fun `invalid domain should throw`() {
assertFailure {
Domain("invalid domain")
}.isInstanceOf<IllegalArgumentException>()
.hasMessage("Not a valid domain name: 'invalid domain'")
}
}