From 2dcced219ef62bfe60611bfadd91506b70d883be Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 20 Jan 2024 15:55:22 +0000 Subject: [PATCH] MockWebServer Named annotations --- gradle/libs.versions.toml | 2 ++ mockwebserver-junit5/README.md | 4 ++-- mockwebserver-junit5/build.gradle.kts | 1 + .../junit5/internal/MockWebServerExtension.kt | 6 +++--- .../junit5/internal/MockWebServerInstance.kt | 20 ------------------- .../ExtensionMultipleInstancesTest.kt | 13 ++++++------ .../internal/ExtensionMultipleTestsTest.kt | 5 +++-- okhttp/src/test/java/okhttp3/CacheTest.kt | 6 +++--- okhttp/src/test/java/okhttp3/CallTest.kt | 4 ++-- .../src/test/java/okhttp3/RouteFailureTest.kt | 4 ++-- .../test/java/okhttp3/URLConnectionTest.kt | 4 ++-- 11 files changed, 27 insertions(+), 42 deletions(-) delete mode 100644 mockwebserver-junit5/src/main/kotlin/mockwebserver3/junit5/internal/MockWebServerInstance.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 52e091840a00..ba804a579871 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,6 +5,7 @@ com-squareup-moshi = "1.15.0" com-squareup-okio = "3.7.0" de-mannodermaus-junit5 = "1.4.0" graalvm = "22.3.2" +javaxInject = "1" kotlinx-serialization = "1.6.2" mockserverClient = "5.15.0" org-bouncycastle = "1.76" @@ -51,6 +52,7 @@ gradlePlugin-spotless = "com.diffplug.spotless:spotless-plugin-gradle:6.24.0" guava-jre = "com.google.guava:guava:32.1.3-jre" hamcrestLibrary = "org.hamcrest:hamcrest-library:2.2" httpClient5 = "org.apache.httpcomponents.client5:httpclient5:5.3" +javax-inject = { module = "javax.inject:javax.inject", version.ref = "javaxInject" } jettyClient = "org.eclipse.jetty:jetty-client:9.4.53.v20231009" jnr-unixsocket = "com.github.jnr:jnr-unixsocket:0.38.21" jsoup = "org.jsoup:jsoup:1.17.2" diff --git a/mockwebserver-junit5/README.md b/mockwebserver-junit5/README.md index 8dce14890953..20c5027535d0 100644 --- a/mockwebserver-junit5/README.md +++ b/mockwebserver-junit5/README.md @@ -56,8 +56,8 @@ Multiple instances can be obtained by naming additional ones: ``` class MyTest( private val server: MockWebServer, - @MockWebServerInstance("server2") private val server2: MockWebServer, - @MockWebServerInstance("server3") private val server3: MockWebServer + @Named("server2") private val server2: MockWebServer, + @Named("server3") private val server3: MockWebServer ) { @Test fun test() { diff --git a/mockwebserver-junit5/build.gradle.kts b/mockwebserver-junit5/build.gradle.kts index 73de96017794..22dc642e1796 100644 --- a/mockwebserver-junit5/build.gradle.kts +++ b/mockwebserver-junit5/build.gradle.kts @@ -24,6 +24,7 @@ dependencies { api(projects.mockwebserver3) api(libs.junit.jupiter.api) compileOnly(libs.animalsniffer.annotations) + api(libs.javax.inject) testRuntimeOnly(libs.junit.jupiter.engine) testImplementation(libs.kotlin.junit5) diff --git a/mockwebserver-junit5/src/main/kotlin/mockwebserver3/junit5/internal/MockWebServerExtension.kt b/mockwebserver-junit5/src/main/kotlin/mockwebserver3/junit5/internal/MockWebServerExtension.kt index b9ece7ec8a55..814a8df34484 100644 --- a/mockwebserver-junit5/src/main/kotlin/mockwebserver3/junit5/internal/MockWebServerExtension.kt +++ b/mockwebserver-junit5/src/main/kotlin/mockwebserver3/junit5/internal/MockWebServerExtension.kt @@ -38,7 +38,7 @@ import org.junit.jupiter.api.extension.ParameterResolver * There are 3 ids for instances * - The test instance default (passed into constructor) * - The test lifecycle default (passed into test method, plus @BeforeEach, @AfterEach) - * - named instances with @MockWebServerInstance. + * - named instances with @Named. */ class MockWebServerExtension : BeforeEachCallback, AfterEachCallback, ParameterResolver { @@ -109,10 +109,10 @@ class MockWebServerExtension : parameterContext: ParameterContext, extensionContext: ExtensionContext, ): Any { - val nameAnnotation = parameterContext.findAnnotation(MockWebServerInstance::class.java) + val nameAnnotation = parameterContext.findAnnotation(javax.inject.Named::class.java) val name = if (nameAnnotation.isPresent) { - nameAnnotation.get().name + nameAnnotation.get().value } else { defaultName } diff --git a/mockwebserver-junit5/src/main/kotlin/mockwebserver3/junit5/internal/MockWebServerInstance.kt b/mockwebserver-junit5/src/main/kotlin/mockwebserver3/junit5/internal/MockWebServerInstance.kt deleted file mode 100644 index 76a775b55386..000000000000 --- a/mockwebserver-junit5/src/main/kotlin/mockwebserver3/junit5/internal/MockWebServerInstance.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (C) 2020 Square, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package mockwebserver3.junit5.internal - -annotation class MockWebServerInstance( - val name: String, -) diff --git a/mockwebserver-junit5/src/test/java/mockwebserver3/junit5/internal/ExtensionMultipleInstancesTest.kt b/mockwebserver-junit5/src/test/java/mockwebserver3/junit5/internal/ExtensionMultipleInstancesTest.kt index 63e14d3518b3..e02044caaaad 100644 --- a/mockwebserver-junit5/src/test/java/mockwebserver3/junit5/internal/ExtensionMultipleInstancesTest.kt +++ b/mockwebserver-junit5/src/test/java/mockwebserver3/junit5/internal/ExtensionMultipleInstancesTest.kt @@ -19,6 +19,7 @@ import assertk.assertThat import assertk.assertions.isEqualTo import assertk.assertions.isNotEqualTo import assertk.assertions.isTrue +import javax.inject.Named import mockwebserver3.MockWebServer import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach @@ -32,8 +33,8 @@ class ExtensionMultipleInstancesTest { @BeforeEach fun setup( defaultInstance: MockWebServer, - @MockWebServerInstance("A") instanceA: MockWebServer, - @MockWebServerInstance("B") instanceB: MockWebServer, + @Named("A") instanceA: MockWebServer, + @Named("B") instanceB: MockWebServer, ) { defaultInstancePort = defaultInstance.port instanceAPort = instanceA.port @@ -48,8 +49,8 @@ class ExtensionMultipleInstancesTest { @AfterEach fun tearDown( defaultInstance: MockWebServer, - @MockWebServerInstance("A") instanceA: MockWebServer, - @MockWebServerInstance("B") instanceB: MockWebServer, + @Named("A") instanceA: MockWebServer, + @Named("B") instanceB: MockWebServer, ) { assertThat(defaultInstance.port).isEqualTo(defaultInstancePort) assertThat(instanceA.port).isEqualTo(instanceAPort) @@ -59,8 +60,8 @@ class ExtensionMultipleInstancesTest { @Test fun testClient( defaultInstance: MockWebServer, - @MockWebServerInstance("A") instanceA: MockWebServer, - @MockWebServerInstance("B") instanceB: MockWebServer, + @Named("A") instanceA: MockWebServer, + @Named("B") instanceB: MockWebServer, ) { assertThat(defaultInstance.port).isEqualTo(defaultInstancePort) assertThat(instanceA.port).isEqualTo(instanceAPort) diff --git a/mockwebserver-junit5/src/test/java/mockwebserver3/junit5/internal/ExtensionMultipleTestsTest.kt b/mockwebserver-junit5/src/test/java/mockwebserver3/junit5/internal/ExtensionMultipleTestsTest.kt index 516650ea4797..7b0daaf3e0e4 100644 --- a/mockwebserver-junit5/src/test/java/mockwebserver3/junit5/internal/ExtensionMultipleTestsTest.kt +++ b/mockwebserver-junit5/src/test/java/mockwebserver3/junit5/internal/ExtensionMultipleTestsTest.kt @@ -17,6 +17,7 @@ package mockwebserver3.junit5.internal import assertk.assertThat import assertk.assertions.isTrue +import javax.inject.Named import mockwebserver3.MockWebServer import org.junit.jupiter.api.Test @@ -24,7 +25,7 @@ class ExtensionMultipleTestsTest { @Test fun testClient1( defaultInstance: MockWebServer, - @MockWebServerInstance("A") instanceA: MockWebServer, + @Named("A") instanceA: MockWebServer, ) { assertThat(seenInstances.add(defaultInstance.port)).isTrue() assertThat(seenInstances.add(instanceA.port)).isTrue() @@ -33,7 +34,7 @@ class ExtensionMultipleTestsTest { @Test fun testClient2( defaultInstance: MockWebServer, - @MockWebServerInstance("A") instanceA: MockWebServer, + @Named("A") instanceA: MockWebServer, ) { assertThat(seenInstances.add(defaultInstance.port)).isTrue() assertThat(seenInstances.add(instanceA.port)).isTrue() diff --git a/okhttp/src/test/java/okhttp3/CacheTest.kt b/okhttp/src/test/java/okhttp3/CacheTest.kt index e6921ddf495e..88ea5818ef8f 100644 --- a/okhttp/src/test/java/okhttp3/CacheTest.kt +++ b/okhttp/src/test/java/okhttp3/CacheTest.kt @@ -35,13 +35,13 @@ import java.util.Locale import java.util.TimeZone import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicReference +import javax.inject.Named import javax.net.ssl.HostnameVerifier import kotlin.test.assertFailsWith import mockwebserver3.MockResponse import mockwebserver3.MockWebServer import mockwebserver3.RecordedRequest import mockwebserver3.SocketPolicy.DisconnectAtEnd -import mockwebserver3.junit5.internal.MockWebServerInstance import okhttp3.Cache.Companion.key import okhttp3.Headers.Companion.headersOf import okhttp3.MediaType.Companion.toMediaType @@ -84,8 +84,8 @@ class CacheTest { @BeforeEach fun setUp( - @MockWebServerInstance(name = "1") server: MockWebServer, - @MockWebServerInstance(name = "2") server2: MockWebServer, + @Named("1") server: MockWebServer, + @Named("2") server2: MockWebServer, ) { this.server = server this.server2 = server2 diff --git a/okhttp/src/test/java/okhttp3/CallTest.kt b/okhttp/src/test/java/okhttp3/CallTest.kt index 2d8827faee08..a002576dc958 100644 --- a/okhttp/src/test/java/okhttp3/CallTest.kt +++ b/okhttp/src/test/java/okhttp3/CallTest.kt @@ -55,6 +55,7 @@ import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicReference +import javax.inject.Named import javax.net.ssl.SSLException import javax.net.ssl.SSLHandshakeException import javax.net.ssl.SSLPeerUnverifiedException @@ -73,7 +74,6 @@ import mockwebserver3.SocketPolicy.FailHandshake import mockwebserver3.SocketPolicy.HalfCloseAfterRequest import mockwebserver3.SocketPolicy.NoResponse import mockwebserver3.SocketPolicy.StallSocketAtStart -import mockwebserver3.junit5.internal.MockWebServerInstance import okhttp3.CallEvent.CallEnd import okhttp3.CallEvent.ConnectStart import okhttp3.CallEvent.ConnectionAcquired @@ -154,7 +154,7 @@ open class CallTest { @BeforeEach fun setUp( server: MockWebServer, - @MockWebServerInstance("server2") server2: MockWebServer, + @Named("server2") server2: MockWebServer, ) { this.server = server this.server2 = server2 diff --git a/okhttp/src/test/java/okhttp3/RouteFailureTest.kt b/okhttp/src/test/java/okhttp3/RouteFailureTest.kt index 4ced615788da..c6a201daddd8 100644 --- a/okhttp/src/test/java/okhttp3/RouteFailureTest.kt +++ b/okhttp/src/test/java/okhttp3/RouteFailureTest.kt @@ -24,10 +24,10 @@ import java.net.InetAddress import java.net.InetSocketAddress import java.net.Proxy import java.net.SocketTimeoutException +import javax.inject.Named import mockwebserver3.MockResponse import mockwebserver3.MockWebServer import mockwebserver3.SocketPolicy.ResetStreamAtStart -import mockwebserver3.junit5.internal.MockWebServerInstance import okhttp3.internal.http.RecordingProxySelector import okhttp3.internal.http2.ErrorCode import okhttp3.testing.PlatformRule @@ -68,7 +68,7 @@ class RouteFailureTest { @BeforeEach fun setUp( server: MockWebServer, - @MockWebServerInstance("server2") server2: MockWebServer, + @Named("server2") server2: MockWebServer, ) { this.server1 = server this.server2 = server2 diff --git a/okhttp/src/test/java/okhttp3/URLConnectionTest.kt b/okhttp/src/test/java/okhttp3/URLConnectionTest.kt index 88ad9e0546de..491eac6d3a95 100644 --- a/okhttp/src/test/java/okhttp3/URLConnectionTest.kt +++ b/okhttp/src/test/java/okhttp3/URLConnectionTest.kt @@ -60,6 +60,7 @@ import java.util.Locale import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicReference import java.util.zip.GZIPInputStream +import javax.inject.Named import javax.net.SocketFactory import javax.net.ssl.SSLException import javax.net.ssl.SSLHandshakeException @@ -75,7 +76,6 @@ import mockwebserver3.SocketPolicy.DisconnectAtEnd import mockwebserver3.SocketPolicy.FailHandshake import mockwebserver3.SocketPolicy.ShutdownInputAtEnd import mockwebserver3.SocketPolicy.ShutdownOutputAtEnd -import mockwebserver3.junit5.internal.MockWebServerInstance import okhttp3.Credentials.basic import okhttp3.Headers.Companion.headersOf import okhttp3.HttpUrl.Companion.toHttpUrl @@ -130,7 +130,7 @@ class URLConnectionTest { @BeforeEach fun setUp( server: MockWebServer, - @MockWebServerInstance("server2") server2: MockWebServer, + @Named("server2") server2: MockWebServer, ) { this.server = server this.server2 = server2