junit: generate parameters method for @Parameterized test class

This commit is contained in:
anna
2012-01-16 12:37:37 +01:00
parent 9c19620366
commit c9f46f788f
8 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2000-2012 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.testIntegration;
import com.intellij.psi.PsiClass;
public class GenerateDataMethodAction extends BaseGenerateTestSupportMethodAction {
public GenerateDataMethodAction() {
super(TestIntegrationUtils.MethodKind.DATA);
}
@Override
protected boolean isValidFor(PsiClass targetClass, TestFramework framework) {
if (framework instanceof JavaTestFramework && super.isValidFor(targetClass, framework)) {
return ((JavaTestFramework)framework).isParameterized(targetClass) && ((JavaTestFramework)framework).findParametersMethod(
targetClass) == null;
}
return false;
}
}

View File

@@ -15,6 +15,7 @@
*/
package com.intellij.testIntegration;
import com.intellij.ide.fileTemplates.FileTemplateDescriptor;
import com.intellij.lang.Language;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.module.Module;
@@ -89,4 +90,18 @@ public abstract class JavaTestFramework implements TestFramework {
@Nullable
protected abstract PsiMethod findOrCreateSetUpMethod(PsiClass clazz) throws IncorrectOperationException;
public boolean isParameterized(PsiClass clazz) {
return false;
}
@Nullable
public PsiMethod findParametersMethod(PsiClass clazz) {
return null;
}
@Nullable
public FileTemplateDescriptor getParametersMethodFileTemplateDescriptor() {
return null;
}
}

View File

@@ -61,6 +61,15 @@ public class TestIntegrationUtils {
public FileTemplateDescriptor getFileTemplateDescriptor(@NotNull TestFramework framework) {
return framework.getTestMethodFileTemplateDescriptor();
}
},
DATA("data") {
@Override
public FileTemplateDescriptor getFileTemplateDescriptor(@NotNull TestFramework framework) {
if (framework instanceof JavaTestFramework) {
return ((JavaTestFramework)framework).getParametersMethodFileTemplateDescriptor();
}
return null;
}
};
private String myDefaultName;

View File

@@ -159,6 +159,7 @@ group.GenerateGroup.text=_Generate
action.GenerateTestMethod.text=Test Method
action.GenerateSetUpMethod.text=SetUp Method
action.GenerateTearDownMethod.text=TearDown Method
action.GenerateDataMethod.text=Parameters Method
action.GenerateConstructor.text=Constructor
action.GenerateGetter.text=Getter
action.GenerateSetter.text=Setter

View File

@@ -25,6 +25,7 @@ import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiUtil;
import com.intellij.testIntegration.JavaTestFramework;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
@@ -137,4 +138,37 @@ public class JUnit4Framework extends JavaTestFramework {
public FileTemplateDescriptor getTestMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit4 Test Method.java");
}
@Override
public FileTemplateDescriptor getParametersMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit4 Parameters Method.java");
}
@Override
public boolean isParameterized(PsiClass clazz) {
final PsiAnnotation annotation = AnnotationUtil.findAnnotation(clazz, "org.junit.runner.RunWith");
if (annotation != null) {
final PsiAnnotationMemberValue value = annotation.findAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
if (value instanceof PsiClassObjectAccessExpression) {
final PsiTypeElement operand = ((PsiClassObjectAccessExpression)value).getOperand();
final PsiClass psiClass = PsiUtil.resolveClassInClassTypeOnly(operand.getType());
return psiClass != null && "org.junit.runners.Parameterized".equals(psiClass.getQualifiedName());
}
}
return false;
}
@Override
public PsiMethod findParametersMethod(PsiClass clazz) {
final PsiMethod[] methods = clazz.getAllMethods();
for (PsiMethod method : methods) {
if (method.hasModifierProperty(PsiModifier.PUBLIC) &&
method.hasModifierProperty(PsiModifier.STATIC) &&
AnnotationUtil.isAnnotated(method, "org.junit.runners.Parameterized.Parameters", false)) {
//todo check return value
return method;
}
}
return null;
}
}

View File

@@ -0,0 +1,4 @@
@org.junit.runners.Parameterized.Parameters
public static java.util.Collection<Object[]> data() {
${BODY}
}

View File

@@ -0,0 +1,26 @@
<html>
<body>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td colspan="3"><font face="verdana" size="-1">
This is a template used by <b>IDEA</b> to create a data method in a JUnit 4 Parameterized test class.</font>
</td>
</tr>
</table>
<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td colspan="3"><font face="verdana" size="-1">Predefined variables will take the following values:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2" color="#7F0000"><b><i>${NAME}</i></b></font></nobr></td>
<td width="10">&nbsp;</td>
<td valign="top"><font face="verdana" size="-1">name of the created method.</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2" color="#7F0000"><b><i>${BODY}</i></b></font></nobr></td>
<td width="10">&nbsp;</td>
<td valign="top"><font face="verdana" size="-1">generated method body.</font></td>
</tr>
</table>
</body>
</html>

View File

@@ -4,6 +4,7 @@
<action id="GenerateTestMethod" class="com.intellij.testIntegration.GenerateTestMethodAction"/>
<action id="GenerateSetUpMethod" class="com.intellij.testIntegration.GenerateSetUpMethodAction"/>
<action id="GenerateTearDownMethod" class="com.intellij.testIntegration.GenerateTearDownMethodAction"/>
<action id="GenerateDataMethod" class="com.intellij.testIntegration.GenerateDataMethodAction"/>
<separator/>
<action id="GenerateConstructor" class="com.intellij.codeInsight.generation.actions.GenerateConstructorAction"/>
<action id="GenerateGetter" class="com.intellij.codeInsight.generation.actions.GenerateGetterAction"/>