-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d79bf6
commit 05dbd51
Showing
9 changed files
with
166 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
plugins { | ||
`kotlin-dsl` | ||
kotlin("jvm") version "1.8.21" | ||
} | ||
|
||
repositories { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package bean | ||
|
||
import helper.AbstractTest | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.mockk | ||
import logging.KEY_ACTIVE_WINDOW | ||
import org.junit.jupiter.api.Test | ||
import utils.InjectedThings.logger | ||
|
||
class FocusableWindowTest : AbstractTest() { | ||
@Test | ||
fun `Should update logging context when gains focus`() { | ||
val window = FakeFocusableWindow() | ||
|
||
logger.addToContext(KEY_ACTIVE_WINDOW, "Foo") | ||
window.windowGainedFocus(mockk()) | ||
logger.loggingContext[KEY_ACTIVE_WINDOW] shouldBe "Fake Window" | ||
} | ||
|
||
private inner class FakeFocusableWindow : FocusableWindow() { | ||
override val windowName = "Fake Window" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package helper | ||
|
||
import com.github.alyssaburlton.swingtest.purgeWindows | ||
import io.kotest.assertions.fail | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.clearAllMocks | ||
import logging.LogDestinationSystemOut | ||
import logging.LogRecord | ||
import logging.Logger | ||
import logging.LoggingCode | ||
import logging.Severity | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import util.AbstractClient | ||
|
||
private val logDestination = FakeLogDestination() | ||
val logger = Logger(listOf(logDestination, LogDestinationSystemOut())) | ||
private var checkedForExceptions = false | ||
|
||
@ExtendWith(BeforeAllTestsExtension::class) | ||
open class AbstractTest { | ||
@BeforeEach | ||
fun beforeEachTest() { | ||
clearLogs() | ||
clearAllMocks() | ||
|
||
AbstractClient.devMode = false | ||
logger.loggingContext.clear() | ||
} | ||
|
||
@AfterEach | ||
fun afterEachTest() { | ||
if (!checkedForExceptions) { | ||
val errors = getErrorsLogged() | ||
if (errors.isNotEmpty()) { | ||
fail("Unexpected error(s) were logged during test: ${errors.map { it.getThrowableStr() } }") | ||
} | ||
errorLogged() shouldBe false | ||
} | ||
|
||
checkedForExceptions = false | ||
purgeWindows() | ||
} | ||
|
||
fun getLastLog() = flushAndGetLogRecords().last() | ||
|
||
fun verifyLog(code: LoggingCode, severity: Severity = Severity.INFO): LogRecord { | ||
val record = flushAndGetLogRecords().findLast { it.loggingCode == code && it.severity == severity } | ||
record.shouldNotBeNull() | ||
|
||
if (severity == Severity.ERROR) { | ||
checkedForExceptions = true | ||
} | ||
|
||
return record | ||
} | ||
|
||
protected fun findLog(code: LoggingCode, severity: Severity = Severity.INFO) = | ||
getLogRecordsSoFar().findLast { it.loggingCode == code && it.severity == severity } | ||
|
||
fun verifyNoLogs(code: LoggingCode) { | ||
flushAndGetLogRecords().any { it.loggingCode == code } shouldBe false | ||
} | ||
|
||
fun errorLogged(): Boolean { | ||
checkedForExceptions = true | ||
return getErrorsLogged().isNotEmpty() | ||
} | ||
|
||
private fun getErrorsLogged() = flushAndGetLogRecords().filter { it.severity == Severity.ERROR } | ||
|
||
fun getLogRecordsSoFar() = logDestination.logRecords.toList() | ||
|
||
fun flushAndGetLogRecords(): List<LogRecord> { | ||
logger.waitUntilLoggingFinished() | ||
return logDestination.logRecords.toList() | ||
} | ||
fun clearLogs() { | ||
logger.waitUntilLoggingFinished() | ||
logDestination.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package helper | ||
|
||
import CURRENT_TIME | ||
import logging.LoggerUncaughtExceptionHandler | ||
import org.junit.jupiter.api.extension.BeforeAllCallback | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import utils.InjectedThings | ||
import java.time.Clock | ||
import java.time.ZoneId | ||
|
||
var doneOneTimeSetup = false | ||
|
||
class BeforeAllTestsExtension : BeforeAllCallback { | ||
override fun beforeAll(context: ExtensionContext?) { | ||
if (!doneOneTimeSetup) { | ||
doOneTimeSetup() | ||
doneOneTimeSetup = true | ||
} | ||
} | ||
|
||
private fun doOneTimeSetup() { | ||
Thread.setDefaultUncaughtExceptionHandler(LoggerUncaughtExceptionHandler()) | ||
|
||
InjectedThings.logger = logger | ||
InjectedThings.clock = Clock.fixed(CURRENT_TIME, ZoneId.of("UTC")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package helper | ||
|
||
import logging.ILogDestination | ||
import logging.LogRecord | ||
|
||
class FakeLogDestination : ILogDestination { | ||
val logRecords: MutableList<LogRecord> = mutableListOf() | ||
|
||
override fun log(record: LogRecord) { | ||
logRecords.add(record) | ||
} | ||
|
||
override fun contextUpdated(context: Map<String, Any?>) {} | ||
|
||
fun clear() { | ||
logRecords.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import java.time.Instant | ||
|
||
val CURRENT_TIME: Instant = Instant.parse("2020-04-13T11:04:00.00Z") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |