Skip to content

Commit

Permalink
Even more assertk (#8151)
Browse files Browse the repository at this point in the history
* Even more assertk

When we split tests into multiplatform we switched them
from AssertJ to assertk. I'm migrating everything over to
assertk so that I can merge tests back together without
changing assertion APIs.

* Fixup some type conversion problems
squarejesse authored Dec 21, 2023
1 parent c056530 commit e285d4e
Showing 69 changed files with 856 additions and 658 deletions.
Original file line number Diff line number Diff line change
@@ -15,7 +15,8 @@
*/
package okhttp3

import org.assertj.core.api.Assertions.assertThat
import assertk.assertThat
import assertk.assertions.hasMessage
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext
@@ -46,7 +47,7 @@ class OkHttpClientTestRuleTest {
fail("")
} catch (expected: AssertionError) {
assertThat(expected).hasMessage("uncaught exception thrown during test")
assertThat(expected.cause).hasMessage("boom!")
assertThat(expected.cause!!).hasMessage("boom!")
}
}
}
Original file line number Diff line number Diff line change
@@ -15,6 +15,9 @@
*/
package okhttp3.tls

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.matchesPredicate
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.ServerSocket
@@ -30,7 +33,6 @@ import okhttp3.Handshake.Companion.handshake
import okhttp3.internal.closeQuietly
import okhttp3.testing.PlatformRule
import okio.ByteString.Companion.toByteString
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
@@ -133,7 +135,9 @@ class HandshakeCertificatesTest {
.toSet()

// It's safe to assume all platforms will have a major Internet certificate issuer.
assertThat(names).anyMatch { it.matches(Regex("[A-Z]+=Entrust.*")) }
assertThat(names).matchesPredicate { strings ->
strings.any { it.matches(Regex("[A-Z]+=Entrust.*")) }
}
}

private fun startTlsServer(): InetSocketAddress {
Original file line number Diff line number Diff line change
@@ -15,6 +15,11 @@
*/
package okhttp3.tls.internal.der

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isNull
import assertk.assertions.isTrue
import java.math.BigInteger
import java.security.KeyFactory
import java.security.spec.PKCS8EncodedKeySpec
@@ -36,7 +41,6 @@ import okio.ByteString.Companion.decodeBase64
import okio.ByteString.Companion.decodeHex
import okio.ByteString.Companion.encodeUtf8
import okio.ByteString.Companion.toByteString
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

internal class DerCertificatesTest {
7 changes: 6 additions & 1 deletion okhttp-tls/src/test/java/okhttp3/tls/internal/der/DerTest.kt
Original file line number Diff line number Diff line change
@@ -15,6 +15,12 @@
*/
package okhttp3.tls.internal.der

import assertk.assertThat
import assertk.assertions.hasMessage
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isNull
import assertk.assertions.isTrue
import java.math.BigInteger
import java.net.InetAddress
import java.net.ProtocolException
@@ -31,7 +37,6 @@ import okio.Buffer
import okio.ByteString.Companion.decodeHex
import okio.ByteString.Companion.encodeUtf8
import okio.ByteString.Companion.toByteString
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.fail
4 changes: 3 additions & 1 deletion okhttp/src/test/java/okhttp3/AddressTest.kt
Original file line number Diff line number Diff line change
@@ -15,9 +15,11 @@
*/
package okhttp3

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNotEqualTo
import java.net.Proxy
import okhttp3.internal.http.RecordingProxySelector
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test

3 changes: 2 additions & 1 deletion okhttp/src/test/java/okhttp3/BouncyCastleTest.kt
Original file line number Diff line number Diff line change
@@ -15,10 +15,11 @@
*/
package okhttp3

import assertk.assertThat
import assertk.assertions.isEqualTo
import mockwebserver3.MockWebServer
import okhttp3.TestUtil.assumeNetwork
import okhttp3.testing.PlatformRule
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
70 changes: 36 additions & 34 deletions okhttp/src/test/java/okhttp3/CacheControlJvmTest.kt
Original file line number Diff line number Diff line change
@@ -15,14 +15,16 @@
*/
package okhttp3

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isSameAs
import assertk.assertions.isTrue
import java.util.concurrent.TimeUnit
import okhttp3.CacheControl.Companion.parse
import okhttp3.Headers.Companion.headersOf
import org.junit.jupiter.api.Test
import java.lang.Exception
import java.lang.IllegalArgumentException
import java.util.concurrent.TimeUnit
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.fail
import org.junit.jupiter.api.Test

class CacheControlJvmTest {
@Test
@@ -42,20 +44,20 @@ class CacheControlJvmTest {
"no-cache, no-store, max-age=1, max-stale=2, min-fresh=3, only-if-cached, "
+ "no-transform, immutable"
)
assertThat(cacheControl.noCache).isTrue
assertThat(cacheControl.noStore).isTrue
assertThat(cacheControl.noCache).isTrue()
assertThat(cacheControl.noStore).isTrue()
assertThat(cacheControl.maxAgeSeconds).isEqualTo(1)
assertThat(cacheControl.maxStaleSeconds).isEqualTo(2)
assertThat(cacheControl.minFreshSeconds).isEqualTo(3)
assertThat(cacheControl.onlyIfCached).isTrue
assertThat(cacheControl.noTransform).isTrue
assertThat(cacheControl.immutable).isTrue
assertThat(cacheControl.onlyIfCached).isTrue()
assertThat(cacheControl.noTransform).isTrue()
assertThat(cacheControl.immutable).isTrue()

// These members are accessible to response headers only.
assertThat(cacheControl.sMaxAgeSeconds).isEqualTo(-1)
assertThat(cacheControl.isPrivate).isFalse
assertThat(cacheControl.isPublic).isFalse
assertThat(cacheControl.mustRevalidate).isFalse
assertThat(cacheControl.isPrivate).isFalse()
assertThat(cacheControl.isPublic).isFalse()
assertThat(cacheControl.mustRevalidate).isFalse()
}

@Test
@@ -65,16 +67,16 @@ class CacheControlJvmTest {
Headers.Builder().set("Cache-Control", "").build()
)
assertThat(cacheControl.toString()).isEqualTo("")
assertThat(cacheControl.noCache).isFalse
assertThat(cacheControl.noStore).isFalse
assertThat(cacheControl.noCache).isFalse()
assertThat(cacheControl.noStore).isFalse()
assertThat(cacheControl.maxAgeSeconds).isEqualTo(-1)
assertThat(cacheControl.sMaxAgeSeconds).isEqualTo(-1)
assertThat(cacheControl.isPublic).isFalse
assertThat(cacheControl.mustRevalidate).isFalse
assertThat(cacheControl.isPublic).isFalse()
assertThat(cacheControl.mustRevalidate).isFalse()
assertThat(cacheControl.maxStaleSeconds).isEqualTo(-1)
assertThat(cacheControl.minFreshSeconds).isEqualTo(-1)
assertThat(cacheControl.onlyIfCached).isFalse
assertThat(cacheControl.mustRevalidate).isFalse
assertThat(cacheControl.onlyIfCached).isFalse()
assertThat(cacheControl.mustRevalidate).isFalse()
}

@Test
@@ -87,17 +89,17 @@ class CacheControlJvmTest {
.set("Cache-Control", header)
.build()
)
assertThat(cacheControl.noCache).isTrue
assertThat(cacheControl.noStore).isTrue
assertThat(cacheControl.noCache).isTrue()
assertThat(cacheControl.noStore).isTrue()
assertThat(cacheControl.maxAgeSeconds).isEqualTo(1)
assertThat(cacheControl.sMaxAgeSeconds).isEqualTo(2)
assertThat(cacheControl.isPrivate).isTrue
assertThat(cacheControl.isPublic).isTrue
assertThat(cacheControl.mustRevalidate).isTrue
assertThat(cacheControl.isPrivate).isTrue()
assertThat(cacheControl.isPublic).isTrue()
assertThat(cacheControl.mustRevalidate).isTrue()
assertThat(cacheControl.maxStaleSeconds).isEqualTo(3)
assertThat(cacheControl.minFreshSeconds).isEqualTo(4)
assertThat(cacheControl.onlyIfCached).isTrue
assertThat(cacheControl.noTransform).isTrue
assertThat(cacheControl.onlyIfCached).isTrue()
assertThat(cacheControl.noTransform).isTrue()
assertThat(cacheControl.toString()).isEqualTo(header)
}

@@ -111,18 +113,18 @@ class CacheControlJvmTest {
.set("Cache-Control", header)
.build()
)
assertThat(cacheControl.noCache).isFalse
assertThat(cacheControl.noStore).isFalse
assertThat(cacheControl.noCache).isFalse()
assertThat(cacheControl.noStore).isFalse()
assertThat(cacheControl.maxAgeSeconds).isEqualTo(-1)
assertThat(cacheControl.sMaxAgeSeconds).isEqualTo(-1)
assertThat(cacheControl.isPrivate).isTrue
assertThat(cacheControl.isPublic).isFalse
assertThat(cacheControl.mustRevalidate).isFalse
assertThat(cacheControl.isPrivate).isTrue()
assertThat(cacheControl.isPublic).isFalse()
assertThat(cacheControl.mustRevalidate).isFalse()
assertThat(cacheControl.maxStaleSeconds).isEqualTo(-1)
assertThat(cacheControl.minFreshSeconds).isEqualTo(-1)
assertThat(cacheControl.onlyIfCached).isFalse
assertThat(cacheControl.noTransform).isFalse
assertThat(cacheControl.immutable).isFalse
assertThat(cacheControl.onlyIfCached).isFalse()
assertThat(cacheControl.noTransform).isFalse()
assertThat(cacheControl.immutable).isFalse()
assertThat(cacheControl.toString()).isEqualTo(header)
}

6 changes: 4 additions & 2 deletions okhttp/src/test/java/okhttp3/CacheCorruptionTest.kt
Original file line number Diff line number Diff line change
@@ -15,6 +15,9 @@
*/
package okhttp3

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.startsWith
import java.net.CookieManager
import java.net.ResponseCache
import java.text.DateFormat
@@ -34,7 +37,6 @@ import okhttp3.okio.LoggingFilesystem
import okhttp3.testing.PlatformRule
import okio.Path.Companion.toPath
import okio.fakefilesystem.FakeFileSystem
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
@@ -94,7 +96,7 @@ class CacheCorruptionTest {
assertThat(cache.networkCount()).isEqualTo(1)
assertThat(cache.hitCount()).isEqualTo(1)

assertThat(response.handshake?.cipherSuite?.javaName).startsWith("SLT_")
assertThat(response.handshake!!.cipherSuite.javaName).startsWith("SLT_")
}

@Test
Loading

0 comments on commit e285d4e

Please sign in to comment.