From 6cc57f84849f73e337b50943bef4c0390b248a44 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 21 Feb 2014 20:13:56 +0100 Subject: [PATCH] EA-54183 (CCE: BooleanConstructorInspection$BooleanConstructorFix.doFix) --- .../BooleanConstructorInspection.java | 9 ++-- .../com/siyeh/ig/IGQuickFixesTestCase.java | 43 ++++++++++++++++++- .../BooleanConstructorFixTest.java | 43 +++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/performance/BooleanConstructorFixTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/BooleanConstructorInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/BooleanConstructorInspection.java index 1a560a01b1db..882496df4484 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/BooleanConstructorInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/BooleanConstructorInspection.java @@ -20,7 +20,6 @@ import com.intellij.openapi.project.Project; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.util.PsiUtil; -import com.intellij.util.IncorrectOperationException; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -84,8 +83,12 @@ public class BooleanConstructorInspection extends BaseInspection { } @Override - public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { - final PsiNewExpression expression = (PsiNewExpression)descriptor.getPsiElement(); + public void doFix(Project project, ProblemDescriptor descriptor) { + final PsiElement element = descriptor.getPsiElement().getParent(); + if (!(element instanceof PsiNewExpression)) { + return; + } + final PsiNewExpression expression = (PsiNewExpression)element; final PsiExpressionList argumentList = expression.getArgumentList(); if (argumentList == null) { return; diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGQuickFixesTestCase.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGQuickFixesTestCase.java index f8013fd5acfa..b56c340f5400 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGQuickFixesTestCase.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGQuickFixesTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * 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. @@ -16,10 +16,14 @@ package com.siyeh.ig; import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.ide.highlighter.JavaFileType; import com.intellij.openapi.application.PluginPathManager; import com.intellij.pom.java.LanguageLevel; import com.intellij.testFramework.builders.JavaModuleFixtureBuilder; import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase; +import org.intellij.lang.annotations.Language; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; /** * @author anna @@ -30,6 +34,20 @@ public abstract class IGQuickFixesTestCase extends JavaCodeInsightFixtureTestCas protected String myDefaultHint = null; protected String myRelativePath = null; + @Override + protected void setUp() throws Exception { + super.setUp(); + + final BaseInspection inspection = getInspection(); + if (inspection != null) { + myFixture.enableInspections(inspection); + } + } + + protected BaseInspection getInspection() { + return null; + } + @Override protected void tuneFixture(final JavaModuleFixtureBuilder builder) throws Exception { builder.setLanguageLevel(LanguageLevel.JDK_1_7); @@ -59,6 +77,29 @@ public abstract class IGQuickFixesTestCase extends JavaCodeInsightFixtureTestCas myFixture.checkResultByFile(getRelativePath() + "/" + testName + ".after.java"); } + protected void doExpressionTest( + String hint, + @Language(value = "JAVA", prefix = "class X {{System.out.print(", suffix = ");}}") @NotNull @NonNls String before, + @Language(value = "JAVA", prefix = "class X {{System.out.print(", suffix = ");}}") @NotNull @NonNls String after) { + doTest(hint, "class X {{System.out.print(" + before + ");}}", "class X {{System.out.print(" + after + ");}}"); + } + + protected void doMemberTest( + String hint, + @Language(value = "JAVA", prefix = "class X {", suffix = "}") @NotNull @NonNls String before, + @Language(value = "JAVA", prefix = "class X {", suffix = "}") @NotNull @NonNls String after) { + doTest(hint, "class X {" + before + "}", "class X {" + after + "}"); + } + + protected void doTest(String hint, @Language("JAVA") @NotNull @NonNls String before, @Language("JAVA") @NotNull @NonNls String after) { + before = before.replace("/**/", ""); + myFixture.configureByText(JavaFileType.INSTANCE, before); + final IntentionAction intention = myFixture.findSingleIntention(hint); + assertNotNull(intention); + myFixture.launchAction(intention); + myFixture.checkResult(after); + } + protected String getRelativePath() { assertNotNull(myRelativePath); return myRelativePath; diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/performance/BooleanConstructorFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/performance/BooleanConstructorFixTest.java new file mode 100644 index 000000000000..feb1ad046183 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/performance/BooleanConstructorFixTest.java @@ -0,0 +1,43 @@ +/* + * 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.siyeh.ig.fixes.performance; + +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.IGQuickFixesTestCase; +import com.siyeh.ig.performance.BooleanConstructorInspection; + +/** + * @author Bas Leijdekkers + */ +public class BooleanConstructorFixTest extends IGQuickFixesTestCase { + + @Override + protected BaseInspection getInspection() { + return new BooleanConstructorInspection(); + } + + public void testSimple() { + doExpressionTest(InspectionGadgetsBundle.message("boolean.constructor.simplify.quickfix"), + "new Boolean/**/(true)", "Boolean.TRUE"); + } + + public void testSimple2() { + doMemberTest(InspectionGadgetsBundle.message("boolean.constructor.simplify.quickfix"), + "void m(boolean b) { Boolean c = new /**/Boolean(b); }", + "void m(boolean b) { Boolean c = Boolean.valueOf(b); }"); + } +}