From d5cd41949b7e62fdc40ef7390661dcda8c18a139 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 19 Nov 2019 20:59:34 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=94=A5=20Replace=20BaseL10n=20with=20Trans?= =?utf8?q?lation?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/template/UnknownDateFilter.java | 20 +++++++++----------- .../net/pterodactylus/sone/web/WebInterface.java | 17 +++++++---------- .../pterodactylus/sone/web/page/PageToadlet.java | 5 ++--- .../net/pterodactylus/sone/main/SoneModule.kt | 1 - .../pterodactylus/sone/web/WebInterfaceModule.kt | 9 ++++----- .../sone/web/ajax/GetTranslationAjaxPage.kt | 2 +- .../pterodactylus/sone/web/page/FreenetRequest.kt | 3 +-- .../net/pterodactylus/sone/web/page/SoneRequest.kt | 12 ++++++------ .../sone/web/pages/SoneTemplatePage.kt | 6 ++++-- .../sone/web/pages/UploadImagePage.kt | 2 +- .../pterodactylus/sone/web/pages/ViewSonePage.kt | 4 ++-- .../pterodactylus/sone/main/DebugLoadersTest.java | 4 +--- .../sone/main/DefaultLoadersTest.java | 4 +--- .../sone/template/UnknownDateFilterTest.kt | 22 ++++++++++++---------- .../sone/web/WebInterfaceModuleTest.kt | 9 ++++++--- .../net/pterodactylus/sone/web/ajax/TestObjects.kt | 14 ++++++++------ .../sone/web/page/FreenetRequestTest.kt | 9 +-------- .../pterodactylus/sone/web/page/SoneRequestTest.kt | 7 ++----- .../sone/web/pages/DeleteSonePageTest.kt | 2 +- .../sone/web/pages/DismissNotificationPageTest.kt | 2 +- .../sone/web/pages/DistrustPageTest.kt | 2 +- .../sone/web/pages/EditAlbumPageTest.kt | 2 +- .../sone/web/pages/EditImagePageTest.kt | 2 +- .../sone/web/pages/EditProfileFieldPageTest.kt | 2 +- .../sone/web/pages/EditProfilePageTest.kt | 2 +- .../sone/web/pages/FollowSonePageTest.kt | 2 +- .../sone/web/pages/ImageBrowserPageTest.kt | 2 +- .../pterodactylus/sone/web/pages/IndexPageTest.kt | 2 +- .../sone/web/pages/KnownSonesPageTest.kt | 2 +- .../sone/web/pages/SoneTemplatePageTest.kt | 2 +- .../pterodactylus/sone/web/pages/WebPageTest.kt | 15 ++++++++------- 31 files changed, 88 insertions(+), 101 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java b/src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java index 0d4872b..5924e5a 100644 --- a/src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java @@ -19,18 +19,18 @@ package net.pterodactylus.sone.template; import java.util.Map; +import net.pterodactylus.sone.freenet.Translation; import net.pterodactylus.util.template.Filter; import net.pterodactylus.util.template.TemplateContext; -import freenet.l10n.BaseL10n; /** * {@link Filter} implementation that replaces a {@link Long} with a value of - * {@code 0} by a {@link String} from an {@link BaseL10n l10n handler}. + * {@code 0} by a {@link String} from a {@link Translation translation}. */ public class UnknownDateFilter implements Filter { - /** The l10n handler. */ - private BaseL10n l10nHandler; + /** The translation. */ + private final Translation translation; /** The key for the text to show. */ private final String unknownKey; @@ -38,13 +38,11 @@ public class UnknownDateFilter implements Filter { /** * Creates a new unknown date filter. * - * @param l10nHandler - * The l10n handler - * @param unknownKey - * The key of the text to show + * @param translation The translation + * @param unknownKey The key of the text to show */ - public UnknownDateFilter(BaseL10n l10nHandler, String unknownKey) { - this.l10nHandler = l10nHandler; + public UnknownDateFilter(Translation translation, String unknownKey) { + this.translation = translation; this.unknownKey = unknownKey; } @@ -55,7 +53,7 @@ public class UnknownDateFilter implements Filter { public Object format(TemplateContext templateContext, Object data, Map parameters) { if (data instanceof Long) { if ((Long) data == 0) { - return l10nHandler.getString(unknownKey); + return translation.translate(unknownKey); } } return data; diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index a173d16..588ffb7 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -44,6 +44,7 @@ import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.L10nFilter; +import net.pterodactylus.sone.freenet.Translation; import net.pterodactylus.sone.main.Loaders; import net.pterodactylus.sone.main.PluginHomepage; import net.pterodactylus.sone.main.PluginVersion; @@ -105,7 +106,6 @@ import net.pterodactylus.util.web.TemplatePage; import freenet.clients.http.SessionManager; import freenet.clients.http.SessionManager.Session; import freenet.clients.http.ToadletContext; -import freenet.l10n.BaseL10n; import com.codahale.metrics.*; import com.google.common.base.Optional; @@ -158,6 +158,7 @@ public class WebInterface implements SessionProvider { private final PageToadletRegistry pageToadletRegistry; private final MetricRegistry metricRegistry; + private final Translation translation; /** The “new Sone” notification. */ private final ListNotification newSoneNotification; @@ -209,7 +210,7 @@ public class WebInterface implements SessionProvider { ParserFilter parserFilter, ShortenFilter shortenFilter, RenderFilter renderFilter, LinkedElementRenderFilter linkedElementRenderFilter, - PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry) { + PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry, Translation translation, L10nFilter l10nFilter) { this.sonePlugin = sonePlugin; this.loaders = loaders; this.listNotificationFilter = listNotificationFilter; @@ -223,9 +224,10 @@ public class WebInterface implements SessionProvider { this.linkedElementRenderFilter = linkedElementRenderFilter; this.pageToadletRegistry = pageToadletRegistry; this.metricRegistry = metricRegistry; + this.l10nFilter = l10nFilter; + this.translation = translation; formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword(); soneTextParser = new SoneTextParser(getCore(), getCore()); - l10nFilter = new L10nFilter(getL10n()); this.templateContextFactory = templateContextFactory; templateContextFactory.addTemplateObject("webInterface", this); @@ -379,13 +381,8 @@ public class WebInterface implements SessionProvider { return listNotificationFilter.filterNotifications(notificationManager.getNotifications(), currentSone); } - /** - * Returns the l10n helper of the node. - * - * @return The node’s l10n helper - */ - public BaseL10n getL10n() { - return sonePlugin.l10n().getBase(); + public Translation getTranslation() { + return translation; } /** diff --git a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java index 2b22377..08c935b 100644 --- a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java +++ b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java @@ -34,7 +34,6 @@ import freenet.clients.http.SessionManager; import freenet.clients.http.Toadlet; import freenet.clients.http.ToadletContext; import freenet.clients.http.ToadletContextClosedException; -import freenet.l10n.NodeL10n; import freenet.support.MultiValueTable; import freenet.support.api.HTTPRequest; @@ -107,7 +106,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil * if the toadlet context is closed */ public void handleMethodGET(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException { - handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext, NodeL10n.getBase(), sessionManager)); + handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext, sessionManager)); } /** @@ -125,7 +124,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil * if the toadlet context is closed */ public void handleMethodPOST(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException { - handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext, NodeL10n.getBase(), sessionManager)); + handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext, sessionManager)); } /** diff --git a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt index fb9156e..97b342a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt @@ -52,7 +52,6 @@ open class SoneModule(private val sonePlugin: SonePlugin, private val eventBus: bind(PluginHomepage::class.java).toInstance(PluginHomepage(sonePlugin.homepage)) bind(Database::class.java).to(MemoryDatabase::class.java).`in`(Singleton::class.java) bind(Translation::class.java).toInstance(BaseL10nTranslation(sonePlugin.l10n().base)) - bind(BaseL10n::class.java).toInstance(sonePlugin.l10n().base) loaders?.let { bind(Loaders::class.java).toInstance(it) } bind(MetricRegistry::class.java).`in`(Singleton::class.java) bind(WebOfTrustConnector::class.java).to(PluginWebOfTrustConnector::class.java).`in`(Singleton::class.java) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt b/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt index 283b1ea..f79eff5 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt @@ -1,7 +1,6 @@ package net.pterodactylus.sone.web import com.google.inject.* -import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.sone.core.* import net.pterodactylus.sone.data.* @@ -99,8 +98,8 @@ class WebInterfaceModule : AbstractModule() { ProfileAccessor(core) @Provides - fun getL10nFilter(l10n: BaseL10n) = - L10nFilter(l10n) + fun getL10nFilter(translation: Translation) = + L10nFilter(translation) @Provides fun getParserFilter(core: Core, soneTextParser: SoneTextParser) = @@ -115,8 +114,8 @@ class WebInterfaceModule : AbstractModule() { LinkedElementsFilter(elementLoader) @Provides - fun getUnknownDateFilter(l10n: BaseL10n) = - UnknownDateFilter(l10n, "View.Sone.Text.UnknownDate") + fun getUnknownDateFilter(translation: Translation) = + UnknownDateFilter(translation, "View.Sone.Text.UnknownDate") @Provides fun getImageLinkFilter(core: Core) = diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTranslationAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTranslationAjaxPage.kt index 41188ea..e2c41b6 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTranslationAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTranslationAjaxPage.kt @@ -16,6 +16,6 @@ class GetTranslationAjaxPage @Inject constructor(webInterface: WebInterface) : J override fun createJsonObject(request: FreenetRequest) = createSuccessJsonObject() - .put("value", webInterface.l10n.getString(request.parameters["key"])) + .put("value", webInterface.translation.translate(request.parameters["key"] ?: "")) } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt index 54c8f7f..65b45d9 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt @@ -19,8 +19,8 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* import freenet.clients.http.SessionManager.* -import freenet.l10n.* import freenet.support.api.* +import net.pterodactylus.sone.freenet.* import net.pterodactylus.util.web.* import java.net.* import java.util.UUID.* @@ -28,7 +28,6 @@ import java.util.UUID.* open class FreenetRequest(uri: URI, method: Method, val httpRequest: HTTPRequest, val toadletContext: ToadletContext, - val l10n: BaseL10n, val sessionManager: SessionManager ) : Request(uri, method) { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt index d3eed37..703f953 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt @@ -1,16 +1,16 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* -import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.sone.core.* import net.pterodactylus.sone.web.* import net.pterodactylus.util.web.* import java.net.* -class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, l10n: BaseL10n, sessionManager: SessionManager, - val core: Core, - val webInterface: WebInterface -) : FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager) +class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, sessionManager: SessionManager, + val core: Core, + val webInterface: WebInterface +) : FreenetRequest(uri, method, httpRequest, toadletContext, sessionManager) -fun FreenetRequest.toSoneRequest(core: Core, webInterface: WebInterface) = SoneRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager, core, webInterface) +fun FreenetRequest.toSoneRequest(core: Core, webInterface: WebInterface) = + SoneRequest(uri, method, httpRequest, toadletContext, sessionManager, core, webInterface) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt index c905ed2..9734a03 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt @@ -2,6 +2,7 @@ package net.pterodactylus.sone.web.pages import freenet.clients.http.* import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.freenet.* import net.pterodactylus.sone.main.* import net.pterodactylus.sone.utils.* import net.pterodactylus.sone.web.* @@ -20,11 +21,12 @@ open class SoneTemplatePage( templateRenderer: TemplateRenderer, private val pageTitleKey: String? = null, private val requiresLogin: Boolean = false, - private val pageTitle: (FreenetRequest) -> String = { pageTitleKey?.let(webInterface.l10n::getString) ?: "" } + private val pageTitle: (FreenetRequest) -> String = { pageTitleKey?.let(webInterface.translation::translate) ?: "" } ) : FreenetTemplatePage(templateRenderer, loaders, "noPermission.html") { private val core = webInterface.core private val sessionProvider: SessionProvider = webInterface + protected val translation: Translation = webInterface.translation protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) = sessionProvider.getCurrentSone(toadletContext, createSession) @@ -89,7 +91,7 @@ open class SoneTemplatePage( private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8") override fun isEnabled(toadletContext: ToadletContext) = - isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, webInterface.sessionManager, core, webInterface)) + isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.sessionManager, core, webInterface)) open fun isEnabled(soneRequest: SoneRequest) = when { requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/UploadImagePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/UploadImagePage.kt index 392b272..0021c6b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/UploadImagePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/UploadImagePage.kt @@ -33,7 +33,7 @@ class UploadImagePage @Inject constructor(webInterface: WebInterface, loaders: L val bytes = uploadedFile.data.use { it.toByteArray() } val bufferedImage = bytes.toImage() if (bufferedImage == null) { - templateContext["messages"] = soneRequest.l10n.getString("Page.UploadImage.Error.InvalidImage") + templateContext["messages"] = translation.translate("Page.UploadImage.Error.InvalidImage") return } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt index a498d24..efaee3f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt @@ -51,7 +51,7 @@ class ViewSonePage @Inject constructor(webInterface: WebInterface, loaders: Load override fun getPageTitle(soneRequest: SoneRequest): String = soneRequest.parameters["sone"]!!.let(soneRequest.core::getSone)?.let { sone -> - "${SoneAccessor.getNiceName(sone)} - ${soneRequest.l10n.getString("Page.ViewSone.Title")}" - } ?: soneRequest.l10n.getString("Page.ViewSone.Page.TitleWithoutSone") + "${SoneAccessor.getNiceName(sone)} - ${translation.translate("Page.ViewSone.Title")}" + } ?: translation.translate("Page.ViewSone.Page.TitleWithoutSone") } diff --git a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java index 7a84978..72f0bf7 100644 --- a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java @@ -23,7 +23,6 @@ import net.pterodactylus.util.web.Response; import freenet.clients.http.SessionManager; import freenet.clients.http.ToadletContext; -import freenet.l10n.BaseL10n; import freenet.support.api.HTTPRequest; import com.google.common.base.Charsets; @@ -41,7 +40,6 @@ public class DebugLoadersTest { @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); - private final BaseL10n l10n = mock(BaseL10n.class); private final StringWriter stringWriter = new StringWriter(); private final TemplateContext templateContext = new TemplateContext(); private Loaders loaders; @@ -72,7 +70,7 @@ public class DebugLoadersTest { HTTPRequest httpRequest = mock(HTTPRequest.class); ToadletContext toadletContext = mock(ToadletContext.class); SessionManager sessionManager = mock(SessionManager.class); - FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager); + FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, sessionManager); OutputStream outputStream = new ByteArrayOutputStream(); Response response = new Response(outputStream); page.handleRequest(request, response); diff --git a/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java index dc51479..d91e9e3 100644 --- a/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java @@ -23,7 +23,6 @@ import net.pterodactylus.util.web.Response; import freenet.clients.http.SessionManager; import freenet.clients.http.ToadletContext; -import freenet.l10n.BaseL10n; import freenet.support.api.HTTPRequest; import org.junit.Test; @@ -33,7 +32,6 @@ import org.junit.Test; */ public class DefaultLoadersTest { - private final BaseL10n l10n = mock(BaseL10n.class); private final Loaders loaders = new DefaultLoaders(); private final StringWriter stringWriter = new StringWriter(); private final TemplateContext templateContext = new TemplateContext(); @@ -53,7 +51,7 @@ public class DefaultLoadersTest { HTTPRequest httpRequest = mock(HTTPRequest.class); ToadletContext toadletContext = mock(ToadletContext.class); SessionManager sessionManager = mock(SessionManager.class); - FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager); + FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, sessionManager); OutputStream outputStream = new ByteArrayOutputStream(); Response response = new Response(outputStream); staticPage.handleRequest(request, response); diff --git a/src/test/kotlin/net/pterodactylus/sone/template/UnknownDateFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/UnknownDateFilterTest.kt index f18033e..b1c2cc5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/UnknownDateFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/UnknownDateFilterTest.kt @@ -1,30 +1,32 @@ package net.pterodactylus.sone.template -import freenet.l10n.BaseL10n -import net.pterodactylus.sone.test.mock -import net.pterodactylus.sone.test.whenever -import org.hamcrest.MatcherAssert.assertThat -import org.hamcrest.Matchers.equalTo -import org.junit.Test +import net.pterodactylus.sone.freenet.* +import net.pterodactylus.sone.test.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import java.util.* /** * Unit test for [UnknownDateFilter]. */ class UnknownDateFilterTest { - private val baseL10n = mock() + private val translation = object : Translation { + override val currentLocale = Locale.ENGLISH + override fun translate(key: String) = if (key == unknownKey) "translated" else "" + } private val unknownKey = "unknown.key" - private val filter = UnknownDateFilter(baseL10n, unknownKey) + private val filter = UnknownDateFilter(translation, unknownKey) @Test fun `filter returns given object for non-longs`() { - val someObject = Any() + val someObject = Any() assertThat(filter.format(null, someObject, null), equalTo(someObject)) } @Test fun `filter returns translated value of unknown key if zero is given`() { - whenever(baseL10n.getString(unknownKey)).thenReturn("translated") assertThat(filter.format(null, 0L, null), equalTo("translated")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt index ec09b20..4d8fc83 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt @@ -20,17 +20,21 @@ import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* +import java.util.* import kotlin.test.Test class WebInterfaceModuleTest { private val webInterfaceModule = WebInterfaceModule() - private val l10n = mock() private val loaders = mock() + private val translation = object : Translation { + override val currentLocale = Locale.ENGLISH + override fun translate(key: String) = if (key == "View.Sone.Text.UnknownDate") "unknown" else key + } private val additionalModules = arrayOf( Core::class.isProvidedByMock(), SoneProvider::class.isProvidedByMock(), - BaseL10n::class.isProvidedBy(l10n), + Translation::class.isProvidedBy(translation), SoneTextParser::class.isProvidedByMock(), ElementLoader::class.isProvidedByMock(), Loaders::class.isProvidedBy(loaders), @@ -191,7 +195,6 @@ class WebInterfaceModuleTest { @Test fun `unknown date filter uses correct l10n key`() { - whenever(l10n.getString("View.Sone.Text.UnknownDate")).thenReturn("unknown") assertThat(getFilter("unknown")!!.format(null, 0L, emptyMap()), equalTo("unknown")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt index a42e5ba..f902533 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt @@ -3,7 +3,6 @@ package net.pterodactylus.sone.web.ajax import com.fasterxml.jackson.databind.ObjectMapper import com.google.common.eventbus.EventBus import freenet.clients.http.ToadletContext -import freenet.l10n.BaseL10n import freenet.support.SimpleReadOnlyArrayBucket import freenet.support.api.HTTPRequest import net.pterodactylus.sone.core.Core @@ -20,6 +19,7 @@ import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.data.Sone.SoneStatus import net.pterodactylus.sone.data.Sone.SoneStatus.idle import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions +import net.pterodactylus.sone.freenet.* import net.pterodactylus.sone.test.deepMock import net.pterodactylus.sone.test.get import net.pterodactylus.sone.test.mock @@ -32,7 +32,7 @@ import net.pterodactylus.util.template.TemplateContextFactory import net.pterodactylus.util.web.Method.GET import net.pterodactylus.util.web.Method.POST import org.mockito.ArgumentMatchers -import java.util.NoSuchElementException +import java.util.* import javax.naming.SizeLimitExceededException /** @@ -44,7 +44,6 @@ open class TestObjects { val webInterface = mock() var formPassword = "form-password" - val l10n = mock() val core = mock() val eventBus = mock() val preferences = Preferences(eventBus) @@ -74,6 +73,11 @@ open class TestObjects { val images = mutableMapOf() val translations = mutableMapOf() + private val translation = object : Translation { + override val currentLocale = Locale.ENGLISH + override fun translate(key: String) = translations[key] ?: "" + } + init { whenever(webInterface.templateContextFactory).thenReturn(TemplateContextFactory()) whenever(webInterface.getCurrentSone(ArgumentMatchers.eq(toadletContext), ArgumentMatchers.anyBoolean())).thenReturn(currentSone) @@ -85,9 +89,7 @@ open class TestObjects { whenever(webInterface.getNotification(ArgumentMatchers.anyString())).then { notifications[it[0]].asOptional() } whenever(webInterface.getNewPosts(currentSone)).thenAnswer { newPosts.values } whenever(webInterface.getNewReplies(currentSone)).thenAnswer { newReplies.values } - whenever(webInterface.l10n).thenReturn(l10n) - - whenever(l10n.getString(ArgumentMatchers.anyString())).then { translations[it[0]] } + whenever(webInterface.translation).thenReturn(translation) whenever(core.preferences).thenReturn(preferences) whenever(core.updateChecker).thenReturn(updateChecker) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt index e05ad45..3295bdd 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt @@ -2,7 +2,6 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* import freenet.clients.http.SessionManager.* -import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.sone.test.* import net.pterodactylus.util.web.* @@ -19,9 +18,8 @@ class FreenetRequestTest { private val method = Method.GET private val httpRequest = mock(HTTPRequest::class.java) private val toadletContext = mock(ToadletContext::class.java) - private val l10n = mock() private val sessionManager = mock() - private val request = FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager) + private val request = FreenetRequest(uri, method, httpRequest, toadletContext, sessionManager) @Test fun `uri is retained correctly`() { @@ -44,11 +42,6 @@ class FreenetRequestTest { } @Test - fun `l10n is retained correctly`() { - assertThat(request.l10n, equalTo(l10n)) - } - - @Test fun `null is returned if no session exists`() { assertThat(request.existingSession, nullValue()) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt index 50c6ce7..5be6ff1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt @@ -19,11 +19,10 @@ class SoneRequestTest { private val method = Method.GET private val httpRequest = Mockito.mock(HTTPRequest::class.java) private val toadletContext = Mockito.mock(ToadletContext::class.java) - private val l10n = mock() private val sessionManager = mock() private val core = mock() private val webInterface = mock() - private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager, core, webInterface) + private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, sessionManager, core, webInterface) @Test fun `freenet request properties are retained correctly`() { @@ -31,7 +30,6 @@ class SoneRequestTest { assertThat(soneRequest.method, equalTo(method)) assertThat(soneRequest.httpRequest, equalTo(httpRequest)) assertThat(soneRequest.toadletContext, equalTo(toadletContext)) - assertThat(soneRequest.l10n, equalTo(l10n)) assertThat(soneRequest.sessionManager, equalTo(sessionManager)) } @@ -47,13 +45,12 @@ class SoneRequestTest { @Test fun `freenet request is wrapped correctly`() { - val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager) + val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext, sessionManager) val wrappedSoneRequest = freenetRequest.toSoneRequest(core, webInterface) assertThat(wrappedSoneRequest.uri, equalTo(uri)) assertThat(wrappedSoneRequest.method, equalTo(method)) assertThat(wrappedSoneRequest.httpRequest, equalTo(httpRequest)) assertThat(wrappedSoneRequest.toadletContext, equalTo(toadletContext)) - assertThat(wrappedSoneRequest.l10n, equalTo(l10n)) assertThat(wrappedSoneRequest.sessionManager, equalTo(sessionManager)) assertThat(wrappedSoneRequest.core, sameInstance(core)) assertThat(wrappedSoneRequest.webInterface, sameInstance(webInterface)) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePageTest.kt index 21cfdfc..5bcdd6a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePageTest.kt @@ -26,7 +26,7 @@ class DeleteSonePageTest : WebPageTest(::DeleteSonePage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.DeleteSone.Title")).thenReturn("delete sone page") + addTranslation("Page.DeleteSone.Title", "delete sone page") assertThat(page.getPageTitle(soneRequest), equalTo("delete sone page")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/DismissNotificationPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/DismissNotificationPageTest.kt index e8583df..7893210 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/DismissNotificationPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/DismissNotificationPageTest.kt @@ -28,7 +28,7 @@ class DismissNotificationPageTest : WebPageTest(::DismissNotificationPage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.DismissNotification.Title")).thenReturn("dismiss notification page") + addTranslation("Page.DismissNotification.Title", "dismiss notification page") assertThat(page.getPageTitle(soneRequest), equalTo("dismiss notification page")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/DistrustPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/DistrustPageTest.kt index d706e05..df16a53 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/DistrustPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/DistrustPageTest.kt @@ -26,7 +26,7 @@ class DistrustPageTest : WebPageTest(::DistrustPage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.Distrust.Title")).thenReturn("distrust page title") + addTranslation("Page.Distrust.Title", "distrust page title") assertThat(page.getPageTitle(soneRequest), equalTo("distrust page title")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt index 4d435b4..72e814b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt @@ -45,7 +45,7 @@ class EditAlbumPageTest : WebPageTest(::EditAlbumPage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.EditAlbum.Title")).thenReturn("edit album page") + addTranslation("Page.EditAlbum.Title", "edit album page") assertThat(page.getPageTitle(soneRequest), equalTo("edit album page")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt index 0961622..e43551c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt @@ -43,7 +43,7 @@ class EditImagePageTest : WebPageTest(::EditImagePage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.EditImage.Title")).thenReturn("edit image page title") + addTranslation("Page.EditImage.Title", "edit image page title") assertThat(page.getPageTitle(soneRequest), equalTo("edit image page title")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPageTest.kt index 7731fa2..4a84644 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPageTest.kt @@ -35,7 +35,7 @@ class EditProfileFieldPageTest : WebPageTest(::EditProfileFieldPage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.EditProfileField.Title")).thenReturn("edit profile field title") + addTranslation("Page.EditProfileField.Title", "edit profile field title") assertThat(page.getPageTitle(soneRequest), equalTo("edit profile field title")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt index a31f04c..74c3851 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt @@ -46,7 +46,7 @@ class EditProfilePageTest : WebPageTest(::EditProfilePage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.EditProfile.Title")).thenReturn("edit profile page title") + addTranslation("Page.EditProfile.Title", "edit profile page title") assertThat(page.getPageTitle(soneRequest), equalTo("edit profile page title")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/FollowSonePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/FollowSonePageTest.kt index ba1dc42..3873434 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/FollowSonePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/FollowSonePageTest.kt @@ -30,7 +30,7 @@ class FollowSonePageTest : WebPageTest(::FollowSonePage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.FollowSone.Title")).thenReturn("follow sone page title") + addTranslation("Page.FollowSone.Title", "follow sone page title") assertThat(page.getPageTitle(soneRequest), equalTo("follow sone page title")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt index a0e9372..1533f4b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt @@ -26,7 +26,7 @@ class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title") + addTranslation("Page.ImageBrowser.Title", "image browser page title") assertThat(page.getPageTitle(soneRequest), equalTo("image browser page title")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt index 9104127..8c86365 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt @@ -34,7 +34,7 @@ class IndexPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> I @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.Index.Title")).thenReturn("index page title") + addTranslation("Page.Index.Title", "index page title") assertThat(page.getPageTitle(soneRequest), equalTo("index page title")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt index 37e0e95..14cae74 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt @@ -73,7 +73,7 @@ class KnownSonesPageTest : WebPageTest(::KnownSonesPage) { @Test fun `page returns correct title`() { - whenever(l10n.getString("Page.KnownSones.Title")).thenReturn("known sones page title") + addTranslation("Page.KnownSones.Title", "known sones page title") assertThat(page.getPageTitle(soneRequest), equalTo("known sones page title")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt index 8e29df1..cffd478 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt @@ -31,7 +31,7 @@ class SoneTemplatePageTest : WebPageTest({ webInterface, loaders, templateRender @Test fun `page title is retrieved from l10n if page title key is given`() { SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = "page.title", requiresLogin = false).let { page -> - whenever(l10n.getString("page.title")).thenReturn("Page Title") + addTranslation("page.title", "Page Title") assertThat(page.getPageTitle(soneRequest), equalTo("Page Title")) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt index 61d1a03..07ae727 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt @@ -6,6 +6,7 @@ import freenet.support.* import freenet.support.api.* import net.pterodactylus.sone.core.* import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.freenet.* import net.pterodactylus.sone.freenet.wot.* import net.pterodactylus.sone.main.* import net.pterodactylus.sone.test.deepMock @@ -26,6 +27,7 @@ import org.mockito.ArgumentMatchers.eq import java.io.* import java.net.* import java.nio.charset.* +import java.util.* import kotlin.text.Charsets.UTF_8 /** @@ -40,7 +42,6 @@ open class WebPageTest(pageSupplier: (WebInterface, Loaders, TemplateRenderer) - val core = webInterface.core val eventBus = mock() val preferences = Preferences(eventBus) - val l10n = webInterface.l10n!! val sessionManager = mock() open val page by lazy { pageSupplier(webInterface, loaders, templateRenderer) } @@ -49,7 +50,6 @@ open class WebPageTest(pageSupplier: (WebInterface, Loaders, TemplateRenderer) - val freenetRequest = mock() init { - whenever(freenetRequest.l10n).thenReturn(l10n) whenever(freenetRequest.sessionManager).thenReturn(sessionManager) whenever(freenetRequest.uri).thenReturn(mock()) } @@ -77,12 +77,16 @@ open class WebPageTest(pageSupplier: (WebInterface, Loaders, TemplateRenderer) - private val notifications = mutableMapOf() private val translations = mutableMapOf() + private val translation = object : Translation { + override val currentLocale = Locale.ENGLISH + override fun translate(key: String) = translations[key] ?: key + } + init { setupCore() setupWebInterface() setupHttpRequest() setupFreenetRequest() - setupTranslations() } private fun setupCore() { @@ -108,6 +112,7 @@ open class WebPageTest(pageSupplier: (WebInterface, Loaders, TemplateRenderer) - whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone) whenever(webInterface.getNotifications(currentSone)).then { notifications.values } whenever(webInterface.getNotification(anyString())).then { notifications[it[0]].asOptional() } + whenever(webInterface.translation).thenReturn(translation) } private fun setupHttpRequest() { @@ -147,10 +152,6 @@ open class WebPageTest(pageSupplier: (WebInterface, Loaders, TemplateRenderer) - whenever(freenetRequest.toadletContext).thenReturn(toadletContext) } - private fun setupTranslations() { - whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] } - } - fun setMethod(method: Method) { whenever(httpRequest.method).thenReturn(method.name) whenever(freenetRequest.method).thenReturn(method) -- 2.7.4