test frameworks: provide beforeClass/afterClass methods generation options (IDEA-128201)

GitOrigin-RevId: a37012fed009329af87089ff701d06cbc6e81209
This commit is contained in:
hisaaki.sioiri
2019-06-28 22:56:18 +02:00
committed by intellij-monorepo-bot
parent ffe72b0bb5
commit d4ff970ecb
22 changed files with 361 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.testIntegration;
import com.intellij.psi.PsiClass;
public class GenerateAfterClassMethodAction extends BaseGenerateTestSupportMethodAction {
public GenerateAfterClassMethodAction() {
super(TestIntegrationUtils.MethodKind.AFTER_CLASS);
}
@Override
protected boolean isValidFor(PsiClass targetClass, TestFramework framework) {
return super.isValidFor(targetClass, framework) && framework.findAfterClassMethod(targetClass) == null;
}
}

View File

@@ -0,0 +1,15 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.testIntegration;
import com.intellij.psi.PsiClass;
public class GenerateBeforeClassMethodAction extends BaseGenerateTestSupportMethodAction {
public GenerateBeforeClassMethodAction() {
super(TestIntegrationUtils.MethodKind.BEFORE_CLASS);
}
@Override
protected boolean isValidFor(PsiClass targetClass, TestFramework framework) {
return super.isValidFor(targetClass, framework) && framework.findBeforeClassMethod(targetClass) == null;
}
}

View File

@@ -85,6 +85,28 @@ public abstract class JavaTestFramework implements TestFramework {
@Nullable
protected abstract PsiMethod findTearDownMethod(@NotNull PsiClass clazz);
@Nullable
@Override
public PsiElement findBeforeClassMethod(@NotNull PsiElement clazz) {
return clazz instanceof PsiClass ? findBeforeClassMethod((PsiClass)clazz) : null;
}
@Nullable
protected PsiMethod findBeforeClassMethod(@NotNull PsiClass clazz) {
return null;
}
@Nullable
@Override
public PsiElement findAfterClassMethod(@NotNull PsiElement clazz) {
return clazz instanceof PsiClass ? findAfterClassMethod((PsiClass)clazz) : null;
}
@Nullable
protected PsiMethod findAfterClassMethod(@NotNull PsiClass clazz) {
return null;
}
@Override
public PsiElement findOrCreateSetUpMethod(@NotNull PsiElement clazz) throws IncorrectOperationException {

View File

@@ -40,12 +40,24 @@ public class TestIntegrationUtils {
return framework.getSetUpMethodFileTemplateDescriptor();
}
},
BEFORE_CLASS("beforeClass") {
@Override
public FileTemplateDescriptor getFileTemplateDescriptor(@NotNull TestFramework framework) {
return framework.getBeforeClassMethodFileTemplateDescriptor();
}
},
TEAR_DOWN("tearDown") {
@Override
public FileTemplateDescriptor getFileTemplateDescriptor(@NotNull TestFramework framework) {
return framework.getTearDownMethodFileTemplateDescriptor();
}
},
AFTER_CLASS("afterClass") {
@Override
public FileTemplateDescriptor getFileTemplateDescriptor(@NotNull TestFramework framework) {
return framework.getAfterClassMethodFileTemplateDescriptor();
}
},
TEST("test") {
@Override
public FileTemplateDescriptor getFileTemplateDescriptor(@NotNull TestFramework framework) {

View File

@@ -64,6 +64,24 @@ public interface TestFramework {
@NotNull
FileTemplateDescriptor getTestMethodFileTemplateDescriptor();
@Nullable
default PsiElement findBeforeClassMethod(@NotNull PsiElement clazz) {
return null;
}
default FileTemplateDescriptor getBeforeClassMethodFileTemplateDescriptor() {
return null;
}
@Nullable
default PsiElement findAfterClassMethod(@NotNull PsiElement clazz) {
return null;
}
default FileTemplateDescriptor getAfterClassMethodFileTemplateDescriptor() {
return null;
}
/**
* should be checked for abstract method error
*/

View File

@@ -206,7 +206,9 @@ action.FullyExpandTreeNode.text=Fully Expand Tree Node
group.GenerateGroup.text=_Generate
action.GenerateTestMethod.text=Test Method
action.GenerateSetUpMethod.text=SetUp Method
action.GenerateBeforeClassMethod.text=BeforeClass Method
action.GenerateTearDownMethod.text=TearDown Method
action.GenerateAfterClassMethod.text=AfterClass Method
action.GenerateDataMethod.text=Parameters Method
action.GenerateConstructor.text=Constructor
action.GenerateConstructor.description=Generate constructor

View File

@@ -64,6 +64,16 @@ public class JUnit4Framework extends JavaTestFramework {
return null;
}
@Nullable
@Override
protected PsiMethod findBeforeClassMethod(@NotNull PsiClass clazz) {
for (PsiMethod each : clazz.getMethods()) {
if (each.hasModifierProperty(PsiModifier.STATIC)
&& AnnotationUtil.isAnnotated(each, JUnitUtil.BEFORE_ANNOTATION_NAME, 0)) return each;
}
return null;
}
@Nullable
@Override
protected PsiMethod findTearDownMethod(@NotNull PsiClass clazz) {
@@ -73,6 +83,16 @@ public class JUnit4Framework extends JavaTestFramework {
return null;
}
@Nullable
@Override
protected PsiMethod findAfterClassMethod(@NotNull PsiClass clazz) {
for (PsiMethod each : clazz.getMethods()) {
if (each.hasModifierProperty(PsiModifier.STATIC)
&& AnnotationUtil.isAnnotated(each, JUnitUtil.AFTER_CLASS_ANNOTATION_NAME, 0)) return each;
}
return null;
}
@Override
@Nullable
protected PsiMethod findOrCreateSetUpMethod(PsiClass clazz) throws IncorrectOperationException {
@@ -139,10 +159,20 @@ public class JUnit4Framework extends JavaTestFramework {
return new FileTemplateDescriptor("JUnit4 SetUp Method.java");
}
@Override
public FileTemplateDescriptor getBeforeClassMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit4 BeforeClass Method.java");
}
@Override
public FileTemplateDescriptor getTearDownMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit4 TearDown Method.java");
}
@Override
public FileTemplateDescriptor getAfterClassMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit4 AfterClass Method.java");
}
@Override
@NotNull

View File

@@ -64,6 +64,16 @@ public class JUnit5Framework extends JavaTestFramework {
return null;
}
@Nullable
@Override
protected PsiMethod findBeforeClassMethod(@NotNull PsiClass clazz) {
for (PsiMethod each : clazz.getMethods()) {
if (each.hasModifierProperty(PsiModifier.STATIC)
&& AnnotationUtil.isAnnotated(each, JUnitUtil.BEFORE_ALL_ANNOTATION_NAME, 0)) return each;
}
return null;
}
@Nullable
@Override
protected PsiMethod findTearDownMethod(@NotNull PsiClass clazz) {
@@ -72,6 +82,16 @@ public class JUnit5Framework extends JavaTestFramework {
}
return null;
}
@Nullable
@Override
protected PsiMethod findAfterClassMethod(@NotNull PsiClass clazz) {
for (PsiMethod each : clazz.getMethods()) {
if (each.hasModifierProperty(PsiModifier.STATIC)
&& AnnotationUtil.isAnnotated(each, JUnitUtil.AFTER_ALL_ANNOTATION_NAME, 0)) return each;
}
return null;
}
@Override
@Nullable
@@ -137,12 +157,22 @@ public class JUnit5Framework extends JavaTestFramework {
public FileTemplateDescriptor getSetUpMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 SetUp Method.java");
}
@Override
public FileTemplateDescriptor getBeforeClassMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 BeforeAll Method.java");
}
@Override
public FileTemplateDescriptor getTearDownMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 TearDown Method.java");
}
@Override
public FileTemplateDescriptor getAfterClassMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 AfterAll Method.java");
}
@Override
@NotNull
public FileTemplateDescriptor getTestMethodFileTemplateDescriptor() {

View File

@@ -0,0 +1,4 @@
@org.junit.AfterClass
public static void afterClass() throws Exception {
${BODY}
}

View File

@@ -0,0 +1,27 @@
<!-- Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<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 to create a afterClass method in a JUnit 4 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="#f45252"><b>${NAME}</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="#f45252"><b>${BODY}</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

@@ -0,0 +1,4 @@
@org.junit.BeforeClass
public static void beforeClass() throws Exception {
${BODY}
}

View File

@@ -0,0 +1,27 @@
<!-- Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<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 to create a beforeClass method in a JUnit 4 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="#f45252"><b>${NAME}</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="#f45252"><b>${BODY}</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

@@ -0,0 +1,4 @@
@org.junit.jupiter.api.AfterAll
static void afterAll() {
${BODY}
}

View File

@@ -0,0 +1,27 @@
<!-- Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<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 to create a afterAll method in a JUnit 5 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="#f45252"><b>${NAME}</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="#f45252"><b>${BODY}</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

@@ -0,0 +1,4 @@
@org.junit.jupiter.api.BeforeAll
static void beforeAll() {
${BODY}
}

View File

@@ -0,0 +1,27 @@
<!-- Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<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 to create a beforeAll method in a JUnit 5 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="#f45252"><b>${NAME}</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="#f45252"><b>${BODY}</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

@@ -0,0 +1,4 @@
@org.testng.annotations.AfterClass
public void afterClass() {
${BODY}
}

View File

@@ -0,0 +1,27 @@
<!-- Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<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 to create a afterClass method in a TestNG 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="#f45252"><b>${NAME}</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="#f45252"><b>${BODY}</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

@@ -0,0 +1,4 @@
@org.testng.annotations.BeforeClass
public void beforeClass() {
${BODY}
}

View File

@@ -0,0 +1,27 @@
<!-- Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<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 to create a beforeClass method in a TestNG 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="#f45252"><b>${NAME}</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="#f45252"><b>${BODY}</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

@@ -76,6 +76,15 @@ public class TestNGFramework extends JavaTestFramework {
return null;
}
@Nullable
@Override
protected PsiMethod findBeforeClassMethod(@NotNull PsiClass clazz) {
for (PsiMethod each : clazz.getMethods()) {
if (AnnotationUtil.isAnnotated(each, "org.testng.annotations.BeforeClass", 0)) return each;
}
return null;
}
@Nullable
@Override
protected PsiMethod findTearDownMethod(@NotNull PsiClass clazz) {
@@ -85,6 +94,16 @@ public class TestNGFramework extends JavaTestFramework {
return null;
}
@Nullable
@Override
protected PsiMethod findAfterClassMethod(@NotNull PsiClass clazz) {
for (PsiMethod each : clazz.getMethods()) {
if (AnnotationUtil.isAnnotated(each, "org.testng.annotations.AfterClass", 0)) return each;
}
return null;
}
@Override
protected PsiMethod findOrCreateSetUpMethod(PsiClass clazz) throws IncorrectOperationException {
PsiMethod method = findSetUpMethod(clazz);
@@ -178,11 +197,21 @@ public class TestNGFramework extends JavaTestFramework {
public FileTemplateDescriptor getSetUpMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("TestNG SetUp Method.java");
}
@Override
public FileTemplateDescriptor getBeforeClassMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("TestNG BeforeClass Method.java");
}
@Override
public FileTemplateDescriptor getTearDownMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("TestNG TearDown Method.java");
}
@Override
public FileTemplateDescriptor getAfterClassMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("TestNG AfterClass Method.java");
}
@Override
@NotNull

View File

@@ -316,6 +316,8 @@
<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="GenerateBeforeClassMethod" class="com.intellij.testIntegration.GenerateBeforeClassMethodAction"/>
<action id="GenerateAfterClassMethod" class="com.intellij.testIntegration.GenerateAfterClassMethodAction"/>
<action id="GenerateDataMethod" class="com.intellij.testIntegration.GenerateDataMethodAction"/>
<separator/>
<action id="GenerateConstructor" class="com.intellij.codeInsight.generation.actions.GenerateConstructorAction"/>