better description and cleanup

This commit is contained in:
Bas Leijdekkers
2012-08-27 11:21:58 +02:00
parent 2d85641543
commit 075c4981cd
2 changed files with 26 additions and 48 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,8 +33,7 @@ public class AssertsWithoutMessagesInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"asserts.without.messages.display.name");
return InspectionGadgetsBundle.message("asserts.without.messages.display.name");
}
@Override
@@ -46,8 +45,7 @@ public class AssertsWithoutMessagesInspection extends BaseInspection {
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"asserts.without.messages.problem.descriptor");
return InspectionGadgetsBundle.message("asserts.without.messages.problem.descriptor");
}
@Override
@@ -55,11 +53,9 @@ public class AssertsWithoutMessagesInspection extends BaseInspection {
return new AssertionsWithoutMessagesVisitor();
}
private static class AssertionsWithoutMessagesVisitor
extends BaseInspectionVisitor {
private static class AssertionsWithoutMessagesVisitor extends BaseInspectionVisitor {
@NonNls private static final Set<String> s_assertMethods =
new HashSet<String>(8);
@NonNls private static final Set<String> s_assertMethods = new HashSet<String>(8);
static {
s_assertMethods.add("assertTrue");
@@ -73,20 +69,23 @@ public class AssertsWithoutMessagesInspection extends BaseInspection {
}
@Override
public void visitMethodCallExpression(
@NotNull PsiMethodCallExpression expression) {
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
if (!isJUnitAssertion(expression)) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
if (methodName == null || !s_assertMethods.contains(methodName)) {
return;
}
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
final PsiMethod method = (PsiMethod)methodExpression.resolve();
final PsiMethod method = expression.resolveMethod();
if (method == null) {
return;
}
final PsiClass containingClass = method.getContainingClass();
if (!InheritanceUtil.isInheritor(containingClass, "junit.framework.Assert") &&
!InheritanceUtil.isInheritor(containingClass, "org.junit.Assert")) {
return;
}
final PsiParameterList parameterList = method.getParameterList();
@NonNls final String methodName = method.getName();
final int parameterCount = parameterList.getParametersCount();
if (parameterCount < 2 && methodName.startsWith("assert")) {
registerMethodCallError(expression);
@@ -99,40 +98,21 @@ public class AssertsWithoutMessagesInspection extends BaseInspection {
final PsiManager psiManager = expression.getManager();
final Project project = psiManager.getProject();
final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
final PsiType stringType = PsiType.getJavaLangString(psiManager,
scope);
final PsiType stringType = PsiType.getJavaLangString(psiManager, scope);
final PsiParameter[] parameters = parameterList.getParameters();
final PsiType parameterType1 = parameters[0].getType();
if (parameterType1.equals(stringType)) {
if (parameters.length == 2) {
final PsiType parameterType2 = parameters[1].getType();
if (parameterType2.equals(stringType)) {
registerMethodCallError(expression);
}
}
}
else {
if (!parameterType1.equals(stringType)) {
registerMethodCallError(expression);
return;
}
}
private static boolean isJUnitAssertion(
PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
if (!s_assertMethods.contains(methodName)) {
return false;
if (parameters.length != 2) {
return;
}
final PsiMethod method = (PsiMethod)methodExpression.resolve();
if (method == null) {
return false;
final PsiType parameterType2 = parameters[1].getType();
if (!parameterType2.equals(stringType)) {
return;
}
final PsiClass targetClass = method.getContainingClass();
return InheritanceUtil.isInheritor(targetClass,
"junit.framework.Assert") ||
InheritanceUtil.isInheritor(targetClass,
"org.junit.Assert");
registerMethodCallError(expression);
}
}
}
@@ -1,9 +1,7 @@
<html>
<body>
This inspection reports calls to JUnit <b>assertXXX()</b> or
<b>fail()</b> methods that don't
report an error message on assertion failure. Error messages may help clarify the test case's
intent.
This inspection reports calls to JUnit <b>assertXXX()</b> or <b>fail()</b> methods that do not have an error message string argument.
An error message on assertion failure may help clarify the test case's intent.
<!-- tooltip end -->
<p>
<small>Powered by InspectionGadgets</small>