Skip to content

Commit

Permalink
upgrade gradle, add test stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Nov 22, 2023
1 parent 1d79bf6 commit 05dbd51
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 8 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
matrix:
project: [core, client, server]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: zulu
java-version: 11.0.18
- name: Build Core
run: ./gradlew :core:build
- name: Build Client
run: ./gradlew :client:build
- name: Build Server
run: ./gradlew :server:build
- name: Build
run: ./gradlew :${{ matrix.project }}:build
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
`kotlin-dsl`
kotlin("jvm") version "1.8.21"
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ dependencies {

implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.junit.jupiter:junit-jupiter:5.7.2")
testImplementation("org.junit.jupiter:junit-jupiter:5.9.2")
testImplementation("io.mockk:mockk:1.13.4")
testImplementation("io.kotest:kotest-assertions-core:5.5.4")
testImplementation("com.github.alexburlton:swing-test:4.0.0")
}

tasks.named<Test>("test") {
Expand Down
23 changes: 23 additions & 0 deletions core/src/test/kotlin/bean/FocusableWindowTest.kt
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"
}
}
84 changes: 84 additions & 0 deletions core/src/test/kotlin/helper/AbstractTest.kt
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()
}
}
27 changes: 27 additions & 0 deletions core/src/test/kotlin/helper/BeforeAllTestsExtension.kt
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"))
}
}
18 changes: 18 additions & 0 deletions core/src/test/kotlin/helper/FakeLogDestination.kt
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()
}
}
3 changes: 3 additions & 0 deletions core/src/test/kotlin/helper/TestConstants.kt
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")
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
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

0 comments on commit 05dbd51

Please sign in to comment.