From: David ‘Bombe’ Roden Date: Sat, 28 Sep 2024 21:24:47 +0000 (+0200) Subject: 🔥 Fix some compiler warnings X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2e088cfcfb40fe9379b305bd833130eca7b4675e;p=Sone.git 🔥 Fix some compiler warnings The following warnings have been taken care of: - Unused “event” parameters, used by Guava’s EventBus to identify event listener methods; reworked as extension methods - A type mismatch caused by an unbounded type parameter - Deprecated usages of String.capitalize() and .toLowerCase() - Replace usage of Guava’s deprecated Files.write() - Unchecked cast (solved by removing the whole piece of code, which was unused) - Some unused parameters and variables - Uses of ExpectedException in tests; replaced by assertThrows() - Warning about usage of deprecated getPluginTalker() method was suppressed - Suppressed some more warnings about unchecked casts - Change a number of casts to wildcard types - Specified some type parameters explicitely The warning on the RenderFilter has been intentionally left in, because at some point I need to figure out _why_ I thought that this expression could be null. --- diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt index eac1fa9..c99efaa 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt @@ -94,12 +94,12 @@ class IdentityManagerImpl @Inject constructor( } @Subscribe - fun strictFilteringActivated(event: StrictFilteringActivatedEvent) { + fun StrictFilteringActivatedEvent.strictFilteringActivated() { strictFiltering.set(true) } @Subscribe - fun strictFilteringDeactivated(event: StrictFilteringDeactivatedEvent) { + fun StrictFilteringDeactivatedEvent.strictFilteringDeactivated() { strictFiltering.set(false) } diff --git a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt index a4533c5..a482dd9 100644 --- a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt @@ -67,7 +67,7 @@ open class SoneModule(private val sonePlugin: SonePlugin, private val eventBus: bind(SessionProvider::class.java).to(FreenetSessionProvider::class.java).`in`(Singleton::class.java) bindListener(Matchers.any(), object : TypeListener { - override fun hear(typeLiteral: TypeLiteral, typeEncounter: TypeEncounter) { + override fun hear(typeLiteral: TypeLiteral, typeEncounter: TypeEncounter) { typeEncounter.register(InjectionListener { injectee -> logger.fine { "Injecting $injectee..." } eventBus.register(injectee) diff --git a/src/main/kotlin/net/pterodactylus/sone/template/HistogramRenderer.kt b/src/main/kotlin/net/pterodactylus/sone/template/HistogramRenderer.kt index dba32b2..b6038e0 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/HistogramRenderer.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/HistogramRenderer.kt @@ -3,6 +3,7 @@ package net.pterodactylus.sone.template import com.codahale.metrics.* import net.pterodactylus.sone.utils.* import net.pterodactylus.util.template.* +import java.util.Locale /** * [Filter] that renders a [Histogram] as a table row. @@ -43,4 +44,4 @@ private val template = """ """.asTemplate() private fun String.dotToCamel() = - split(".").joinToString("", transform = String::capitalize) + split(".").joinToString("", transform = { it.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } }) diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt index 7b7957b..876041c 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt @@ -5,6 +5,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.web.Method.GET import net.pterodactylus.util.web.Method.POST import net.pterodactylus.util.web.Request +import java.util.Locale val Request.isGET get() = this.method == GET val Request.isPOST get() = this.method == POST @@ -32,5 +33,5 @@ val FreenetRequest.headers get() = Headers(httpRequest) val HTTPRequest.headers get() = Headers(this) class Headers(private val request: HTTPRequest) { - operator fun get(name: String): String? = request.getHeader(name.toLowerCase()) + operator fun get(name: String): String? = request.getHeader(name.lowercase(Locale.US)) } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt b/src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt index 1026f36..6799ada 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt @@ -48,5 +48,3 @@ class NewElements @Inject constructor( .flatMap(ListNotification::elements) .filter { replyVisibilityFilter.isReplyVisible(null, it) } } - -private fun Collection<*>.cast(): List = map { it as R } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt index 959788b..98b15fb 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateSonePage.kt @@ -8,6 +8,7 @@ import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.template.* import java.util.logging.* import javax.inject.* +import java.util.Locale /** * The “create Sone” page lets the user create a new Sone. @@ -22,7 +23,7 @@ class CreateSonePage @Inject constructor(webInterface: WebInterface, loaders: Lo override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { templateContext["sones"] = soneRequest.core.localSones.sortedWith(niceNameComparator) - templateContext["identitiesWithoutSone"] = soneRequest.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}".toLowerCase() } + templateContext["identitiesWithoutSone"] = soneRequest.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}".lowercase(Locale.US) } if (soneRequest.isPOST) { val identity = soneRequest.httpRequest.getPartAsStringFailsafe("identity", 43) soneRequest.core.identityManager.allOwnIdentities.firstOrNull { it.id == identity }?.let { ownIdentity -> diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoggedInPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoggedInPage.kt index 5d389b9..e738c7b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoggedInPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoggedInPage.kt @@ -13,7 +13,7 @@ abstract class LoggedInPage(pageTitleKey: String, webInterface: WebInterface, lo SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = pageTitleKey, requiresLogin = true) { final override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { - handleRequest(soneRequest, getCurrentSone(soneRequest.toadletContext, false)!!, templateContext) + handleRequest(soneRequest, getCurrentSone(soneRequest.toadletContext)!!, templateContext) } protected abstract fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt index 6244325..977d9e6 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt @@ -35,7 +35,7 @@ class LoginPage @Inject constructor(webInterface: WebInterface, loaders: Loaders override fun isEnabled(soneRequest: SoneRequest) = when { soneRequest.core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess -> false - else -> getCurrentSone(soneRequest.toadletContext, false) == null + else -> getCurrentSone(soneRequest.toadletContext) == null } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt index 027aa86..b6507f0 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt @@ -25,7 +25,7 @@ class NewPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = "Page.New.Title") { override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) = - getCurrentSone(soneRequest.toadletContext).let { currentSone -> + getCurrentSone(soneRequest.toadletContext).let { _ -> (newElements.newPosts + newElements.newReplies.mapPresent { it.post }) .distinct() .sortedByDescending { it.time } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt index 9bbc3e5..a0c76dc 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt @@ -13,6 +13,7 @@ import net.pterodactylus.util.template.* import net.pterodactylus.util.text.* import java.util.concurrent.TimeUnit.* import javax.inject.* +import java.util.Locale /** * This page lets the user search for posts and replies that contain certain @@ -84,15 +85,15 @@ class SearchPage(webInterface: WebInterface, loaders: Loaders, templateRenderer: } private fun Sone.allText(soneNameCache: (Sone) -> String) = - (soneNameCache(this) + profile.fields.map { "${it.name} ${it.value}" }.joinToString(" ", " ")).toLowerCase() + (soneNameCache(this) + profile.fields.map { "${it.name} ${it.value}" }.joinToString(" ", " ")).lowercase(Locale.US) private fun Post.allText(soneNameCache: (Sone) -> String, getReplies: (String) -> Collection) = (text + recipient.orNull()?.let { " ${soneNameCache(it)}" } + getReplies(id) .filter(noFutureReply) - .map { "${soneNameCache(it.sone)} ${it.text}" }.joinToString(" ", " ")).toLowerCase() + .map { "${soneNameCache(it.sone)} ${it.text}" }.joinToString(" ", " ")).lowercase(Locale.US) private fun Iterable.indicesFor(text: String, predicate: (Phrase) -> Boolean) = - filter(predicate).map(Phrase::phrase).map(String::toLowerCase).flatMap { text.findAll(it) } + filter(predicate).map(Phrase::phrase).map { it.lowercase(Locale.US) }.flatMap { text.findAll(it) } private fun score(text: String, phrases: Iterable): Double { val requiredPhrases = phrases.count { it.required } 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 86131c3..7b81c50 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt @@ -28,7 +28,7 @@ open class SoneTemplatePage( private val sessionProvider: SessionProvider = webInterface protected val translation: Translation = webInterface.translation - protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) = + protected fun getCurrentSone(toadletContext: ToadletContext) = sessionProvider.getCurrentSone(toadletContext) protected fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) = diff --git a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java index e85c131..a9018cd 100644 --- a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java @@ -1,5 +1,7 @@ package net.pterodactylus.sone.main; +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.nio.file.Files.write; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; @@ -12,6 +14,8 @@ import java.io.OutputStream; import java.io.StringWriter; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Template; @@ -24,8 +28,6 @@ import net.pterodactylus.util.web.Response; import freenet.clients.http.ToadletContext; import freenet.support.api.HTTPRequest; -import com.google.common.base.Charsets; -import com.google.common.io.Files; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -47,11 +49,11 @@ public class DebugLoadersTest { public void setupLoader() throws IOException { String templatePath = temporaryFolder.newFolder("temps").getPath(); loaders = new DebugLoaders(templatePath); - File templateFile = new File(templatePath, "template.txt"); - Files.write("<%if foo>foo<%else>bar<%/if>", templateFile, Charsets.UTF_8); + Path templateFile = Paths.get(templatePath, "template.txt"); + write(templateFile, "<%if foo>foo<%else>bar<%/if>".getBytes(UTF_8)); new File(templatePath, "templates").mkdir(); - File secondTemplateFile = new File(templatePath, "templates/template.txt"); - Files.write("<%if foo>foo<%else>bar<%/if>", secondTemplateFile, Charsets.UTF_8); + Path secondTemplateFile = Paths.get(templatePath, "templates/template.txt"); + write(secondTemplateFile, "<%if foo>foo<%else>bar<%/if>".getBytes(UTF_8)); } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/core/ConfigurationSoneParserTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/ConfigurationSoneParserTest.kt index 29ade5f..48b0c4c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/ConfigurationSoneParserTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/ConfigurationSoneParserTest.kt @@ -9,7 +9,7 @@ import net.pterodactylus.util.config.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* -import org.junit.rules.* +import org.junit.Assert.assertThrows import org.mockito.ArgumentMatchers.* import org.mockito.ArgumentMatchers.eq @@ -18,10 +18,6 @@ import org.mockito.ArgumentMatchers.eq */ class ConfigurationSoneParserTest { - @Rule - @JvmField - val expectedException = ExpectedException.none()!! - private val configuration = mock() private val sone = mock().apply { whenever(this.id).thenReturn("1") @@ -125,8 +121,9 @@ class ConfigurationSoneParserTest { @Test fun postWithoutTimeIsRecognized() { setupPostWithoutTime() - expectedException.expect() - configurationSoneParser.parsePosts(createPostBuilderFactory()) + assertThrows(InvalidPostFound::class.java) { + configurationSoneParser.parsePosts(createPostBuilderFactory()) + } } private fun setupPostWithoutTime() { @@ -136,8 +133,9 @@ class ConfigurationSoneParserTest { @Test fun postWithoutTextIsRecognized() { setupPostWithoutText() - expectedException.expect() - configurationSoneParser.parsePosts(createPostBuilderFactory()) + assertThrows(InvalidPostFound::class.java) { + configurationSoneParser.parsePosts(createPostBuilderFactory()) + } } private fun setupPostWithoutText() { @@ -188,8 +186,9 @@ class ConfigurationSoneParserTest { @Test fun missingPostIdIsRecognized() { setupPostReplyWithMissingPostId() - expectedException.expect() - configurationSoneParser.parsePostReplies(null) + assertThrows(InvalidPostReplyFound::class.java) { + configurationSoneParser.parsePostReplies(null) + } } private fun setupPostReplyWithMissingPostId() { @@ -199,8 +198,9 @@ class ConfigurationSoneParserTest { @Test fun missingPostReplyTimeIsRecognized() { setupPostReplyWithMissingPostReplyTime() - expectedException.expect() - configurationSoneParser.parsePostReplies(null) + assertThrows(InvalidPostReplyFound::class.java) { + configurationSoneParser.parsePostReplies(null) + } } private fun setupPostReplyWithMissingPostReplyTime() { @@ -210,8 +210,9 @@ class ConfigurationSoneParserTest { @Test fun missingPostReplyTextIsRecognized() { setupPostReplyWithMissingPostReplyText() - expectedException.expect() - configurationSoneParser.parsePostReplies(null) + assertThrows(InvalidPostReplyFound::class.java) { + configurationSoneParser.parsePostReplies(null) + } } private fun setupPostReplyWithMissingPostReplyText() { @@ -305,22 +306,25 @@ class ConfigurationSoneParserTest { @Test fun albumWithInvalidTitleIsRecognized() { setupAlbum(0, "A1", null, null, "D1", "I1") - expectedException.expect() - configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) + assertThrows(InvalidAlbumFound::class.java) { + configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) + } } @Test fun albumWithInvalidDescriptionIsRecognized() { setupAlbum(0, "A1", null, "T1", null, "I1") - expectedException.expect() - configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) + assertThrows(InvalidAlbumFound::class.java) { + configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) + } } @Test fun albumWithInvalidParentIsRecognized() { setupAlbum(0, "A1", "A0", "T1", "D1", "I1") - expectedException.expect() - configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) + assertThrows(InvalidParentAlbumFound::class.java) { + configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) + } } @Test @@ -365,8 +369,9 @@ class ConfigurationSoneParserTest { setupTopLevelAlbums() configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) setupImage(0, "I1", null, 1000L, "K1", "T1", "D1", 16, 9) - expectedException.expect() - configurationSoneParser.parseImages(createImageBuilderFactory()) + assertThrows(InvalidImageFound::class.java) { + configurationSoneParser.parseImages(createImageBuilderFactory()) + } } @Test @@ -374,8 +379,9 @@ class ConfigurationSoneParserTest { setupTopLevelAlbums() configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) setupImage(0, "I1", "A4", 1000L, "K1", "T1", "D1", 16, 9) - expectedException.expect() - configurationSoneParser.parseImages(createImageBuilderFactory()) + assertThrows(InvalidParentAlbumFound::class.java) { + configurationSoneParser.parseImages(createImageBuilderFactory()) + } } @Test @@ -383,8 +389,9 @@ class ConfigurationSoneParserTest { setupTopLevelAlbums() configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) setupImage(0, "I1", "A1", null, "K1", "T1", "D1", 16, 9) - expectedException.expect() - configurationSoneParser.parseImages(createImageBuilderFactory()) + assertThrows(InvalidImageFound::class.java) { + configurationSoneParser.parseImages(createImageBuilderFactory()) + } } @Test @@ -392,8 +399,9 @@ class ConfigurationSoneParserTest { setupTopLevelAlbums() configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) setupImage(0, "I1", "A1", 1000L, null, "T1", "D1", 16, 9) - expectedException.expect() - configurationSoneParser.parseImages(createImageBuilderFactory()) + assertThrows(InvalidImageFound::class.java) { + configurationSoneParser.parseImages(createImageBuilderFactory()) + } } @Test @@ -401,8 +409,9 @@ class ConfigurationSoneParserTest { setupTopLevelAlbums() configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) setupImage(0, "I1", "A1", 1000L, "K1", null, "D1", 16, 9) - expectedException.expect() - configurationSoneParser.parseImages(createImageBuilderFactory()) + assertThrows(InvalidImageFound::class.java) { + configurationSoneParser.parseImages(createImageBuilderFactory()) + } } @Test @@ -410,8 +419,9 @@ class ConfigurationSoneParserTest { setupTopLevelAlbums() configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) setupImage(0, "I1", "A1", 1000L, "K1", "T1", null, 16, 9) - expectedException.expect() - configurationSoneParser.parseImages(createImageBuilderFactory()) + assertThrows(InvalidImageFound::class.java) { + configurationSoneParser.parseImages(createImageBuilderFactory()) + } } @Test @@ -419,8 +429,9 @@ class ConfigurationSoneParserTest { setupTopLevelAlbums() configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", null, 9) - expectedException.expect() - configurationSoneParser.parseImages(createImageBuilderFactory()) + assertThrows(InvalidImageFound::class.java) { + configurationSoneParser.parseImages(createImageBuilderFactory()) + } } @Test @@ -428,8 +439,9 @@ class ConfigurationSoneParserTest { setupTopLevelAlbums() configurationSoneParser.parseTopLevelAlbums(createAlbumBuilderFactory()) setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", 16, null) - expectedException.expect() - configurationSoneParser.parseImages(createImageBuilderFactory()) + assertThrows(InvalidImageFound::class.java) { + configurationSoneParser.parseImages(createImageBuilderFactory()) + } } } diff --git a/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt index ca5e21b..3bf5791 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt @@ -26,7 +26,6 @@ import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.notNullValue import org.hamcrest.Matchers.nullValue import org.junit.* -import org.junit.rules.* import org.mockito.* import org.mockito.ArgumentCaptor.* import org.mockito.ArgumentMatchers.eq @@ -34,6 +33,7 @@ import org.mockito.Mockito.* import java.io.* import java.util.* import kotlin.test.Test +import org.junit.Assert.assertThrows /** * Unit test for [FreenetInterface]. @@ -42,10 +42,6 @@ class FreenetInterfaceTest { @Rule @JvmField - val expectionException: ExpectedException = ExpectedException.none() - - @Rule - @JvmField val silencedLogging = silencedLogging() @Suppress("UnstableApiUsage") @@ -163,8 +159,9 @@ class FreenetInterfaceTest { whenever(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext) val insertBlockCaptor = forClass(InsertBlock::class.java) whenever(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(null as String?), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenThrow(InsertException::class.java) - expectionException.expect(SoneInsertException::class.java) - freenetInterface.insertImage(temporaryImage, image, insertToken) + assertThrows(SoneInsertException::class.java) { + freenetInterface.insertImage(temporaryImage, image, insertToken) + } } @Test @@ -180,8 +177,9 @@ class FreenetInterfaceTest { @Test fun `insert exception is forwarded as sone exception`() { whenever(highLevelSimpleClient.insertManifest(any(), any(), any())).thenThrow(InsertException::class.java) - expectionException.expect(SoneException::class.java) - freenetInterface.insertDirectory(null, null, null) + assertThrows(SoneException::class.java) { + freenetInterface.insertDirectory(null, null, null) + } } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.kt index 6f8d5d1..480cdd3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.kt @@ -99,7 +99,7 @@ class ConfigurationLoaderTest { val sone1Id = setupStringValue("SoneFollowingTimes/0/Sone", "Sone1") val sone1Time = setupLongValue("SoneFollowingTimes/0/Time", 1000L) val sone2Id = setupStringValue("SoneFollowingTimes/1/Sone", "Sone2") - val sone2Time = setupLongValue("SoneFollowingTimes/1/Time", 2000L) + setupLongValue("SoneFollowingTimes/1/Time", 2000L) setupStringValue("SoneFollowingTimes/2/Sone") configurationLoader.removeSoneFollowingTime("Sone1") assertThat(sone1Id.value, equalTo("Sone2")) diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt index 9b9ed8b..0d07b7b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt @@ -6,6 +6,7 @@ import net.pterodactylus.sone.test.whenever import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.nullValue +import org.junit.Assert.assertThrows import org.junit.Test /** @@ -84,8 +85,9 @@ class GetSoneCommandTest : SoneCommandTest() { whenever(localSone.id).thenReturn("RemoteSone") parameters += "Sone" to "SoneId" parameters += "LocalSone" to "RemoteSone" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt index 7041f25..c01b456 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt @@ -6,6 +6,7 @@ import net.pterodactylus.sone.test.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* +import org.junit.Assert.assertThrows import org.mockito.Mockito.* /** @@ -37,31 +38,35 @@ class LikePostCommandTest : SoneCommandTest() { @Test fun `request with invalid post id results in FCP exception`() { parameters += "Post" to "InvalidPostId" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } @Test fun `request with missing local sone results in FCP exception`() { parameters += "Post" to "PostId" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } @Test fun `request with invalid sone results in FCP exception`() { parameters += "Post" to "PostId" parameters += "Sone" to "InvalidSoneId" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } @Test fun `request with valid remote sone results in FCP exception`() { parameters += "Post" to "PostId" parameters += "Sone" to "RemoteSoneId" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt index 0d0579a..47d48d4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt @@ -6,6 +6,7 @@ import net.pterodactylus.sone.test.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* +import org.junit.Assert.assertThrows import org.mockito.Mockito.* /** @@ -37,31 +38,35 @@ class LikeReplyCommandTest : SoneCommandTest() { @Test fun `request with invalid reply results in FCP exception`() { parameters += "Reply" to "InvalidReplyId" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } @Test fun `request without sone results in FCP exception`() { parameters += "Reply" to "ReplyId" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } @Test fun `request with invalid sone results in FCP exception`() { parameters += "Reply" to "ReplyId" parameters += "Sone" to "InvalidSoneId" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } @Test fun `request with remote sone results in FCP exception`() { parameters += "Reply" to "ReplyId" parameters += "Sone" to "RemoteSoneId" - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt index 91eabf5..fafeb8a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -8,7 +8,7 @@ import net.pterodactylus.sone.template.* import net.pterodactylus.sone.test.* import net.pterodactylus.sone.utils.* import org.junit.* -import org.junit.rules.* +import org.junit.Assert.assertThrows import org.mockito.ArgumentMatchers.* /** @@ -16,8 +16,6 @@ import org.mockito.ArgumentMatchers.* */ abstract class SoneCommandTest { - @Rule @JvmField val expectedException = ExpectedException.none()!! - protected val core = mock() protected val command: AbstractSoneCommand by lazy { createCommand(core) } @@ -63,13 +61,15 @@ abstract class SoneCommandTest { } protected fun executeCommandAndExpectFcpException() { - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } protected fun requestWithoutAnyParameterResultsInFcpException() { - expectedException.expect(FcpException::class.java) - command.execute(parameters) + assertThrows(FcpException::class.java) { + command.execute(parameters) + } } protected fun requestWithEmptySoneParameterResultsInFcpException() { diff --git a/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt index 476b935..cc314ab 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt @@ -13,7 +13,7 @@ import net.pterodactylus.sone.test.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* -import org.junit.rules.* +import org.junit.Assert.assertThrows import org.mockito.* import org.mockito.Mockito.* @@ -22,10 +22,6 @@ import org.mockito.Mockito.* */ class FreenetModuleTest { - @Rule - @JvmField - val expectedException = ExpectedException.none()!! - private val sessionManager = mock() private val uskManager = mock() private val clientContext = mock() @@ -47,8 +43,9 @@ class FreenetModuleTest { @Test fun `plugin respirator is not bound`() { - expectedException.expect(Exception::class.java) - injector.getInstance() + assertThrows(Exception::class.java) { + injector.getInstance() + } } @Test @@ -103,6 +100,7 @@ class FreenetModuleTest { } @Test + @Suppress("DEPRECATION") fun `plugin respirator facade is returned correctly`() { val pluginRespiratorFacade = injector.getInstance() pluginRespiratorFacade.getPluginTalker(mock(), "test.plugin", "test-request-1") diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt index dfa4830..27edbbd 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt @@ -219,6 +219,7 @@ class SonePluginTest { assertThat(shutdownReceived.get(), equalTo(true)) } + @Suppress("UNCHECKED_CAST") private fun getInjected(clazz: Class, annotation: Annotation? = null): T? = injected[TypeLiteral.get(clazz) to annotation] as? T diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/DefaultListNotificationFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/DefaultListNotificationFilterTest.kt index 33aac9f..e55cb53 100644 --- a/src/test/kotlin/net/pterodactylus/sone/notify/DefaultListNotificationFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/notify/DefaultListNotificationFilterTest.kt @@ -106,7 +106,7 @@ class DefaultListNotificationFilterTest { newPostNotification.add(createPost()) val listNotificationFilter = DefaultListNotificationFilter(matchThisPost(newPostNotification.elements[1]), showAllReplies) val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone) - assertThat((filteredNotifications[0] as ListNotification).elements, contains(newPostNotification.elements[1])) + assertThat((filteredNotifications[0] as ListNotification<*>).elements, contains(newPostNotification.elements[1])) } @Test @@ -119,7 +119,7 @@ class DefaultListNotificationFilterTest { val listNotificationFilter = DefaultListNotificationFilter(showAllPosts, matchThisReply(newReplyNotification.elements[1])) val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone) assertThat(filteredNotifications, hasSize(1)) - assertThat((filteredNotifications[0] as ListNotification).elements[0], equalTo(newReplyNotification.elements[1])) + assertThat((filteredNotifications[0] as ListNotification<*>).elements[0], equalTo(newReplyNotification.elements[1])) } @Test @@ -130,7 +130,7 @@ class DefaultListNotificationFilterTest { newReplyNotification.add(createPostReply()) newReplyNotification.add(createPostReply()) val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone) - assertThat(filteredNotifications, contains(newReplyNotification)) + assertThat(filteredNotifications, contains(newReplyNotification)) } @Test @@ -173,7 +173,7 @@ class DefaultListNotificationFilterTest { val listNotificationFilter = DefaultListNotificationFilter(matchThisPost(mentionNotification.elements[1]), showAllReplies) val filteredNotifications = listNotificationFilter.filterNotifications(listOf(mentionNotification), null) assertThat(filteredNotifications, hasSize(1)) - assertThat((filteredNotifications[0] as ListNotification).elements[0], equalTo(mentionNotification.elements[1])) + assertThat((filteredNotifications[0] as ListNotification<*>).elements[0], equalTo(mentionNotification.elements[1])) } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/template/FilesystemTemplateTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/FilesystemTemplateTest.kt index f6c704c..0d5f948 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/FilesystemTemplateTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/FilesystemTemplateTest.kt @@ -4,20 +4,17 @@ import net.pterodactylus.util.template.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* -import org.junit.rules.* import java.io.* import java.lang.Thread.* import kotlin.test.Test +import net.pterodactylus.sone.template.FilesystemTemplate.TemplateFileNotFoundException +import org.junit.Assert.assertThrows /** * Unit test for [FilesystemTemplate]. */ class FilesystemTemplateTest() { - @Rule - @JvmField - val expectedException: ExpectedException = ExpectedException.none() - private val tempFile = File.createTempFile("template-", ".dat") private val filesystemTemplate: FilesystemTemplate private val templateContext = TemplateContext() @@ -48,8 +45,9 @@ class FilesystemTemplateTest() { @Test fun `loading template from non existing file throws exception`() { val filesystemTemplate = FilesystemTemplate("/a/b/c.dat") - expectedException.expect(FilesystemTemplate.TemplateFileNotFoundException::class.java) - filesystemTemplate.initialContext + assertThrows(TemplateFileNotFoundException::class.java) { + filesystemTemplate.initialContext + } } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt index bb92810..3d822fe 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt @@ -34,7 +34,7 @@ class PostAccessorTest { createPostReply(-4000) ) whenever(core.getReplies("post-id")).thenReturn(replies) - val repliesForPost = accessor[null, post, "replies"] as Collection + val repliesForPost = accessor[null, post, "replies"] as Collection<*> assertThat(repliesForPost, contains( replies[1], replies[2], @@ -51,7 +51,7 @@ class PostAccessorTest { fun `accessor returns the liking sones`() { val sones = setOf() whenever(core.getLikes(post)).thenReturn(sones) - val likingSones = accessor[null, post, "likes"] as Set + val likingSones = accessor[null, post, "likes"] as Set<*> assertThat(likingSones, equalTo(sones)) } diff --git a/src/test/kotlin/net/pterodactylus/sone/template/RenderFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/RenderFilterTest.kt index 5592808..4775453 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/RenderFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/RenderFilterTest.kt @@ -108,7 +108,7 @@ class RenderFilterTest { private fun setupPost(sone: Sone, value: String) = mock().apply { whenever(id).thenReturn(POST_ID) - whenever(this.sone).thenReturn(this@RenderFilterTest.sone) + whenever(this.sone).thenReturn(sone) whenever(text).thenReturn(value) } diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt index 4d345b3..b669dba 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt @@ -30,5 +30,6 @@ inline fun argumentCaptor(): ArgumentCaptor = ArgumentCaptor.forC fun eq(t: T): T { ArgumentMatchers.eq(t) + @Suppress("UNCHECKED_CAST") return null as T } diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt index 76cc723..65df9ec 100644 --- a/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt @@ -23,13 +23,13 @@ class ObjectsTest { @Test fun `exception is thrown for null and true condition`() { assertFailsWith(IllegalArgumentException::class) { - null.throwOnNullIf(true) { IllegalArgumentException() } + null.throwOnNullIf(true) { IllegalArgumentException() } } } @Test fun `exception is not thrown for null and false condition`() { - assertThat(null.throwOnNullIf(false) { IllegalArgumentException() }, nullValue()) + assertThat(null.throwOnNullIf(false) { IllegalArgumentException() }, nullValue()) } @Test @@ -47,13 +47,13 @@ class ObjectsTest { @Test fun `onNull is executed on null`() { val called = CountDownLatch(1) - null.onNull { called.countDown() } + null.onNull { called.countDown() } assertThat(called.count, equalTo(0L)) } @Test fun `onNull returns null when called on null`() { - assertThat(null.onNull {}, nullValue()) + assertThat(null.onNull {}, nullValue()) } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt index 2134661..d38ecae 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt @@ -5,10 +5,9 @@ import freenet.clients.http.* import net.pterodactylus.sone.main.* import net.pterodactylus.sone.test.* import net.pterodactylus.sone.web.page.* -import org.junit.* -import org.junit.rules.* import org.mockito.Mockito.* import kotlin.test.Test +import org.junit.Assert.assertThrows class PageToadletRegistryTest { @@ -24,9 +23,6 @@ class PageToadletRegistryTest { SonePlugin::class.isProvidedBy(sonePlugin) ) - @JvmField - @Rule - val expectedException: ExpectedException = ExpectedException.none() private val pageToadletRegistry = injector.getInstance() @Test @@ -60,8 +56,9 @@ class PageToadletRegistryTest { val toadletWithMenuname = createPageToadlet("Test") whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadletWithMenuname) pageToadletRegistry.registerToadlets() - expectedException.expect(IllegalStateException::class.java) - pageToadletRegistry.addPage(page) + assertThrows(IllegalStateException::class.java) { + pageToadletRegistry.addPage(page) + } } @Test @@ -136,8 +133,9 @@ class PageToadletRegistryTest { val toadlet = createPageToadlet() whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet) pageToadletRegistry.registerToadlets() - expectedException.expect(IllegalStateException::class.java) - pageToadletRegistry.addDebugPage(page) + assertThrows(IllegalStateException::class.java) { + pageToadletRegistry.addDebugPage(page) + } } private fun createPageToadlet(menuName: String? = null) = diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt index f9554f3..bfac084 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt @@ -130,6 +130,6 @@ class JsonPageBaseTest : TestObjects() { assertThat(page.isPrefixPage, equalTo(false)) } - private fun String.asJson() = objectMapper.readValue(this, Map::class.java) as Map + private fun String.asJson() = objectMapper.readValue(this, Map::class.java) as Map<*, *> } 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 17fc9a9..b3240ee 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt @@ -115,7 +115,7 @@ open class TestObjects { whenever(freenetRequest.httpRequest).thenReturn(httpRequest) whenever(httpRequest.method).thenReturn("GET") - whenever(httpRequest.getHeader(ArgumentMatchers.anyString())).thenAnswer { requestHeaders[it.get(0).toLowerCase()] } + whenever(httpRequest.getHeader(ArgumentMatchers.anyString())).thenAnswer { requestHeaders[it.get(0).lowercase(Locale.US)] } whenever(httpRequest.getParam(ArgumentMatchers.anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: "" } whenever(httpRequest.getParam(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: it.getArgument(1) } whenever(httpRequest.getParam(ArgumentMatchers.anyString(), ArgumentMatchers.isNull())).thenAnswer { requestParameters[it.getArgument(0)] } @@ -148,7 +148,7 @@ open class TestObjects { } protected fun addRequestHeader(key: String, value: String) { - requestHeaders += key.toLowerCase() to value + requestHeaders += key.lowercase(Locale.US) to value } protected fun addRequestParameter(key: String, value: String) { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/TemplateRendererTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/TemplateRendererTest.kt index cb03c61..82f2fd4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/TemplateRendererTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/TemplateRendererTest.kt @@ -8,13 +8,10 @@ import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* -import org.junit.rules.* +import org.junit.Assert.assertThrows class TemplateRendererTest { - @Rule - @JvmField - val expectedException: ExpectedException = ExpectedException.none() private val templateContextFactory = TemplateContextFactory() private val templateRenderer = TemplateRenderer(templateContextFactory) @@ -45,8 +42,9 @@ class TemplateRendererTest { @Test fun `redirect exceptions are thrown`() { - expectedException.expect(RedirectException::class.java) - templateRenderer.render(Template()) { _ -> throw RedirectException("foo") } + assertThrows(RedirectException::class.java) { + templateRenderer.render(Template()) { _ -> throw RedirectException("foo") } + } } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt index 2424b2d..a64feac 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt @@ -23,18 +23,12 @@ import net.pterodactylus.sone.web.WebTestUtils.* import net.pterodactylus.sone.web.page.FreenetTemplatePage.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* -import org.junit.Rule -import org.junit.rules.* -import org.junit.rules.ExpectedException.* import org.mockito.Mockito.* import kotlin.test.* +import org.junit.Assert.assertThrows class DebugPageTest : WebPageTest(::DebugPage) { - @Rule - @JvmField - val expectedException: ExpectedException = none() - @Test fun `page returns correct path`() { assertThat(page.path, equalTo("debug")) @@ -61,9 +55,10 @@ class DebugPageTest : WebPageTest(::DebugPage) { @Test fun `get request redirects to index`() { - expectedException.expect(redirectsTo("./")) - page.handleRequest(soneRequest, templateContext) + val redirect = assertThrows(RedirectException::class.java) { + page.handleRequest(soneRequest, templateContext) + } + assertThat(redirect, redirectsTo("./")) } } - 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 23987b6..8059f0f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt @@ -10,6 +10,7 @@ import net.pterodactylus.sone.web.page.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* +import java.util.Locale /** * Unit test for [KnownSonesPage]. @@ -46,7 +47,7 @@ class KnownSonesPageTest : WebPageTest(::KnownSonesPage) { val rootAlbum = AlbumImpl(this).also { it.addAlbum(album) } whenever(this.rootAlbum).thenReturn(rootAlbum) whenever(this.profile).thenReturn(mock()) - whenever(id).thenReturn(name.toLowerCase()) + whenever(id).thenReturn(name.lowercase(Locale.US)) whenever(this.name).thenReturn(name) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt index 329f458..d7e5890 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt @@ -258,7 +258,7 @@ class SearchPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> createSoneWithPost("without-match", "no match") addHttpRequestParameter("query", "recipient") verifyNoRedirect { - assertThat(this["postHits"], contains(postWithMatch)) + assertThat(get>("postHits"), contains(postWithMatch)) } } @@ -270,7 +270,7 @@ class SearchPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> createSoneWithPost("without-match", "no match") addHttpRequestParameter("query", "value") verifyNoRedirect { - assertThat(this["soneHits"], contains(soneWithProfileField)) + assertThat(get>("soneHits"), contains(soneWithProfileField)) } } @@ -282,7 +282,7 @@ class SearchPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> addHttpRequestParameter("query", "sone") verifyNoRedirect { assertThat(this["sonePagination"], isOnPage(0).hasPages(2)) - assertThat(this["soneHits"], contains(sones[0], sones[2])) + assertThat(get>("soneHits"), contains(sones[0], sones[2])) } } @@ -295,7 +295,7 @@ class SearchPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> addHttpRequestParameter("sonePage", "1") verifyNoRedirect { assertThat(this["sonePagination"], isOnPage(1).hasPages(2)) - assertThat(this["soneHits"], contains(sones[3])) + assertThat(get>("soneHits"), contains(sones[3])) } } @@ -306,7 +306,7 @@ class SearchPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> addHttpRequestParameter("query", "sone") verifyNoRedirect { assertThat(this["postPagination"], isOnPage(0).hasPages(2)) - assertThat(this["postHits"], contains(sones[0], sones[2])) + assertThat(get>("postHits"), contains(sones[0], sones[2])) } } @@ -318,7 +318,7 @@ class SearchPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> addHttpRequestParameter("postPage", "1") verifyNoRedirect { assertThat(this["postPagination"], isOnPage(1).hasPages(2)) - assertThat(this["postHits"], contains(sones[3])) + assertThat(get>("postHits"), contains(sones[3])) } } @@ -331,7 +331,7 @@ class SearchPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> addSone("sone", sone) addHttpRequestParameter("query", "text") verifyNoRedirect { - assertThat(this["postHits"], contains(post)) + assertThat(get>("postHits"), contains(post)) } verifyNoRedirect { assertThat(callCounter.get(), equalTo(1)) @@ -347,7 +347,7 @@ class SearchPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> addSone("sone", sone) addHttpRequestParameter("query", "text") verifyNoRedirect { - assertThat(this["postHits"], contains(post)) + assertThat(get>("postHits"), contains(post)) } whenever(ticker.read()).thenReturn(TimeUnit.MINUTES.toNanos(5) + 1) verifyNoRedirect { 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 badc2bc..6bfeeca 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt @@ -112,7 +112,7 @@ open class WebPageTest(pageSupplier: (WebInterface, Loaders, TemplateRenderer) - private fun setupHttpRequest() { whenever(httpRequest.method).thenReturn("GET") - whenever(httpRequest.getHeader(anyString())).then { requestHeaders[it.get(0).toLowerCase()] } + whenever(httpRequest.getHeader(anyString())).then { requestHeaders[it.get(0).lowercase(Locale.US)] } whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() } whenever(httpRequest.parameterNames).then { getRequestParameters.keys } whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters } @@ -158,7 +158,7 @@ open class WebPageTest(pageSupplier: (WebInterface, Loaders, TemplateRenderer) - } fun addHttpRequestHeader(name: String, value: String) { - requestHeaders[name.toLowerCase()] = value + requestHeaders[name.lowercase(Locale.US)] = value } fun addHttpRequestParameter(name: String, value: String) {