]> source.dussan.org Git - sonarqube.git/commitdiff
SONARIDE-334 Title is not displayed for some rules
authorJulien Lancelot <julien.lancelot@gmail.com>
Tue, 4 Dec 2012 10:59:12 +0000 (11:59 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Tue, 4 Dec 2012 10:59:12 +0000 (11:59 +0100)
RuleI18nManager has now a new method to get the rule name by searching the name in the i18n, and then if not found return the rule name property.

sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java
sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java
sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java

index 4edb28f633adcec3ab18eaebcc53b2148affa387..6f478c2fda7378b387c8ded74005f5f86761ce07 100644 (file)
@@ -133,7 +133,7 @@ public class DryRunExporter implements BatchComponent {
   }
 
   private String name(Rule rule) {
-    return ruleI18nManager.getName(rule.getRepositoryKey(), rule.getKey(), Locale.getDefault());
+    return ruleI18nManager.getName(rule, Locale.getDefault());
   }
 
   @VisibleForTesting
index c434b09afb3cf3be5e350be998de2f79729d23d4..a2a6ce5dc71738a4c7a33175b9fbf8c3cd11befc 100644 (file)
@@ -80,13 +80,15 @@ public class DryRunExporterTest {
 
   @Test
   public void should_export_violations() {
+    Rule rule = Rule.create("pmd", "RULE_KEY");
+
     when(server.getVersion()).thenReturn("3.4");
     when(violation.getResource()).thenReturn(resource);
     when(violation.getLineId()).thenReturn(1);
     when(violation.getMessage()).thenReturn("VIOLATION");
-    when(violation.getRule()).thenReturn(Rule.create("pmd", "RULE_KEY"));
+    when(violation.getRule()).thenReturn(rule);
     when(violation.getSeverity()).thenReturn(RulePriority.INFO);
-    when(ruleI18nManager.getName("pmd", "RULE_KEY", Locale.getDefault())).thenReturn("RULE_NAME");
+    when(ruleI18nManager.getName(rule, Locale.getDefault())).thenReturn("RULE_NAME");
     doReturn(Arrays.asList(violation)).when(dryRunExporter).getViolations(resource);
 
     StringWriter output = new StringWriter();
@@ -100,13 +102,15 @@ public class DryRunExporterTest {
 
   @Test
   public void should_export_violation_with_no_line() {
+    Rule rule = Rule.create("pmd", "RULE_KEY");
+
     when(server.getVersion()).thenReturn("3.4");
     when(violation.getResource()).thenReturn(resource);
     when(violation.getLineId()).thenReturn(null);
     when(violation.getMessage()).thenReturn("VIOLATION");
-    when(violation.getRule()).thenReturn(Rule.create("pmd", "RULE_KEY"));
+    when(violation.getRule()).thenReturn(rule);
     when(violation.getSeverity()).thenReturn(RulePriority.INFO);
-    when(ruleI18nManager.getName("pmd", "RULE_KEY", Locale.getDefault())).thenReturn("RULE_NAME");
+    when(ruleI18nManager.getName(rule, Locale.getDefault())).thenReturn("RULE_NAME");
     doReturn(Arrays.asList(violation)).when(dryRunExporter).getViolations(resource);
 
     StringWriter output = new StringWriter();
index bf396aee97831e8fcd25aba77aa926563f781881..00ed8a789a73b4f8a6505dfb6e50f9f766a06c31 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.commons.lang.StringUtils;
 import org.sonar.api.BatchExtension;
 import org.sonar.api.ServerExtension;
 import org.sonar.api.i18n.RuleI18n;
+import org.sonar.api.rules.Rule;
 
 import java.util.List;
 import java.util.Locale;
@@ -54,6 +55,11 @@ public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtensio
     return message(repositoryKey, ruleKey, locale, NAME_SUFFIX);
   }
 
+  public String getName(Rule rule, Locale locale) {
+    String name = message(rule.getRepositoryKey(), rule.getKey(), locale, NAME_SUFFIX);
+    return name != null ? name : rule.getName();
+  }
+
   public String getDescription(String repositoryKey, String ruleKey, Locale locale) {
     String relatedProperty = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(NAME_SUFFIX).toString();
 
index 4a6f17c82bfc7b709ac23e234feef79b0934e2ef..81804e2490eb58502888dee1866c24b662858dd9 100644 (file)
  */
 package org.sonar.core.i18n;
 
+import com.google.common.collect.Sets;
+import org.fest.assertions.Assertions;
+import org.hamcrest.core.Is;
+import org.junit.Test;
+import org.sonar.api.rules.Rule;
+
+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;
@@ -28,16 +38,8 @@ 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 org.hamcrest.core.Is;
-import org.junit.Test;
-
-import com.google.common.collect.Sets;
-
 public class RuleI18nManagerTest {
+
   @Test
   public void shouldGetName() {
     I18nManager i18n = mock(I18nManager.class);
@@ -50,6 +52,19 @@ public class RuleI18nManagerTest {
     verifyNoMoreInteractions(i18n);
   }
 
+  @Test
+  public void shouldGetRuleNameIfNoLocalizationFound() {
+    String propertyKey = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name";
+    I18nManager i18n = mock(I18nManager.class);
+    when(i18n.message(Locale.ENGLISH, propertyKey, null)).thenReturn(null);
+    RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
+
+    String ruleName = "RULE_NAME";
+    Rule rule = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", ruleName);
+    String name = ruleI18n.getName(rule, Locale.ENGLISH);
+    Assertions.assertThat(name).isEqualTo(ruleName);
+  }
+
   @Test
   public void shouldGetParamDescription() {
     I18nManager i18n = mock(I18nManager.class);
@@ -65,7 +80,7 @@ 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");
 
@@ -81,7 +96,7 @@ public class RuleI18nManagerTest {
   @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");
@@ -99,7 +114,7 @@ public class RuleI18nManagerTest {
   @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");
index a2a083a787fbc5e2d7541611eae69c6a766730a6..4afdb62539411f29ef43f8f8d837dcae757290e6 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.api.i18n;
 
 import org.sonar.api.BatchComponent;
 import org.sonar.api.ServerComponent;
+import org.sonar.api.rules.Rule;
 
 import java.util.Locale;
 
@@ -35,15 +36,27 @@ public interface RuleI18n extends ServerComponent, BatchComponent {
    * Returns the localized name of the rule identified by its repository key and rule key.
    * <br>
    * If the name is not found in the given locale, then the default name is returned (the English one).
-   * As a rule must have a name (this is a constraint in Sonar), this method never returns null.
+   * This method could return null if no default name found. This is the cause for instance the copies rules.
    *
    * @param repositoryKey the repository key
    * @param ruleKey the rule key
    * @param locale the locale to translate into
-   * @return the translated name of the rule, or the default English one if the given locale is not supported
+   * @return the translated name of the rule, or the default English one if the given locale is not supported, or null
    */
   String getName(String repositoryKey, String ruleKey, Locale locale);
 
+  /**
+   * Returns the localized name or the name of the rule.
+   * <br>
+   * If the name is not found in the given locale, then the default name is returned (the English one).
+   * It the default name is not found, then the rule name is returned.
+   *
+   * @param rule the rule
+   * @param locale the locale to translate into
+   * @return the translated name of the rule, or the default English one if the given locale is not supported, or the rule name.
+   */
+  String getName(Rule rule, Locale locale);
+
   /**
    * Returns the localized description of the rule identified by its repository key and rule key.
    * <br>