From 7ad796bce6ae648a74486a434fc155737a1cf98e Mon Sep 17 00:00:00 2001 From: Godin Date: Thu, 28 Oct 2010 13:14:57 +0000 Subject: [PATCH] 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 --- .../sonar/plugins/pmd/PmdProfileExporter.java | 26 ++++++---- .../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 properties = null; if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) { - properties = new ArrayList(); + List properties = new ArrayList(); 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"; -- 2.39.5