Skip to content

Commit 7231ad7

Browse files
jimonthebarnyusufugurozbek
authored andcommitted
Improve testability and add tests for DataSourceUrlExtrator
*
1 parent 8cb2732 commit 7231ad7

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/main/kotlin/com/github/yusufugurozbek/testcontainers/port/updater/DataSourceUrl.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import com.github.yusufugurozbek.testcontainers.port.updater.settings.MatchMode.
55
import com.intellij.database.dataSource.LocalDataSource
66
import com.intellij.database.util.common.isNotNullOrEmpty
77

8-
data class DataSourceUrl(val beforePort: String, val port: String, val afterPort: String?) {
8+
data class DataSourceUrl(val beforePort: String, val port: String, val afterPort: String?, val urlExtractor : DataSourceUrlExtractor = DataSourceUrlExtractor()) {
99

10+
init {
11+
extractor = urlExtractor
12+
}
1013

1114
companion object {
12-
private var dataSourceUrlExtractor: DataSourceUrlExtractor = DataSourceUrlExtractor()
15+
lateinit var extractor: DataSourceUrlExtractor
1316

1417
fun from(dataSource: LocalDataSource): DataSourceUrl? = from(dataSource.url)
1518

1619
fun from(url: String?): DataSourceUrl? {
1720
return url?.takeIf { it.isNotNullOrEmpty }
18-
?.let { dataSourceUrlExtractor.extract(it)?.let(::toDataSourceUrl) }
21+
?.let { extractor.extract(it)?.let(::toDataSourceUrl) }
1922
}
2023

2124
private fun toDataSourceUrl(matchResult: MatchResult?): DataSourceUrl? {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.yusufugurozbek.testcontainers.port.updater
2+
3+
import com.github.yusufugurozbek.testcontainers.port.updater.settings.MatchMode
4+
import com.intellij.testFramework.UsefulTestCase
5+
import junit.framework.TestCase
6+
import junit.framework.TestCase.*
7+
8+
internal class DataSourceUrlExtractorTest : UsefulTestCase() {
9+
10+
private val sut = DataSourceUrlExtractor()
11+
12+
fun `test parts can be successfully extracted from well formed input`() {
13+
val result = sut.extract("jdbc:postgresql://localhost:11111/test")
14+
15+
assertEquals("jdbc:postgresql://localhost:11111/test", result?.groups?.get(0)?.value)
16+
assertEquals("jdbc:postgresql://localhost", result?.groups?.get(1)?.value)
17+
assertEquals("11111", result?.groups?.get(2)?.value)
18+
assertEquals("/test", result?.groups?.get(3)?.value)
19+
}
20+
21+
fun `test extraction of null value returns null`() {
22+
assertNull(sut.extract(null))
23+
}
24+
25+
fun `test extraction of empty value returns null`() {
26+
assertNull(sut.extract(""))
27+
}
28+
29+
}

0 commit comments

Comments
 (0)