diff --git a/java/java-impl/src/com/intellij/testIntegration/GenerateAfterClassMethodAction.java b/java/java-impl/src/com/intellij/testIntegration/GenerateAfterClassMethodAction.java new file mode 100644 index 000000000000..ee6d40bfde50 --- /dev/null +++ b/java/java-impl/src/com/intellij/testIntegration/GenerateAfterClassMethodAction.java @@ -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; + } +} diff --git a/java/java-impl/src/com/intellij/testIntegration/GenerateBeforeClassMethodAction.java b/java/java-impl/src/com/intellij/testIntegration/GenerateBeforeClassMethodAction.java new file mode 100644 index 000000000000..9dc2575436ba --- /dev/null +++ b/java/java-impl/src/com/intellij/testIntegration/GenerateBeforeClassMethodAction.java @@ -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; + } +} diff --git a/java/java-impl/src/com/intellij/testIntegration/JavaTestFramework.java b/java/java-impl/src/com/intellij/testIntegration/JavaTestFramework.java index 2d83f324f1f6..0d340d289a5e 100644 --- a/java/java-impl/src/com/intellij/testIntegration/JavaTestFramework.java +++ b/java/java-impl/src/com/intellij/testIntegration/JavaTestFramework.java @@ -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 { diff --git a/java/java-impl/src/com/intellij/testIntegration/TestIntegrationUtils.java b/java/java-impl/src/com/intellij/testIntegration/TestIntegrationUtils.java index 6588c574678f..b24c27577097 100644 --- a/java/java-impl/src/com/intellij/testIntegration/TestIntegrationUtils.java +++ b/java/java-impl/src/com/intellij/testIntegration/TestIntegrationUtils.java @@ -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) { diff --git a/platform/core-api/src/com/intellij/testIntegration/TestFramework.java b/platform/core-api/src/com/intellij/testIntegration/TestFramework.java index 277d510df70e..683afea410a0 100644 --- a/platform/core-api/src/com/intellij/testIntegration/TestFramework.java +++ b/platform/core-api/src/com/intellij/testIntegration/TestFramework.java @@ -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 */ diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index a2411c02e85e..903cb011ccca 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -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 diff --git a/plugins/junit/src/com/intellij/execution/junit/JUnit4Framework.java b/plugins/junit/src/com/intellij/execution/junit/JUnit4Framework.java index 898be926e9cf..841063b52891 100644 --- a/plugins/junit/src/com/intellij/execution/junit/JUnit4Framework.java +++ b/plugins/junit/src/com/intellij/execution/junit/JUnit4Framework.java @@ -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 diff --git a/plugins/junit/src/com/intellij/execution/junit/JUnit5Framework.java b/plugins/junit/src/com/intellij/execution/junit/JUnit5Framework.java index 3c52dcf85153..8430b8799b9e 100644 --- a/plugins/junit/src/com/intellij/execution/junit/JUnit5Framework.java +++ b/plugins/junit/src/com/intellij/execution/junit/JUnit5Framework.java @@ -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() { diff --git a/plugins/junit/src/fileTemplates/code/JUnit4 AfterClass Method.java.ft b/plugins/junit/src/fileTemplates/code/JUnit4 AfterClass Method.java.ft new file mode 100644 index 000000000000..a26be0256904 --- /dev/null +++ b/plugins/junit/src/fileTemplates/code/JUnit4 AfterClass Method.java.ft @@ -0,0 +1,4 @@ +@org.junit.AfterClass +public static void afterClass() throws Exception { + ${BODY} +} \ No newline at end of file diff --git a/plugins/junit/src/fileTemplates/code/JUnit4 AfterClass Method.java.html b/plugins/junit/src/fileTemplates/code/JUnit4 AfterClass Method.java.html new file mode 100644 index 000000000000..cb765bab764a --- /dev/null +++ b/plugins/junit/src/fileTemplates/code/JUnit4 AfterClass Method.java.html @@ -0,0 +1,27 @@ + + + + + + + +
+ This is a template used to create a afterClass method in a JUnit 4 test class. +
+ + + + + + + + + + + + + + +
Predefined variables will take the following values:
${NAME} name of the created method.
${BODY} generated method body.
+ + \ No newline at end of file diff --git a/plugins/junit/src/fileTemplates/code/JUnit4 BeforeClass Method.java.ft b/plugins/junit/src/fileTemplates/code/JUnit4 BeforeClass Method.java.ft new file mode 100644 index 000000000000..44f6549a8343 --- /dev/null +++ b/plugins/junit/src/fileTemplates/code/JUnit4 BeforeClass Method.java.ft @@ -0,0 +1,4 @@ +@org.junit.BeforeClass +public static void beforeClass() throws Exception { + ${BODY} +} \ No newline at end of file diff --git a/plugins/junit/src/fileTemplates/code/JUnit4 BeforeClass Method.java.html b/plugins/junit/src/fileTemplates/code/JUnit4 BeforeClass Method.java.html new file mode 100644 index 000000000000..732b9af4f904 --- /dev/null +++ b/plugins/junit/src/fileTemplates/code/JUnit4 BeforeClass Method.java.html @@ -0,0 +1,27 @@ + + + + + + + +
+ This is a template used to create a beforeClass method in a JUnit 4 test class. +
+ + + + + + + + + + + + + + +
Predefined variables will take the following values:
${NAME} name of the created method.
${BODY} generated method body.
+ + \ No newline at end of file diff --git a/plugins/junit/src/fileTemplates/code/JUnit5 AfterAll Method.java.ft b/plugins/junit/src/fileTemplates/code/JUnit5 AfterAll Method.java.ft new file mode 100644 index 000000000000..9f25d971a772 --- /dev/null +++ b/plugins/junit/src/fileTemplates/code/JUnit5 AfterAll Method.java.ft @@ -0,0 +1,4 @@ +@org.junit.jupiter.api.AfterAll +static void afterAll() { + ${BODY} +} \ No newline at end of file diff --git a/plugins/junit/src/fileTemplates/code/JUnit5 AfterAll Method.java.html b/plugins/junit/src/fileTemplates/code/JUnit5 AfterAll Method.java.html new file mode 100644 index 000000000000..2c7f8c457ddd --- /dev/null +++ b/plugins/junit/src/fileTemplates/code/JUnit5 AfterAll Method.java.html @@ -0,0 +1,27 @@ + + + + + + + +
+ This is a template used to create a afterAll method in a JUnit 5 test class. +
+ + + + + + + + + + + + + + +
Predefined variables will take the following values:
${NAME} name of the created method.
${BODY} generated method body.
+ + \ No newline at end of file diff --git a/plugins/junit/src/fileTemplates/code/JUnit5 BeforeAll Method.java.ft b/plugins/junit/src/fileTemplates/code/JUnit5 BeforeAll Method.java.ft new file mode 100644 index 000000000000..3d6ea2541b0e --- /dev/null +++ b/plugins/junit/src/fileTemplates/code/JUnit5 BeforeAll Method.java.ft @@ -0,0 +1,4 @@ +@org.junit.jupiter.api.BeforeAll +static void beforeAll() { + ${BODY} +} \ No newline at end of file diff --git a/plugins/junit/src/fileTemplates/code/JUnit5 BeforeAll Method.java.html b/plugins/junit/src/fileTemplates/code/JUnit5 BeforeAll Method.java.html new file mode 100644 index 000000000000..45c08890deb3 --- /dev/null +++ b/plugins/junit/src/fileTemplates/code/JUnit5 BeforeAll Method.java.html @@ -0,0 +1,27 @@ + + + + + + + +
+ This is a template used to create a beforeAll method in a JUnit 5 test class. +
+ + + + + + + + + + + + + + +
Predefined variables will take the following values:
${NAME} name of the created method.
${BODY} generated method body.
+ + \ No newline at end of file diff --git a/plugins/testng/resources/fileTemplates/code/TestNG AfterClass Method.java.ft b/plugins/testng/resources/fileTemplates/code/TestNG AfterClass Method.java.ft new file mode 100644 index 000000000000..28e2f60cb3e8 --- /dev/null +++ b/plugins/testng/resources/fileTemplates/code/TestNG AfterClass Method.java.ft @@ -0,0 +1,4 @@ +@org.testng.annotations.AfterClass +public void afterClass() { + ${BODY} +} \ No newline at end of file diff --git a/plugins/testng/resources/fileTemplates/code/TestNG AfterClass Method.java.html b/plugins/testng/resources/fileTemplates/code/TestNG AfterClass Method.java.html new file mode 100644 index 000000000000..527bd8535254 --- /dev/null +++ b/plugins/testng/resources/fileTemplates/code/TestNG AfterClass Method.java.html @@ -0,0 +1,27 @@ + + + + + + + +
+ This is a template used to create a afterClass method in a TestNG test class. +
+ + + + + + + + + + + + + + +
Predefined variables will take the following values:
${NAME} name of the created method.
${BODY} generated method body.
+ + \ No newline at end of file diff --git a/plugins/testng/resources/fileTemplates/code/TestNG BeforeClass Method.java.ft b/plugins/testng/resources/fileTemplates/code/TestNG BeforeClass Method.java.ft new file mode 100644 index 000000000000..2ec07e9d06f1 --- /dev/null +++ b/plugins/testng/resources/fileTemplates/code/TestNG BeforeClass Method.java.ft @@ -0,0 +1,4 @@ +@org.testng.annotations.BeforeClass +public void beforeClass() { + ${BODY} +} \ No newline at end of file diff --git a/plugins/testng/resources/fileTemplates/code/TestNG BeforeClass Method.java.html b/plugins/testng/resources/fileTemplates/code/TestNG BeforeClass Method.java.html new file mode 100644 index 000000000000..834df89a89fd --- /dev/null +++ b/plugins/testng/resources/fileTemplates/code/TestNG BeforeClass Method.java.html @@ -0,0 +1,27 @@ + + + + + + + +
+ This is a template used to create a beforeClass method in a TestNG test class. +
+ + + + + + + + + + + + + + +
Predefined variables will take the following values:
${NAME} name of the created method.
${BODY} generated method body.
+ + \ No newline at end of file diff --git a/plugins/testng/src/com/theoryinpractice/testng/TestNGFramework.java b/plugins/testng/src/com/theoryinpractice/testng/TestNGFramework.java index bcf3e2c1351c..ae5b0645b5f2 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/TestNGFramework.java +++ b/plugins/testng/src/com/theoryinpractice/testng/TestNGFramework.java @@ -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 diff --git a/resources/src/idea/JavaActions.xml b/resources/src/idea/JavaActions.xml index ef2e766b7a16..129d1fc79f26 100644 --- a/resources/src/idea/JavaActions.xml +++ b/resources/src/idea/JavaActions.xml @@ -316,6 +316,8 @@ + +