part 2 of IDEA-106425 (Ignore some 'Code maturity issues'-inspections for test code)

This commit is contained in:
Bas Leijdekkers
2013-11-04 20:40:04 +01:00
parent d7a7025354
commit 5d2c8a92a7
3 changed files with 77 additions and 6 deletions
@@ -258,7 +258,7 @@ use.obsolete.collection.type.ignore.library.arguments.option=Ignore obsolete col
inspection.suppression.annotation.display.name=Inspection suppression annotation
inspection.suppression.annotation.problem.descriptor=Inspection suppression annotation <code>#ref</code> #loc
use.system.out.err.display.name=Use of System.out or System.err
use.system.out.err.problem.descriptor=Uses of 'System.out' and 'System.err' should probably be replaced with more robust logging #loc
use.system.out.err.problem.descriptor=Uses of <code>#ref</code> should probably be replaced with more robust logging #loc
dumpstack.call.display.name=Call to 'Thread.dumpStack()'
dumpstack.call.problem.descriptor=Call to <code>Thread.#ref()</code> should probably be replaced with more robust logging #loc
printstacktrace.call.display.name=Call to 'printStackTrace()'
@@ -2050,4 +2050,4 @@ package.info.without.package.quickfix=add ''package {0};''
package.info.without.package.family.quickfix=add package statement
auto.closeable.resource.display.name=AutoCloseable used without 'try'-with-resources
auto.closeable.resource.problem.descriptor=''{0}'' used without ''try''-with-resources statement
auto.closeable.resource.returned.option=Ignore AutoCloseable instances returned from method calls
auto.closeable.resource.returned.option=Ignore AutoCloseable instances returned from method calls
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -15,6 +15,7 @@
*/
package com.siyeh.ig.maturity;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
@@ -23,10 +24,17 @@ import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TestUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
public class SystemOutErrInspection extends BaseInspection {
@SuppressWarnings("PublicField")
public boolean ignoreInTestCode = false;
@Override
@NotNull
public String getID() {
@@ -47,16 +55,21 @@ public class SystemOutErrInspection extends BaseInspection {
"use.system.out.err.problem.descriptor");
}
@Nullable
@Override
public JComponent createOptionsPanel() {
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("ignore.in.test.code"), this, "ignoreInTestCode");
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new SystemOutErrVisitor();
}
private static class SystemOutErrVisitor extends BaseInspectionVisitor {
private class SystemOutErrVisitor extends BaseInspectionVisitor {
@Override
public void visitReferenceExpression(
@NotNull PsiReferenceExpression expression) {
public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
super.visitReferenceExpression(expression);
final String name = expression.getReferenceName();
if (!HardcodedMethodConstants.OUT.equals(name) &&
@@ -76,6 +89,9 @@ public class SystemOutErrInspection extends BaseInspection {
if (!"java.lang.System".equals(className)) {
return;
}
if (ignoreInTestCode && TestUtils.isInTestCode(expression)) {
return;
}
registerError(expression);
}
}
@@ -0,0 +1,55 @@
/*
* Copyright 2000-2013 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.siyeh.ig.maturity;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
/**
* @author Bas Leijdekkers
*/
public class SystemOutErrInspectionTest extends LightInspectionTestCase {
public void testSimple() {
doStatementTest("/*Uses of 'System.out' should probably be replaced with more robust logging*/System.out/**/.println(\"debugging\");");
}
public void testInTest() {
addEnvironmentClass("package org.junit;" +
"@Retention(RetentionPolicy.RUNTIME) " +
"@Target({ElementType.METHOD}) " +
"public @interface Test {}");
doMemberTest("@org.junit.Test public void testSomething() {" +
" System.out.println(\"debugger\");" +
"}");
}
public void testMultiple() {
doMemberTest("public void foo() {" +
" /*Uses of 'System.out' should probably be replaced with more robust logging*/System.out/**/.println(0);" +
" /*Uses of 'System.err' should probably be replaced with more robust logging*/System.err/**/.println(0);" +
" final java.io.PrintStream out = /*Uses of 'System.out' should probably be replaced with more robust logging*/System.out/**/;" +
" final java.io.PrintStream err = /*Uses of 'System.err' should probably be replaced with more robust logging*/System.err/**/;" +
"}");
}
@Override
protected InspectionProfileEntry getInspection() {
final SystemOutErrInspection inspection = new SystemOutErrInspection();
inspection.ignoreInTestCode = true;
return inspection;
}
}