From: Fabrice Bellingard Date: Thu, 20 Sep 2012 15:22:17 +0000 (+0200) Subject: SONAR-3722 Misleading exception message when no l10n of rule name X-Git-Tag: 3.3~268 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=085fac7fb3e4ecf16ed2e273f5553f5f6e9856fa;p=sonarqube.git SONAR-3722 Misleading exception message when no l10n of rule name --- diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java index 8c849a376f2..839672c4050 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java @@ -33,7 +33,12 @@ import org.sonar.api.utils.TimeProfiler; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.jpa.session.DatabaseSessionFactory; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; public final class RegisterRules { @@ -118,7 +123,13 @@ public final class RegisterRules { throw new SonarException("The following rule (repository: " + repositoryKey + ") must have a name: " + rule); } if (rule.getDescription() == null && ruleI18nManager.getDescription(repositoryKey, rule.getKey(), Locale.ENGLISH) == null) { - throw new SonarException("The following rule (repository: " + repositoryKey + ") must have a description: " + rule); + if (rule.getName() != null && ruleI18nManager.getName(repositoryKey, rule.getKey(), Locale.ENGLISH) == null) { + // specific case + throw new SonarException("No description found for the rule '" + rule.getName() + "' (repository: " + repositoryKey + ") because the entry 'rule." + + repositoryKey + "." + rule.getKey() + ".name' is missing from the bundle."); + } else { + throw new SonarException("The following rule (repository: " + repositoryKey + ") must have a description: " + rule); + } } } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java index 4c222392359..59d7a3e7a6b 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java @@ -19,23 +19,6 @@ */ package org.sonar.server.startup; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.number.OrderingComparisons.greaterThan; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - import org.junit.Before; import org.junit.Test; import org.sonar.api.rules.ActiveRule; @@ -48,15 +31,32 @@ import org.sonar.api.utils.SonarException; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.jpa.test.AbstractDbUnitTestCase; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.number.OrderingComparisons.greaterThan; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class RegisterRulesTest extends AbstractDbUnitTestCase { private RegisterRules task; @Before public void init() { - task = new RegisterRules(getSessionFactory(), new RuleRepository[]{new FakeRepository()}, null); + task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new FakeRepository()}, null); } - + @Test public void saveNewRepositories() { setupData("shared"); @@ -78,8 +78,8 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase { task.start(); List rules = getSession() - .createQuery("from " + Rule.class.getSimpleName() + " where pluginName<>'fake'") - .getResultList(); + .createQuery("from " + Rule.class.getSimpleName() + " where pluginName<>'fake'") + .getResultList(); assertThat(rules.size(), greaterThan(0)); for (Rule rule : rules) { assertThat(rule.isEnabled(), is(false)); @@ -190,14 +190,14 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase { @Test public void volumeTesting() { - task = new RegisterRules(getSessionFactory(), new RuleRepository[]{new VolumeRepository()}, null); + task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new VolumeRepository()}, null); setupData("shared"); task.start(); List result = getSession().getResults(Rule.class, "enabled", true); assertThat(result.size(), is(VolumeRepository.SIZE)); } - + // http://jira.codehaus.org/browse/SONAR-3305 @Test public void shouldFailRuleWithoutName() throws Exception { @@ -212,17 +212,18 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase { } catch (SonarException e) { assertThat(e.getMessage(), containsString("must have a name")); } - + // now it is ok, the rule has a name in the English bundle when(ruleI18nManager.getName(anyString(), anyString(), any(Locale.class))).thenReturn("Name"); when(ruleI18nManager.getDescription(anyString(), anyString(), any(Locale.class))).thenReturn("Description"); task.start(); } - + // http://jira.codehaus.org/browse/SONAR-3305 @Test public void shouldFailRuleWithoutDescription() throws Exception { RuleI18nManager ruleI18nManager = mock(RuleI18nManager.class); + when(ruleI18nManager.getName(anyString(), anyString(), any(Locale.class))).thenReturn("Name"); task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutDescriptionRepository()}, ruleI18nManager); setupData("shared"); @@ -233,12 +234,29 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase { } catch (SonarException e) { assertThat(e.getMessage(), containsString("must have a description")); } - + // now it is ok, the rule has a name & a description in the English bundle when(ruleI18nManager.getName(anyString(), anyString(), any(Locale.class))).thenReturn("Name"); when(ruleI18nManager.getDescription(anyString(), anyString(), any(Locale.class))).thenReturn("Description"); task.start(); } + + // http://jira.codehaus.org/browse/SONAR-3722 + @Test + public void shouldFailRuleWithoutNameInBundle() throws Exception { + RuleI18nManager ruleI18nManager = mock(RuleI18nManager.class); + task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithoutDescriptionRepository()}, ruleI18nManager); + setupData("shared"); + + // the rule has no name, it should fail + try { + task.start(); + fail("Rule must have a description"); + } catch (SonarException e) { + assertThat(e.getMessage(), containsString("No description found for the rule 'Rule 1' (repository: rule-without-description-repo) " + + "because the entry 'rule.rule-without-description-repo.rule1.name' is missing from the bundle.")); + } + } } class FakeRepository extends RuleRepository {