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.junit.Test;
23 import org.sonar.api.database.model.MeasureModel;
24 import org.sonar.api.database.model.Snapshot;
25 import org.sonar.api.measures.Metric;
26 import org.sonar.jpa.test.AbstractDbUnitTestCase;
28 import java.util.Arrays;
29 import java.util.List;
31 import static org.hamcrest.CoreMatchers.anyOf;
32 import static org.hamcrest.CoreMatchers.is;
33 import static org.hamcrest.MatcherAssert.assertThat;
34 import static org.junit.internal.matchers.IsCollectionContaining.hasItems;
36 public class PastMeasuresLoaderTest extends AbstractDbUnitTestCase {
38 private static final int PROJECT_SNAPSHOT_ID = 1000;
39 private static final int PROJECT_ID = 1;
40 private static final int FILE_ID = 3;
43 public void shouldGetPastResourceMeasures() {
46 List<Metric> metrics = selectMetrics();
47 Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", PROJECT_SNAPSHOT_ID);
49 PastMeasuresLoader loader = new PastMeasuresLoader(getSession(), metrics);
50 List<MeasureModel> measures = loader.getPastMeasures(FILE_ID, projectSnapshot);
51 assertThat(measures.size(), is(2));
53 for (MeasureModel measure : measures) {
54 assertThat(measure.getId(), anyOf(is(5L), is(6L)));
55 assertThat(measure.getValue(), anyOf(is(5.0), is(60.0)));
60 public void shouldGetPastProjectMeasures() {
63 List<Metric> metrics = selectMetrics();
64 Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", PROJECT_SNAPSHOT_ID);
66 PastMeasuresLoader loader = new PastMeasuresLoader(getSession(), metrics);
67 List<MeasureModel> measures = loader.getPastMeasures(PROJECT_ID, projectSnapshot);
68 assertThat(measures.size(), is(2));
70 for (MeasureModel measure : measures) {
71 assertThat(measure.getId(), anyOf(is(1L), is(2L)));
72 assertThat(measure.getValue(), anyOf(is(60.0), is(80.0)));
77 public void shouldKeepOnlyNumericalMetrics() {
78 Metric ncloc = new Metric("ncloc", Metric.ValueType.INT);
80 Metric complexity = new Metric("complexity", Metric.ValueType.INT);
82 Metric data = new Metric("data", Metric.ValueType.DATA);
84 List<Metric> metrics = Arrays.asList(ncloc, complexity, data);
86 PastMeasuresLoader loader = new PastMeasuresLoader(getSession(), metrics);
88 assertThat(loader.getMetrics().size(), is(2));
89 assertThat(loader.getMetrics(), hasItems(ncloc, complexity));
92 private List<Metric> selectMetrics() {
93 return getSession().getResults(Metric.class);