test assistance: @TestDataPath propagated to @Parameterized tests

This commit is contained in:
Anna Kozlova
2014-01-09 11:11:37 +01:00
parent cedfc01a85
commit 0230339ea1
8 changed files with 67 additions and 28 deletions
@@ -18,11 +18,13 @@ package com.intellij.refactoring;
import com.intellij.JavaTestUtil;
import com.intellij.refactoring.anonymousToInner.AnonymousToInnerHandler;
import com.intellij.testFramework.LightCodeInsightTestCase;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.annotations.NotNull;
/**
* @author yole
*/
@TestDataPath("$CONTENT_ROOT/testData")
public class AnonymousToInnerTest extends LightCodeInsightTestCase {
private static final String TEST_ROOT = "/refactoring/anonymousToInner/";
@@ -5,16 +5,13 @@ import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNamedElement;
import com.intellij.refactoring.invertBoolean.InvertBooleanProcessor;
import com.intellij.testFramework.TestDataPath;
/**
* @author ven
*/
@TestDataPath("$CONTENT_ROOT/testData/refactoring/invertBoolean/")
public class InvertBooleanTest extends LightRefactoringParameterizedTestCase {
@Override
public String getRelativeBasePath(){
return "/refactoring/invertBoolean/";
}
@Override
protected void perform() {
PsiElement element = TargetElementUtilBase.findTargetElement(myEditor, TargetElementUtilBase.ELEMENT_NAME_ACCEPTED);
@@ -55,12 +55,12 @@ public abstract class LightRefactoringParameterizedTestCase extends LightRefacto
@Override
public void run() {
try {
final String filePath = getRelativeBasePath() + getBeforeFile(myFileSuffix);
final String filePath = getBeforeFile(myFileSuffix);
configureByFile(filePath);
final File testDir = new File(filePath).getParentFile();
final File testDir = new File(getTestDataPath(), filePath).getParentFile();
final String afterName = getAfterFile(myFileSuffix);
final boolean conflictShouldBeFound = !new File(getTestDataPath() + "/" + testDir, afterName).exists();
final boolean conflictShouldBeFound = !new File(testDir, afterName).exists();
try {
perform();
if (conflictShouldBeFound) {
@@ -71,7 +71,7 @@ public abstract class LightRefactoringParameterizedTestCase extends LightRefacto
if (!conflictShouldBeFound) {
fail("Conflict not expected");
} else {
final File conflicts = new File(getTestDataPath() + "/" + testDir, FileUtilRt.getNameWithoutExtension(myFileSuffix) + CONFLICTS_SUFFIX);
final File conflicts = new File(testDir, FileUtilRt.getNameWithoutExtension(myFileSuffix) + CONFLICTS_SUFFIX);
if (!conflicts.exists()) {
fail("Conflict file " + conflicts.getPath() + " not found");
}
@@ -82,7 +82,7 @@ public abstract class LightRefactoringParameterizedTestCase extends LightRefacto
}
if (!conflictShouldBeFound) {
checkResultByFile(getRelativeBasePath() + getAfterFile(myFileSuffix));
checkResultByFile(getAfterFile(myFileSuffix));
}
}
catch (Throwable e) {
@@ -15,13 +15,13 @@
*/
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.testFramework.FileBasedTestCaseHelper;
import com.intellij.testFramework.FileBasedTestCaseHelperEx;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(com.intellij.testFramework.Parameterized.class)
public abstract class LightQuickFixParameterizedTestCase extends LightQuickFixTestCase implements FileBasedTestCaseHelper {
public abstract class LightQuickFixParameterizedTestCase extends LightQuickFixTestCase implements FileBasedTestCaseHelperEx {
@Override
public String getRelativeBasePath() {
return getBasePath();
@@ -19,16 +19,15 @@ import org.jetbrains.annotations.Nullable;
/**
* Should be implemented by a test together with annotation @RunWith(com.intellij.testFramework.Parameterized.class)
* in order to get test run on all test data files located in {@link FileBasedTestCaseHelper#getRelativeBasePath()}.
*
* in order to get test run on all test data files located in directory. The desired directory could be configured
* whether by implementing {@link FileBasedTestCaseHelperEx#getRelativeBasePath()} or by annotating test case
* with {@link com.intellij.testFramework.TestDataPath} (annotation would enable additional test assistance support, e.g.
* navigation from test data to test class/method as well as starting tests right from test data files).
* <br/><br/>
* BTW @RunWith works also on abstract super classes.
* @see LightPlatformCodeInsightTestCase#params(java.lang.Class<?>)
*/
public interface FileBasedTestCaseHelper {
/**
* @return path related to the test data root according to {@link LightPlatformCodeInsightTestCase#getTestDataPath()}
*/
String getRelativeBasePath();
/**
* @return for 'before' files should return core file name or null otherwise
*/
@@ -0,0 +1,24 @@
/*
* Copyright 2000-2014 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.testFramework;
public interface FileBasedTestCaseHelperEx extends FileBasedTestCaseHelper {
/**
* @return path related to the test data root according to {@link LightPlatformCodeInsightTestCase#getTestDataPath()}
*/
String getRelativeBasePath();
}
@@ -658,7 +658,7 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
/**
* file parameterized tests support
* @see FileBasedTestCaseHelper
* @see FileBasedTestCaseHelperEx
*/
/**
@@ -667,6 +667,11 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
@Parameterized.Parameter(0)
public String myFileSuffix;
/**
* path to the root of test data in case of com.intellij.testFramework.FileBasedTestCaseHelperEx
* or
* path to the directory with current test data in case of @TestDataPath
*/
@Parameterized.Parameter(1)
public String myTestDataPath;
@@ -681,20 +686,32 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
if (!(testCase instanceof FileBasedTestCaseHelper)) {
fail("Parameterized test should implement FileBasedTestCaseHelper");
}
final FileBasedTestCaseHelper fileBasedTestCase = (FileBasedTestCaseHelper)testCase;
final String path = fileBasedTestCase.getRelativeBasePath();
assertNotNull("getBasePath() should not return null!", path);
PathManagerEx.replaceLookupStrategy(klass, LightPlatformCodeInsightTestCase.class, com.intellij.testFramework.Parameterized.class);
final FileBasedTestCaseHelper fileBasedTestCase = (FileBasedTestCaseHelper)testCase;
String testDataPath = testCase.getTestDataPath();
final String testDirPath = testDataPath.replace(File.separatorChar, '/') + path;
File testDir = new File(testDirPath);
File testDir = null;
if (fileBasedTestCase instanceof FileBasedTestCaseHelperEx) {
testDir = new File(testDataPath, ((FileBasedTestCaseHelperEx)fileBasedTestCase).getRelativeBasePath());
} else {
final TestDataPath annotation = klass.getAnnotation(TestDataPath.class);
if (annotation == null) {
fail("TestCase should implement com.intellij.testFramework.FileBasedTestCaseHelperEx or be annotated with com.intellij.testFramework.TestDataPath");
} else {
final String trimmedRoot = StringUtil.trimStart(StringUtil.trimStart(annotation.value(), "$CONTENT_ROOT"), "$PROJECT_ROOT");
final String lastPathComponent = new File(testDataPath).getName();
final int idx = trimmedRoot.indexOf(lastPathComponent);
testDataPath = testDataPath.replace(File.separatorChar, '/') + (idx > 0 ? trimmedRoot.substring(idx + lastPathComponent.length()) : trimmedRoot);
testDir = new File(testDataPath);
}
}
final File[] files = testDir.listFiles();
if (files == null) {
fail("Test files not found in " + testDirPath);
fail("Test files not found in " + testDir.getPath());
}
final List<Object[]> result = new ArrayList<Object[]>();
@@ -27,7 +27,7 @@ import java.lang.annotation.Target;
*
* @author yole
*/
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface TestDataPath {
String value();