From 075c4981cd3f7b9fd55b88d464a40a8692fec0db Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 27 Aug 2012 11:21:58 +0200 Subject: [PATCH] better description and cleanup --- .../AssertsWithoutMessagesInspection.java | 68 +++++++------------ .../AssertsWithoutMessages.html | 6 +- 2 files changed, 26 insertions(+), 48 deletions(-) diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/AssertsWithoutMessagesInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/AssertsWithoutMessagesInspection.java index 5b4db4099c34..2b05a972e67e 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/AssertsWithoutMessagesInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/AssertsWithoutMessagesInspection.java @@ -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 s_assertMethods = - new HashSet(8); + @NonNls private static final Set s_assertMethods = new HashSet(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); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/AssertsWithoutMessages.html b/plugins/InspectionGadgets/src/inspectionDescriptions/AssertsWithoutMessages.html index 32d7b0725db0..bdc0e5b2f78e 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/AssertsWithoutMessages.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/AssertsWithoutMessages.html @@ -1,9 +1,7 @@ -This inspection reports calls to JUnit assertXXX() or -fail() 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 assertXXX() or fail() methods that do not have an error message string argument. +An error message on assertion failure may help clarify the test case's intent.

Powered by InspectionGadgets