diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-04-28 01:22:08 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-04-28 01:22:08 +0200 |
commit | bd604e40905e54bef42a206a8a638f03783aba3a (patch) | |
tree | a6adc8867f9473dfe09c7e719c0d0e6b81e9b99c /sonar-batch/src | |
parent | ffdc8908a25b4f5e071c1797d0bf7b40a9f9d8d5 (diff) | |
download | sonarqube-bd604e40905e54bef42a206a8a638f03783aba3a.tar.gz sonarqube-bd604e40905e54bef42a206a8a638f03783aba3a.zip |
SONAR-2202 Calculate variations on quality model measures
Diffstat (limited to 'sonar-batch/src')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java | 41 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java | 31 |
2 files changed, 46 insertions, 26 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java index 19efcadd46d..95e5dff7bee 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java @@ -23,7 +23,6 @@ import com.google.common.collect.Maps; import org.apache.commons.lang.ObjectUtils; import org.sonar.api.BatchExtension; import org.sonar.api.database.DatabaseSession; -import org.sonar.api.database.model.MeasureModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.measures.Metric; import org.sonar.api.measures.MetricFinder; @@ -57,24 +56,18 @@ public class PastMeasuresLoader implements BatchExtension { return metricByIds.values(); } - public List<MeasureModel> getPastMeasures(Resource resource, PastSnapshot projectPastSnapshot) { - return getPastMeasures(resource, projectPastSnapshot.getProjectSnapshot()); - } - - public List<MeasureModel> getPastMeasures(Resource resource, Snapshot projectSnapshot) { - if (isPersisted(resource) && projectSnapshot!=null) { - return getPastMeasures(resource.getId(), projectSnapshot); + public List<Object[]> getPastMeasures(Resource resource, PastSnapshot projectPastSnapshot) { + if (isPersisted(resource) && projectPastSnapshot != null && projectPastSnapshot.getProjectSnapshot()!=null) { + return getPastMeasures(resource.getId(), projectPastSnapshot.getProjectSnapshot()); } return Collections.emptyList(); } - public List<MeasureModel> getPastMeasures(int resourceId, Snapshot projectSnapshot) { - // TODO improvement : select only some columns - // TODO support measure on characteristics - String hql = "select m from " + MeasureModel.class.getSimpleName() + " m, " + Snapshot.class.getSimpleName() + " s " + - "where m.snapshotId=s.id and m.metricId in (:metricIds) and m.ruleId=null and m.rulePriority=null and m.characteristic=null " - + "and (s.rootId=:rootSnapshotId or s.id=:rootSnapshotId) and s.resourceId=:resourceId and s.status=:status"; - return session.createQuery(hql) + public List<Object[]> getPastMeasures(int resourceId, Snapshot projectSnapshot) { + String sql = "select m.metric_id, m.characteristic_id, m.value from project_measures m, snapshots s" + + " where m.snapshot_id=s.id and m.metric_id in (:metricIds) and m.rule_id is null and m.rule_priority is null " + + " and (s.root_snapshot_id=:rootSnapshotId or s.id=:rootSnapshotId) and s.project_id=:resourceId and s.status=:status"; + return session.createNativeQuery(sql) .setParameter("metricIds", metricByIds.keySet()) .setParameter("rootSnapshotId", ObjectUtils.defaultIfNull(projectSnapshot.getRootId(), projectSnapshot.getId())) .setParameter("resourceId", resourceId) @@ -83,6 +76,22 @@ public class PastMeasuresLoader implements BatchExtension { } private boolean isPersisted(Resource resource) { - return resource.getId()!=null; + return resource.getId() != null; + } + + public static Integer getMetricId(Object[] row) { + return (Integer) row[0]; + } + + public static Integer getCharacteristicId(Object[] row) { + return (Integer) row[1]; + } + + public static boolean hasValue(Object[] row) { + return row[2] != null; + } + + public static double getValue(Object[] row) { + return ((Number) row[2]).doubleValue(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java index 2b2137eb288..74e19b28ddf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java @@ -32,6 +32,7 @@ import java.util.List; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.nullValue; import static org.junit.internal.matchers.IsCollectionContaining.hasItems; public class PastMeasuresLoaderTest extends AbstractDbUnitTestCase { @@ -48,13 +49,18 @@ public class PastMeasuresLoaderTest extends AbstractDbUnitTestCase { Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", PROJECT_SNAPSHOT_ID); PastMeasuresLoader loader = new PastMeasuresLoader(getSession(), metrics); - List<MeasureModel> measures = loader.getPastMeasures(FILE_ID, projectSnapshot); + List<Object[]> measures = loader.getPastMeasures(FILE_ID, projectSnapshot); assertThat(measures.size(), is(2)); - for (MeasureModel measure : measures) { - assertThat(measure.getId(), anyOf(is(5L), is(6L))); - assertThat(measure.getValue(), anyOf(is(5.0), is(60.0))); - } + Object[] pastMeasure = measures.get(0); + assertThat(PastMeasuresLoader.getMetricId(pastMeasure), is(1)); + assertThat(PastMeasuresLoader.getCharacteristicId(pastMeasure), nullValue()); + assertThat(PastMeasuresLoader.getValue(pastMeasure), is(5.0)); + + pastMeasure = measures.get(1); + assertThat(PastMeasuresLoader.getMetricId(pastMeasure), is(2)); + assertThat(PastMeasuresLoader.getCharacteristicId(pastMeasure), nullValue()); + assertThat(PastMeasuresLoader.getValue(pastMeasure), is(60.0)); } @Test @@ -65,13 +71,18 @@ public class PastMeasuresLoaderTest extends AbstractDbUnitTestCase { Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", PROJECT_SNAPSHOT_ID); PastMeasuresLoader loader = new PastMeasuresLoader(getSession(), metrics); - List<MeasureModel> measures = loader.getPastMeasures(PROJECT_ID, projectSnapshot); + List<Object[]> measures = loader.getPastMeasures(PROJECT_ID, projectSnapshot); assertThat(measures.size(), is(2)); - for (MeasureModel measure : measures) { - assertThat(measure.getId(), anyOf(is(1L), is(2L))); - assertThat(measure.getValue(), anyOf(is(60.0), is(80.0))); - } + Object[] pastMeasure = measures.get(0); + assertThat(PastMeasuresLoader.getMetricId(pastMeasure), is(1)); + assertThat(PastMeasuresLoader.getCharacteristicId(pastMeasure), nullValue()); + assertThat(PastMeasuresLoader.getValue(pastMeasure), is(60.0)); + + pastMeasure = measures.get(1); + assertThat(PastMeasuresLoader.getMetricId(pastMeasure), is(2)); + assertThat(PastMeasuresLoader.getCharacteristicId(pastMeasure), nullValue()); + assertThat(PastMeasuresLoader.getValue(pastMeasure), is(80.0)); } @Test |