aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/test/java/org/sonar/batch/index
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-03-20 16:12:20 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2015-03-23 10:55:44 +0100
commit7b2066e95e177700b41f86b30468cbca12cb5f5a (patch)
tree0bd38a280402b9483b5e5d552fbe6f8d2e870f37 /sonar-batch/src/test/java/org/sonar/batch/index
parent99c983079b5cd16c34f6407fcfac9cb5b7e821d9 (diff)
downloadsonarqube-7b2066e95e177700b41f86b30468cbca12cb5f5a.tar.gz
sonarqube-7b2066e95e177700b41f86b30468cbca12cb5f5a.zip
SONAR-6275 Feed measures in compute report
Diffstat (limited to 'sonar-batch/src/test/java/org/sonar/batch/index')
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/DuplicationPersisterTest.java94
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java222
2 files changed, 0 insertions, 316 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DuplicationPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DuplicationPersisterTest.java
deleted file mode 100644
index c12eee78b4c..00000000000
--- a/sonar-batch/src/test/java/org/sonar/batch/index/DuplicationPersisterTest.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.index;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.sensor.duplication.Duplication;
-import org.sonar.api.batch.sensor.duplication.internal.DefaultDuplication;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.MetricFinder;
-import org.sonar.api.resources.File;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.batch.duplication.DuplicationCache;
-import org.sonar.core.persistence.AbstractDaoTestCase;
-
-import java.util.Arrays;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class DuplicationPersisterTest extends AbstractDaoTestCase {
-
- @org.junit.Rule
- public ExpectedException thrown = ExpectedException.none();
-
- public static final int FILE_SNAPSHOT_ID = 3003;
-
- DuplicationPersister duplicationPersister;
- RuleFinder ruleFinder = mock(RuleFinder.class);
- File aFile = File.create("org/foo/Bar.java");
- Snapshot fileSnapshot = snapshot(FILE_SNAPSHOT_ID);
-
- private DuplicationCache duplicationCache;
-
- @Before
- public void mockResourcePersister() {
- duplicationCache = mock(DuplicationCache.class);
-
- ResourceCache resourceCache = mock(ResourceCache.class);
- BatchResource batchResource = mock(BatchResource.class);
- when(batchResource.resource()).thenReturn(aFile);
- when(batchResource.snapshotId()).thenReturn(FILE_SNAPSHOT_ID);
- when(resourceCache.get("foo:org/foo/Bar.java")).thenReturn(batchResource);
-
- MetricFinder metricFinder = mock(MetricFinder.class);
- when(metricFinder.findByKey(CoreMetrics.DUPLICATIONS_DATA_KEY)).thenReturn(CoreMetrics.DUPLICATIONS_DATA.setId(2));
-
- duplicationPersister = new DuplicationPersister(getMyBatis(), ruleFinder, resourceCache, duplicationCache, metricFinder);
- }
-
- @Test
- public void should_insert_duplications() {
- setupData("empty");
-
- Duplication.Block originBlock = new Duplication.Block("foo:org/foo/Bar.java", 1, 4);
-
- DefaultDuplication group = new DefaultDuplication().setOriginBlock(originBlock);
- group.duplicates().add(new Duplication.Block("foo:org/foo/Foo.java", 5, 9));
-
- when(duplicationCache.componentKeys()).thenReturn(Arrays.asList("foo:org/foo/Bar.java"));
-
- when(duplicationCache.byComponent("foo:org/foo/Bar.java")).thenReturn(Arrays.asList(group));
-
- duplicationPersister.persist();
-
- checkTables("shouldInsertDuplication", "project_measures");
- }
-
- private static Snapshot snapshot(int id) {
- Snapshot snapshot = mock(Snapshot.class);
- when(snapshot.getId()).thenReturn(id);
- return snapshot;
- }
-
-}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
deleted file mode 100644
index fb59aa4d0a5..00000000000
--- a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
+++ /dev/null
@@ -1,222 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.index;
-
-import org.apache.commons.lang.StringUtils;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
-import org.sonar.api.measures.PersistenceMode;
-import org.sonar.api.measures.RuleMeasure;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Project;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RulePriority;
-import org.sonar.batch.scan.measure.MeasureCache;
-import org.sonar.core.persistence.AbstractDaoTestCase;
-
-import java.util.Arrays;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class MeasurePersisterTest extends AbstractDaoTestCase {
-
- @org.junit.Rule
- public ExpectedException thrown = ExpectedException.none();
-
- private static final String TOO_LONG_FOR_VARCHAR_4000 = StringUtils.repeat("0123456789", 401);
-
- public static final int PROJECT_SNAPSHOT_ID = 3001;
- public static final int PACKAGE_SNAPSHOT_ID = 3002;
- public static final int FILE_SNAPSHOT_ID = 3003;
- public static final int COVERAGE_METRIC_ID = 2;
-
- MeasurePersister measurePersister;
- RuleFinder ruleFinder = mock(RuleFinder.class);
- Project project = new Project("foo");
- Directory aDirectory = Directory.create("org/foo");
- File aFile = File.create("org/foo/Bar.java");
- BatchResource projectResource = batchResource(project, PROJECT_SNAPSHOT_ID);
- BatchResource dirResource = batchResource(aDirectory, PACKAGE_SNAPSHOT_ID);
- BatchResource fileResource = batchResource(aFile, FILE_SNAPSHOT_ID);
- MeasureCache measureCache;
-
- @Before
- public void mockResourcePersister() {
- measureCache = mock(MeasureCache.class);
- ResourceCache resourceCache = mock(ResourceCache.class);
- when(resourceCache.get("foo")).thenReturn(projectResource);
- when(resourceCache.get("foo:org/foo/Bar.java")).thenReturn(fileResource);
- when(resourceCache.get("foo:org/foo")).thenReturn(dirResource);
-
- MetricFinder metricFinder = mock(MetricFinder.class);
- Metric ncloc = ncloc();
- Metric coverage = coverage();
- when(metricFinder.findByKey(ncloc.getKey())).thenReturn(ncloc);
- when(metricFinder.findByKey(coverage.getKey())).thenReturn(coverage);
-
- measurePersister = new MeasurePersister(getMyBatis(), ruleFinder, metricFinder, measureCache, resourceCache);
- }
-
- @Test
- public void should_insert_measure() {
- setupData("empty");
-
- Measure measure = new Measure(ncloc()).setValue(1234.0);
- when(measureCache.entries()).thenReturn(Arrays.asList(new Cache.Entry<Measure>(new String[] {"foo", "ncloc"}, measure)));
- measurePersister.persist();
-
- checkTables("shouldInsertMeasure", "project_measures");
- }
-
- @Test
- public void should_display_message_when_error_during_insert_measure() {
- setupData("empty");
-
- Measure measure = new Measure(ncloc()).setValue(1234.0).setAlertText(TOO_LONG_FOR_VARCHAR_4000);
- when(measureCache.entries()).thenReturn(Arrays.asList(new Cache.Entry<Measure>(new String[] {"foo", "ncloc"}, measure)));
-
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Unable to save some measures");
-
- measurePersister.persist();
- }
-
- @Test
- public void should_insert_rule_measure() {
- setupData("empty");
-
- Rule rule = Rule.create("pmd", "key");
- when(ruleFinder.findByKey(rule.ruleKey())).thenReturn(rule);
-
- Measure measure = new RuleMeasure(ncloc(), rule, RulePriority.MAJOR, 1).setValue(1234.0);
- when(measureCache.entries()).thenReturn(Arrays.asList(new Cache.Entry<Measure>(new String[] {"foo", "ncloc"}, measure)));
-
- measurePersister.persist();
-
- checkTables("shouldInsertRuleMeasure", "project_measures");
- }
-
- @Test
- public void should_insert_measure_with_text_data() {
- setupData("empty");
-
- Measure withLargeData = new Measure(ncloc()).setData(TOO_LONG_FOR_VARCHAR_4000);
- when(measureCache.entries()).thenReturn(Arrays.asList(new Cache.Entry<Measure>(new String[] {"foo", "ncloc"}, withLargeData)));
-
- measurePersister.persist();
-
- checkTables("shouldInsertMeasureWithLargeData", "project_measures");
- }
-
- @Test
- public void should_not_save_best_values() {
- setupData("empty");
-
- Measure measure = new Measure(coverage()).setValue(100.0);
- when(measureCache.entries()).thenReturn(Arrays.asList(new Cache.Entry<Measure>(new String[] {"foo:org/foo/Bar.java", "coverage"}, measure)));
-
- measurePersister.persist();
-
- assertEmptyTables("project_measures");
- }
-
- @Test
- public void should_not_save_memory_only_measures() {
- setupData("empty");
-
- Measure measure = new Measure("ncloc").setPersistenceMode(PersistenceMode.MEMORY);
- when(measureCache.entries()).thenReturn(Arrays.asList(new Cache.Entry<Measure>(new String[] {"foo:org/foo/Bar.java", "ncloc"}, measure)));
-
- measurePersister.persist();
-
- assertEmptyTables("project_measures");
- }
-
- @Test
- public void should_always_save_non_file_measures() {
- setupData("empty");
-
- Measure measure1 = new Measure(ncloc()).setValue(200.0);
- Measure measure2 = new Measure(ncloc()).setValue(300.0);
- when(measureCache.entries()).thenReturn(Arrays.asList(
- new Cache.Entry<Measure>(new String[] {"foo", "ncloc"}, measure1),
- new Cache.Entry<Measure>(new String[] {"foo:org/foo", "ncloc"}, measure2)));
-
- measurePersister.persist();
-
- checkTables("shouldAlwaysPersistNonFileMeasures", "project_measures");
- }
-
- @Test
- public void should_not_save_some_file_measures_with_best_value() {
- assertThat(MeasurePersister.shouldPersistMeasure(aFile, new Measure(CoreMetrics.LINES, 200.0))).isTrue();
- assertThat(MeasurePersister.shouldPersistMeasure(aFile, new Measure(CoreMetrics.DUPLICATED_LINES_DENSITY, 3.0))).isTrue();
-
- Measure duplicatedLines = new Measure(CoreMetrics.DUPLICATED_LINES_DENSITY, 0.0);
- assertThat(MeasurePersister.shouldPersistMeasure(aFile, duplicatedLines)).isFalse();
-
- duplicatedLines.setVariation1(0.0);
- assertThat(MeasurePersister.shouldPersistMeasure(aFile, duplicatedLines)).isFalse();
-
- duplicatedLines.setVariation1(-3.0);
- assertThat(MeasurePersister.shouldPersistMeasure(aFile, duplicatedLines)).isTrue();
- }
-
- @Test
- public void should_not_save_measures_without_data() {
- assertThat(MeasurePersister.shouldPersistMeasure(aFile, new Measure(CoreMetrics.LINES))).isFalse();
-
- Measure duplicatedLines = new Measure(CoreMetrics.DUPLICATED_LINES_DENSITY);
- assertThat(MeasurePersister.shouldPersistMeasure(aFile, duplicatedLines)).isFalse();
- }
-
- private static BatchResource batchResource(Resource resource, int id) {
- Snapshot snapshot = mock(Snapshot.class);
- when(snapshot.getId()).thenReturn(id);
- return new BatchResource(1, resource, null).setSnapshot(snapshot);
- }
-
- private static Metric ncloc() {
- Metric ncloc = mock(Metric.class);
- when(ncloc.getId()).thenReturn(1);
- when(ncloc.getKey()).thenReturn("ncloc");
- return ncloc;
- }
-
- private static Metric coverage() {
- Metric coverage = mock(Metric.class);
- when(coverage.getId()).thenReturn(COVERAGE_METRIC_ID);
- when(coverage.getKey()).thenReturn("coverage");
- when(coverage.isOptimizedBestValue()).thenReturn(true);
- when(coverage.getBestValue()).thenReturn(100.0);
- return coverage;
- }
-}