+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
package org.sonar.plugins.core.timemachine;
import org.apache.commons.lang.time.DateUtils;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Violation;
+import org.sonar.api.utils.SonarException;
import java.util.Date;
Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS);
for (int index = 0; index < 3; index++) {
int days = timeMachineConfiguration.getDiffPeriodInDays(index);
- setDiffValue(measure, index, calculate(context, days));
+ double value = calculate(context, days) + sumChildren(context, index);
+ setDiffValue(measure, index, value);
}
context.saveMeasure(measure);
}
return newViolations;
}
+ double sumChildren(DecoratorContext context, int index) {
+ double sum = 0;
+ for (Measure measure : context.getChildrenMeasures(CoreMetrics.NEW_VIOLATIONS)) {
+ sum = sum + getDiffValue(measure, index);
+ }
+ return sum;
+ }
+
private Date getTargetDate(Project project, int distanceInDays) {
return DateUtils.addDays(project.getAnalysisDate(), -distanceInDays);
}
measure.setDiffValue2(value);
break;
case 2:
- measure.setDiffValue2(value);
+ measure.setDiffValue3(value);
break;
default:
- break;
+ throw new SonarException("Should never happen");
+ }
+ }
+
+ private double getDiffValue(Measure measure, int index) {
+ switch (index) {
+ case 0:
+ return measure.getDiffValue1();
+ case 1:
+ return measure.getDiffValue2();
+ case 2:
+ return measure.getDiffValue3();
+ default:
+ throw new SonarException("Should never happen");
}
}
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.Violation;
}
@Test
- public void test() {
+ public void shouldCalculate() {
DecoratorContext context = mock(DecoratorContext.class);
Date date1 = new Date();
Date date2 = DateUtils.addDays(date1, -20);
assertThat(decorator.calculate(context, 10), is(1));
assertThat(decorator.calculate(context, 30), is(2));
}
+
+ @Test
+ public void shouldSumChildren() {
+ DecoratorContext context = mock(DecoratorContext.class);
+ Measure measure1 = new Measure(CoreMetrics.NEW_VIOLATIONS).setDiffValue1(1.0).setDiffValue2(1.0);
+ Measure measure2 = new Measure(CoreMetrics.NEW_VIOLATIONS).setDiffValue1(1.0).setDiffValue2(2.0);
+ when(context.getChildrenMeasures(CoreMetrics.NEW_VIOLATIONS)).thenReturn(Arrays.asList(measure1, measure2));
+
+ assertThat(decorator.sumChildren(context, 0), is(2.0));
+ assertThat(decorator.sumChildren(context, 1), is(3.0));
+ }
}
*/
package org.sonar.plugins.core.timemachine;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.nullValue;
+
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Test;
import org.sonar.api.CoreProperties;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
public class TimeMachineConfigurationTest {
@Test
assertThat(new TimeMachineConfiguration(conf, null).skipTendencies(), is(false));
}
+ @Test
+ public void shouldReturnDiffPeriodInDays() {
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.timemachine.diff0", "30");
+ assertThat(new TimeMachineConfiguration(conf, null).getDiffPeriodInDays(0), is(30));
+ assertThat(new TimeMachineConfiguration(conf, null).getDiffPeriodInDays(1), nullValue());
+ }
+
}