]> source.dussan.org Git - sonarqube.git/blob
ea31e62d13216fb13627b725937699b074e41e6a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program 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.
10  *
11  * This program 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.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.computation.task.projectanalysis.formula.counter;
21
22 import com.google.common.base.Optional;
23 import com.google.common.collect.ImmutableList;
24 import java.util.List;
25 import org.junit.Test;
26 import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
27 import org.sonar.server.computation.task.projectanalysis.period.Period;
28 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.guava.api.Assertions.assertThat;
32
33 public class LongVariationValueArrayTest {
34   private static final List<Period> PERIODS;
35
36   static {
37     ImmutableList.Builder<Period> builder = ImmutableList.builder();
38     for (int i = 1; i <= PeriodsHolder.MAX_NUMBER_OF_PERIODS; i++) {
39       builder.add(createPeriod(i));
40     }
41     PERIODS = builder.build();
42   }
43
44   @Test
45   public void newArray_always_returns_a_new_instance() {
46     assertThat(LongVariationValue.newArray()).isNotSameAs(LongVariationValue.newArray());
47   }
48
49   @Test
50   public void get_returns_unset_LongVariationValue_for_each_Period_index() {
51     LongVariationValue.Array array = LongVariationValue.newArray();
52     for (Period period : PERIODS) {
53       LongVariationValue value = array.get(period);
54       verifyUnsetLongVariation(value);
55     }
56   }
57
58   @Test
59   public void get_returns_set_LongVariationValue_for_each_Period_index_if_increment_has_been_called() {
60     LongVariationValue.Array array = LongVariationValue.newArray();
61     for (Period period : PERIODS) {
62       array.increment(period, 66L);
63       LongVariationValue value = array.get(period);
64       verifySetLongVariation(value, 66L);
65     }
66   }
67
68   @Test
69   public void incrementAll_increments_internals_from_all_set_LongVariationValue_from_source() {
70     LongVariationValue.Array source = LongVariationValue.newArray()
71       .increment(createPeriod(2), 20l)
72       .increment(createPeriod(5), 5l);
73
74     LongVariationValue.Array target = LongVariationValue.newArray()
75       .increment(createPeriod(1), 1L)
76       .increment(createPeriod(5), 30L);
77     target.incrementAll(source);
78
79     verifySetLongVariation(target.get(createPeriod(1)), 1L);
80     verifySetLongVariation(target.get(createPeriod(2)), 20L);
81     verifyUnsetLongVariation(target.get(createPeriod(3)));
82     verifyUnsetLongVariation(target.get(createPeriod(4)));
83     verifySetLongVariation(target.get(createPeriod(5)), 35L);
84   }
85
86   @Test
87   public void toMeasureVariations_returns_absent_if_no_LongVariationValue_has_been_set() {
88     assertThat(LongVariationValue.newArray().toMeasureVariations()).isAbsent();
89   }
90
91   @Test
92   public void toMeasureVariations_returns_value_of_set_LongVariationValue_as_double() {
93     Optional<MeasureVariations> variationsOptional = LongVariationValue.newArray()
94       .increment(createPeriod(2), 2L)
95       .increment(createPeriod(5), 15L)
96       .toMeasureVariations();
97
98     assertThat(variationsOptional).isPresent();
99     MeasureVariations variations = variationsOptional.get();
100     assertThat(variations.hasVariation1()).isFalse();
101     assertThat(variations.getVariation2()).isEqualTo(2d);
102     assertThat(variations.hasVariation3()).isFalse();
103     assertThat(variations.hasVariation4()).isFalse();
104     assertThat(variations.getVariation5()).isEqualTo(15d);
105   }
106
107   private static void verifyUnsetLongVariation(LongVariationValue value) {
108     assertThat(value.isSet()).isFalse();
109     assertThat(value.getValue()).isEqualTo(0L);
110   }
111
112   private static void verifySetLongVariation(LongVariationValue value, long expectedValue) {
113     assertThat(value.isSet()).isTrue();
114     assertThat(value.getValue()).isEqualTo(expectedValue);
115   }
116
117   private static Period createPeriod(int i) {
118     return new Period(i, "mode " + i, null, 100L + i, String.valueOf(753L + i));
119   }
120 }