summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java2
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java12
-rw-r--r--sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java6
-rw-r--r--sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java39
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java17
5 files changed, 57 insertions, 19 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
index 4edb28f633a..6f478c2fda7 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
@@ -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
diff --git a/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
index c434b09afb3..a2a6ce5dc71 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
@@ -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();
diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java b/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java
index bf396aee978..00ed8a789a7 100644
--- a/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java
+++ b/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java
@@ -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();
diff --git a/sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java b/sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java
index 4a6f17c82bf..81804e2490e 100644
--- a/sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java
@@ -19,6 +19,16 @@
*/
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);
@@ -51,6 +53,19 @@ public class RuleI18nManagerTest {
}
@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);
RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
@@ -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");
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java
index a2a083a787f..4afdb625394 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java
@@ -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,16 +36,28 @@ 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>
* If the description is not found in the given locale, then the default description is returned (the English one).