]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3319 Prevent conflicts in l10n of description for rules
authorFabrice Bellingard <bellingard@gmail.com>
Wed, 14 Mar 2012 17:20:14 +0000 (18:20 +0100)
committerFabrice Bellingard <bellingard@gmail.com>
Wed, 14 Mar 2012 17:20:14 +0000 (18:20 +0100)
Indeed, repository key must be used.

=> HTML description files must now be stored in:
       org.sonar.l10n.<pluginKey>_<Language>.rules.<repoKey>
   instead of:
       org.sonar.l10n.<pluginKey>_<Language>

(which means that existing files just have to be moved into a
 subfolder named "rules/<repoKey>")

=> Backward compatibility is ensured
=> This also works if several plugins define rules for the same repo

sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java
sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java

index 8318347ef00735187d3dfcbc311f457cd1dff4d6..4ff61bdc04b8d23904bf3071e8d199ae466938be 100644 (file)
@@ -56,9 +56,15 @@ public class RuleI18nManager implements ServerComponent {
     String relatedProperty = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(NAME_SUFFIX).toString();
 
     Locale localeWithoutCountry = (locale.getCountry()==null ? locale : new Locale(locale.getLanguage()));
-    String description = i18nManager.messageFromFile(localeWithoutCountry, ruleKey + ".html", relatedProperty, true);
-    if (description == null && !"en".equals(localeWithoutCountry.getLanguage())) {
-      description = i18nManager.messageFromFile(Locale.ENGLISH, ruleKey + ".html", relatedProperty, true);
+    String ruleDescriptionFilePath = "rules/" + repositoryKey + "/" + ruleKey + ".html";
+    String description = i18nManager.messageFromFile(localeWithoutCountry, ruleDescriptionFilePath, relatedProperty, true);
+    if (description == null) {
+      // For backward compatibility, let's search at the root folder as it used to be before Sonar 2.15
+      description = i18nManager.messageFromFile(localeWithoutCountry, ruleKey + ".html", relatedProperty, true);
+      if (description == null && !"en".equals(localeWithoutCountry.getLanguage())) {
+        // nothing was found, let's get the value of the default bundle
+        description = i18nManager.messageFromFile(Locale.ENGLISH, ruleDescriptionFilePath, relatedProperty, true);
+      }
     }
     return description;
   }
index f0b4ba360e4e665676448a7221d65218ba81b9fc..cc26a5d4ec15be02da5879bf56699f1e2dc8ea1b 100644 (file)
@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.Locale;
 
 import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.matchers.JUnitMatchers.hasItem;
 import static org.mockito.Mockito.*;
@@ -59,12 +60,33 @@ public class RuleI18nManagerTest {
 
   @Test
   public void shouldGetDescriptionFromFile() {
+    String propertyKeyForName = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name";
+    
     I18nManager i18n = mock(I18nManager.class);
+    when(i18n.messageFromFile(Locale.ENGLISH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true)).thenReturn("Description");
+
     RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
+    String description = ruleI18n.getDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", Locale.ENGLISH);
+    assertThat(description, is("Description"));
 
-    ruleI18n.getDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", Locale.ENGLISH);
+    verify(i18n).messageFromFile(Locale.ENGLISH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
+    verifyNoMoreInteractions(i18n);
+  }
 
+  // see http://jira.codehaus.org/browse/SONAR-3319
+  @Test
+  public void shouldGetDescriptionFromFileWithBackwardCompatibility() {
     String propertyKeyForName = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name";
+    
+    I18nManager i18n = mock(I18nManager.class);
+    // this is the "old" way of storing HTML description files for rules (they are not in the "rules/<repo-key>" folder)
+    when(i18n.messageFromFile(Locale.ENGLISH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true)).thenReturn("Description");
+
+    RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
+    String description = ruleI18n.getDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", Locale.ENGLISH);
+    assertThat(description, is("Description"));
+
+    verify(i18n).messageFromFile(Locale.ENGLISH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
     verify(i18n).messageFromFile(Locale.ENGLISH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
     verifyNoMoreInteractions(i18n);
   }
@@ -97,8 +119,8 @@ public class RuleI18nManagerTest {
 
     String propertyKeyForName = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name";
     verify(i18n).messageFromFile(Locale.FRENCH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
-    verify(i18n).messageFromFile(Locale.ENGLISH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
-    verifyNoMoreInteractions(i18n);
+//    verify(i18n).messageFromFile(Locale.ENGLISH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
+//    verifyNoMoreInteractions(i18n);
   }
 
   @Test