1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-24 18:41:06 +03:00

Testing with JDK 12 and latest 11.0.3 (#5212)

JDK 12 and be more specific about when we expect failures, e.g. 11.0.3 has fixes we should take into account.
This commit is contained in:
Yuri Schimke
2019-07-02 07:21:58 +01:00
committed by GitHub
parent 81b0feeea9
commit bdd6c3944f
21 changed files with 235 additions and 55 deletions

View File

@@ -13,18 +13,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3
package okhttp3.testing
import okhttp3.internal.platform.ConscryptPlatform
import okhttp3.internal.platform.Jdk8WithJettyBootPlatform
import okhttp3.internal.platform.Jdk9Platform
import okhttp3.internal.platform.Platform
import org.conscrypt.Conscrypt
import org.hamcrest.BaseMatcher
import org.hamcrest.CoreMatchers
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.not
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.StringDescription
import org.hamcrest.TypeSafeMatcher
import org.junit.Assert
import org.junit.Assume.assumeThat
import org.junit.Assume.assumeTrue
import org.junit.rules.ExternalResource
import org.junit.AssumptionViolatedException
import org.junit.rules.TestRule
import org.junit.runners.model.Statement
import java.security.Security
/**
@@ -37,8 +46,34 @@ import java.security.Security
open class PlatformRule @JvmOverloads constructor(
val requiredPlatformName: String? = null,
val platform: Platform? = null
) : ExternalResource() {
override fun before() {
) : TestRule {
private val versionChecks = mutableListOf<Pair<Matcher<out Any>, Matcher<out Any>>>()
override fun apply(base: Statement, description: org.junit.runner.Description): Statement {
return object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
var failed = false
try {
setupPlatform()
base.evaluate()
} catch (e: AssumptionViolatedException) {
throw e
} catch (e: Throwable) {
failed = true
rethrowIfNotExpected(e)
} finally {
resetPlatform()
}
if (!failed) {
failIfExpected()
}
}
}
}
fun setupPlatform() {
if (requiredPlatformName != null) {
assumeThat(getPlatformSystemProperty(), equalTo(requiredPlatformName))
}
@@ -50,12 +85,72 @@ open class PlatformRule @JvmOverloads constructor(
}
}
override fun after() {
fun resetPlatform() {
if (platform != null) {
Platform.resetForTests()
}
}
fun expectFailureOnConscryptPlatform() {
expectFailure(platformMatches(CONSCRYPT_PROPERTY))
}
fun expectFailureFromJdkVersion(majorVersion: Int) {
expectFailure(fromMajor(majorVersion))
}
private fun expectFailure(
versionMatcher: Matcher<out Any>,
failureMatcher: Matcher<out Any> = CoreMatchers.anything()
) {
versionChecks.add(Pair(versionMatcher, failureMatcher))
}
fun platformMatches(platform: String): Matcher<Any> = object : BaseMatcher<Any>() {
override fun describeTo(description: Description) {
description.appendText(platform)
}
override fun matches(item: Any?): Boolean {
return getPlatformSystemProperty() == platform
}
}
fun fromMajor(version: Int): Matcher<PlatformVersion> {
return object : TypeSafeMatcher<PlatformVersion>() {
override fun describeTo(description: org.hamcrest.Description) {
description.appendText("JDK with version from $version")
}
override fun matchesSafely(item: PlatformVersion): Boolean {
return item.majorVersion >= version
}
}
}
fun rethrowIfNotExpected(e: Throwable) {
versionChecks.forEach { (versionMatcher, failureMatcher) ->
if (versionMatcher.matches(PlatformVersion) && failureMatcher.matches(e)) {
return
}
}
throw e
}
fun failIfExpected() {
versionChecks.forEach { (versionMatcher, failureMatcher) ->
if (versionMatcher.matches(PlatformVersion)) {
val description = StringDescription()
versionMatcher.describeTo(description)
description.appendText(" expected to fail with exception that ")
failureMatcher.describeTo(description)
Assert.fail(description.toString())
}
}
}
fun isConscrypt() = getPlatformSystemProperty() == CONSCRYPT_PROPERTY
fun isJdk9() = getPlatformSystemProperty() == JDK9_PROPERTY
@@ -148,7 +243,8 @@ open class PlatformRule @JvmOverloads constructor(
@JvmStatic
fun getPlatformSystemProperty(): String {
var property: String? = System.getProperty(PROPERTY_NAME)
var property: String? = System.getProperty(
PROPERTY_NAME)
if (property == null) {
property = when (Platform.get()) {
@@ -163,7 +259,8 @@ open class PlatformRule @JvmOverloads constructor(
}
@JvmStatic
fun conscrypt() = PlatformRule(CONSCRYPT_PROPERTY)
fun conscrypt() = PlatformRule(
CONSCRYPT_PROPERTY)
@JvmStatic
fun jdk9() = PlatformRule(JDK9_PROPERTY)
@@ -172,7 +269,8 @@ open class PlatformRule @JvmOverloads constructor(
fun jdk8() = PlatformRule(JDK8_PROPERTY)
@JvmStatic
fun jdk8alpn() = PlatformRule(JDK8_ALPN_PROPERTY)
fun jdk8alpn() = PlatformRule(
JDK8_ALPN_PROPERTY)
@JvmStatic
fun isAlpnBootEnabled(): Boolean = try {

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2019 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 okhttp3.testing
object PlatformVersion {
val majorVersion: Int by lazy {
when (val jvmSpecVersion = getJvmSpecVersion()) {
"1.8" -> 8
else -> jvmSpecVersion.toInt()
}
}
fun getJvmSpecVersion(): String {
return System.getProperty("java.specification.version", "unknown")
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2019 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 okhttp3.testing
import okhttp3.internal.platform.Platform
import org.junit.Rule
import org.junit.Test
/**
* Sanity test for checking which environment and IDE is picking up.
*/
class PlatformRuleTest {
@Suppress("RedundantVisibilityModifier")
@JvmField
@Rule public val platform = PlatformRule()
@Test
fun testMode() {
println(PlatformRule.getPlatformSystemProperty())
println(Platform.get().javaClass.simpleName)
}
@Test
fun testGreenCase() {
}
@Test
fun testGreenCaseFailingOnLater() {
platform.expectFailureFromJdkVersion(PlatformVersion.majorVersion + 1)
}
@Test
fun failureCase() {
platform.expectFailureFromJdkVersion(PlatformVersion.majorVersion)
check(false)
}
}