From 595a5109cbe2245101cbbd304a4570f2de3e0030 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 19 Aug 2015 15:32:44 +0200 Subject: [PATCH] RunsInActiveStoreMode and DisposeModulesRule --- lib/org/junit/runner/annotations.xml | 5 ++ .../com/intellij/testFramework/FixtureRule.kt | 68 ++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 lib/org/junit/runner/annotations.xml diff --git a/lib/org/junit/runner/annotations.xml b/lib/org/junit/runner/annotations.xml new file mode 100644 index 000000000000..586189a1e80e --- /dev/null +++ b/lib/org/junit/runner/annotations.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/platform/testFramework/testSrc/com/intellij/testFramework/FixtureRule.kt b/platform/testFramework/testSrc/com/intellij/testFramework/FixtureRule.kt index 0b567cdade3b..b7ed262db990 100644 --- a/platform/testFramework/testSrc/com/intellij/testFramework/FixtureRule.kt +++ b/platform/testFramework/testSrc/com/intellij/testFramework/FixtureRule.kt @@ -20,6 +20,7 @@ import com.intellij.idea.IdeaTestApplication import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.module.ModuleManager import com.intellij.openapi.project.ProjectManager import com.intellij.openapi.project.ex.ProjectEx import com.intellij.openapi.project.ex.ProjectManagerEx @@ -102,6 +103,9 @@ public class ProjectRule() : ExternalResource() { } } + public val projectIfOpened: ProjectEx? + get() = if (projectOpened.get()) sharedProject else null + public val project: ProjectEx get() { var result = sharedProject @@ -207,13 +211,15 @@ public fun runInEdtAndWait(runnable: () -> Unit) { } } +private fun Description.getOwnOrClassAnnotation(annotationClass: Class) = getAnnotation(annotationClass) ?: getTestClass()?.getAnnotation(annotationClass) + @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD, ElementType.TYPE) annotation public class RunsInEdt public class EdtRule : TestRule { override fun apply(base: Statement, description: Description): Statement { - return if ((description.getAnnotation(javaClass()) ?: description.getTestClass()!!.getAnnotation(javaClass())) == null) { + return if (description.getOwnOrClassAnnotation(javaClass()) == null) { base } else { @@ -224,4 +230,64 @@ public class EdtRule : TestRule { } } } +} + +/** + * Do not optimise test load speed. + * @see ProjectEx.setOptimiseTestLoadSpeed + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD, ElementType.TYPE) +annotation public class RunsInActiveStoreMode + +public class ActiveStoreRule(private val projectRule: ProjectRule) : TestRule { + override fun apply(base: Statement, description: Description): Statement { + return if (description.getOwnOrClassAnnotation(javaClass()) == null) { + base + } + else { + object : Statement() { + override fun evaluate() { + val project = projectRule.project + val isModeDisabled = project.isOptimiseTestLoadSpeed() + if (isModeDisabled) { + project.setOptimiseTestLoadSpeed(false) + } + try { + base.evaluate() + } + finally { + if (isModeDisabled) { + project.setOptimiseTestLoadSpeed(true) + } + } + } + } + } + } +} + +public class DisposeModulesRule(private val projectRule: ProjectRule) : ExternalResource() { + override fun after() { + projectRule.projectIfOpened?.let { + var errors: MutableList? = null + val moduleManager = ModuleManager.getInstance(it) + for (module in moduleManager.getModules()) { + if (module.isDisposed()) { + continue + } + + try { + moduleManager.disposeModule(module) + } + catch(e: Throwable) { + if (errors == null) { + errors = SmartList() + } + errors.add(e) + } + } + CompoundRuntimeException.doThrow(errors) + } + } } \ No newline at end of file