From d097c3e9f14b98c70fcb555d086d02e5c1a98ed3 Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Fri, 1 Apr 2016 19:52:21 +0300 Subject: [PATCH] KT-11743 Intention to replace kotlin-test with kotlin-test-junit --- .../KotlinTestJUnitInspection.kt | 67 +++++++++++++++++++ .../KotlinMavenInspectionTestGenerated.java | 6 ++ .../kotlinTestWithJunit.fixed.1.xml | 57 ++++++++++++++++ .../maven-inspections/kotlinTestWithJunit.xml | 57 ++++++++++++++++ idea/src/META-INF/maven.xml | 8 +++ 5 files changed, 195 insertions(+) create mode 100644 idea/idea-maven/src/org/jetbrains/kotlin/idea/configuration/KotlinTestJUnitInspection.kt create mode 100644 idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.fixed.1.xml create mode 100644 idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.xml diff --git a/idea/idea-maven/src/org/jetbrains/kotlin/idea/configuration/KotlinTestJUnitInspection.kt b/idea/idea-maven/src/org/jetbrains/kotlin/idea/configuration/KotlinTestJUnitInspection.kt new file mode 100644 index 00000000000..fed65189499 --- /dev/null +++ b/idea/idea-maven/src/org/jetbrains/kotlin/idea/configuration/KotlinTestJUnitInspection.kt @@ -0,0 +1,67 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.configuration + +import com.intellij.codeInspection.LocalQuickFix +import com.intellij.codeInspection.ProblemDescriptor +import com.intellij.lang.annotation.HighlightSeverity +import com.intellij.openapi.project.Project +import com.intellij.util.xml.DomFileElement +import com.intellij.util.xml.highlighting.DomElementAnnotationHolder +import com.intellij.util.xml.highlighting.DomElementsInspection +import org.jetbrains.idea.maven.dom.model.MavenDomDependency +import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel +import org.jetbrains.idea.maven.project.MavenProjectsManager + +class KotlinTestJUnitInspection : DomElementsInspection(MavenDomProjectModel::class.java) { + override fun checkFileElement(domFileElement: DomFileElement?, holder: DomElementAnnotationHolder?) { + if (domFileElement == null || holder == null) { + return + } + + val module = domFileElement.module ?: return + val manager = MavenProjectsManager.getInstance(module.project) ?: return + val mavenProject = manager.findProject(module) ?: return + + val hasJunit = mavenProject.dependencies.any { it.groupId == "junit" && it.artifactId == "junit" } + if (!hasJunit) { + return + } + + val kotlinTestDependencies = domFileElement.rootElement.dependencies + .dependencies.filter { it.groupId.rawText == KotlinMavenConfigurator.GROUP_ID && it.artifactId.rawText == KotlinJavaMavenConfigurator.TEST_LIB_ID } + + kotlinTestDependencies.forEach { + holder.createProblem(it.artifactId, + HighlightSeverity.WEAK_WARNING, + "kotlin-test-junit is better with junit", + ReplaceToKotlinTest(it) + ) + } + } + + private class ReplaceToKotlinTest(val dependency: MavenDomDependency) : LocalQuickFix { + override fun getName() = "Replace with kotlin-test-junit" + override fun getFamilyName() = "Kotlin" + + override fun applyFix(project: Project, descriptor: ProblemDescriptor) { + if (dependency.isValid) { + dependency.artifactId.stringValue = "kotlin-test-junit" + } + } + } +} \ No newline at end of file diff --git a/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenInspectionTestGenerated.java b/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenInspectionTestGenerated.java index aeac29d7f01..34cc70f0c49 100644 --- a/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenInspectionTestGenerated.java +++ b/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenInspectionTestGenerated.java @@ -47,6 +47,12 @@ public class KotlinMavenInspectionTestGenerated extends AbstractKotlinMavenInspe doTest(fileName); } + @TestMetadata("kotlinTestWithJunit.xml") + public void testKotlinTestWithJunit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.xml"); + doTest(fileName); + } + @TestMetadata("missingDependencies.xml") public void testMissingDependencies() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/idea-maven/testData/maven-inspections/missingDependencies.xml"); diff --git a/idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.fixed.1.xml b/idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.fixed.1.xml new file mode 100644 index 00000000000..fc1000c80db --- /dev/null +++ b/idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.fixed.1.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + org.jetbrains.kotlin.test + configure-maven-test + 1.0-SNAPSHOT + + + 1.0.1 + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-test-junit + ${kotlin.version} + test + + + junit + junit + 4.12 + + + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + js + + + + + + + + + + + + diff --git a/idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.xml b/idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.xml new file mode 100644 index 00000000000..ca3695003e3 --- /dev/null +++ b/idea/idea-maven/testData/maven-inspections/kotlinTestWithJunit.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + org.jetbrains.kotlin.test + configure-maven-test + 1.0-SNAPSHOT + + + 1.0.1 + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + + + junit + junit + 4.12 + + + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + js + + + + + + + + + + + + diff --git a/idea/src/META-INF/maven.xml b/idea/src/META-INF/maven.xml index 42bb175da3e..d4f65e1af30 100644 --- a/idea/src/META-INF/maven.xml +++ b/idea/src/META-INF/maven.xml @@ -16,6 +16,14 @@ hasStaticDescription="true" level="WARNING" /> + + org.jetbrains.kotlin.idea.configuration.MavenPluginSourcesMoveToBuild Kotlin