</exclusion>
</exclusions>
</dependency>
- <dependency>
- <groupId>org.codehaus.sonar</groupId>
- <artifactId>sonar-deprecated</artifactId>
- <version>${project.version}</version>
- <scope>provided</scope>
- </dependency>
<!-- unit tests -->
<dependency>
*/
package org.sonar.plugins.findbugs;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-import org.sonar.api.CoreProperties;
import org.sonar.api.platform.ServerFileSystem;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Java;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;
-import org.sonar.api.rules.RulesManager;
import org.sonar.api.rules.XMLRuleParser;
import org.sonar.test.TestUtils;
import org.xml.sax.SAXException;
return activeRules;
}
- protected RulesManager createRulesManager() {
- RulesManager rulesManager = mock(RulesManager.class);
-
- when(rulesManager.getPluginRule(eq(CoreProperties.FINDBUGS_PLUGIN), anyString())).thenAnswer(new Answer<Rule>() {
-
- public Rule answer(InvocationOnMock invocationOnMock) throws Throwable {
- Object[] args = invocationOnMock.getArguments();
- Rule rule = Rule.create();
- rule.setPluginName((String) args[0]);
- rule.setKey((String) args[1]);
- return rule;
- }
- });
- return rulesManager;
- }
-
protected RulesProfile createRulesProfileWithActiveRules() {
RulesProfile profile = RulesProfile.create();
profile.setName(RulesProfile.SONAR_WAY_FINDBUGS_NAME);
</properties>
<dependencies>
- <dependency>
- <groupId>org.codehaus.sonar</groupId>
- <artifactId>sonar-deprecated</artifactId>
- <version>${project.version}</version>
- </dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-java-api</artifactId>
</plugin>
</plugins>
</build>
-</project>
\ No newline at end of file
+</project>
*/
package org.sonar.plugins.pmd;
-import org.sonar.api.batch.AbstractViolationsStaxParser;
+import java.io.File;
+
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Project;
-import org.sonar.api.rules.RulesManager;
+import org.sonar.api.rules.RuleFinder;
import org.sonar.api.utils.Logs;
import org.sonar.api.utils.XmlParserException;
-import java.io.File;
-
public class PmdSensor implements Sensor {
private RulesProfile profile;
- private RulesManager rulesManager;
+ private RuleFinder rulesFinder;
private PmdExecutor executor;
- public PmdSensor(RulesProfile profile, RulesManager rulesManager, PmdExecutor executor) {
+ public PmdSensor(RulesProfile profile, RuleFinder rulesFinder, PmdExecutor executor) {
this.profile = profile;
- this.rulesManager = rulesManager;
+ this.rulesFinder = rulesFinder;
this.executor = executor;
}
}
try {
File xmlReport = executor.execute();
- AbstractViolationsStaxParser parser = getStaxParser(project, context);
- parser.parse(xmlReport);
+ getStaxParser(project, context).parse(xmlReport);
} catch (Exception e) {
// TOFIX
(!profile.getActiveRulesByRepository(PmdConstants.REPOSITORY_KEY).isEmpty() || project.getReuseExistingRulesConfig());
}
- private AbstractViolationsStaxParser getStaxParser(Project project, SensorContext context) {
- return new PmdViolationsXmlParser(project, context, rulesManager, profile);
+ private PmdViolationsXmlParser getStaxParser(Project project, SensorContext context) {
+ return new PmdViolationsXmlParser(project, rulesFinder, context);
}
@Override
*/
package org.sonar.plugins.pmd;
+import java.io.File;
+
+import javax.xml.stream.XMLStreamException;
+
import org.apache.commons.lang.StringUtils;
+import org.codehaus.staxmate.in.SMHierarchicCursor;
import org.codehaus.staxmate.in.SMInputCursor;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.AbstractViolationsStaxParser;
import org.sonar.api.batch.SensorContext;
-import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.RulesManager;
-
-import javax.xml.stream.XMLStreamException;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.Violation;
+import org.sonar.api.utils.StaxParser;
-class PmdViolationsXmlParser extends AbstractViolationsStaxParser {
+class PmdViolationsXmlParser {
private Project project;
+ private RuleFinder ruleFinder;
+ private SensorContext context;
- PmdViolationsXmlParser(Project project, SensorContext context, RulesManager rulesManager, RulesProfile profile) {
- super(context, rulesManager, profile);
+ public PmdViolationsXmlParser(Project project, RuleFinder ruleFinder, SensorContext context) {
this.project = project;
+ this.ruleFinder = ruleFinder;
+ this.context = context;
}
- @Override
- protected String keyForPlugin() {
- return CoreProperties.PMD_PLUGIN;
+ public void parse(File file) throws XMLStreamException {
+ StaxParser parser = new StaxParser(new StreamHandler(), true);
+ parser.parse(file);
}
- @Override
- protected SMInputCursor cursorForResources(SMInputCursor rootCursor) throws XMLStreamException {
- return rootCursor.descendantElementCursor("file");
- }
+ private class StreamHandler implements StaxParser.XmlStreamHandler {
+ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
+ rootCursor.advance();
- @Override
- protected SMInputCursor cursorForViolations(SMInputCursor resourcesCursor) throws XMLStreamException {
- return resourcesCursor.descendantElementCursor("violation");
- }
+ SMInputCursor fileCursor = rootCursor.descendantElementCursor("file");
+ while (fileCursor.getNext() != null) {
+ String name = fileCursor.getAttrValue("name");
+ Resource resource = JavaFile.fromAbsolutePath(name, project.getFileSystem().getSourceDirs(), false);
- @Override
- protected String lineNumberForViolation(SMInputCursor violationCursor) throws XMLStreamException {
- return violationCursor.getAttrValue("beginline");
- }
+ // Save violations only for existing resources
+ if (context.getResource(resource) != null) {
+ streamViolations(fileCursor, resource);
+ }
+ }
+ }
- @Override
- protected String messageFor(SMInputCursor violationCursor) throws XMLStreamException {
- return StringUtils.trim(violationCursor.collectDescendantText());
- }
+ private void streamViolations(SMInputCursor fileCursor, Resource resource) throws XMLStreamException {
+ SMInputCursor violationCursor = fileCursor.descendantElementCursor("violation");
+ while (violationCursor.getNext() != null) {
+ int lineId = Integer.parseInt(violationCursor.getAttrValue("beginline"));
+ String ruleKey = violationCursor.getAttrValue("rule");
+ String message = StringUtils.trim(violationCursor.collectDescendantText());
- @Override
- protected String ruleKey(SMInputCursor violationCursor) throws XMLStreamException {
- return violationCursor.getAttrValue("rule");
+ Rule rule = ruleFinder.findByKey(CoreProperties.PMD_PLUGIN, ruleKey);
+ // Save violations only for enabled rules
+ if (rule != null) {
+ Violation violation = Violation.create(rule, resource).setLineId(lineId).setMessage(message);
+ context.saveViolation(violation);
+ }
+ }
+ }
}
- @Override
- protected Resource toResource(SMInputCursor resourcesCursor) throws XMLStreamException {
- return JavaFile.fromAbsolutePath(resourcesCursor.getAttrValue("name"), project.getFileSystem().getSourceDirs(), false);
- }
}
*/
package org.sonar.plugins.pmd;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.net.URISyntaxException;
+import java.util.Arrays;
+
+import javax.xml.stream.XMLStreamException;
+
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Test;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Mockito.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sonar.api.batch.SensorContext;
-import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.DefaultProjectFileSystem;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
-import org.sonar.api.rules.*;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.Violation;
import org.sonar.api.test.IsViolation;
-import javax.xml.stream.XMLStreamException;
-import java.io.File;
-import java.net.URISyntaxException;
-import java.util.Arrays;
-
public class PmdViolationsXmlParserTest {
private void parse(SensorContext context, String xmlPath) throws URISyntaxException, XMLStreamException {
Project project = mock(Project.class);
when(project.getFileSystem()).thenReturn(fileSystem);
- RulesManager manager = mock(RulesManager.class);
- when(manager.getPluginRule(anyString(), anyString())).thenAnswer(new Answer<Rule>() {
- public Rule answer(InvocationOnMock invocation) {
+ RuleFinder ruleFinder = mock(RuleFinder.class);
+ when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {
+ public Rule answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
- return new Rule((String) args[1], (String) args[1], null, (String) args[0], "");
+ return Rule.create((String) args[0], (String) args[1], "");
}
});
- RulesProfile profile = mock(RulesProfile.class);
- when(profile.getActiveRule(anyString(), anyString())).thenReturn(new ActiveRule(null, null, RulePriority.MINOR));
- PmdViolationsXmlParser parser = new PmdViolationsXmlParser(project, context, manager, profile);
+
+ when(context.getResource((JavaFile) any())).thenReturn(new JavaFile(""));
+
+ PmdViolationsXmlParser parser = new PmdViolationsXmlParser(project, ruleFinder, context);
File xmlFile = new File(getClass().getResource(xmlPath).toURI());
parser.parse(xmlFile);
verify(context, times(30)).saveViolation(argThat(new IsViolationOnJavaClass()));
verify(context, times(4)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("ch.hortis.sonar.mvn.ClassWithComments"))));
- Violation wanted = Violation.create((Rule)null, new JavaFile("ch.hortis.sonar.mvn.ClassWithComments"))
+ Violation wanted = Violation.create((Rule) null, new JavaFile("ch.hortis.sonar.mvn.ClassWithComments"))
.setMessage("Avoid unused local variables such as 'toto'.")
.setLineId(22);
verify(context, times(1)).saveViolation(argThat(new IsViolation(wanted)));
verify(context, times(2)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("test.Test"))));
}
-
@Test
public void ISOControlCharsXMLFile() throws URISyntaxException, XMLStreamException {
SensorContext context = mock(SensorContext.class);
verify(context, times(1)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("test.Test"))));
}
-
private class IsViolationOnJavaClass extends BaseMatcher<Violation> {
private JavaFile javaClass;
<description>Squid analyzer for Java.</description>
<dependencies>
- <dependency>
- <groupId>org.codehaus.sonar</groupId>
- <artifactId>sonar-deprecated</artifactId>
- <scope>provided</scope>
- </dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-java-api</artifactId>