/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/CreateAssertPredicate.java

https://bitbucket.org/nbargnesi/idea · Java · 90 lines · 70 code · 5 blank · 15 comment · 15 complexity · d4edf002115b220d3683513f50783e8c MD5 · raw file

  1. /*
  2. * Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.siyeh.ipp.junit;
  17. import com.intellij.codeInsight.AnnotationUtil;
  18. import com.intellij.openapi.project.Project;
  19. import com.intellij.psi.*;
  20. import com.intellij.psi.search.GlobalSearchScope;
  21. import com.intellij.psi.util.InheritanceUtil;
  22. import com.intellij.psi.util.PsiTreeUtil;
  23. import com.siyeh.ipp.base.PsiElementPredicate;
  24. import org.jetbrains.annotations.NonNls;
  25. class CreateAssertPredicate implements PsiElementPredicate {
  26. public boolean satisfiedBy(PsiElement element) {
  27. if (!(element instanceof PsiExpressionStatement)) {
  28. return false;
  29. }
  30. final PsiExpressionStatement statement =
  31. (PsiExpressionStatement)element;
  32. final PsiExpression expression = statement.getExpression();
  33. final PsiElement parent = expression.getParent();
  34. if (!(parent instanceof PsiExpressionStatement)) {
  35. return false;
  36. }
  37. final PsiType type = expression.getType();
  38. if (!PsiType.BOOLEAN.equals(type)) {
  39. return false;
  40. }
  41. final PsiMethod containingMethod =
  42. PsiTreeUtil.getParentOfType(expression, PsiMethod.class);
  43. return isTestMethod(containingMethod);
  44. }
  45. private static boolean isTestMethod(PsiMethod method) {
  46. if (method == null) {
  47. return false;
  48. }
  49. if (AnnotationUtil.isAnnotated(method, "org.junit.Test", true)) {
  50. return true;
  51. }
  52. if (method.hasModifierProperty(PsiModifier.ABSTRACT) ||
  53. !method.hasModifierProperty(PsiModifier.PUBLIC)) {
  54. return false;
  55. }
  56. final PsiType returnType = method.getReturnType();
  57. if (returnType == null) {
  58. return false;
  59. }
  60. if (!returnType.equals(PsiType.VOID)) {
  61. return false;
  62. }
  63. final PsiParameterList parameterList = method.getParameterList();
  64. final PsiParameter[] parameters = parameterList.getParameters();
  65. if (parameters.length != 0) {
  66. return false;
  67. }
  68. @NonNls final String methodName = method.getName();
  69. if (!methodName.startsWith("test")) {
  70. return false;
  71. }
  72. final PsiClass containingClass = method.getContainingClass();
  73. return isTestClass(containingClass);
  74. }
  75. private static boolean isTestClass(PsiClass aClass) {
  76. if (aClass == null) {
  77. return false;
  78. }
  79. final Project project = aClass.getProject();
  80. final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
  81. final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
  82. final PsiClass ancestorClass = psiFacade.findClass("junit.framework.TestCase", scope);
  83. return InheritanceUtil.isInheritorOrSelf(aClass, ancestorClass, true);
  84. }
  85. }