junit 5: initial templates

This commit is contained in:
Anna Kozlova
2016-09-13 16:42:50 +03:00
parent a02afd8c8a
commit da0b06947a
12 changed files with 307 additions and 5 deletions
+1
View File
@@ -40,6 +40,7 @@
<cantBeStatic implementation="com.intellij.execution.junit2.inspection.JUnitCantBeStaticExtension" />
<testFramework implementation="com.intellij.execution.junit.JUnit3Framework"/>
<testFramework implementation="com.intellij.execution.junit.JUnit4Framework" id="junit4"/>
<testFramework implementation="com.intellij.execution.junit.JUnit5Framework" id="junit5"/>
<configurationType implementation="com.intellij.execution.junit.JUnitConfigurationType"/>
<library.dependencyScopeSuggester implementation="com.intellij.execution.junit.JUnitDependencyScopeSuggester"/>
@@ -87,6 +87,12 @@ public class JUnit4Framework extends JavaTestFramework {
@Override
@Nullable
protected PsiMethod findOrCreateSetUpMethod(PsiClass clazz) throws IncorrectOperationException {
String beforeClassAnnotationName = JUnitUtil.BEFORE_CLASS_ANNOTATION_NAME;
String beforeAnnotationName = JUnitUtil.BEFORE_ANNOTATION_NAME;
return findOrCreateSetUpMethod(clazz, beforeClassAnnotationName, beforeAnnotationName);
}
private PsiMethod findOrCreateSetUpMethod(PsiClass clazz, String beforeClassAnnotationName, String beforeAnnotationName) {
PsiMethod method = findSetUpMethod(clazz);
if (method != null) return method;
@@ -96,14 +102,14 @@ public class JUnit4Framework extends JavaTestFramework {
method = createSetUpPatternMethod(factory);
PsiMethod existingMethod = clazz.findMethodBySignature(method, false);
if (existingMethod != null) {
if (AnnotationUtil.isAnnotated(existingMethod, JUnitUtil.BEFORE_CLASS_ANNOTATION_NAME, false)) return existingMethod;
if (AnnotationUtil.isAnnotated(existingMethod, beforeClassAnnotationName, false)) return existingMethod;
int exit = ApplicationManager.getApplication().isUnitTestMode() ?
Messages.OK :
Messages.showOkCancelDialog("Method setUp already exist but is not annotated as @Before. Annotate?",
CommonBundle.getWarningTitle(),
Messages.getWarningIcon());
Messages.showOkCancelDialog("Method setUp already exist but is not annotated as @Before. Annotate?",
CommonBundle.getWarningTitle(),
Messages.getWarningIcon());
if (exit == Messages.OK) {
new AddAnnotationFix(JUnitUtil.BEFORE_ANNOTATION_NAME, existingMethod).invoke(existingMethod.getProject(), null, existingMethod.getContainingFile());
new AddAnnotationFix(beforeAnnotationName, existingMethod).invoke(existingMethod.getProject(), null, existingMethod.getContainingFile());
return existingMethod;
}
}
@@ -0,0 +1,158 @@
/*
* Copyright 2000-2016 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.execution.junit;
import com.intellij.CommonBundle;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.intention.AddAnnotationFix;
import com.intellij.execution.junit2.info.MethodLocation;
import com.intellij.icons.AllIcons;
import com.intellij.ide.fileTemplates.FileTemplateDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.roots.ExternalLibraryDescriptor;
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;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
public class JUnit5Framework extends JavaTestFramework {
@NotNull
public String getName() {
return "JUnit5";
}
@NotNull
@Override
public Icon getIcon() {
return AllIcons.RunConfigurations.Junit;
}
protected String getMarkerClassFQName() {
return JUnitUtil.TEST5_ANNOTATION;
}
@Nullable
@Override
public ExternalLibraryDescriptor getFrameworkLibraryDescriptor() {
return JUnitExternalLibraryDescriptor.JUNIT5;
}
@Nullable
public String getDefaultSuperClass() {
return null;
}
public boolean isTestClass(PsiClass clazz, boolean canBePotential) {
if (canBePotential) return isUnderTestSources(clazz);
return JUnitUtil.isJUnit4TestClass(clazz);
}
@Nullable
@Override
protected PsiMethod findSetUpMethod(@NotNull PsiClass clazz) {
for (PsiMethod each : clazz.getMethods()) {
if (AnnotationUtil.isAnnotated(each, JUnitUtil.BEFORE_EACH_ANNOTATION_NAME, false)) return each;
}
return null;
}
@Nullable
@Override
protected PsiMethod findTearDownMethod(@NotNull PsiClass clazz) {
for (PsiMethod each : clazz.getMethods()) {
if (AnnotationUtil.isAnnotated(each, JUnitUtil.AFTER_EACH_ANNOTATION_NAME, false)) return each;
}
return null;
}
@Override
@Nullable
protected PsiMethod findOrCreateSetUpMethod(PsiClass clazz) throws IncorrectOperationException {
PsiMethod method = findSetUpMethod(clazz);
if (method != null) return method;
PsiManager manager = clazz.getManager();
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
method = createSetUpPatternMethod(factory);
PsiMethod existingMethod = clazz.findMethodBySignature(method, false);
if (existingMethod != null) {
if (AnnotationUtil.isAnnotated(existingMethod, JUnitUtil.BEFORE_ALL_ANNOTATION_NAME, false)) return existingMethod;
int exit = ApplicationManager.getApplication().isUnitTestMode() ?
Messages.OK :
Messages.showOkCancelDialog("Method setUp already exist but is not annotated as @BeforeEach. Annotate?",
CommonBundle.getWarningTitle(),
Messages.getWarningIcon());
if (exit == Messages.OK) {
new AddAnnotationFix(JUnitUtil.BEFORE_EACH_ANNOTATION_NAME, existingMethod).invoke(existingMethod.getProject(), null, existingMethod.getContainingFile());
return existingMethod;
}
}
final PsiMethod testMethod = JUnitUtil.findFirstTestMethod(clazz);
if (testMethod != null) {
method = (PsiMethod)clazz.addBefore(method, testMethod);
} else {
method = (PsiMethod)clazz.add(method);
}
JavaCodeStyleManager.getInstance(manager.getProject()).shortenClassReferences(method);
return method;
}
@Override
public boolean isIgnoredMethod(PsiElement element) {
final PsiMethod testMethod = element instanceof PsiMethod ? JUnitUtil.getTestMethod(element) : null;
return testMethod != null && AnnotationUtil.isAnnotated(testMethod, JUnitUtil.IGNORE_ANNOTATION, false);
}
@Override
public boolean isTestMethod(PsiElement element) {
return element instanceof PsiMethod && JUnitUtil.getTestMethod(element) != null;
}
@Override
public boolean isTestMethod(PsiMethod method, PsiClass myClass) {
return JUnitUtil.isTestMethod(MethodLocation.elementInClass(method, myClass));
}
public FileTemplateDescriptor getSetUpMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 SetUp Method.java");
}
public FileTemplateDescriptor getTearDownMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 TearDown Method.java");
}
public FileTemplateDescriptor getTestMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 Test Method.java");
}
@Override
public char getMnemonic() {
return '5';
}
@Override
public FileTemplateDescriptor getTestClassFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 Test Class.java");
}
}
@@ -17,8 +17,11 @@ package com.intellij.execution.junit;
import com.intellij.openapi.projectRoots.ex.JavaSdkUtil;
import com.intellij.openapi.roots.ExternalLibraryDescriptor;
import com.intellij.util.PathUtil;
import com.intellij.util.ReflectionUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -40,6 +43,14 @@ public abstract class JUnitExternalLibraryDescriptor extends ExternalLibraryDesc
return JavaSdkUtil.getJUnit4JarPaths();
}
};
public static final ExternalLibraryDescriptor JUNIT5 = new ExternalLibraryDescriptor("org.junit.jupiter", "junit-jupiter-api", "5.0.0-M2", null) {
@NotNull
@Override
public List<String> getLibraryClassesRoots() {
return Arrays.asList(PathUtil.getJarPathForClass(ReflectionUtil.forName("org.junit.jupiter.api.Test")),
PathUtil.getJarPathForClass(ReflectionUtil.forName("org.opentest4j.AssertionFailedError")));
}
};
private final String myVersion;
public JUnitExternalLibraryDescriptor(String version) {
@@ -0,0 +1,4 @@
@org.junit.jupiter.api.BeforeEach
void setUp() {
${BODY}
}
@@ -0,0 +1,26 @@
<html>
<body>
<table width="100%" 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 setUp method in a JUnit 5 test class.</font>
</td>
</tr>
</table>
<table width="100%" 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"><b>${NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" 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"><b>${BODY}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">generated method body.</font></td>
</tr>
</table>
</body>
</html>
@@ -0,0 +1,4 @@
@org.junit.jupiter.api.AfterEach
void tearDown() {
${BODY}
}
@@ -0,0 +1,26 @@
<html>
<body>
<table width="100%" 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 tearDown method in a JUnit 5 test class.</font>
</td>
</tr>
</table>
<table width="100%" 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"><b>${NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" 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"><b>${BODY}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">generated method body.</font></td>
</tr>
</table>
</body>
</html>
@@ -0,0 +1,5 @@
import static org.junit.jupiter.api.Assertions.*;
#parse("File Header.java")
class ${NAME} {
${BODY}
}
@@ -0,0 +1,31 @@
<html>
<body>
<table width="100%" 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 JUnit 5 test class.</font>
</td>
</tr>
</table>
<table width="100%" 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"><b>${NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">name of the created class.</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CLASS_NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">name of the tested class.</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${BODY}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">generated class body.</font></td>
</tr>
</table>
</body>
</html>
@@ -0,0 +1,4 @@
@org.junit.jupiter.api.Test
void ${NAME}() {
${BODY}
}
@@ -0,0 +1,26 @@
<html>
<body>
<table width="100%" 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 test method in a JUnit 5 test class.</font>
</td>
</tr>
</table>
<table width="100%" 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"><b>${NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" 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"><b>${BODY}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">generated method body.</font></td>
</tr>
</table>
</body>
</html>