Select the asserter based on test framework presence in classpath
Simplify asserter lookup: enumerate all AsserterContributors when the file class with lookupAsserter function is initialized. It's assumed that AsserterContributor initialization should be fast. #KT-21154 Fixed
This commit is contained in:
@@ -1,48 +1,15 @@
|
||||
package kotlin.test
|
||||
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.*
|
||||
import java.util.concurrent.locks.*
|
||||
import kotlin.concurrent.withLock
|
||||
import java.util.ServiceLoader
|
||||
|
||||
private val inited = AtomicBoolean()
|
||||
private val lock = ReentrantLock()
|
||||
private val contributors = ArrayList<AsserterContributor>()
|
||||
|
||||
internal actual fun lookupAsserter(): Asserter = lookup()
|
||||
private val contributors = ServiceLoader.load(AsserterContributor::class.java).toList()
|
||||
|
||||
private val defaultAsserter = DefaultAsserter()
|
||||
|
||||
internal fun lookup(): Asserter {
|
||||
initContributorsIfNeeded()
|
||||
|
||||
internal actual fun lookupAsserter(): Asserter {
|
||||
for (contributor in contributors) {
|
||||
val asserter = contributor.contribute()
|
||||
if (asserter != null) {
|
||||
return asserter
|
||||
}
|
||||
if (asserter != null) return asserter
|
||||
}
|
||||
|
||||
return defaultAsserter
|
||||
}
|
||||
|
||||
private fun initContributors() {
|
||||
contributors.clear()
|
||||
val loader = ServiceLoader.load(AsserterContributor::class.java)
|
||||
|
||||
for (contributor in loader) {
|
||||
if (contributor != null) {
|
||||
contributors.add(contributor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun initContributorsIfNeeded() {
|
||||
if (!inited.get()) {
|
||||
lock.withLock {
|
||||
if (inited.compareAndSet(false, true)) {
|
||||
initContributors()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user