diff --git a/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/MultiplePluginVersionGradleImportingTestCase.kt b/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/MultiplePluginVersionGradleImportingTestCase.kt index dd1836b9c17..c56fc4dd5c7 100644 --- a/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/MultiplePluginVersionGradleImportingTestCase.kt +++ b/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/MultiplePluginVersionGradleImportingTestCase.kt @@ -11,36 +11,54 @@ package org.jetbrains.kotlin.idea.codeInsight.gradle import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile +import org.junit.Rule import org.junit.runners.Parameterized import java.util.* abstract class MultiplePluginVersionGradleImportingTestCase : GradleImportingTestCase() { + @Rule + @JvmField + var pluginVersionMatchingRule = PluginTargetVersionsRule() + val project: Project get() = myProject + + override fun isApplicableTest(): Boolean { + return pluginVersionMatchingRule.matches( + gradleVersion, gradleKotlinPluginVersion, + gradleKotlinPluginVersionType == LATEST_SUPPORTED_VERSION + ) + } + @JvmField @Parameterized.Parameter(1) - var gradleKotlinPluginVersion: String = MINIMAL_SUPPORTED_VERSION + var gradleKotlinPluginVersionType: String = MINIMAL_SUPPORTED_VERSION + + val gradleKotlinPluginVersion: String + get() = KOTLIN_GRADLE_PLUGIN_VERSION_DESCRIPTION_TO_VERSION[gradleKotlinPluginVersionType] ?: gradleKotlinPluginVersion companion object { const val MINIMAL_SUPPORTED_VERSION = "minimal"// minimal supported version - private val KOTLIN_GRADLE_PLUGIN_VERSIONS = listOf(MINIMAL_SUPPORTED_VERSION, "1.3.50") + const val LATEST_STABLE_VERSUON = "latest stable" + const val LATEST_SUPPORTED_VERSION = "master"// gradle plugin from current build + private val KOTLIN_GRADLE_PLUGIN_VERSIONS = listOf(MINIMAL_SUPPORTED_VERSION, LATEST_STABLE_VERSUON, LATEST_SUPPORTED_VERSION) + private val KOTLIN_GRADLE_PLUGIN_VERSION_DESCRIPTION_TO_VERSION = mapOf( + MINIMAL_SUPPORTED_VERSION to MINIMAL_SUPPORTED_GRADLE_PLUGIN_VERSION, + LATEST_STABLE_VERSUON to LATEST_STABLE_GRADLE_PLUGIN_VERSION, + LATEST_SUPPORTED_VERSION to "master" + ) @JvmStatic @Parameterized.Parameters(name = "{index}: Gradle-{0}, KotlinGradlePlugin-{1}") fun data(): Collection> { return AbstractModelBuilderTest.SUPPORTED_GRADLE_VERSIONS.flatMap { gradleVersion -> - if ((gradleVersion[0] as String).startsWith("3.")) { - listOf(arrayOf(gradleVersion[0], MINIMAL_SUPPORTED_VERSION)) - } else { - KOTLIN_GRADLE_PLUGIN_VERSIONS.map { kotlinVersion -> - arrayOf( - gradleVersion[0], - kotlinVersion - ) - } + KOTLIN_GRADLE_PLUGIN_VERSIONS.map { kotlinVersion -> + arrayOf( + gradleVersion[0], + kotlinVersion + ) } - }.toList() } } diff --git a/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/PluginTargetVersionsRule.java b/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/PluginTargetVersionsRule.java new file mode 100644 index 00000000000..aab70e9e64b --- /dev/null +++ b/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/PluginTargetVersionsRule.java @@ -0,0 +1,74 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ +package org.jetbrains.kotlin.idea.codeInsight.gradle; + +import org.gradle.util.GradleVersion; +import org.hamcrest.CustomMatcher; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.gradle.tooling.annotation.PluginTargetVersions; +import org.jetbrains.plugins.gradle.tooling.annotation.TargetVersions; +import org.jetbrains.plugins.gradle.tooling.util.VersionMatcher; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import java.lang.annotation.Annotation; + +// modified copy of org.jetbrains.plugins.gradle.tooling.VersionMatcherRule +public class PluginTargetVersionsRule extends TestWatcher { + + private class TargetVersionsImpl implements TargetVersions { + private final String value; + + TargetVersionsImpl(String value) { + this.value = value; + } + + @Override + public String value() { + return value; + } + + @Override + public boolean checkBaseVersions() { + return true; + } + + @Override + public Class annotationType() { + return null; + } + } + + + @Nullable + private CustomMatcher gradleVersionMatcher; + + @Nullable + private CustomMatcher pluginVersionMatcher; + + + @NotNull + public boolean matches(String gradleVersion, String pluginVersion, boolean isLatestPluginVersion) { + boolean matchGradleVersion = gradleVersionMatcher == null || gradleVersionMatcher.matches(gradleVersion); + if (isLatestPluginVersion) { + return matchGradleVersion; + } + boolean pluginVersionMatches = pluginVersionMatcher == null || pluginVersionMatcher.matches(pluginVersion); + return matchGradleVersion && pluginVersionMatches; + } + + @Override + protected void starting(Description d) { + final PluginTargetVersions pluginTargetVersions = d.getAnnotation(PluginTargetVersions.class); + if (d.getAnnotation(TargetVersions.class) != null && pluginTargetVersions != null) { + throw new IllegalArgumentException(String.format("Annotations %s and %s could not be used together. ", + TargetVersions.class.getName(), PluginTargetVersions.class.getName())); + } + if (pluginTargetVersions == null) return; + + gradleVersionMatcher = VersionMatcherRule.produceMatcher("Gradle", new TargetVersionsImpl(pluginTargetVersions.gradleVersion())); + pluginVersionMatcher = VersionMatcherRule.produceMatcher("Plugin", new TargetVersionsImpl(pluginTargetVersions.pluginVersion())); + } +} diff --git a/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/VersionMatcherRule.java b/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/VersionMatcherRule.java index 4c7052d7a74..c4621189c54 100644 --- a/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/VersionMatcherRule.java +++ b/idea/idea-gradle/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/VersionMatcherRule.java @@ -42,7 +42,11 @@ public class VersionMatcherRule extends TestWatcher { final TargetVersions targetVersions = d.getAnnotation(TargetVersions.class); if (targetVersions == null) return; - myMatcher = new CustomMatcher("Gradle version '" + targetVersions.value() + "'") { + myMatcher = produceMatcher("Gradle", targetVersions); + } + + public static CustomMatcher produceMatcher(String caption, TargetVersions targetVersions) { + return new CustomMatcher(caption + " version '" + targetVersions.value() + "'") { @Override public boolean matches(Object item) { return item instanceof String && new VersionMatcher(GradleVersion.version(item.toString())).isVersionMatch(targetVersions); diff --git a/idea/idea-gradle/tests/org/jetbrains/plugins/gradle/tooling/annotation/PluginTargetVersions.java b/idea/idea-gradle/tests/org/jetbrains/plugins/gradle/tooling/annotation/PluginTargetVersions.java new file mode 100644 index 00000000000..940b883d355 --- /dev/null +++ b/idea/idea-gradle/tests/org/jetbrains/plugins/gradle/tooling/annotation/PluginTargetVersions.java @@ -0,0 +1,18 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.plugins.gradle.tooling.annotation; + + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface PluginTargetVersions { + + String gradleVersion(); + + String pluginVersion(); +} \ No newline at end of file