From c53d2b910d809908528b1cae53fbb7be3d42ee0b Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 10 Apr 2015 18:04:06 +0200 Subject: [PATCH] junit: test to ensure that test from concrete class would be rerun --- plugins/junit/junit.iml | 5 +- .../intellij/execution/junit/TestMethods.java | 30 +++++---- .../junit/JUnitRerunFailedTestsTest.java | 65 +++++++++++++++++++ 3 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 plugins/junit/test/com/intellij/execution/junit/JUnitRerunFailedTestsTest.java diff --git a/plugins/junit/junit.iml b/plugins/junit/junit.iml index 04538142d4df..0a03d6ac8029 100644 --- a/plugins/junit/junit.iml +++ b/plugins/junit/junit.iml @@ -4,6 +4,7 @@ + @@ -19,6 +20,7 @@ + @@ -172,5 +174,4 @@ - - + \ No newline at end of file diff --git a/plugins/junit/src/com/intellij/execution/junit/TestMethods.java b/plugins/junit/src/com/intellij/execution/junit/TestMethods.java index 93acf04d8ea4..2356ba0bc645 100644 --- a/plugins/junit/src/com/intellij/execution/junit/TestMethods.java +++ b/plugins/junit/src/com/intellij/execution/junit/TestMethods.java @@ -33,6 +33,7 @@ import com.intellij.psi.PsiMethod; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.util.Function; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collection; @@ -59,25 +60,28 @@ public class TestMethods extends TestMethod { addClassesListToJavaParameters(myFailedTests, new Function() { @Override public String fun(AbstractTestProxy testInfo) { - if (testInfo != null) { - final Location location = testInfo.getLocation(project, searchScope); - LOG.assertTrue(location != null); - final PsiElement element = location.getPsiElement(); - if (element instanceof PsiMethod) { - final PsiClass containingClass = location instanceof MethodLocation ? ((MethodLocation)location).getContainingClass() - : ((PsiMethod)element).getContainingClass(); - if (containingClass != null) { - return JavaExecutionUtil.getRuntimeQualifiedName(containingClass) + "," + testInfo.getName(); - } - } - } - return null; + return testInfo != null ? getTestPresentation(testInfo, project, searchScope) : null; } }, data.getPackageName(), true, javaParameters); return javaParameters; } + @Nullable + public static String getTestPresentation(AbstractTestProxy testInfo, Project project, GlobalSearchScope searchScope) { + final Location location = testInfo.getLocation(project, searchScope); + LOG.assertTrue(location != null); + final PsiElement element = location.getPsiElement(); + if (element instanceof PsiMethod) { + final PsiClass containingClass = location instanceof MethodLocation ? ((MethodLocation)location).getContainingClass() + : ((PsiMethod)element).getContainingClass(); + if (containingClass != null) { + return JavaExecutionUtil.getRuntimeQualifiedName(containingClass) + "," + testInfo.getName(); + } + } + return null; + } + @Override public String suggestActionName() { return ActionsBundle.message("action.RerunFailedTests.text"); diff --git a/plugins/junit/test/com/intellij/execution/junit/JUnitRerunFailedTestsTest.java b/plugins/junit/test/com/intellij/execution/junit/JUnitRerunFailedTestsTest.java new file mode 100644 index 000000000000..73a8b2e96d31 --- /dev/null +++ b/plugins/junit/test/com/intellij/execution/junit/JUnitRerunFailedTestsTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2015 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 com.intellij.execution.junit; + +import com.intellij.execution.Location; +import com.intellij.execution.junit2.info.MethodLocation; +import com.intellij.execution.testframework.JavaTestLocationProvider; +import com.intellij.execution.testframework.sm.runner.SMTestProxy; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; + +public class JUnitRerunFailedTestsTest extends LightCodeInsightFixtureTestCase { + + @Override + protected void setUp() throws Exception { + super.setUp(); + myFixture.addClass("package junit.framework; public class TestCase {}"); + } + + public void testIncludeMethodsToRerunFromChildClass() throws Exception { + myFixture.addClass("abstract class ATest extends junit.framework.TestCase {" + + " public void testMe() {}\n" + + "}"); + myFixture.addClass("public class ChildTest extends ATest {}"); + + final SMTestProxy testProxy = new SMTestProxy("testMe", false, "java:test://ChildTest.testMe"); + final Project project = getProject(); + final GlobalSearchScope searchScope = GlobalSearchScope.projectScope(project); + testProxy.setLocator(new JavaTestLocationProvider(searchScope)); + + final Location location = testProxy.getLocation(project, searchScope); + assertNotNull(location); + assertInstanceOf(location, MethodLocation.class); + + //navigation to the method in abstract super class + final PsiElement element = location.getPsiElement(); + assertInstanceOf(element, PsiMethod.class); + final PsiMethod method = (PsiMethod)element; + assertEquals("testMe", method.getName()); + final PsiClass containingClass = method.getContainingClass(); + assertNotNull(containingClass); + assertEquals("ATest", containingClass.getQualifiedName()); + + //include method "from" child class to rerun + final String presentation = TestMethods.getTestPresentation(testProxy, project, searchScope); + assertEquals("ChildTest,testMe", presentation); + } +}