2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
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.apache.commons.configuration.PropertiesConfiguration;
23 import org.junit.Test;
24 import org.sonar.api.batch.DecoratorContext;
25 import org.sonar.api.batch.TimeMachine;
26 import org.sonar.api.batch.TimeMachineQuery;
27 import org.sonar.api.measures.CoreMetrics;
28 import org.sonar.api.measures.Measure;
29 import org.sonar.api.measures.MetricFinder;
30 import org.sonar.api.resources.JavaPackage;
31 import org.sonar.api.resources.Project;
33 import java.text.ParseException;
34 import java.text.SimpleDateFormat;
35 import java.util.Arrays;
36 import java.util.Date;
38 import static org.hamcrest.CoreMatchers.is;
39 import static org.hamcrest.MatcherAssert.assertThat;
40 import static org.junit.matchers.JUnitMatchers.hasItems;
41 import static org.mockito.Mockito.*;
43 public class TendencyDecoratorTest {
46 public void initQuery() throws ParseException {
47 Project project = mock(Project.class);
48 when(project.getAnalysisDate()).thenReturn(date("2009-12-25"));
50 MetricFinder metricFinder = mock(MetricFinder.class);
51 when(metricFinder.findAll()).thenReturn(Arrays.asList(CoreMetrics.LINES, CoreMetrics.COVERAGE, CoreMetrics.COVERAGE_LINE_HITS_DATA, CoreMetrics.PROFILE));
53 TendencyDecorator decorator = new TendencyDecorator(null, metricFinder, newConf());
55 TimeMachineQuery query = decorator.initQuery(project);
56 assertThat(query.getMetrics().size(), is(2));
57 assertThat(query.getMetrics(), hasItems(CoreMetrics.LINES, CoreMetrics.COVERAGE));
58 assertThat(query.getFrom(), is(date("2009-11-25")));
59 assertThat(query.isToCurrentAnalysis(), is(true));
62 private TimeMachineConfiguration newConf() {
63 return new TimeMachineConfiguration(new PropertiesConfiguration());
67 public void includeCurrentMeasures() throws ParseException {
68 TendencyAnalyser analyser = mock(TendencyAnalyser.class);
69 TimeMachineQuery query = new TimeMachineQuery(null).setMetrics(CoreMetrics.LINES, CoreMetrics.COVERAGE);
70 TimeMachine timeMachine = mock(TimeMachine.class);
72 when(timeMachine.getMeasuresFields(query)).thenReturn(Arrays.<Object[]>asList(
73 new Object[]{date("2009-12-01"), CoreMetrics.LINES, 1200.0},
74 new Object[]{date("2009-12-01"), CoreMetrics.COVERAGE, 80.5},
75 new Object[]{date("2009-12-02"), CoreMetrics.LINES, 1300.0},
76 new Object[]{date("2009-12-02"), CoreMetrics.COVERAGE, 79.6},
77 new Object[]{date("2009-12-15"), CoreMetrics.LINES, 1150.0}
80 DecoratorContext context = mock(DecoratorContext.class);
81 when(context.getMeasure(CoreMetrics.LINES)).thenReturn(new Measure(CoreMetrics.LINES, 1400.0));
82 when(context.getMeasure(CoreMetrics.COVERAGE)).thenReturn(new Measure(CoreMetrics.LINES, 90.0));
84 TendencyDecorator decorator = new TendencyDecorator(timeMachine, query, analyser, newConf());
85 decorator.decorate(new JavaPackage("org.foo"), context);
87 verify(analyser).analyseLevel(Arrays.asList(1200.0, 1300.0, 1150.0, 1400.0));
88 verify(analyser).analyseLevel(Arrays.asList(80.5, 79.6, 90.0));
92 public void noTendencyIfNoCurrentMeasures() throws ParseException {
93 TendencyAnalyser analyser = mock(TendencyAnalyser.class);
94 TimeMachineQuery query = new TimeMachineQuery(null).setMetrics(CoreMetrics.LINES, CoreMetrics.COVERAGE);
95 TimeMachine timeMachine = mock(TimeMachine.class);
97 when(timeMachine.getMeasuresFields(query)).thenReturn(Arrays.<Object[]>asList(
98 new Object[]{date("2009-12-01"), CoreMetrics.LINES, 1200.0},
99 new Object[]{date("2009-12-02"), CoreMetrics.LINES, 1300.0}
102 DecoratorContext context = mock(DecoratorContext.class);
103 TendencyDecorator decorator = new TendencyDecorator(timeMachine, query, analyser, newConf());
104 decorator.decorate(new JavaPackage("org.foo"), context);
106 verify(analyser, never()).analyseLevel(anyList());
109 private Date date(String date) throws ParseException {
110 return new SimpleDateFormat("yyyy-MM-dd").parse(date);