From 4af98a3470ba214fb452b0f0d908b707811fa496 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Sep 2017 07:11:14 +0200 Subject: [PATCH] Make test timezone-independent --- .../java/net/pterodactylus/sone/web/WebInterface.java | 2 +- .../net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt | 12 +++++++++--- .../pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt | 15 ++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 97c9e5d..0475359 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -774,7 +774,7 @@ public class WebInterface implements SessionProvider { pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLinkedElementAjaxPage(this, elementLoader, linkedElementRenderFilter))); - pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this, timeTextConverter, l10nFilter))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this, timeTextConverter, l10nFilter, TimeZone.getDefault()))); pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this))); diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt index 358003a..9310786 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt @@ -8,15 +8,19 @@ import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest import java.text.SimpleDateFormat +import java.util.TimeZone /** * Ajax page that returns a formatted, relative timestamp for replies or posts. */ class GetTimesAjaxPage(webInterface: WebInterface, private val timeTextConverter: TimeTextConverter, - private val l10nFilter: L10nFilter) : JsonPage("getTimes.ajax", webInterface) { + private val l10nFilter: L10nFilter, + timeZone: TimeZone) : JsonPage("getTimes.ajax", webInterface) { - private val dateTimeFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss") + private val dateTimeFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply { + this.timeZone = timeZone + } override fun needsFormPassword() = false override fun requiresLogin() = false @@ -36,7 +40,9 @@ class GetTimesAjaxPage(webInterface: WebInterface, id to jsonObject( "timeText" to l10nFilter.format(null, timeText.l10nText, emptyMap()), "refreshTime" to timeText.refreshTime / 1000, - "tooltip" to dateTimeFormatter.format(time)) + "tooltip" to synchronized(dateTimeFormatter) { + dateTimeFormatter.format(time) + }) }.forEach { this@jsonObject.put(it.first, it.second) } } 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 585316d..af33eea 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt @@ -19,6 +19,7 @@ import org.junit.Before import org.junit.Test import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.anyLong +import java.util.TimeZone.getTimeZone /** * Unit test for [GetTimesAjaxPage]. @@ -27,7 +28,7 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f private val timeTextConverter = mock() private val l10nFilter = mock() - override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter) } + override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter, getTimeZone("UTC")) } private val posts = listOf(createPost(1), createPost(2)) private val replies = listOf(createReply(1), createReply(2)) @@ -64,7 +65,7 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f addRequestParameter("posts", "post1") assertThat(json.isSuccess, equalTo(true)) assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( - "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01") + "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01") )) assertThat(json["replyTimes"].toList(), emptyIterable()) } @@ -76,7 +77,7 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f assertThat(json.isSuccess, equalTo(true)) assertThat(json["postTimes"].toList(), emptyIterable()) assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( - "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01") + "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01") )) } @@ -87,8 +88,8 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f addRequestParameter("posts", "post1,post2,post3") assertThat(json.isSuccess, equalTo(true)) assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( - "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01"), - "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 01:00:02") + "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"), + "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02") )) assertThat(json["replyTimes"].toList(), emptyIterable()) } @@ -101,8 +102,8 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f assertThat(json.isSuccess, equalTo(true)) assertThat(json["postTimes"].toList(), emptyIterable()) assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( - "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01"), - "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 01:00:02") + "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"), + "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02") )) } -- 2.7.4