]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1352 add alert period when importing profile
authorJulien Lancelot <julien.lancelot@gmail.com>
Wed, 5 Dec 2012 15:58:29 +0000 (16:58 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Wed, 5 Dec 2012 15:58:29 +0000 (16:58 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileParser.java
sonar-plugin-api/src/test/java/org/sonar/api/profiles/XMLProfileParserTest.java
sonar-plugin-api/src/test/resources/org/sonar/api/profiles/XMLProfileParserTest/importProfileWithAlerts.xml

index 8c8f876c00b09143a9ec30823a4d01988ecf1db1..054a4c49c34093763f75eeb2e63dc61d1cf453bb 100644 (file)
@@ -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);
       }
     }
index 358f3898a2d4eef2eacc69cfffcc55357946f734..38d1100a451756a385e900f0eb9d79a6eff38542 100644 (file)
@@ -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) {
index 5b5f42632cbfa7d09576bf620d480d2610fd3a52..95f857b0e22b7d082bbc7b680144a79ff0c4962f 100644 (file)
     </rule>
   </rules>
   <alerts>
+    <alert>
+      <metric>lines</metric>
+      <operator>&lt;</operator>
+      <warning>0</warning>
+      <error>10</error>
+    </alert>
     <alert>
       <metric>complexity</metric>
       <period>1</period>