diff options
Diffstat (limited to 'plugins/testng/src/com/theoryinpractice/testng/inspection')
2 files changed, 142 insertions, 9 deletions
diff --git a/plugins/testng/src/com/theoryinpractice/testng/inspection/DependsOnMethodInspection.java b/plugins/testng/src/com/theoryinpractice/testng/inspection/DependsOnMethodInspection.java index b8cf78a83d5d..aeda466432a2 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/inspection/DependsOnMethodInspection.java +++ b/plugins/testng/src/com/theoryinpractice/testng/inspection/DependsOnMethodInspection.java @@ -18,14 +18,19 @@ package com.theoryinpractice.testng.inspection; import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInspection.*; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.Condition; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.HashSet; import com.theoryinpractice.testng.util.TestNGUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -35,7 +40,7 @@ import java.util.regex.Pattern; public class DependsOnMethodInspection extends BaseJavaLocalInspectionTool { private static final Logger LOGGER = Logger.getInstance("TestNG Runner"); - private static final Pattern PATTERN = Pattern.compile("\"([a-zA-Z1-9_\\(\\)]*)\""); + private static final Pattern PATTERN = Pattern.compile("\"([a-zA-Z1-9_\\(\\)\\*]*)\""); @NotNull @Override @@ -63,17 +68,13 @@ public class DependsOnMethodInspection extends BaseJavaLocalInspectionTool @Nullable public ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) { - //LOGGER.info("Looking for dependsOnMethods problems in " + psiClass.getName()); - - if (!psiClass.getContainingFile().isWritable()) return null; - PsiAnnotation[] annotations = TestNGUtil.getTestNGAnnotations(psiClass); if(annotations.length == 0) return ProblemDescriptor.EMPTY_ARRAY; List<ProblemDescriptor> problemDescriptors = new ArrayList<ProblemDescriptor>(); for (PsiAnnotation annotation : annotations) { final PsiAnnotationMemberValue value = annotation.findDeclaredAttributeValue("dependsOnMethods"); - if (value != null) { + if (value != null && !TestNGUtil.isDisabled(annotation)) { String text = value.getText(); if (value instanceof PsiReferenceExpression) { final PsiElement resolve = ((PsiReferenceExpression)value).resolve(); @@ -84,10 +85,25 @@ public class DependsOnMethodInspection extends BaseJavaLocalInspectionTool } } } - Matcher matcher = PATTERN.matcher(text); + final Set<String> names = new HashSet<String>(); + final Matcher matcher = PATTERN.matcher(text); + int idx = 0; while (matcher.find()) { - String methodName = matcher.group(1); + String methodName = matcher.group(1); + if (!names.add(methodName)) { + PsiAnnotationMemberValue element2Highlight = value; + if (value instanceof PsiArrayInitializerMemberValue) { + final PsiAnnotationMemberValue[] initializers = ((PsiArrayInitializerMemberValue)value).getInitializers(); + if (idx < initializers.length) { + element2Highlight = initializers[idx]; + } + } + problemDescriptors.add(manager.createProblemDescriptor(element2Highlight, "Duplicated method name: " + methodName, + (LocalQuickFix)null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, + isOnTheFly)); + } checkMethodNameDependency(manager, psiClass, methodName, value, problemDescriptors, isOnTheFly); + idx++; } } } @@ -111,7 +127,19 @@ public class DependsOnMethodInspection extends BaseJavaLocalInspectionTool } else { final String configAnnotation = TestNGUtil.getConfigAnnotation(PsiTreeUtil.getParentOfType(value, PsiMethod.class)); - PsiMethod[] foundMethods = psiClass.findMethodsByName(methodName, true); + final PsiMethod[] foundMethods; + if (methodName.endsWith("*")) { + final String methodNameMask = StringUtil.trimEnd(methodName, "*"); + final List<PsiMethod> methods = ContainerUtil.filter(psiClass.getMethods(), new Condition<PsiMethod>() { + @Override + public boolean value(PsiMethod method) { + return method.getName().startsWith(methodNameMask); + } + }); + foundMethods = methods.toArray(new PsiMethod[methods.size()]); + } else { + foundMethods = psiClass.findMethodsByName(methodName, true); + } if (foundMethods.length == 0) { LOGGER.debug("dependsOnMethods method doesn't exist:" + methodName); ProblemDescriptor descriptor = manager.createProblemDescriptor(value, diff --git a/plugins/testng/src/com/theoryinpractice/testng/inspection/TestNGMethodNamingConventionInspection.java b/plugins/testng/src/com/theoryinpractice/testng/inspection/TestNGMethodNamingConventionInspection.java new file mode 100644 index 000000000000..2cc894faf064 --- /dev/null +++ b/plugins/testng/src/com/theoryinpractice/testng/inspection/TestNGMethodNamingConventionInspection.java @@ -0,0 +1,105 @@ +/* + * 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.theoryinpractice.testng.inspection; + +import com.intellij.psi.PsiIdentifier; +import com.intellij.psi.PsiMethod; +import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.RenameFix; +import com.siyeh.ig.naming.ConventionInspection; +import com.siyeh.ig.psiutils.LibraryUtil; +import com.siyeh.ig.psiutils.MethodUtils; +import com.theoryinpractice.testng.util.TestNGUtil; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class TestNGMethodNamingConventionInspection extends ConventionInspection { + @Nls + @NotNull + @Override + public String getDisplayName() { + return "TestNG test method naming convention"; + } + + @NotNull + @Override + protected String buildErrorString(Object... infos) { + final String methodName = (String)infos[0]; + final int length = methodName.length(); + if (length < getMinLength()) { + return "TestNG test method name <code>#ref</code> is too short (" + length + " < " + getMinLength() + ") #loc"; + } + else if (length > getMaxLength()) { + return "TestNG test method name <code>#ref</code> is too long (" + length + " > " + getMaxLength() + ") #loc"; + } + return "JUnit4 test method name <code>#ref</code> doesn't match regex '{0}' #loc"; + } + + @Override + protected String getDefaultRegex() { + return "[a-z][A-Za-z_\\d]*"; + } + + @Override + protected int getDefaultMinLength() { + return 4; + } + + @Override + protected int getDefaultMaxLength() { + return 64; + } + + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + return new RenameFix(); + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new TestNGMethodNamingConventionVisitor(); + } + + private class TestNGMethodNamingConventionVisitor extends BaseInspectionVisitor { + + @Override + public void visitMethod(PsiMethod method) { + super.visitMethod(method); + if (!TestNGUtil.hasTest(method)) { + return; + } + final PsiIdentifier nameIdentifier = method.getNameIdentifier(); + if (nameIdentifier == null) { + return; + } + final String name = method.getName(); + if (isValid(name)) { + return; + } + if (!isOnTheFly() && MethodUtils.hasSuper(method)) { + return; + } + if (LibraryUtil.isOverrideOfLibraryMethod(method)) { + return; + } + registerMethodError(method, name); + } + } +} |