diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-11-29 12:05:54 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-11-29 12:06:18 +0100 |
commit | 2f4e11fb0c0bd16ccb2d8c35292df3a86d879778 (patch) | |
tree | 07159b1e7f10d1d1046762a0d293e983f305314b /sonar-core | |
parent | e820ac79c6365cf3b86d9a028cddeba64657db94 (diff) | |
download | sonarqube-2f4e11fb0c0bd16ccb2d8c35292df3a86d879778.tar.gz sonarqube-2f4e11fb0c0bd16ccb2d8c35292df3a86d879778.zip |
SONAR-4885 Stop supporting the internationalization of rule descriptions and titles
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java | 81 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/i18n/RuleI18nManagerTest.java | 99 |
2 files changed, 46 insertions, 134 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 b256c223fb7..6b732add0fe 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 @@ -21,6 +21,7 @@ package org.sonar.core.i18n; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; +import org.picocontainer.Startable; import org.sonar.api.BatchExtension; import org.sonar.api.ServerExtension; import org.sonar.api.i18n.RuleI18n; @@ -31,7 +32,7 @@ import javax.annotation.CheckForNull; import java.util.List; import java.util.Locale; -public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtension { +public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtension, Startable { private static final String NAME_SUFFIX = ".name"; private static final String RULE_PREFIX = "rule."; @@ -43,6 +44,7 @@ public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtensio this.i18nManager = i18nManager; } + @Override public void start() { List<RuleKey> list = Lists.newArrayList(); for (String propertyKey : i18nManager.getPropertyKeys()) { @@ -53,30 +55,55 @@ public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtensio this.ruleKeys = list.toArray(new RuleKey[list.size()]); } - @CheckForNull + @Override + public void stop() { + + } + + @Override + @Deprecated public String getName(String repositoryKey, String ruleKey, Locale locale) { - return message(repositoryKey, ruleKey, locale, NAME_SUFFIX); + return getName(repositoryKey, ruleKey); } - @CheckForNull + @Override + @Deprecated public String getName(Rule rule, Locale locale) { - String name = message(rule.getRepositoryKey(), rule.getKey(), locale, NAME_SUFFIX); - return name != null ? name : rule.getName(); + return getName(rule); } + @Override + @Deprecated public String getDescription(String repositoryKey, String ruleKey, Locale locale) { + return getDescription(repositoryKey, ruleKey); + } + + @Override + @Deprecated + public String getParamDescription(String repositoryKey, String ruleKey, String paramKey, Locale locale) { + return getParamDescription(repositoryKey, ruleKey, paramKey); + } + + + @CheckForNull + public String getName(String repositoryKey, String ruleKey) { + return message(repositoryKey, ruleKey, NAME_SUFFIX); + } + + @CheckForNull + public String getName(Rule rule) { + String name = message(rule.getRepositoryKey(), rule.getKey(), NAME_SUFFIX); + return name != null ? name : rule.getName(); + } + + public String getDescription(String repositoryKey, String ruleKey) { 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 ruleDescriptionFilePath = "rules/" + repositoryKey + "/" + ruleKey + ".html"; - String description = i18nManager.messageFromFile(localeWithoutCountry, ruleDescriptionFilePath, relatedProperty, true); + String description = i18nManager.messageFromFile(Locale.ENGLISH, ruleDescriptionFilePath, relatedProperty, true); if (description == null) { // Following line is to ensure backward compatibility (SONAR-3319) - description = lookUpDescriptionInFormerLocation(ruleKey, relatedProperty, localeWithoutCountry); - 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); - } + description = lookUpDescriptionInFormerLocation(ruleKey, relatedProperty); } return description; } @@ -87,35 +114,19 @@ public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtensio * * See http://jira.codehaus.org/browse/SONAR-3319 */ - private String lookUpDescriptionInFormerLocation(String ruleKey, String relatedProperty, Locale localeWithoutCountry) { - 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; + private String lookUpDescriptionInFormerLocation(String ruleKey, String relatedProperty) { + return i18nManager.messageFromFile(Locale.ENGLISH, ruleKey + ".html", relatedProperty, true); } @CheckForNull - public String getParamDescription(String repositoryKey, String ruleKey, String paramKey, Locale locale) { - return message(repositoryKey, ruleKey, locale, ".param." + paramKey); + public String getParamDescription(String repositoryKey, String ruleKey, String paramKey) { + return message(repositoryKey, ruleKey, ".param." + paramKey); } @CheckForNull - String message(String repositoryKey, String ruleKey, Locale locale, String suffix) { + String message(String repositoryKey, String ruleKey, String suffix) { String propertyKey = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(suffix).toString(); - return i18nManager.message(locale, propertyKey, null); - } - - public List<RuleKey> searchNames(String search, Locale locale) { - List<RuleKey> result = Lists.newArrayList(); - for (RuleKey ruleKey : ruleKeys) { - String name = i18nManager.message(locale, ruleKey.getNameProperty(), null); - if (name != null && StringUtils.indexOfIgnoreCase(name, search) >= 0) { - result.add(ruleKey); - } - } - return result; + return i18nManager.message(Locale.ENGLISH, propertyKey, null); } RuleKey[] getRuleKeys() { 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 e778ba117e4..6ecdb1078f1 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 @@ -53,19 +53,6 @@ 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); @@ -110,36 +97,6 @@ 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); - RuleI18nManager ruleI18n = new RuleI18nManager(i18n); - - ruleI18n.getDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", new Locale("fr", "BE")); - - String propertyKeyForName = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name"; - verify(i18n).messageFromFile(new Locale("fr"), "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName, true); - } - @Test public void shoudlReturnNullIfMissingDescription() { I18nManager i18n = mock(I18nManager.class); @@ -149,24 +106,6 @@ public class RuleI18nManagerTest { } @Test - public void shouldUseEnglishIfMissingLocale() { - I18nManager i18n = mock(I18nManager.class); - RuleI18nManager ruleI18n = new RuleI18nManager(i18n); - - ruleI18n.getDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", Locale.FRENCH); - - 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); - } - - @Test public void shouldBeRuleKey() { assertThat(RuleI18nManager.isRuleProperty("rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name"), Is.is(true)); assertThat(RuleI18nManager.isRuleProperty("rule.pmd.Header.name"), Is.is(true)); @@ -211,42 +150,4 @@ public class RuleI18nManagerTest { assertThat(keys, hasItem(new RuleI18nManager.RuleKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck"))); } - @Test - public void shouldSearchEnglishNames() { - I18nManager i18n = mock(I18nManager.class); - when(i18n.getPropertyKeys()).thenReturn(Sets.newHashSet("rule.pmd.Header.name", "rule.checkstyle.AnnotationUseStyleCheck.name")); - when(i18n.message(Locale.ENGLISH, "rule.pmd.Header.name", null)).thenReturn("HEADER PMD CHECK"); - when(i18n.message(Locale.ENGLISH, "rule.checkstyle.AnnotationUseStyleCheck.name", null)).thenReturn("check annotation style"); - - RuleI18nManager ruleI18n = new RuleI18nManager(i18n); - ruleI18n.start(); - - List<RuleI18nManager.RuleKey> result = ruleI18n.searchNames("ANNOTATion", Locale.ENGLISH); - assertThat(result.size(), Is.is(1)); - assertThat(result.get(0).getRepositoryKey(), Is.is("checkstyle")); - - result = ruleI18n.searchNames("bibopaloula", Locale.ENGLISH); - assertThat(result.size(), Is.is(0)); - } - - @Test - public void shouldSearchLocalizedNames() { - I18nManager i18n = mock(I18nManager.class); - when(i18n.getPropertyKeys()).thenReturn(Sets.newHashSet("rule.pmd.Header.name", "rule.checkstyle.AnnotationUseStyleCheck.name")); - when(i18n.message(Locale.ENGLISH, "rule.pmd.Header.name", null)).thenReturn("HEADER PMD CHECK"); - when(i18n.message(Locale.ENGLISH, "rule.checkstyle.AnnotationUseStyleCheck.name", null)).thenReturn("check annotation style"); - when(i18n.message(Locale.FRENCH, "rule.checkstyle.AnnotationUseStyleCheck.name", null)).thenReturn("vérifie le style des annotations"); - - RuleI18nManager ruleI18n = new RuleI18nManager(i18n); - ruleI18n.start(); - - List<RuleI18nManager.RuleKey> result = ruleI18n.searchNames("annotation", Locale.FRENCH); - assertThat(result.size(), Is.is(1)); - assertThat(result.get(0).getKey(), Is.is("AnnotationUseStyleCheck")); - - // search only in the french bundle - result = ruleI18n.searchNames("vérifie", Locale.FRENCH); - assertThat(result.size(), Is.is(1)); - assertThat(result.get(0).getKey(), Is.is("AnnotationUseStyleCheck")); - } } |