Simplify JUnit 4 test rules in LightPlatform4TestCase and CidrTestCase

GitOrigin-RevId: 468766529cd45c350f2a9bcf8906abc6dfe3ed73
This commit is contained in:
Eldar Abusalimov
2020-06-17 06:42:33 +03:00
committed by intellij-monorepo-bot
parent 7470db33d8
commit 613d4368f1
@@ -3,13 +3,13 @@ package com.intellij.testFramework
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.ExceptionUtil
import org.junit.Rule
import org.junit.internal.AssumptionViolatedException
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.runners.model.Statement
import java.util.concurrent.atomic.AtomicReference
@RunWith(JUnit4::class)
abstract class LightPlatform4TestCase : LightPlatformTestCase() {
@@ -23,47 +23,23 @@ abstract class LightPlatform4TestCase : LightPlatformTestCase() {
setName(if (name.startsWith("test")) name else "test" + StringUtil.capitalize(name))
setUp()
/*
* Allows to throw the original exceptions rather than them being wrapped into a RuntimeException.
*/
val failedAssumption: AtomicReference<AssumptionViolatedException?> = AtomicReference()
val failure: AtomicReference<Throwable?> = AtomicReference()
ApplicationManager.getApplication().invokeAndWait {
try {
base.evaluate()
}
catch (ave: AssumptionViolatedException) {
failedAssumption.set(ave)
}
catch (t: Throwable) {
/*
* This is either a failed assertion (AssertionError) or an unexpected failure.
*/
failure.set(t)
}
finally {
try {
ApplicationManager.getApplication().invokeAndWait {
try {
base.evaluate()
}
finally {
tearDown()
} catch (tearDownFailure: Throwable) {
when (val firstFailure = failure.get()) {
null -> failure.set(tearDownFailure)
else -> firstFailure.addSuppressed(tearDownFailure)
}
}
}
}
/*
* 1. Throw a failure, if any.
*/
failure.get()?.let { throw it }
/*
* 2. Throw a failed assumption, if any.
*/
failedAssumption.get()?.let { throw it }
catch (e: Throwable) {
// invokeAndWait() can wrap the real exception into RuntimeException.
ExceptionUtil.findCause(e, AssumptionViolatedException::class.java)?.let { ave ->
throw ave
}
throw e
}
}
}
}