/* * Sonar, open source software quality management tool. * Copyright (C) 2008-2012 SonarSource * mailto:contact AT sonarsource DOT com * * Sonar is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * Sonar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ package org.sonar.api.measures; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import org.junit.Test; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class CountDistributionBuilderTest { @Test public void buildDistribution() { CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION); Measure measure = builder .add("foo") .add("bar") .add("foo") .add("hello") .build(); assertThat(measure.getData(), is("bar=1;foo=2;hello=1")); } @Test public void addZeroValues() { CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION); Measure measure = builder .addZero("foo") .add("bar") .add("foo") .addZero("hello") .build(); assertThat(measure.getData(), is("bar=1;foo=1;hello=0")); } @Test public void addDistributionMeasureAsStrings() { Measure measureToAdd = mock(Measure.class); when(measureToAdd.getData()).thenReturn("foo=3;hello=5;none=0"); CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION); Measure measure = builder .add("bar") .add("foo") .add(measureToAdd) .build(); assertThat(measure.getData(), is("bar=1;foo=4;hello=5;none=0")); } @Test public void intervalsAreSorted() { Measure measureToAdd = mock(Measure.class); when(measureToAdd.getData()).thenReturn("10=5;3=2;1=3"); CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION); Measure measure = builder .add(10) .add(measureToAdd) .add(1) .build(); assertThat(measure.getData(), is("1=4;3=2;10=6")); } }