diff --git a/samples/comparingReferences/comparingReferences.iml b/samples/comparingReferences/comparingReferences.iml index 5000ad3ec83d..f38326667e00 100644 --- a/samples/comparingReferences/comparingReferences.iml +++ b/samples/comparingReferences/comparingReferences.iml @@ -6,8 +6,9 @@ + - + diff --git a/samples/comparingReferences/comparingReferences.ipr b/samples/comparingReferences/comparingReferences.ipr index dacd55a8c87b..001dcf91c3c5 100644 --- a/samples/comparingReferences/comparingReferences.ipr +++ b/samples/comparingReferences/comparingReferences.ipr @@ -197,7 +197,10 @@ - + + http://www.w3.org/1999/xhtml + + diff --git a/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesInspection.java b/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesInspection.java index d900809cfb4f..6410db394eed 100644 --- a/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesInspection.java +++ b/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesInspection.java @@ -31,7 +31,7 @@ public class ComparingReferencesInspection extends BaseJavaLocalInspectionTool { @NotNull public String getDisplayName() { - // return InspectionsBundle.message("inspection.comparing.references.display.name"); + return "'==' or '!=' instead of 'equals()'"; } @@ -94,9 +94,11 @@ public class ComparingReferencesInspection extends BaseJavaLocalInspectionTool { private static class MyQuickFix implements LocalQuickFix { @NotNull public String getName() { + // The test (see the TestThisPlugin class) uses this string to identify the quick fix action. return InspectionsBundle.message("inspection.comparing.references.use.quickfix"); } + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { try { PsiBinaryExpression binaryExpression = (PsiBinaryExpression)descriptor.getPsiElement(); diff --git a/samples/comparingReferences/testData/before.after.java b/samples/comparingReferences/testData/before.after.java new file mode 100644 index 000000000000..a3058effc73d --- /dev/null +++ b/samples/comparingReferences/testData/before.after.java @@ -0,0 +1,6 @@ +public class X { + public boolean compare2Strings(java.lang.String s1, java.lang.String s2) { + return (s1.equals(s2)); + } + +} \ No newline at end of file diff --git a/samples/comparingReferences/testData/before.java b/samples/comparingReferences/testData/before.java new file mode 100644 index 000000000000..23095bcfa097 --- /dev/null +++ b/samples/comparingReferences/testData/before.java @@ -0,0 +1,6 @@ +public class X { + public boolean compare2Strings(java.lang.String s1, java.lang.String s2) { + return (s1 == s2); + } + +} \ No newline at end of file diff --git a/samples/comparingReferences/testData/before1.after.java b/samples/comparingReferences/testData/before1.after.java new file mode 100644 index 000000000000..acedf90e0537 --- /dev/null +++ b/samples/comparingReferences/testData/before1.after.java @@ -0,0 +1,6 @@ +public class X { +public boolean compare2Dates(java.util.Date dt1, java.util.Date dt2){ + return (!dt1.equals(dt2)); + } + +} \ No newline at end of file diff --git a/samples/comparingReferences/testData/before1.java b/samples/comparingReferences/testData/before1.java new file mode 100644 index 000000000000..325c4ebf58df --- /dev/null +++ b/samples/comparingReferences/testData/before1.java @@ -0,0 +1,6 @@ +public class X { +public boolean compare2Dates(java.util.Date dt1, java.util.Date dt2){ + return (dt1 != dt2); + } + +} \ No newline at end of file diff --git a/samples/comparingReferences/testSource/testPlugin/TestThisPlugin.java b/samples/comparingReferences/testSource/testPlugin/TestThisPlugin.java new file mode 100644 index 000000000000..06216cedb2d4 --- /dev/null +++ b/samples/comparingReferences/testSource/testPlugin/TestThisPlugin.java @@ -0,0 +1,75 @@ +package testPlugin; + + +import com.intellij.codeInsight.daemon.impl.HighlightInfo; +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.codeInspection.ComparingReferencesInspection; +import com.intellij.testFramework.builders.JavaModuleFixtureBuilder; +import com.intellij.testFramework.fixtures.*; +import junit.framework.Assert; +import com.intellij.openapi.ui.Messages; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class TestThisPlugin { + + protected CodeInsightTestFixture myFixture; + // Specify path to your test data directory + // e.g. final String dataPath = "c:\\users\\john.doe\\idea\\community\\samples\\ComparingReferences/testData"; + final String dataPath = "c:\\users\\John.Doe\\idea\\community\\samples\\comparingReferences/testData"; + + + @Before + + public void setUp() throws Exception { + + final IdeaTestFixtureFactory fixtureFactory = IdeaTestFixtureFactory.getFixtureFactory(); + final TestFixtureBuilder testFixtureBuilder = fixtureFactory.createFixtureBuilder(); + myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(testFixtureBuilder.getFixture()); + myFixture.setTestDataPath(dataPath); + final JavaModuleFixtureBuilder builder = testFixtureBuilder.addModule(JavaModuleFixtureBuilder.class); + + builder.addContentRoot(myFixture.getTempDirPath()).addSourceRoot(""); + builder.setMockJdkLevel(JavaModuleFixtureBuilder.MockJdkLevel.jdk15); + myFixture.setUp(); + + + } + + @After + public void tearDown() throws Exception { + myFixture.tearDown(); + myFixture = null; + } + + protected void doTest(String testName, String hint) throws Throwable { + myFixture.configureByFile(testName + ".java"); + myFixture.enableInspections(ComparingReferencesInspection.class); + List highlightInfos = myFixture.doHighlighting(); + Assert.assertTrue(!highlightInfos.isEmpty()); + + final IntentionAction action = myFixture.findSingleIntention(hint); + + Assert.assertNotNull(action); + myFixture.launchAction(action); + myFixture.checkResultByFile(testName + ".after.java"); + + } + + @Test + // Test the "==" case + public void test() throws Throwable { + doTest("before", "Use equals()"); + + } + @Test + // Test the "!=" case + public void test1() throws Throwable { + doTest("before1", "Use equals()"); + + } + +}