3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.formula.counter;
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;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.guava.api.Assertions.assertThat;
33 public class DoubleVariationValueArrayTest {
34 private static final List<Period> PERIODS;
37 ImmutableList.Builder<Period> builder = ImmutableList.builder();
38 for (int i = 1; i <= PeriodsHolder.MAX_NUMBER_OF_PERIODS; i++) {
39 builder.add(createPeriod(i));
41 PERIODS = builder.build();
45 public void newArray_always_returns_a_new_instance() {
46 assertThat(DoubleVariationValue.newArray()).isNotSameAs(DoubleVariationValue.newArray());
50 public void get_returns_unset_DoubleVariationValue_for_each_Period_index() {
51 DoubleVariationValue.Array array = DoubleVariationValue.newArray();
52 for (Period period : PERIODS) {
53 DoubleVariationValue value = array.get(period);
54 verifyUnsetVariationValue(value);
59 public void get_returns_set_DoubleVariationValue_for_each_Period_index_if_increment_has_been_called() {
60 DoubleVariationValue.Array array = DoubleVariationValue.newArray();
61 for (Period period : PERIODS) {
62 array.increment(period, 66.66);
63 DoubleVariationValue value = array.get(period);
64 verifySetDoubleVariation(value, 66.66);
69 public void incrementAll_increments_internals_from_all_set_DoubleVariationValue_from_source() {
70 DoubleVariationValue.Array source = DoubleVariationValue.newArray()
71 .increment(createPeriod(2), 20.6)
72 .increment(createPeriod(5), 5.5);
74 DoubleVariationValue.Array target = DoubleVariationValue.newArray()
75 .increment(createPeriod(1), 1.7)
76 .increment(createPeriod(5), 30.5);
77 target.incrementAll(source);
79 verifySetDoubleVariation(target.get(createPeriod(1)), 1.7);
80 verifySetDoubleVariation(target.get(createPeriod(2)), 20.6);
81 verifyUnsetVariationValue(target.get(createPeriod(3)));
82 verifyUnsetVariationValue(target.get(createPeriod(4)));
83 verifySetDoubleVariation(target.get(createPeriod(5)), 36);
87 public void toMeasureVariations_returns_absent_if_no_DoubleVariationValue_has_been_set() {
88 assertThat(DoubleVariationValue.newArray().toMeasureVariations()).isAbsent();
92 public void toMeasureVariations_returns_value_of_set_DoubleVariationValue_as_double() {
93 Optional<MeasureVariations> variationsOptional = DoubleVariationValue.newArray()
94 .increment(createPeriod(2), 2.6)
95 .increment(createPeriod(5), 15.9)
96 .toMeasureVariations();
98 assertThat(variationsOptional).isPresent();
99 MeasureVariations variations = variationsOptional.get();
100 assertThat(variations.hasVariation1()).isFalse();
101 assertThat(variations.getVariation2()).isEqualTo(2.6d);
102 assertThat(variations.hasVariation3()).isFalse();
103 assertThat(variations.hasVariation4()).isFalse();
104 assertThat(variations.getVariation5()).isEqualTo(15.9d);
107 private static void verifyUnsetVariationValue(DoubleVariationValue value) {
108 assertThat(value.isSet()).isFalse();
109 assertThat(value.getValue()).isEqualTo(0);
112 private static void verifySetDoubleVariation(DoubleVariationValue value, double expectedValue) {
113 assertThat(value.isSet()).isTrue();
114 assertThat(value.getValue()).isEqualTo(expectedValue);
117 private static Period createPeriod(int i) {
118 return new Period(i, "mode " + i, null, 100L + i, String.valueOf(753L + i));