From 734ad36b7c016f974c691bfa6f1b689ef91739df Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 23 Feb 2011 20:09:08 +0300 Subject: [PATCH] SONAR-2231 Don't use module sonar-deprecated in core plugins * Create new implementation of PmdViolationsXmlParser for sonar-pmd-plugin * Remove unused method in sonar-findbugs-plugin and remove dependency * Just remove dependency in sonar-squid-java-plugin --- plugins/sonar-findbugs-plugin/pom.xml | 6 -- .../sonar/plugins/findbugs/FindbugsTests.java | 23 ------ plugins/sonar-pmd-plugin/pom.xml | 7 +- .../java/org/sonar/plugins/pmd/PmdSensor.java | 20 +++-- .../plugins/pmd/PmdViolationsXmlParser.java | 79 +++++++++++-------- .../pmd/PmdViolationsXmlParserTest.java | 46 ++++++----- plugins/sonar-squid-java-plugin/pom.xml | 5 -- 7 files changed, 81 insertions(+), 105 deletions(-) diff --git a/plugins/sonar-findbugs-plugin/pom.xml b/plugins/sonar-findbugs-plugin/pom.xml index f5c63f130d6..2cceb3c3dc4 100644 --- a/plugins/sonar-findbugs-plugin/pom.xml +++ b/plugins/sonar-findbugs-plugin/pom.xml @@ -69,12 +69,6 @@ - - org.codehaus.sonar - sonar-deprecated - ${project.version} - provided - diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java index 900032273b2..55a63bd3bd3 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java @@ -19,25 +19,18 @@ */ 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; @@ -70,22 +63,6 @@ public abstract class FindbugsTests { return activeRules; } - protected RulesManager createRulesManager() { - RulesManager rulesManager = mock(RulesManager.class); - - when(rulesManager.getPluginRule(eq(CoreProperties.FINDBUGS_PLUGIN), anyString())).thenAnswer(new Answer() { - - 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); diff --git a/plugins/sonar-pmd-plugin/pom.xml b/plugins/sonar-pmd-plugin/pom.xml index 80e5344578f..29f01871f79 100644 --- a/plugins/sonar-pmd-plugin/pom.xml +++ b/plugins/sonar-pmd-plugin/pom.xml @@ -18,11 +18,6 @@ - - org.codehaus.sonar - sonar-deprecated - ${project.version} - org.codehaus.sonar sonar-java-api @@ -99,4 +94,4 @@ - \ No newline at end of file + diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java index 6228fd8af62..62d11cc7639 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java @@ -19,26 +19,25 @@ */ 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; } @@ -48,8 +47,7 @@ public class PmdSensor implements Sensor { } try { File xmlReport = executor.execute(); - AbstractViolationsStaxParser parser = getStaxParser(project, context); - parser.parse(xmlReport); + getStaxParser(project, context).parse(xmlReport); } catch (Exception e) { // TOFIX @@ -62,8 +60,8 @@ public class PmdSensor implements Sensor { (!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 diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java index 37cbc4f7801..6d34fe66170 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java @@ -19,60 +19,71 @@ */ 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); - } } diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java index 98e9a9a27fc..5286fcd3df8 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java @@ -19,27 +19,34 @@ */ 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 { @@ -49,16 +56,17 @@ public class PmdViolationsXmlParserTest { Project project = mock(Project.class); when(project.getFileSystem()).thenReturn(fileSystem); - RulesManager manager = mock(RulesManager.class); - when(manager.getPluginRule(anyString(), anyString())).thenAnswer(new Answer() { - public Rule answer(InvocationOnMock invocation) { + RuleFinder ruleFinder = mock(RuleFinder.class); + when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer() { + 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); @@ -72,7 +80,7 @@ public class PmdViolationsXmlParserTest { 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))); @@ -92,7 +100,6 @@ public class PmdViolationsXmlParserTest { verify(context, times(2)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("test.Test")))); } - @Test public void ISOControlCharsXMLFile() throws URISyntaxException, XMLStreamException { SensorContext context = mock(SensorContext.class); @@ -100,7 +107,6 @@ public class PmdViolationsXmlParserTest { verify(context, times(1)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("test.Test")))); } - private class IsViolationOnJavaClass extends BaseMatcher { private JavaFile javaClass; diff --git a/plugins/sonar-squid-java-plugin/pom.xml b/plugins/sonar-squid-java-plugin/pom.xml index 561fa6c7256..9832702f770 100644 --- a/plugins/sonar-squid-java-plugin/pom.xml +++ b/plugins/sonar-squid-java-plugin/pom.xml @@ -14,11 +14,6 @@ Squid analyzer for Java. - - org.codehaus.sonar - sonar-deprecated - provided - org.codehaus.sonar sonar-java-api -- 2.39.5