2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.core.timemachine;
22 import org.junit.Test;
23 import org.mockito.Matchers;
24 import org.sonar.api.batch.DecoratorContext;
25 import org.sonar.api.database.model.MeasureModel;
26 import org.sonar.api.database.model.Snapshot;
27 import org.sonar.api.measures.Measure;
28 import org.sonar.api.measures.MeasuresFilter;
29 import org.sonar.api.measures.Metric;
30 import org.sonar.api.measures.MetricFinder;
31 import org.sonar.api.resources.*;
32 import org.sonar.batch.components.PastMeasuresLoader;
33 import org.sonar.batch.components.PastSnapshot;
34 import org.sonar.jpa.test.AbstractDbUnitTestCase;
36 import java.util.Arrays;
37 import java.util.Date;
39 import static org.hamcrest.CoreMatchers.is;
40 import static org.hamcrest.MatcherAssert.assertThat;
41 import static org.hamcrest.core.IsNull.nullValue;
42 import static org.mockito.Mockito.*;
44 public class VariationDecoratorTest extends AbstractDbUnitTestCase {
46 public static final Metric NCLOC = new Metric("ncloc").setId(12);
47 public static final Metric COVERAGE = new Metric("coverage").setId(16);
50 public void shouldNotCalculateVariationsOnFiles() {
51 assertThat(VariationDecorator.shouldCalculateVariations(new Project("foo")), is(true));
52 assertThat(VariationDecorator.shouldCalculateVariations(new JavaPackage("org.foo")), is(true));
53 assertThat(VariationDecorator.shouldCalculateVariations(new Directory("org/foo")), is(true));
55 assertThat(VariationDecorator.shouldCalculateVariations(new JavaFile("org.foo.Bar")), is(false));
56 assertThat(VariationDecorator.shouldCalculateVariations(new JavaFile("org.foo.Bar", true)), is(false));
57 assertThat(VariationDecorator.shouldCalculateVariations(new File("org/foo/Bar.php")), is(false));
61 public void shouldCompareAndSaveVariation() {
62 Resource javaPackage = new JavaPackage("org.foo");
64 PastMeasuresLoader pastMeasuresLoader = mock(PastMeasuresLoader.class);
65 PastSnapshot pastSnapshot1 = new PastSnapshot("days", new Date()).setIndex(1);
66 PastSnapshot pastSnapshot3 = new PastSnapshot("days", new Date()).setIndex(3);
68 // first past analysis
69 when(pastMeasuresLoader.getPastMeasures(javaPackage, pastSnapshot1)).thenReturn(Arrays.asList(
70 newMeasureModel(NCLOC, 180.0),
71 newMeasureModel(COVERAGE, 75.0)));
73 // second past analysis
74 when(pastMeasuresLoader.getPastMeasures(javaPackage, pastSnapshot3)).thenReturn(Arrays.asList(
75 newMeasureModel(NCLOC, 240.0)));
78 DecoratorContext context = mock(DecoratorContext.class);
79 Measure currentNcloc = newMeasure(NCLOC, 200.0);
80 Measure currentCoverage = newMeasure(COVERAGE, 80.0);
81 when(context.getMeasures(Matchers.<MeasuresFilter>anyObject())).thenReturn(Arrays.asList(currentNcloc, currentCoverage));
83 VariationDecorator decorator = new VariationDecorator(pastMeasuresLoader, mock(MetricFinder.class), Arrays.asList(pastSnapshot1, pastSnapshot3));
84 decorator.decorate(javaPackage, context);
86 // context updated for each variation : 2 times for ncloc and 1 time for coverage
87 verify(context, times(3)).saveMeasure(Matchers.<Measure>anyObject());
89 assertThat(currentNcloc.getVariation1(), is(20.0));
90 assertThat(currentNcloc.getVariation2(), nullValue());
91 assertThat(currentNcloc.getVariation3(), is(-40.0));
93 assertThat(currentCoverage.getVariation1(), is(5.0));
94 assertThat(currentCoverage.getVariation2(), nullValue());
95 assertThat(currentCoverage.getVariation3(), nullValue());
98 private Measure newMeasure(Metric metric, double value) {
99 return new Measure(metric, value);
102 private MeasureModel newMeasureModel(Metric metric, double value) {
103 return new MeasureModel(metric.getId(), value);