Updated version of the "comparingReference" sample plugin.

This commit is contained in:
Alexey
2010-09-17 12:23:15 +04:00
parent 2a8bd76a9e
commit 4adb058bf9
8 changed files with 108 additions and 3 deletions
@@ -6,8 +6,9 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/source" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/testSource" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="IDEA IC-96.1124" jdkType="IDEA JDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
@@ -197,7 +197,10 @@
<module fileurl="file://$PROJECT_DIR$/comparingReferences.iml" filepath="$PROJECT_DIR$/comparingReferences.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA IC-96.1068" project-jdk-type="IDEA JDK" />
<component name="ProjectResources">
<default-html-doctype>http://www.w3.org/1999/xhtml</default-html-doctype>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA IC-96.1097" project-jdk-type="IDEA JDK" />
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
@@ -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();
@@ -0,0 +1,6 @@
public class X {
public boolean compare2Strings(java.lang.String s1, java.lang.String s2) {
return (s1.equals(s2));
}
}
@@ -0,0 +1,6 @@
public class X {
public boolean compare2Strings(java.lang.String s1, java.lang.String s2) {
return (<caret>s1 == s2);
}
}
@@ -0,0 +1,6 @@
public class X {
public boolean compare2Dates(java.util.Date dt1, java.util.Date dt2){
return (!dt1.equals(dt2));
}
}
@@ -0,0 +1,6 @@
public class X {
public boolean compare2Dates(java.util.Date dt1, java.util.Date dt2){
return (dt1 <caret>!= dt2);
}
}
@@ -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<IdeaProjectTestFixture> 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<HighlightInfo> 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()");
}
}