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;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
+import java.util.Locale;
/**
* @since 3.4
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) {
.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();
}
}
}
+ private String name(Rule rule) {
+ return ruleI18nManager.getName(rule.getRepositoryKey(), rule.getKey(), Locale.getDefault());
+ }
+
@VisibleForTesting
List<Violation> getViolations(Resource resource) {
return sonarIndex.getViolations(resource);
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;
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
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();
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
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();
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