aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabrice Bellingard <bellingard@gmail.com>2012-03-14 18:20:14 +0100
committerFabrice Bellingard <bellingard@gmail.com>2012-03-14 18:20:14 +0100
commitfcf38c6acf101d49089a9d3faf961a3bb7544b74 (patch)
tree59a7655febb77b9559d6c581aad68b91af888958
parent58fda0511bd186fbcfd7596d1d43a863f8017c9e (diff)
downloadsonarqube-fcf38c6acf101d49089a9d3faf961a3bb7544b74.tar.gz
sonarqube-fcf38c6acf101d49089a9d3faf961a3bb7544b74.zip
SONAR-3319 Prevent conflicts in l10n of description for rules
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
-rw-r--r--sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java12
-rw-r--r--sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java28
2 files changed, 34 insertions, 6 deletions
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 8318347ef00..4ff61bdc04b 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
@@ -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;
}
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 f0b4ba360e4..cc26a5d4ec1 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
@@ -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