i18n: duplicate string literal inspection checks that corresponded field initializer...
authorDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Tue, 7 Jun 2016 10:07:39 +0000 (13:07 +0300)
committerDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Tue, 7 Jun 2016 12:31:43 +0000 (15:31 +0300)
plugins/java-i18n/src/com/intellij/codeInspection/duplicateStringLiteral/DuplicateStringLiteralInspection.java
plugins/java-i18n/testData/inspections/duplicateStringLiteral/ApplyRenameForWholeFile.java [new file with mode: 0644]
plugins/java-i18n/testData/inspections/duplicateStringLiteral/ApplyRenameForWholeFileAfter.java [new file with mode: 0644]
plugins/java-i18n/testData/inspections/duplicateStringLiteral/PropertyKey.java [moved from plugins/java-i18n/testData/inspections/duplicateStringLiteral/propertyKey/src/Test.java with 52% similarity]
plugins/java-i18n/testData/inspections/duplicateStringLiteral/propertyKey/expected.xml [deleted file]
plugins/java-i18n/testSrc/com/intellij/codeInspection/i18n/DuplicateStringLiteralInspectionTest.java

index 5d600bcdf1fb480bed393f17daf16e6f722382e8..de7533394e1a6e38bf937ec27215e803f80badd3 100644 (file)
@@ -21,6 +21,7 @@ import com.intellij.codeInspection.*;
 import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
 import com.intellij.codeInspection.i18n.JavaI18nUtil;
 import com.intellij.lang.java.JavaLanguage;
+import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.progress.ProgressManager;
@@ -305,7 +306,7 @@ public class DuplicateStringLiteralInspection extends BaseLocalInspectionTool {
 
     @Override
     public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
-      SwingUtilities.invokeLater(() -> {
+      ApplicationManager.getApplication().invokeLater(() -> {
         if (project.isDisposed()) return;
         final List<PsiExpression> expressions = new ArrayList<PsiExpression>();
         for(SmartPsiElementPointer ptr: myExpressions) {
@@ -377,6 +378,10 @@ public class DuplicateStringLiteralInspection extends BaseLocalInspectionTool {
       final PsiLiteralExpression myOriginalExpression = (PsiLiteralExpression)startElement;
       final PsiField myConstant = myConst.getElement();
       if (myConstant == null) return;
+      final PsiExpression initializer = myConstant.getInitializer();
+      if (!(initializer instanceof PsiLiteralExpression)) {
+        return;
+      }
       if (!FileModificationService.getInstance().prepareFileForWrite(myOriginalExpression.getContainingFile())) return;
       try {
         final PsiReferenceExpression reference = createReferenceTo(myConstant, myOriginalExpression);
diff --git a/plugins/java-i18n/testData/inspections/duplicateStringLiteral/ApplyRenameForWholeFile.java b/plugins/java-i18n/testData/inspections/duplicateStringLiteral/ApplyRenameForWholeFile.java
new file mode 100644 (file)
index 0000000..bdfd666
--- /dev/null
@@ -0,0 +1,11 @@
+class ClassA {
+  public static final String ABC = "ABCDEF";
+
+  public static void main(String[] args) {
+    System.out.println("ABC = " + ABC);
+  }
+}
+
+class ClassB {
+  public static final String ABC = "ABCDEF";
+}
\ No newline at end of file
diff --git a/plugins/java-i18n/testData/inspections/duplicateStringLiteral/ApplyRenameForWholeFileAfter.java b/plugins/java-i18n/testData/inspections/duplicateStringLiteral/ApplyRenameForWholeFileAfter.java
new file mode 100644 (file)
index 0000000..47a6094
--- /dev/null
@@ -0,0 +1,11 @@
+class ClassA {
+  public static final String ABC = ClassB.ABC;
+
+  public static void main(String[] args) {
+    System.out.println("ABC = " + ABC);
+  }
+}
+
+class ClassB {
+  public static final String ABC = "ABCDEF";
+}
\ No newline at end of file
similarity index 52%
rename from plugins/java-i18n/testData/inspections/duplicateStringLiteral/propertyKey/src/Test.java
rename to plugins/java-i18n/testData/inspections/duplicateStringLiteral/PropertyKey.java
index 48560e51fc28bbb0e304aa0ede354f1959d99775..be892b8ec0fa02a71853e6f39460e0b7d6fda4f9 100644 (file)
@@ -3,11 +3,11 @@ import org.jetbrains.annotations.*;
 @SuppressWarnings({"HardCodedStringLiteral"})
 class Test {
   @SuppressWarnings({"HardCodedStringLiteral"})
-  String s = "abcdefghhijklmnop";
+  String s = <warning descr="Duplicate string literal found in&nbsp;&nbsp;&nbsp;'Test' (in this file)">"abcdefghhijklmnop"</warning>;
 
   @SuppressWarnings({"HardCodedStringLiteral"})
   void f(@PropertyKey(resourceBundle = "xxx.yyy") String key) {
-     String s = "abcdefghhijklmnop";
+     String s = <warning descr="Duplicate string literal found in&nbsp;&nbsp;&nbsp;'Test' (in this file)">"abcdefghhijklmnop"</warning>;
      f("abcdefghhijklmnop"); //no way
   }
 }
\ No newline at end of file
diff --git a/plugins/java-i18n/testData/inspections/duplicateStringLiteral/propertyKey/expected.xml b/plugins/java-i18n/testData/inspections/duplicateStringLiteral/propertyKey/expected.xml
deleted file mode 100644 (file)
index 6727b98..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<problems>
-  <problem>
-    <file>Test.java</file>
-    <line>6</line>
-    <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Duplicate String Literal</problem_class>
-    <description>&lt;html&gt;&lt;body&gt;Duplicate string literal found in&lt;br&gt;'Test'&lt;/body&gt;&lt;/html&gt;</description>
-  </problem>
-
-  <problem>
-    <file>Test.java</file>
-    <line>10</line>
-    <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Duplicate String Literal</problem_class>
-    <description>&lt;html&gt;&lt;body&gt;Duplicate string literal found in&lt;br&gt;'Test'&lt;/body&gt;&lt;/html&gt;</description>
-  </problem>
-</problems>
\ No newline at end of file
index 0cf9f36e18598d50395be89a70fec2d3483d9577..0a2df3ed3b892f914f3de71b5fc8cfe60e1fe6a4 100644 (file)
@@ -3,22 +3,39 @@
  */
 package com.intellij.codeInspection.i18n;
 
+import com.intellij.codeInsight.intention.IntentionAction;
+import com.intellij.codeInspection.InspectionsBundle;
 import com.intellij.codeInspection.duplicateStringLiteral.DuplicateStringLiteralInspection;
-import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
 import com.intellij.openapi.application.PluginPathManager;
-import com.intellij.testFramework.InspectionTestCase;
+import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
 
-public class DuplicateStringLiteralInspectionTest extends InspectionTestCase {
+import java.util.List;
+
+public class DuplicateStringLiteralInspectionTest extends JavaCodeInsightFixtureTestCase {
   private final DuplicateStringLiteralInspection myInspection = new DuplicateStringLiteralInspection();
 
-  private void doTest() throws Exception {
-    doTest("duplicateStringLiteral/"+getTestName(true), new LocalInspectionToolWrapper(myInspection), "java 1.5");
+  @Override
+  public void setUp() throws Exception {
+    super.setUp();
+    myFixture.enableInspections(myInspection);
   }
 
-  public void testPropertyKey() throws Exception{ myInspection.IGNORE_PROPERTY_KEYS = true; doTest(); }
+  public void testPropertyKey() throws Exception {
+    myInspection.IGNORE_PROPERTY_KEYS = true;
+    myFixture.testHighlighting("PropertyKey.java");
+  }
+
+  public void testBatchApplication() {
+    final List<IntentionAction> fixes = myFixture.getAllQuickFixes("ApplyRenameForWholeFile.java");
+    fixes
+      .stream()
+      .filter(i -> InspectionsBundle.message("inspection.duplicates.replace.family.quickfix").equals(i.getFamilyName()))
+      .forEach(myFixture::launchAction);
+    myFixture.checkResultByFile("ApplyRenameForWholeFileAfter.java");
+  }
 
   @Override
   protected String getTestDataPath() {
-    return PluginPathManager.getPluginHomePath("java-i18n") + "/testData/inspections";
+    return PluginPathManager.getPluginHomePath("java-i18n") + "/testData/inspections/duplicateStringLiteral/";
   }
 }
\ No newline at end of file