2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 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;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
28 import static org.hamcrest.CoreMatchers.nullValue;
29 import static org.junit.Assert.*;
31 public class TendencyAnalyserTest {
32 private TendencyAnalyser analyser = new TendencyAnalyser();
34 private List<Double> getValues(Double[] array) {
35 return Arrays.asList(array);
39 protected void assertBetween(String typeLabel, Double value, Double min, Double max) {
40 assertTrue(typeLabel + " " + value + "<" + min, value >= min);
41 assertTrue(typeLabel + "=" + value + ">" + max, value <= max);
45 public void testNoData() {
46 assertThat(analyser.analyse(Collections.<Double>emptyList()), nullValue());
50 public void testNotEnoughData() {
51 assertThat(analyser.analyseLevel(Arrays.asList(10.0)), nullValue());
55 public void testTendencyOnThreeDays() {
56 Double[] doubles = new Double[]{10.0, null, 9.9};
57 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
58 assertBetween("slope", slopeData.getSlope(), -0.5, 0.5);
59 assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel());
63 public void testTendencyOnTwoZeroDays() {
64 Double[] doubles = new Double[]{0.0, 0.0};
65 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
66 assertBetween("slope", slopeData.getSlope(), -0.0, 0.0);
67 assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel());
71 public void testTendencyOnThreeZeroDays() {
72 Double[] doubles = new Double[]{0.0, 0.0, 0.0};
73 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
74 assertBetween("slope", slopeData.getSlope(), -0.0, 0.0);
75 assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel());
79 public void testBigDownOnThreeDays() {
80 Double[] doubles = new Double[]{90.0, 91.0, 50.0};
81 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
82 assertTrue("slope", slopeData.getSlope() < -2.0);
83 assertEquals(TendencyAnalyser.TENDENCY_BIG_DOWN, slopeData.getLevel());
87 public void testFlatTendency() {
88 Double[] doubles = new Double[]{10.0, 10.2, 9.9};
89 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
90 assertBetween("slope", slopeData.getSlope(), -0.5, 0.5);
91 assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel());
95 public void testFlatTendencyWithPeak() {
96 Double[] doubles = new Double[]{10.0, 15.0, 10.0};
97 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
98 assertBetween("slope", slopeData.getSlope(), -0.5, 0.5);
99 assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel());
103 public void testBigUpTendencyOnThreeValues() {
104 Double[] doubles = new Double[]{10.0, 12.0, 15.5};
105 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
106 assertBetween("slope", slopeData.getSlope(), 2.5, 3.0);
107 assertEquals(TendencyAnalyser.TENDENCY_BIG_UP, slopeData.getLevel());
111 public void testBigUpTendencyOnTenValues() {
112 Double[] doubles = new Double[]{45.0, 60.0, 57.0, 65.0, 58.0, 68.0, 59.0, 66.0, 76.0, 80.0};
113 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
114 assertBetween("slope", slopeData.getSlope(), 2.5, 3.0);
115 assertEquals(TendencyAnalyser.TENDENCY_BIG_UP, slopeData.getLevel());
119 public void testMediumUpTendency() {
120 Double[] doubles = new Double[]{5.0, 4.5, 5.1, 5.5, 5.3, 6.4, 6.3, 6.6, 6.8, 6.5};
121 TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles));
122 assertBetween("slope", slopeData.getSlope(), 0.0, 1.0);
123 assertEquals(TendencyAnalyser.TENDENCY_UP, slopeData.getLevel());
127 public void testAsymetricAlgorithm() {
128 TendencyAnalyser.SlopeData slopeData1 = analyser.analyse(getValues(new Double[]{45.0, 47.0, 95.0}));
129 TendencyAnalyser.SlopeData slopeData2 = analyser.analyse(getValues(new Double[]{95.0, 45.0, 47.0}));
130 assertTrue(slopeData1.getSlope() != slopeData2.getSlope());