Skip to content

Commit 3e897ca

Browse files
committed
Replace @BeforeClass with by-lazy
1 parent 36d66d0 commit 3e897ca

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

lib/src/test/java/at/bitfire/cert4android/CustomCertManagerTest.kt

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111
package at.bitfire.cert4android
1212

13-
import org.junit.AssumptionViolatedException
1413
import org.junit.Before
15-
import org.junit.BeforeClass
1614
import org.junit.Test
17-
import java.io.IOException
1815
import java.net.URL
1916
import java.security.cert.CertificateException
2017
import java.security.cert.X509Certificate
2118
import javax.net.ssl.HttpsURLConnection
2219

2320
class CustomCertManagerTest {
2421

22+
private val siteCerts: List<X509Certificate> by lazy {
23+
getSiteCertificates(URL("https://www.davx5.com"))
24+
}
2525
private lateinit var certStore: CertStore
2626
private lateinit var certManager: CustomCertManager
2727
private lateinit var paranoidCertManager: CustomCertManager
@@ -77,34 +77,19 @@ class CustomCertManagerTest {
7777
certStore.setUntrustedByUser(siteCerts.first())
7878
}
7979

80-
companion object {
81-
private lateinit var siteCerts: List<X509Certificate>
82-
83-
@JvmStatic
84-
@BeforeClass
85-
fun setUp() {
86-
siteCerts = try {
87-
getSiteCertificates(URL("https://www.davx5.com"))
88-
} catch (_: IOException) {
89-
// Skip all tests if the certs can't be fetched
90-
throw AssumptionViolatedException("Couldn't load certificate from Web")
91-
}
92-
}
93-
94-
fun getSiteCertificates(url: URL): List<X509Certificate> {
95-
val conn = url.openConnection() as HttpsURLConnection
96-
try {
97-
conn.connectTimeout = 5000
98-
conn.readTimeout = 5000
99-
conn.inputStream.use { stream ->
100-
stream.read()
101-
val certs = mutableListOf<X509Certificate>()
102-
conn.serverCertificates.forEach { certs += it as X509Certificate }
103-
return certs
104-
}
105-
} finally {
106-
conn.disconnect()
80+
fun getSiteCertificates(url: URL): List<X509Certificate> {
81+
val conn = url.openConnection() as HttpsURLConnection
82+
try {
83+
conn.connectTimeout = 5000
84+
conn.readTimeout = 5000
85+
conn.inputStream.use { stream ->
86+
stream.read()
87+
val certs = mutableListOf<X509Certificate>()
88+
conn.serverCertificates.forEach { certs += it as X509Certificate }
89+
return certs
10790
}
91+
} finally {
92+
conn.disconnect()
10893
}
10994
}
11095

0 commit comments

Comments
 (0)