From 1c4f8c4b434d15b7bac640e5581f91dd0a2a844a Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 5 Dec 2012 16:58:29 +0100 Subject: [PATCH] SONAR-1352 add alert period when importing profile --- .../sonar/api/profiles/XMLProfileParser.java | 10 +++- .../api/profiles/XMLProfileParserTest.java | 55 +++++++++++-------- .../importProfileWithAlerts.xml | 6 ++ 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileParser.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileParser.java index 8c8f876c00b..054a4c49c34 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileParser.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileParser.java @@ -209,6 +209,7 @@ public final class XMLProfileParser implements ServerComponent { SMInputCursor alertCursor = alertsCursor.childElementCursor(); String metricKey = null, operator = "", valueError = "", valueWarning = ""; + Integer period = null; while (alertCursor.getNext() != null) { String nodeName = alertCursor.getLocalName(); @@ -216,7 +217,12 @@ public final class XMLProfileParser implements ServerComponent { if (StringUtils.equals("metric", nodeName)) { metricKey = StringUtils.trim(alertCursor.collectDescendantText(false)); - } else if (StringUtils.equals("operator", nodeName)) { + } else if (StringUtils.equals("period", nodeName)) { + String periodParameter = StringUtils.trim(alertCursor.collectDescendantText(false)); + if (StringUtils.isNotBlank(periodParameter)) { + period = Integer.parseInt(periodParameter); + } + }else if (StringUtils.equals("operator", nodeName)) { operator = StringUtils.trim(alertCursor.collectDescendantText(false)); } else if (StringUtils.equals("warning", nodeName)) { @@ -231,7 +237,7 @@ public final class XMLProfileParser implements ServerComponent { if (metric == null) { messages.addWarningText("Metric '" + metricKey + "' does not exist"); } else { - Alert alert = new Alert(profile, metric, operator, valueError, valueWarning); + Alert alert = new Alert(profile, metric, operator, valueError, valueWarning, period); profile.getAlerts().add(alert); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileParserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileParserTest.java index 358f3898a2d..38d1100a451 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileParserTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileParserTest.java @@ -30,11 +30,7 @@ import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RulePriority; import org.sonar.api.utils.ValidationMessages; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.internal.matchers.StringContains.containsString; +import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -46,11 +42,12 @@ public class XMLProfileParserTest { ValidationMessages validation = ValidationMessages.create(); RulesProfile profile = parse("importProfile.xml", validation); - assertThat(profile.getLanguage(), is("java")); - assertThat(profile.getName(), is("sonar way")); - assertThat(validation.hasErrors(), is(false)); - assertNotNull(profile); - assertThat(profile.getActiveRule("checkstyle", "IllegalRegexp").getSeverity(), is(RulePriority.CRITICAL)); + assertThat(profile.getLanguage()).isEqualTo("java"); + assertThat(profile.getName()).isEqualTo("sonar way"); + assertThat(validation.hasErrors()).isFalse(); + assertThat(profile).isNotNull(); + + assertThat(profile.getActiveRule("checkstyle", "IllegalRegexp").getSeverity()).isEqualTo(RulePriority.CRITICAL); } @Test @@ -58,8 +55,8 @@ public class XMLProfileParserTest { ValidationMessages validation = ValidationMessages.create(); parse("nameAndLanguageShouldBeMandatory.xml", validation); - assertThat(validation.getErrors().size(), is(2)); - assertThat(validation.getErrors().get(0), containsString("")); + assertThat(validation.getErrors()).hasSize(2); + assertThat(validation.getErrors().get(0)).contains(""); } @Test @@ -67,11 +64,12 @@ public class XMLProfileParserTest { ValidationMessages validation = ValidationMessages.create(); RulesProfile profile = parse("importProfileWithRuleParameters.xml", validation); - assertThat(validation.hasErrors(), is(false)); - assertThat(validation.hasWarnings(), is(false)); + assertThat(validation.hasErrors()).isFalse(); + assertThat(validation.hasWarnings()).isFalse(); + ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp"); - assertThat(rule.getParameter("format"), is("foo")); - assertThat(rule.getParameter("message"), is("with special characters < > &")); + assertThat(rule.getParameter("format")).isEqualTo("foo"); + assertThat(rule.getParameter("message")).isEqualTo("with special characters < > &"); } @Test @@ -79,9 +77,9 @@ public class XMLProfileParserTest { ValidationMessages validation = ValidationMessages.create(); RulesProfile profile = parse("importProfileWithUnknownRuleParameter.xml", validation); - assertThat(validation.getWarnings().size(), is(1)); + assertThat(validation.getWarnings()).hasSize(1); ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp"); - assertThat(rule.getParameter("unknown"), nullValue()); + assertThat(rule.getParameter("unknown")).isNull(); } @Test @@ -89,12 +87,21 @@ public class XMLProfileParserTest { ValidationMessages validation = ValidationMessages.create(); RulesProfile profile = parse("importProfileWithAlerts.xml", validation); - assertThat(profile.getAlerts().size(), is(1)); + assertThat(profile.getAlerts()).hasSize(2); + Alert alert = profile.getAlerts().get(0); - assertThat(alert.getMetric().getKey(), is("complexity")); - assertThat(alert.getOperator(), is(Alert.OPERATOR_GREATER)); - assertThat(alert.getValueWarning(), is("10")); - assertThat(alert.getValueError(), is("12")); + assertThat(alert.getMetric().getKey()).isEqualTo("lines"); + assertThat(alert.getOperator()).isEqualTo(Alert.OPERATOR_SMALLER); + assertThat(alert.getValueWarning()).isEqualTo("0"); + assertThat(alert.getValueError()).isEqualTo("10"); + assertThat(alert.getPeriod()).isNull(); + + alert = profile.getAlerts().get(1); + assertThat(alert.getMetric().getKey()).isEqualTo("complexity"); + assertThat(alert.getOperator()).isEqualTo(Alert.OPERATOR_GREATER); + assertThat(alert.getValueWarning()).isEqualTo("10"); + assertThat(alert.getValueError()).isEqualTo("12"); + assertThat(alert.getPeriod()).isEqualTo(1); } @Test @@ -103,7 +110,7 @@ public class XMLProfileParserTest { RulesProfile profile = new XMLProfileParser(newRuleFinder(), null) .parseResource(getClass().getClassLoader(), getResourcePath("importProfileWithAlerts.xml"), validation); - assertThat(profile.getAlerts().size(), is(0)); + assertThat(profile.getAlerts()).isEmpty(); } private RulesProfile parse(String resource, ValidationMessages validation) { diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithAlerts.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithAlerts.xml index 5b5f42632cb..95f857b0e22 100644 --- a/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithAlerts.xml +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithAlerts.xml @@ -11,6 +11,12 @@ + + lines + < + 0 + 10 + complexity 1 -- 2.39.5