Make test timezone-independent
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Sep 2017 05:11:14 +0000 (07:11 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Sep 2017 05:11:14 +0000 (07:11 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt

index 97c9e5d..0475359 100644 (file)
@@ -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)));
index 358003a..9310786 100644 (file)
@@ -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) }
        }
 
index 585316d..af33eea 100644 (file)
@@ -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<TimeTextConverter>()
        private val l10nFilter = mock<L10nFilter>()
-       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<Pair<String, JsonNode>>(
-                               "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<Pair<String, JsonNode>>(
-                               "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<Pair<String, JsonNode>>(
-                               "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<Pair<String, JsonNode>>(
-                               "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")
                ))
        }