From: David ‘Bombe’ Roden Date: Sat, 23 Jul 2022 21:57:33 +0000 (+0200) Subject: ✅ Fix tests failing with non-English locales X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=bc8f2705a88d987f597c925fab05b2eb1cf73daa;p=Sone.git ✅ Fix tests failing with non-English locales For most of the tests it was sufficient to use the OverrideLocale TestRule added in the previous commit but in one case the creation of a date formatter needed to be delayed so that it uses the update locale during the test. --- diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt index 0b31588..19cd7f9 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt @@ -31,8 +31,10 @@ class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: E @Inject constructor(webInterface: WebInterface, elementLoader: ElementLoader, newElements: NewElements, timeTextConverter: TimeTextConverter, l10nFilter: L10nFilter): this(webInterface, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getDefault()) - private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply { - this.timeZone = timeZone + private val dateFormatter by lazy { + SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply { + this.timeZone = timeZone + } } override fun createJsonObject(request: FreenetRequest) = diff --git a/src/test/kotlin/net/pterodactylus/sone/template/DurationFormatFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/DurationFormatFilterTest.kt index 0af4a8e..e14b46f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/DurationFormatFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/DurationFormatFilterTest.kt @@ -17,14 +17,20 @@ package net.pterodactylus.sone.template +import net.pterodactylus.sone.test.OverrideLocale import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* +import org.junit.Rule +import java.util.Locale.ENGLISH import kotlin.test.* class DurationFormatFilterTest { private val filter = DurationFormatFilter() + @get:Rule + val overrideLocale = OverrideLocale(ENGLISH) + @Test fun `random object is returned as it is`() { val randomObject = Any() diff --git a/src/test/kotlin/net/pterodactylus/sone/template/HistogramRendererTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/HistogramRendererTest.kt index bc87086..fd6311a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/HistogramRendererTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/HistogramRendererTest.kt @@ -19,6 +19,7 @@ package net.pterodactylus.sone.template import com.codahale.metrics.* import net.pterodactylus.sone.freenet.* +import net.pterodactylus.sone.test.OverrideLocale import net.pterodactylus.util.template.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* @@ -26,12 +27,16 @@ import org.jsoup.* import org.jsoup.nodes.* import org.junit.* import java.util.* +import java.util.Locale.ENGLISH /** * Unit test for [HistogramRenderer]. */ class HistogramRendererTest { + @get:Rule + val overrideLocale = OverrideLocale(ENGLISH) + private val translation = object : Translation { override val currentLocale = Locale.ENGLISH override fun translate(key: String) = "Metric Name".takeIf { key == "Page.Metrics.TestHistogram.Title" } ?: "" diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt index 651d839..601ce38 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt @@ -7,6 +7,7 @@ import net.pterodactylus.sone.data.Sone.SoneStatus.downloading import net.pterodactylus.sone.data.Sone.SoneStatus.inserting import net.pterodactylus.sone.freenet.L10nFilter import net.pterodactylus.sone.freenet.L10nText +import net.pterodactylus.sone.test.OverrideLocale import net.pterodactylus.sone.test.deepMock import net.pterodactylus.sone.test.getInstance import net.pterodactylus.sone.test.isProvidedByMock @@ -26,9 +27,11 @@ import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.hasEntry import org.hamcrest.Matchers.notNullValue import org.junit.Before +import org.junit.Rule import org.junit.Test import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.anyLong +import java.util.Locale.ENGLISH import java.util.TimeZone /** @@ -36,6 +39,9 @@ import java.util.TimeZone */ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = false, needsFormPassword = false) { + @get:Rule + val overrideLocale = OverrideLocale(ENGLISH) + private val timeTextConverter = mock() private val l10nFilter = mock() override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getTimeZone("UTC")) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt index 62a34d1..2a0773e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt @@ -5,6 +5,7 @@ import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.freenet.L10nFilter import net.pterodactylus.sone.freenet.L10nText +import net.pterodactylus.sone.test.OverrideLocale import net.pterodactylus.sone.test.get import net.pterodactylus.sone.test.getInstance import net.pterodactylus.sone.test.isProvidedByMock @@ -19,9 +20,11 @@ import org.hamcrest.Matchers.containsInAnyOrder import org.hamcrest.Matchers.emptyIterable import org.hamcrest.Matchers.notNullValue import org.junit.Before +import org.junit.Rule import org.junit.Test import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.anyLong +import java.util.Locale.ENGLISH import java.util.TimeZone import java.util.TimeZone.getTimeZone @@ -30,6 +33,9 @@ import java.util.TimeZone.getTimeZone */ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = false, requiresLogin = false) { + @get:Rule + val overrideLocale = OverrideLocale(ENGLISH) + private val timeTextConverter = mock() private val l10nFilter = mock() override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter, getTimeZone("UTC")) }