parameterized test: sample cases

This commit is contained in:
Anna Kozlova
2014-01-08 12:50:00 +01:00
parent a16d50baa6
commit cda9ec5baf
6 changed files with 290 additions and 142 deletions
@@ -0,0 +1 @@
Method is used in method reference expression
@@ -1,54 +1,42 @@
package com.intellij.refactoring;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNamedElement;
import com.intellij.refactoring.invertBoolean.InvertBooleanProcessor;
import org.jetbrains.annotations.NotNull;
/**
* @author ven
*/
public class InvertBooleanTest extends LightRefactoringTestCase {
@NotNull
public class InvertBooleanTest extends LightRefactoringParameterizedTestCase {
@Override
protected String getTestDataPath() {
return JavaTestUtil.getJavaTestDataPath();
public String getRelativeBasePath(){
return "/refactoring/invertBoolean/";
}
private static final String TEST_ROOT = "/refactoring/invertBoolean/";
public void test1() throws Exception { doTest(); }
public void test2() throws Exception { doTest(); } //inverting breaks overriding
public void testParameter() throws Exception { doTest(); } //inverting boolean parameter
public void testParameter1() throws Exception { doTest(); } //inverting boolean parameter more advanced stuff
public void testUnusedReturnValue() throws Exception { doTest(); }
public void testInnerClasses() throws Exception {doTest();}
public void testAnonymousClasses() throws Exception {doTest();}
public void testMethodRefs() throws Exception {
try {
doTest();
fail("Conflict expected.");
}
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
assertEquals("Method is used in method reference expression", e.getMessage());
}
}
private void doTest() throws Exception {
configureByFile(TEST_ROOT + getTestName(true) + ".java");
@Override
protected void perform() {
PsiElement element = TargetElementUtilBase.findTargetElement(myEditor, TargetElementUtilBase.ELEMENT_NAME_ACCEPTED);
assertTrue(element instanceof PsiNamedElement);
final PsiNamedElement namedElement = (PsiNamedElement)element;
final String name = namedElement.getName();
new InvertBooleanProcessor(namedElement, name + "Inverted").run();
checkResultByFile(TEST_ROOT + getTestName(true) + "_after.java");
}
@Override
protected String getAfterFile(String fileName) {
return FileUtilRt.getNameWithoutExtension(fileName) + "_" + AFTER_PREFIX + "." + FileUtilRt.getExtension(fileName);
}
@Override
protected String getBeforeFile(String fileName) {
return fileName;
}
@Override
public String getFileSuffix(String beforeFile) {
return !beforeFile.contains(AFTER_PREFIX) && !beforeFile.endsWith(CONFLICTS_SUFFIX) ? beforeFile : null;
}
}
@@ -0,0 +1,111 @@
/*
* 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.
*/
/*
* Created by IntelliJ IDEA.
* User: dsl
* Date: 04.06.2002
* Time: 20:01:43
* To change template for new class use
* Code Style | Class Templates options (Tools | IDE Options).
*/
package com.intellij.refactoring;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.FileBasedTestCaseHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.File;
@RunWith(com.intellij.testFramework.Parameterized.class)
public abstract class LightRefactoringParameterizedTestCase extends LightRefactoringTestCase implements FileBasedTestCaseHelper {
protected static final String BEFORE_PREFIX = "before";
protected static final String AFTER_PREFIX = "after";
protected static final String CONFLICTS_SUFFIX = ".conflicts.txt";
protected abstract void perform();
protected abstract String getAfterFile(String fileNameCore);
protected abstract String getBeforeFile(String fileNameCore);
@Test
public void runSingle() throws Throwable {
final Throwable[] throwables = new Throwable[1];
final Runnable runnable = new Runnable() {
@Override
public void run() {
try {
final String filePath = getRelativeBasePath() + getBeforeFile(myFileSuffix);
configureByFile(filePath);
final File testDir = new File(filePath).getParentFile();
final String afterName = getAfterFile(myFileSuffix);
final boolean conflictShouldBeFound = !new File(getTestDataPath() + "/" + testDir, afterName).exists();
try {
perform();
if (conflictShouldBeFound) {
fail("Conflict expected.");
}
}
catch (BaseRefactoringProcessor.ConflictsInTestsException exception) {
if (!conflictShouldBeFound) {
fail("Conflict not expected");
} else {
final File conflicts = new File(getTestDataPath() + "/" + testDir, FileUtilRt.getNameWithoutExtension(myFileSuffix) + CONFLICTS_SUFFIX);
if (!conflicts.exists()) {
fail("Conflict file " + conflicts.getPath() + " not found");
}
final VirtualFile conflictsFile = VfsUtil.findFileByIoFile(conflicts, false);
assertNotNull(conflictsFile);
assertEquals(LoadTextUtil.loadText(conflictsFile).toString(), exception.getMessage());
}
}
if (!conflictShouldBeFound) {
checkResultByFile(getRelativeBasePath() + getAfterFile(myFileSuffix));
}
}
catch (Throwable e) {
throwables[0] = e;
}
}
};
invokeTestRunnable(new Runnable() {
@Override
public void run() {
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
runnable.run();
}
}, "", null);
}
});
if (throwables[0] != null) {
throw throwables[0];
}
}
}
@@ -15,64 +15,23 @@
*/
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.openapi.application.ex.PathManagerEx;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import com.intellij.testFramework.FileBasedTestCaseHelper;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@RunWith(com.intellij.testFramework.Parameterized.class)
public abstract class LightQuickFixParameterizedTestCase extends LightQuickFixTestCase {
@Parameterized.Parameter
public String myFileSuffix;
@Parameterized.Parameter(1)
public String myTestDataPath;
@com.intellij.testFramework.Parameterized.Parameters(name = "{0}")
public static List<Object[]> params(Class<?> klass) throws Throwable{
final QuickFixTestCase testCase = ((LightQuickFixTestCase)klass.newInstance()).createWrapper();
final String path = testCase.getBasePath();
assertNotNull("getBasePath() should not return null!", path);
PathManagerEx.replaceLookupStrategy(klass, LightQuickFixTestCase.class, com.intellij.testFramework.Parameterized.class);
String testDataPath = testCase.getTestDataPath();
final String testDirPath = testDataPath.replace(File.separatorChar, '/') + path;
File testDir = new File(testDirPath);
final File[] files = testDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, @NonNls String name) {
return name.startsWith(BEFORE_PREFIX);
}
});
if (files == null) {
fail("Test files not found in " + testDirPath);
}
final List<Object[]> result = new ArrayList<Object[]>();
for (File file : files) {
final String testName = file.getName().substring(BEFORE_PREFIX.length());
result.add(new Object[] {testName, testDataPath});
}
return result;
public abstract class LightQuickFixParameterizedTestCase extends LightQuickFixTestCase implements FileBasedTestCaseHelper {
@Override
public String getRelativeBasePath() {
return getBasePath();
}
@Parameterized.Parameters(name = "{0}")
public static List<Object[]> params() throws Throwable{
return Collections.emptyList();
@Nullable
@Override
public String getFileSuffix(String fileName) {
if (!fileName.startsWith(BEFORE_PREFIX)) return null;
return fileName.substring(BEFORE_PREFIX.length());
}
@Override
@@ -80,64 +39,6 @@ public abstract class LightQuickFixParameterizedTestCase extends LightQuickFixTe
super.doAllTests();
}
@NotNull
@Override
protected String getTestDataPath() {
if (myTestDataPath != null) {
return myTestDataPath;
}
return super.getTestDataPath();
}
@Override
public String getName() {
if (myFileSuffix != null) {
return "test" + myFileSuffix;
}
return super.getName();
}
@Before
public void before() throws Throwable {
final Throwable[] throwables = new Throwable[1];
invokeTestRunnable(new Runnable() {
@Override
public void run() {
try {
LightQuickFixParameterizedTestCase.this.setUp();
}
catch (Throwable e) {
throwables[0] = e;
}
}
});
if (throwables[0] != null) {
throw throwables[0];
}
}
@After
public void after() throws Throwable {
final Throwable[] throwables = new Throwable[1];
invokeTestRunnable(new Runnable() {
@Override
public void run() {
try {
LightQuickFixParameterizedTestCase.this.tearDown();
}
catch (Throwable e) {
throwables[0] = e;
}
}
});
if (throwables[0] != null) {
throw throwables[0];
}
}
@Test
public void runSingle() throws Throwable {
final Throwable[] throwables = new Throwable[1];
@@ -0,0 +1,37 @@
/*
* 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;
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()}.
*
* BTW @RunWith works also on abstract super classes.
*/
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
*/
@Nullable
String getFileSuffix(String fileName);
}
@@ -58,9 +58,15 @@ import com.intellij.rt.execution.junit.FileComparisonFailure;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.runners.Parameterized;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTestCase {
private static final Logger LOG = Logger.getInstance("#com.intellij.testFramework.LightCodeInsightTestCase");
@@ -134,6 +140,9 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
@NonNls
@NotNull
protected String getTestDataPath() {
if (myTestDataPath != null) {
return myTestDataPath;
}
return PathManagerEx.getTestDataPath();
}
@@ -646,4 +655,105 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
}
};
}
/**
* file parameterized tests support
* @see FileBasedTestCaseHelper
*/
/**
* @Parameterized.Parameter fields are injected on parameterized test creation.
*/
@Parameterized.Parameter(0)
public String myFileSuffix;
@Parameterized.Parameter(1)
public String myTestDataPath;
@Parameterized.Parameters(name = "{0}")
public static List<Object[]> params() throws Throwable {
return Collections.emptyList();
}
@com.intellij.testFramework.Parameterized.Parameters(name = "{0}")
public static List<Object[]> params(Class<?> klass) throws Throwable{
final LightPlatformCodeInsightTestCase testCase = (LightPlatformCodeInsightTestCase)klass.newInstance();
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);
String testDataPath = testCase.getTestDataPath();
final String testDirPath = testDataPath.replace(File.separatorChar, '/') + path;
File testDir = new File(testDirPath);
final File[] files = testDir.listFiles();
if (files == null) {
fail("Test files not found in " + testDirPath);
}
final List<Object[]> result = new ArrayList<Object[]>();
for (File file : files) {
final String fileSuffix = fileBasedTestCase.getFileSuffix(file.getName());
if (fileSuffix != null) {
result.add(new Object[] {fileSuffix, testDataPath});
}
}
return result;
}
@Override
public String getName() {
if (myFileSuffix != null) {
return "test" + myFileSuffix;
}
return super.getName();
}
@Before
public void before() throws Throwable {
final Throwable[] throwables = new Throwable[1];
invokeTestRunnable(new Runnable() {
@Override
public void run() {
try {
setUp();
}
catch (Throwable e) {
throwables[0] = e;
}
}
});
if (throwables[0] != null) {
throw throwables[0];
}
}
@After
public void after() throws Throwable {
final Throwable[] throwables = new Throwable[1];
invokeTestRunnable(new Runnable() {
@Override
public void run() {
try {
tearDown();
}
catch (Throwable e) {
throwables[0] = e;
}
}
});
if (throwables[0] != null) {
throw throwables[0];
}
}
}