]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3895 Fix rule name
authorDavid Gageot <david@gageot.net>
Fri, 2 Nov 2012 16:38:18 +0000 (17:38 +0100)
committerDavid Gageot <david@gageot.net>
Fri, 2 Nov 2012 16:43:25 +0000 (17:43 +0100)
sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java

index 3d25d1fb163b044ba1fa7814338e5ade8ba17607..c292007b31808b6769a4188b0403278a811c467f 100644 (file)
@@ -29,10 +29,12 @@ import org.sonar.api.batch.SensorContext;
 import org.sonar.api.platform.Server;
 import org.sonar.api.resources.ProjectFileSystem;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.Violation;
 import org.sonar.api.utils.SonarException;
 import org.sonar.batch.bootstrap.DryRun;
 import org.sonar.batch.index.DefaultIndex;
+import org.sonar.core.i18n.RuleI18nManager;
 
 import java.io.BufferedWriter;
 import java.io.File;
@@ -41,6 +43,7 @@ import java.io.IOException;
 import java.io.Writer;
 import java.util.Collection;
 import java.util.List;
+import java.util.Locale;
 
 /**
  * @since 3.4
@@ -52,12 +55,14 @@ public class DryRunExporter implements BatchComponent {
   private final DefaultIndex sonarIndex;
   private final ProjectFileSystem projectFileSystem;
   private final Server server;
+  private final RuleI18nManager ruleI18nManager;
 
-  public DryRunExporter(DryRun dryRun, DefaultIndex sonarIndex, ProjectFileSystem projectFileSystem, Server server) {
+  public DryRunExporter(DryRun dryRun, DefaultIndex sonarIndex, ProjectFileSystem projectFileSystem, Server server, RuleI18nManager ruleI18nManager) {
     this.dryRun = dryRun;
     this.sonarIndex = sonarIndex;
     this.projectFileSystem = projectFileSystem;
     this.server = server;
+    this.ruleI18nManager = ruleI18nManager;
   }
 
   public void execute(SensorContext context) {
@@ -110,7 +115,8 @@ public class DryRunExporter implements BatchComponent {
               .name("message").value(violation.getMessage())
               .name("severity").value(violation.getSeverity().name())
               .name("rule_key").value(violation.getRule().getKey())
-              .name("rule_name").value(violation.getRule().getName())
+              .name("rule_repository").value(violation.getRule().getRepositoryKey())
+              .name("rule_name").value(name(violation.getRule()))
               .endObject();
         }
 
@@ -127,6 +133,10 @@ public class DryRunExporter implements BatchComponent {
     }
   }
 
+  private String name(Rule rule) {
+    return ruleI18nManager.getName(rule.getRepositoryKey(), rule.getKey(), Locale.getDefault());
+  }
+
   @VisibleForTesting
   List<Violation> getViolations(Resource resource) {
     return sonarIndex.getViolations(resource);
index 35a5ce2f4bdd9395c3315502a9a696fd6e7261c9..733893f9860e7f1c0a226d84aae488c9e62928f2 100644 (file)
@@ -32,12 +32,14 @@ import org.sonar.api.rules.RulePriority;
 import org.sonar.api.rules.Violation;
 import org.sonar.batch.bootstrap.DryRun;
 import org.sonar.batch.index.DefaultIndex;
+import org.sonar.core.i18n.RuleI18nManager;
 import org.sonar.java.api.JavaClass;
 
 import java.io.File;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.util.Arrays;
+import java.util.Locale;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.doReturn;
@@ -56,13 +58,14 @@ public class DryRunExporterTest {
   Violation violation = mock(Violation.class);
   ProjectFileSystem projectFileSystem = mock(ProjectFileSystem.class);
   Server server = mock(Server.class);
+  RuleI18nManager ruleI18nManager = mock(RuleI18nManager.class);
 
   @org.junit.Rule
   public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
   @Before
   public void setUp() {
-    dryRunExporter = spy(new DryRunExporter(dryRun, sonarIndex, projectFileSystem, server));
+    dryRunExporter = spy(new DryRunExporter(dryRun, sonarIndex, projectFileSystem, server, ruleI18nManager));
   }
 
   @Test
@@ -79,8 +82,9 @@ public class DryRunExporterTest {
     when(violation.getResource()).thenReturn(resource);
     when(violation.getLineId()).thenReturn(1);
     when(violation.getMessage()).thenReturn("VIOLATION");
-    when(violation.getRule()).thenReturn(Rule.create("pmd", "RULE_KEY").setName("RULE_NAME"));
+    when(violation.getRule()).thenReturn(Rule.create("pmd", "RULE_KEY"));
     when(violation.getSeverity()).thenReturn(RulePriority.INFO);
+    when(ruleI18nManager.getName("pmd", "RULE_KEY", Locale.getDefault())).thenReturn("RULE_NAME");
     doReturn(Arrays.asList(violation)).when(dryRunExporter).getViolations(resource);
 
     StringWriter output = new StringWriter();
@@ -89,7 +93,7 @@ public class DryRunExporterTest {
 
     assertThat(json)
         .isEqualTo(
-            "{\"version\":\"3.4\",\"violations_per_resource\":{\"KEY\":[{\"line\":1,\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_name\":\"RULE_NAME\"}]}}");
+            "{\"version\":\"3.4\",\"violations_per_resource\":{\"KEY\":[{\"line\":1,\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_repository\":\"pmd\",\"rule_name\":\"RULE_NAME\"}]}}");
   }
 
   @Test
@@ -99,8 +103,9 @@ public class DryRunExporterTest {
     when(violation.getResource()).thenReturn(resource);
     when(violation.getLineId()).thenReturn(null);
     when(violation.getMessage()).thenReturn("VIOLATION");
-    when(violation.getRule()).thenReturn(Rule.create("pmd", "RULE_KEY").setName("RULE_NAME"));
+    when(violation.getRule()).thenReturn(Rule.create("pmd", "RULE_KEY"));
     when(violation.getSeverity()).thenReturn(RulePriority.INFO);
+    when(ruleI18nManager.getName("pmd", "RULE_KEY", Locale.getDefault())).thenReturn("RULE_NAME");
     doReturn(Arrays.asList(violation)).when(dryRunExporter).getViolations(resource);
 
     StringWriter output = new StringWriter();
@@ -108,7 +113,7 @@ public class DryRunExporterTest {
     String json = output.toString();
 
     assertThat(json).isEqualTo(
-        "{\"version\":\"3.4\",\"violations_per_resource\":{\"KEY\":[{\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_name\":\"RULE_NAME\"}]}}");
+        "{\"version\":\"3.4\",\"violations_per_resource\":{\"KEY\":[{\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_repository\":\"pmd\",\"rule_name\":\"RULE_NAME\"}]}}");
   }
 
   @Test