]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3722 Misleading exception message when no l10n of rule name
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Thu, 20 Sep 2012 15:22:17 +0000 (17:22 +0200)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Thu, 20 Sep 2012 16:22:09 +0000 (18:22 +0200)
sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java
sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java

index 8c849a376f26af768dfd6e136795db468054c825..839672c40505725138bc550bd9768b0575778ef2 100644 (file)
@@ -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);
+      }
     }
   }
 
index 4c222392359d7097d00c69876fb25dc04eb45568..59d7a3e7a6bedb610edf64fb851b8e25868de3b9 100644 (file)
  */
 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<Rule> 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<Rule> 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 {