diff options
author | Godin <mandrikov@gmail.com> | 2010-10-28 13:14:57 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-10-28 13:14:57 +0000 |
commit | 7ad796bce6ae648a74486a434fc155737a1cf98e (patch) | |
tree | a8f60aead6657df4ca4c3a180ccbe08d9a656419 /plugins | |
parent | d40b348450366c393f3de5505c13c03dac2deab5 (diff) | |
download | sonarqube-7ad796bce6ae648a74486a434fc155737a1cf98e.tar.gz sonarqube-7ad796bce6ae648a74486a434fc155737a1cf98e.zip |
SONAR-1871: Generate a meaningful error message when the PMD XPATH rule is activated without setting the "XPATH_EXPRESSION_PARAM" and "XPATH_MESSAGE_PARAM" values
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java | 26 | ||||
-rw-r--r-- | plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java | 49 |
2 files changed, 52 insertions, 23 deletions
diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java index 87d9d1f5c3c..b2dc55984a6 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java @@ -19,12 +19,6 @@ */ package org.sonar.plugins.pmd; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.List; - import org.jdom.CDATA; import org.jdom.Document; import org.jdom.Element; @@ -41,6 +35,12 @@ import org.sonar.plugins.pmd.xml.PmdProperty; import org.sonar.plugins.pmd.xml.PmdRule; import org.sonar.plugins.pmd.xml.PmdRuleset; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + public class PmdProfileExporter extends ProfileExporter { public PmdProfileExporter() { @@ -66,14 +66,13 @@ public class PmdProfileExporter extends ProfileExporter { if (activeRule.getRule().getPluginName().equals(CoreProperties.PMD_PLUGIN)) { String configKey = activeRule.getRule().getConfigKey(); PmdRule rule = new PmdRule(configKey, PmdLevelUtils.toLevel(activeRule.getPriority())); - List<PmdProperty> properties = null; if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) { - properties = new ArrayList<PmdProperty>(); + List<PmdProperty> properties = new ArrayList<PmdProperty>(); for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) { properties.add(new PmdProperty(activeRuleParam.getRuleParam().getKey(), activeRuleParam.getValue())); } + rule.setProperties(properties); } - rule.setProperties(properties); ruleset.addRule(rule); processXPathRule(activeRule.getRuleKey(), rule); } @@ -84,9 +83,16 @@ public class PmdProfileExporter extends ProfileExporter { protected void processXPathRule(String sonarRuleKey, PmdRule rule) { if (PmdConstants.XPATH_CLASS.equals(rule.getRef())) { rule.setRef(null); - rule.setMessage(rule.getProperty(PmdConstants.XPATH_MESSAGE_PARAM).getValue()); + PmdProperty xpathMessage = rule.getProperty(PmdConstants.XPATH_MESSAGE_PARAM); + if (xpathMessage == null) { + throw new SonarException("Property '" + PmdConstants.XPATH_MESSAGE_PARAM + "' should be set for PMD rule " + sonarRuleKey); + } + rule.setMessage(xpathMessage.getValue()); rule.removeProperty(PmdConstants.XPATH_MESSAGE_PARAM); PmdProperty xpathExp = rule.getProperty(PmdConstants.XPATH_EXPRESSION_PARAM); + if (xpathExp == null) { + throw new SonarException("Property '" + PmdConstants.XPATH_EXPRESSION_PARAM + "' should be set for PMD rule " + sonarRuleKey); + } xpathExp.setCdataValue(xpathExp.getValue()); rule.setClazz(PmdConstants.XPATH_CLASS); rule.setName(sonarRuleKey); diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java index 438bb9d3c4d..c35e9440f45 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java @@ -1,29 +1,30 @@ package org.sonar.plugins.pmd; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.Collection; -import java.util.List; - import org.apache.commons.lang.StringUtils; import org.junit.Test; import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.rules.*; +import org.sonar.api.utils.SonarException; import org.sonar.api.utils.ValidationMessages; import org.sonar.plugins.pmd.xml.PmdProperty; import org.sonar.plugins.pmd.xml.PmdRule; import org.sonar.test.TestUtils; import org.xml.sax.SAXException; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Collection; +import java.util.List; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + public class PmdProfileExporterTest { private PmdProfileExporter exporter = new PmdProfileExporter(); @@ -60,6 +61,28 @@ public class PmdProfileExporterTest { StringUtils.remove(xmlOutput.toString(), '\r')); } + @Test(expected = SonarException.class) + public void shouldFailIfMessageNotProvidedForXPathRule() { + String xpathExpression = "xpathExpression"; + + PmdRule rule = new PmdRule(PmdConstants.XPATH_CLASS); + rule.addProperty(new PmdProperty(PmdConstants.XPATH_EXPRESSION_PARAM, xpathExpression)); + rule.setName("MyOwnRule"); + + exporter.processXPathRule("xpathKey", rule); + } + + @Test(expected = SonarException.class) + public void shouldFailIfXPathNotProvidedForXPathRule() { + String message = "This is bad"; + + PmdRule rule = new PmdRule(PmdConstants.XPATH_CLASS); + rule.addProperty(new PmdProperty(PmdConstants.XPATH_MESSAGE_PARAM, message)); + rule.setName("MyOwnRule"); + + exporter.processXPathRule("xpathKey", rule); + } + @Test public void testProcessingXPathRule() { String message = "This is bad"; |