1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-26 06:43:09 +03:00

Use clean assumptions APIs (#6405)

This commit is contained in:
Yuri Schimke
2020-11-08 15:14:50 +00:00
committed by GitHub
parent fd43f489f0
commit 81b1b14a56

View File

@@ -17,29 +17,22 @@ package okhttp3.testing
import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider
import com.amazon.corretto.crypto.provider.SelfTestStatus import com.amazon.corretto.crypto.provider.SelfTestStatus
import java.lang.reflect.Method
import java.security.Security
import okhttp3.internal.platform.ConscryptPlatform import okhttp3.internal.platform.ConscryptPlatform
import okhttp3.internal.platform.Jdk8WithJettyBootPlatform import okhttp3.internal.platform.Jdk8WithJettyBootPlatform
import okhttp3.internal.platform.Jdk9Platform import okhttp3.internal.platform.Jdk9Platform
import okhttp3.internal.platform.OpenJSSEPlatform import okhttp3.internal.platform.OpenJSSEPlatform
import okhttp3.internal.platform.Platform import okhttp3.internal.platform.Platform
import org.assertj.core.api.Assumptions.assumeThat
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider
import org.conscrypt.Conscrypt import org.conscrypt.Conscrypt
import org.hamcrest.BaseMatcher import org.hamcrest.BaseMatcher
import org.hamcrest.CoreMatchers import org.hamcrest.CoreMatchers.anything
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.not
import org.hamcrest.Description import org.hamcrest.Description
import org.hamcrest.Matcher import org.hamcrest.Matcher
import org.hamcrest.StringDescription import org.hamcrest.StringDescription
import org.hamcrest.TypeSafeMatcher import org.hamcrest.TypeSafeMatcher
import org.junit.Assert import org.junit.jupiter.api.Assertions.fail
import org.junit.Assume.assumeFalse
import org.junit.Assume.assumeThat
import org.junit.Assume.assumeTrue
import org.junit.AssumptionViolatedException
import org.junit.jupiter.api.extension.AfterEachCallback import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.BeforeEachCallback import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext import org.junit.jupiter.api.extension.ExtensionContext
@@ -48,6 +41,9 @@ import org.junit.jupiter.api.extension.ReflectiveInvocationContext
import org.junit.rules.TestRule import org.junit.rules.TestRule
import org.junit.runners.model.Statement import org.junit.runners.model.Statement
import org.openjsse.net.ssl.OpenJSSE import org.openjsse.net.ssl.OpenJSSE
import org.opentest4j.TestAbortedException
import java.lang.reflect.Method
import java.security.Security
/** /**
* Marks a test as Platform aware, before the test runs a consistent Platform will be * Marks a test as Platform aware, before the test runs a consistent Platform will be
@@ -78,7 +74,7 @@ open class PlatformRule @JvmOverloads constructor(
var failed = false var failed = false
try { try {
invocation.proceed() invocation.proceed()
} catch (e: AssumptionViolatedException) { } catch (e: TestAbortedException) {
throw e throw e
} catch (e: Throwable) { } catch (e: Throwable) {
failed = true failed = true
@@ -103,7 +99,7 @@ open class PlatformRule @JvmOverloads constructor(
setupPlatform() setupPlatform()
base.evaluate() base.evaluate()
} catch (e: AssumptionViolatedException) { } catch (e: TestAbortedException) {
throw e throw e
} catch (e: Throwable) { } catch (e: Throwable) {
failed = true failed = true
@@ -120,7 +116,7 @@ open class PlatformRule @JvmOverloads constructor(
fun setupPlatform() { fun setupPlatform() {
if (requiredPlatformName != null) { if (requiredPlatformName != null) {
assumeThat(getPlatformSystemProperty(), equalTo(requiredPlatformName)) assumeThat(getPlatformSystemProperty()).isEqualTo(requiredPlatformName)
} }
if (platform != null) { if (platform != null) {
@@ -162,7 +158,7 @@ open class PlatformRule @JvmOverloads constructor(
private fun expectFailure( private fun expectFailure(
versionMatcher: Matcher<out Any>, versionMatcher: Matcher<out Any>,
failureMatcher: Matcher<out Any> = CoreMatchers.anything() failureMatcher: Matcher<out Any> = anything()
) { ) {
versionChecks.add(Pair(versionMatcher, failureMatcher)) versionChecks.add(Pair(versionMatcher, failureMatcher))
} }
@@ -219,7 +215,7 @@ open class PlatformRule @JvmOverloads constructor(
description.appendText(" expected to fail with exception that ") description.appendText(" expected to fail with exception that ")
failureMatcher.describeTo(description) failureMatcher.describeTo(description)
Assert.fail(description.toString()) fail<Any>(description.toString())
} }
} }
} }
@@ -237,146 +233,82 @@ open class PlatformRule @JvmOverloads constructor(
fun hasHttp2Support() = !isJdk8() fun hasHttp2Support() = !isJdk8()
fun assumeConscrypt() { fun assumeConscrypt() {
assumeThat( assumeThat(getPlatformSystemProperty()).isEqualTo(CONSCRYPT_PROPERTY)
getPlatformSystemProperty(), equalTo(
CONSCRYPT_PROPERTY
)
)
} }
fun assumeJdk9() { fun assumeJdk9() {
assumeThat( assumeThat(getPlatformSystemProperty()).isEqualTo(JDK9_PROPERTY)
getPlatformSystemProperty(), equalTo(
JDK9_PROPERTY
)
)
} }
fun assumeOpenJSSE() { fun assumeOpenJSSE() {
assumeThat( assumeThat(getPlatformSystemProperty()).isEqualTo(OPENJSSE_PROPERTY)
getPlatformSystemProperty(), equalTo(
OPENJSSE_PROPERTY
)
)
} }
fun assumeJdk8() { fun assumeJdk8() {
assumeThat( assumeThat(getPlatformSystemProperty()).isEqualTo(JDK8_PROPERTY)
getPlatformSystemProperty(), equalTo(
JDK8_PROPERTY
)
)
} }
fun assumeJdk8Alpn() { fun assumeJdk8Alpn() {
assumeThat( assumeThat(getPlatformSystemProperty()).isEqualTo(JDK8_ALPN_PROPERTY)
getPlatformSystemProperty(), equalTo(
JDK8_ALPN_PROPERTY
)
)
} }
fun assumeCorretto() { fun assumeCorretto() {
assumeThat( assumeThat(getPlatformSystemProperty()).isEqualTo(CORRETTO_PROPERTY)
getPlatformSystemProperty(), equalTo(
CORRETTO_PROPERTY
)
)
} }
fun assumeBouncyCastle() { fun assumeBouncyCastle() {
assumeThat( assumeThat(getPlatformSystemProperty()).isEqualTo(BOUNCYCASTLE_PROPERTY)
getPlatformSystemProperty(), equalTo(
BOUNCYCASTLE_PROPERTY
)
)
} }
fun assumeHttp2Support() { fun assumeHttp2Support() {
assumeThat( assumeThat(getPlatformSystemProperty()).isNotEqualTo(JDK8_PROPERTY)
getPlatformSystemProperty(), not(
JDK8_PROPERTY
)
)
} }
fun assumeAndroid() { fun assumeAndroid() {
assumeTrue("Only Android platform supported", Platform.isAndroid) assumeThat(Platform.isAndroid).isTrue
} }
fun assumeNotConscrypt() { fun assumeNotConscrypt() {
assumeThat( assumeThat(getPlatformSystemProperty()).isNotEqualTo(CONSCRYPT_PROPERTY)
getPlatformSystemProperty(), not(
CONSCRYPT_PROPERTY
)
)
} }
fun assumeNotJdk9() { fun assumeNotJdk9() {
assumeThat( assumeThat(getPlatformSystemProperty()).isNotEqualTo(JDK9_PROPERTY)
getPlatformSystemProperty(), not(
JDK9_PROPERTY
)
)
} }
fun assumeNotJdk8() { fun assumeNotJdk8() {
assumeThat( assumeThat(getPlatformSystemProperty()).isNotEqualTo(JDK8_PROPERTY)
getPlatformSystemProperty(), not(
JDK8_PROPERTY
)
)
} }
fun assumeNotJdk8Alpn() { fun assumeNotJdk8Alpn() {
assumeThat( assumeThat(getPlatformSystemProperty()).isNotEqualTo(JDK8_ALPN_PROPERTY)
getPlatformSystemProperty(), not(
JDK8_ALPN_PROPERTY
)
)
} }
fun assumeNotOpenJSSE() { fun assumeNotOpenJSSE() {
assumeThat( assumeThat(getPlatformSystemProperty()).isNotEqualTo(OPENJSSE_PROPERTY)
getPlatformSystemProperty(), not(
OPENJSSE_PROPERTY
)
)
} }
fun assumeNotCorretto() { fun assumeNotCorretto() {
assumeThat( assumeThat(getPlatformSystemProperty()).isNotEqualTo(CORRETTO_PROPERTY)
getPlatformSystemProperty(), not(
CORRETTO_PROPERTY
)
)
} }
fun assumeNotBouncyCastle() { fun assumeNotBouncyCastle() {
// Most failures are with MockWebServer // Most failures are with MockWebServer
// org.bouncycastle.tls.TlsFatalAlertReceived: handshake_failure(40) // org.bouncycastle.tls.TlsFatalAlertReceived: handshake_failure(40)
// at org.bouncycastle.tls.TlsProtocol.handleAlertMessage(TlsProtocol.java:241) // at org.bouncycastle.tls.TlsProtocol.handleAlertMessage(TlsProtocol.java:241)
assumeThat( assumeThat(getPlatformSystemProperty()).isNotEqualTo(BOUNCYCASTLE_PROPERTY)
getPlatformSystemProperty(), not(
BOUNCYCASTLE_PROPERTY
)
)
} }
fun assumeNotHttp2Support() { fun assumeNotHttp2Support() {
assumeThat( assumeThat(getPlatformSystemProperty()).isEqualTo(JDK8_PROPERTY)
getPlatformSystemProperty(), equalTo(
JDK8_PROPERTY
)
)
} }
fun assumeJettyBootEnabled() { fun assumeJettyBootEnabled() {
assumeTrue("ALPN Boot not enabled", isAlpnBootEnabled()) assumeThat(isAlpnBootEnabled()).isTrue
} }
fun assumeNotAndroid() { fun assumeNotAndroid() {
assumeFalse("Android platform not supported", Platform.isAndroid) assumeThat(Platform.isAndroid).isFalse
} }
companion object { companion object {