]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3319 Add missing case
authorFabrice Bellingard <bellingard@gmail.com>
Thu, 5 Apr 2012 09:26:25 +0000 (11:26 +0200)
committerFabrice Bellingard <bellingard@gmail.com>
Thu, 5 Apr 2012 09:26:25 +0000 (11:26 +0200)
sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java
sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java

index c19d9475a55333bf29bba02a456284c2bb5ee3aa..fb1503e9674efbb3926098154a651f6ab0650c44 100644 (file)
@@ -76,7 +76,12 @@ public class RuleI18nManager implements ServerComponent {
    * See http://jira.codehaus.org/browse/SONAR-3319
    */
   private String lookUpDescriptionInFormerLocation(String ruleKey, String relatedProperty, Locale localeWithoutCountry) {
-    return i18nManager.messageFromFile(localeWithoutCountry, ruleKey + ".html", relatedProperty, true);
+    String 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, ruleKey + ".html", relatedProperty, true);
+    }
+    return description;
   }
 
   public String getParamDescription(String repositoryKey, String ruleKey, String paramKey, Locale locale) {
index 27684d08e33c029b4cdb66a251f80d128a0611db..4a6f17c82bfc7b709ac23e234feef79b0934e2ef 100644 (file)
  */
 package org.sonar.core.i18n;
 
-import com.google.common.collect.Sets;
-import org.hamcrest.core.Is;
-import org.junit.Test;
+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.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
 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.*;
+import org.hamcrest.core.Is;
+import org.junit.Test;
+
+import com.google.common.collect.Sets;
 
 public class RuleI18nManagerTest {
   @Test
@@ -91,6 +95,25 @@ public class RuleI18nManagerTest {
     verifyNoMoreInteractions(i18n);
   }
 
+  // see http://jira.codehaus.org/browse/SONAR-3319
+  @Test
+  public void shouldGetDescriptionFromFileWithBackwardCompatibilityWithSpecificLocale() {
+    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.FRENCH);
+    assertThat(description, is("Description"));
+
+    verify(i18n).messageFromFile(Locale.FRENCH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
+    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);
+  }
+
   @Test
   public void shouldUseOnlyLanguage() {
     I18nManager i18n = mock(I18nManager.class);
@@ -119,7 +142,11 @@ public class RuleI18nManagerTest {
 
     String propertyKeyForName = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name";
     verify(i18n).messageFromFile(Locale.FRENCH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
+    // check for backward compatibility in French
     verify(i18n).messageFromFile(Locale.FRENCH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
+    // check for backward compatibility in English
+    verify(i18n).messageFromFile(Locale.ENGLISH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
+    // and finally get the default English bundle
     verify(i18n).messageFromFile(Locale.ENGLISH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true);
     verifyNoMoreInteractions(i18n);
   }