Refactoring: extract method for configuring runtime without sdk change

This commit is contained in:
Nikolay Krasko
2015-04-27 20:04:53 +03:00
parent dbe6d2743d
commit b82f7d5c33
8 changed files with 44 additions and 40 deletions
@@ -48,30 +48,34 @@ public class ConfigLibraryUtil {
return editor; return editor;
} }
public static void configureKotlinRuntime(Module module, Sdk sdk) { public static void configureKotlinRuntimeAndSdk(Module module, Sdk sdk) {
configureLibrary(module, sdk, getKotlinRuntimeLibEditor(DEFAULT_JAVA_RUNTIME_LIB_NAME, PathUtil.getKotlinPathsForDistDirectory().getRuntimePath()));
}
public static void configureKotlinJsRuntime(Module module, Sdk sdk) {
configureLibrary(module, sdk, getKotlinRuntimeLibEditor(DEFAULT_KOTLIN_JS_STDLIB_NAME, PathUtil.getKotlinPathsForDistDirectory().getJsStdLibJarPath()));
}
public static void unConfigureKotlinRuntime(Module module, Sdk sdk) {
unConfigureLibrary(module, sdk, DEFAULT_JAVA_RUNTIME_LIB_NAME);
}
public static void unConfigureKotlinJsRuntime(Module module, Sdk sdk) {
unConfigureLibrary(module, sdk, DEFAULT_KOTLIN_JS_STDLIB_NAME);
}
public static void configureLibrary(Module module, Sdk sdk, NewLibraryEditor libraryEditor) {
configureSdk(module, sdk); configureSdk(module, sdk);
addLibrary(libraryEditor, module); configureKotlinRuntime(module);
} }
public static void unConfigureLibrary(Module module, Sdk sdk, String libraryName) { public static void configureKotlinJsRuntimeAndSdk(Module module, Sdk sdk) {
configureSdk(module, sdk); configureSdk(module, sdk);
removeLibrary(module, libraryName); configureKotlinJsRuntime(module);
}
public static void configureKotlinRuntime(Module module) {
addLibrary(getKotlinRuntimeLibEditor(DEFAULT_JAVA_RUNTIME_LIB_NAME, PathUtil.getKotlinPathsForDistDirectory().getRuntimePath()),
module);
}
public static void configureKotlinJsRuntime(Module module) {
addLibrary(getKotlinRuntimeLibEditor(DEFAULT_KOTLIN_JS_STDLIB_NAME,
PathUtil.getKotlinPathsForDistDirectory().getJsStdLibJarPath()), module);
}
public static void unConfigureKotlinRuntimeAndSdk(Module module, Sdk sdk) {
configureSdk(module, sdk);
removeLibrary(module, DEFAULT_JAVA_RUNTIME_LIB_NAME);
}
public static void unConfigureKotlinJsRuntimeAndSdk(Module module, Sdk sdk) {
configureSdk(module, sdk);
removeLibrary(module, DEFAULT_KOTLIN_JS_STDLIB_NAME);
} }
public static void configureSdk(@NotNull final Module module, @NotNull final Sdk sdk) { public static void configureSdk(@NotNull final Module module, @NotNull final Sdk sdk) {
@@ -68,7 +68,7 @@ public abstract class AbstractJetInspectionTest: LightCodeInsightFixtureTestCase
try { try {
if (isWithRuntime) { if (isWithRuntime) {
ConfigLibraryUtil.configureKotlinRuntime( ConfigLibraryUtil.configureKotlinRuntimeAndSdk(
myFixture.getModule(), myFixture.getModule(),
if (fullJdk) PluginTestCaseBase.fullJdk() else PluginTestCaseBase.mockJdk() if (fullJdk) PluginTestCaseBase.fullJdk() else PluginTestCaseBase.mockJdk()
) )
@@ -85,7 +85,7 @@ public abstract class AbstractJetInspectionTest: LightCodeInsightFixtureTestCase
} }
finally { finally {
if (isWithRuntime) { if (isWithRuntime) {
ConfigLibraryUtil.unConfigureKotlinRuntime(myFixture.getModule(), IdeaTestUtil.getMockJdk17()) ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(myFixture.getModule(), IdeaTestUtil.getMockJdk17())
} }
} }
} }
@@ -117,7 +117,7 @@ public abstract class AbstractIntentionTest extends KotlinCodeInsightTestCase {
try { try {
if (isWithRuntime) { if (isWithRuntime) {
ConfigLibraryUtil.configureKotlinRuntime(getModule(), PluginTestCaseBase.mockJdk()); ConfigLibraryUtil.configureKotlinRuntimeAndSdk(getModule(), PluginTestCaseBase.mockJdk());
} }
DirectiveBasedActionUtils.checkForUnexpectedErrors((JetFile) getFile()); DirectiveBasedActionUtils.checkForUnexpectedErrors((JetFile) getFile());
@@ -126,7 +126,7 @@ public abstract class AbstractIntentionTest extends KotlinCodeInsightTestCase {
} }
finally { finally {
if (isWithRuntime) { if (isWithRuntime) {
ConfigLibraryUtil.unConfigureKotlinRuntime(getModule(), getTestProjectJdk()); ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(getModule(), getTestProjectJdk());
} }
} }
} }
@@ -74,7 +74,7 @@ public abstract class AbstractQuickFixMultiFileTest extends KotlinDaemonAnalyzer
boolean withRuntime = InTextDirectivesUtils.isDirectiveDefined(originalFileText, "// WITH_RUNTIME"); boolean withRuntime = InTextDirectivesUtils.isDirectiveDefined(originalFileText, "// WITH_RUNTIME");
if (withRuntime) { if (withRuntime) {
ConfigLibraryUtil.configureKotlinRuntime(myModule, PluginTestCaseBase.mockJdk()); ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk());
} }
try { try {
@@ -142,7 +142,7 @@ public abstract class AbstractQuickFixMultiFileTest extends KotlinDaemonAnalyzer
} }
finally { finally {
if (withRuntime) { if (withRuntime) {
ConfigLibraryUtil.unConfigureKotlinRuntime(myModule, PluginTestCaseBase.mockJdk()); ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk());
} }
} }
} }
@@ -83,19 +83,19 @@ public abstract class AbstractQuickFixTest extends KotlinLightQuickFixTestCase {
private static void configureRuntimeIfNeeded(@NotNull String beforeFileName) { private static void configureRuntimeIfNeeded(@NotNull String beforeFileName) {
if (beforeFileName.endsWith("JsRuntime.kt")) { if (beforeFileName.endsWith("JsRuntime.kt")) {
ConfigLibraryUtil.configureKotlinJsRuntime(getModule(), getFullJavaJDK()); ConfigLibraryUtil.configureKotlinJsRuntimeAndSdk(getModule(), getFullJavaJDK());
} }
else if (beforeFileName.endsWith("Runtime.kt")) { else if (beforeFileName.endsWith("Runtime.kt")) {
ConfigLibraryUtil.configureKotlinRuntime(getModule(), getFullJavaJDK()); ConfigLibraryUtil.configureKotlinRuntimeAndSdk(getModule(), getFullJavaJDK());
} }
} }
private void unConfigureRuntimeIfNeeded(@NotNull String beforeFileName) { private static void unConfigureRuntimeIfNeeded(@NotNull String beforeFileName) {
if (beforeFileName.endsWith("JsRuntime.kt")) { if (beforeFileName.endsWith("JsRuntime.kt")) {
ConfigLibraryUtil.unConfigureKotlinJsRuntime(getModule(), getProjectJDK()); ConfigLibraryUtil.unConfigureKotlinJsRuntimeAndSdk(getModule(), getProjectJDK());
} }
else if (beforeFileName.endsWith("Runtime.kt")) { else if (beforeFileName.endsWith("Runtime.kt")) {
ConfigLibraryUtil.unConfigureKotlinRuntime(getModule(), getProjectJDK()); ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(getModule(), getProjectJDK());
} }
} }
@@ -225,7 +225,7 @@ public abstract class AbstractJetExtractionTest() : JetLightCodeInsightFixtureTe
val addKotlinRuntime = InTextDirectivesUtils.findStringWithPrefixes(file.getText(), "// WITH_RUNTIME") != null val addKotlinRuntime = InTextDirectivesUtils.findStringWithPrefixes(file.getText(), "// WITH_RUNTIME") != null
if (addKotlinRuntime) { if (addKotlinRuntime) {
ConfigLibraryUtil.configureKotlinRuntime(myModule, PluginTestCaseBase.mockJdk()) ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk())
} }
try { try {
@@ -240,7 +240,7 @@ public abstract class AbstractJetExtractionTest() : JetLightCodeInsightFixtureTe
} }
finally { finally {
if (addKotlinRuntime) { if (addKotlinRuntime) {
ConfigLibraryUtil.unConfigureKotlinRuntime(myModule, PluginTestCaseBase.mockJdk()) ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk())
} }
} }
} }
@@ -80,7 +80,7 @@ public abstract class AbstractJetMoveTest : KotlinMultiFileTestCase() {
val withRuntime = config["withRuntime"]?.getAsBoolean() ?: false val withRuntime = config["withRuntime"]?.getAsBoolean() ?: false
if (withRuntime) { if (withRuntime) {
ConfigLibraryUtil.configureKotlinRuntime(myModule, PluginTestCaseBase.mockJdk()) ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk())
} }
doTest({ rootDir, rootAfter -> doTest({ rootDir, rootAfter ->
@@ -114,7 +114,7 @@ public abstract class AbstractJetMoveTest : KotlinMultiFileTestCase() {
EditorFactory.getInstance()!!.releaseEditor(editor) EditorFactory.getInstance()!!.releaseEditor(editor)
if (withRuntime) { if (withRuntime) {
ConfigLibraryUtil.unConfigureKotlinRuntime(myModule, PluginTestCaseBase.mockJdk()) ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk())
} }
} }
}, },
@@ -41,8 +41,8 @@ import com.intellij.testFramework.PsiTestUtil
import org.jetbrains.kotlin.idea.search.allScope import org.jetbrains.kotlin.idea.search.allScope
import org.jetbrains.kotlin.idea.stubindex.JetTopLevelFunctionFqnNameIndex import org.jetbrains.kotlin.idea.stubindex.JetTopLevelFunctionFqnNameIndex
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil.configureKotlinJsRuntime import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil.configureKotlinJsRuntimeAndSdk
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil.configureKotlinRuntime import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil.configureKotlinRuntimeAndSdk
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.psi.JetNamedDeclaration import org.jetbrains.kotlin.psi.JetNamedDeclaration
@@ -91,11 +91,11 @@ class RunConfigurationTest: CodeInsightTestCase() {
} }
fun testClassesAndObjects() { fun testClassesAndObjects() {
doTest(ConfigLibraryUtil::configureKotlinRuntime) doTest(ConfigLibraryUtil::configureKotlinRuntimeAndSdk)
} }
fun testInJsModule() { fun testInJsModule() {
doTest(ConfigLibraryUtil::configureKotlinJsRuntime) doTest(ConfigLibraryUtil::configureKotlinJsRuntimeAndSdk)
} }
private fun doTest(configureRuntime: (Module, Sdk) -> Unit) { private fun doTest(configureRuntime: (Module, Sdk) -> Unit) {
@@ -133,7 +133,7 @@ class RunConfigurationTest: CodeInsightTestCase() {
Assert.assertEquals(expectedClasses, actualClasses) Assert.assertEquals(expectedClasses, actualClasses)
} }
finally { finally {
ConfigLibraryUtil.unConfigureKotlinRuntime(createModuleResult.module, PluginTestCaseBase.mockJdk()) ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(createModuleResult.module, PluginTestCaseBase.mockJdk())
} }
} }