diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2012-07-10 10:46:19 +0200 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-07-12 10:33:31 +0200 |
commit | 24d7c761a444ec9dd861a7ac7831d4d7946fb9f5 (patch) | |
tree | 066e70b1f7496c181d486e1c2f48ab0b57621388 /sonar-batch | |
parent | 238334f4b4a9cbf9b4c1384e185082937548c058 (diff) | |
download | sonarqube-24d7c761a444ec9dd861a7ac7831d4d7946fb9f5.tar.gz sonarqube-24d7c761a444ec9dd861a7ac7831d4d7946fb9f5.zip |
SONAR-2496 Support 'previous-version' value for differential views
Diffstat (limited to 'sonar-batch')
9 files changed, 242 insertions, 30 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java index d4ebebb4889..45020ddf099 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java @@ -29,8 +29,23 @@ import org.sonar.batch.DefaultFileLinesContextFactory; import org.sonar.batch.DefaultResourceCreationLock; import org.sonar.batch.ProjectConfigurator; import org.sonar.batch.ProjectTree; -import org.sonar.batch.components.*; -import org.sonar.batch.index.*; +import org.sonar.batch.components.PastMeasuresLoader; +import org.sonar.batch.components.PastSnapshotFinder; +import org.sonar.batch.components.PastSnapshotFinderByDate; +import org.sonar.batch.components.PastSnapshotFinderByDays; +import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis; +import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion; +import org.sonar.batch.components.PastSnapshotFinderByVersion; +import org.sonar.batch.index.DefaultIndex; +import org.sonar.batch.index.DefaultPersistenceManager; +import org.sonar.batch.index.DefaultResourcePersister; +import org.sonar.batch.index.DependencyPersister; +import org.sonar.batch.index.EventPersister; +import org.sonar.batch.index.LinkPersister; +import org.sonar.batch.index.MeasurePersister; +import org.sonar.batch.index.MemoryOptimizer; +import org.sonar.batch.index.ReadOnlyPersistenceManager; +import org.sonar.batch.index.SourcePersister; import org.sonar.core.metric.CacheMetricFinder; import org.sonar.core.notification.DefaultNotificationManager; import org.sonar.core.rule.CacheRuleFinder; @@ -79,6 +94,7 @@ public class BatchModule extends Module { addCoreSingleton(PastSnapshotFinderByDays.class); addCoreSingleton(PastSnapshotFinderByPreviousAnalysis.class); addCoreSingleton(PastSnapshotFinderByVersion.class); + addCoreSingleton(PastSnapshotFinderByPreviousVersion.class); addCoreSingleton(PastMeasuresLoader.class); addCoreSingleton(PastSnapshotFinder.class); addCoreSingleton(DefaultNotificationManager.class); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java index ad5c54a59e9..f4087abe039 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java @@ -121,6 +121,13 @@ public class PastSnapshot { } return label; } + if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION)) { + String label = "Compare to previous version"; + if (isRelatedToSnapshot()) { + label += String.format(" (%s)", DateUtils.formatDate(getDate())); + } + return label; + } if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DATE)) { String label = "Compare to date " + DateUtils.formatDate(getTargetDate()); if (isRelatedToSnapshot()) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java index 2734e165077..7968679b26e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java @@ -39,13 +39,16 @@ public class PastSnapshotFinder implements BatchExtension { private PastSnapshotFinderByVersion finderByVersion; private PastSnapshotFinderByDate finderByDate; private PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis; + private PastSnapshotFinderByPreviousVersion finderByPreviousVersion; public PastSnapshotFinder(PastSnapshotFinderByDays finderByDays, PastSnapshotFinderByVersion finderByVersion, - PastSnapshotFinderByDate finderByDate, PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis) { + PastSnapshotFinderByDate finderByDate, PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis, + PastSnapshotFinderByPreviousVersion finderByPreviousVersion) { this.finderByDays = finderByDays; this.finderByVersion = finderByVersion; this.finderByDate = finderByDate; this.finderByPreviousAnalysis = finderByPreviousAnalysis; + this.finderByPreviousVersion = finderByPreviousVersion; } public PastSnapshot find(Snapshot projectSnapshot, Configuration conf, int index) { @@ -94,7 +97,10 @@ public class PastSnapshotFinder implements BatchExtension { if (result == null) { result = findByPreviousAnalysis(projectSnapshot, property); if (result == null) { - result = findByVersion(projectSnapshot, property); + result = findByPreviousVersion(projectSnapshot, property); + if (result == null) { + result = findByVersion(projectSnapshot, property); + } } } } @@ -114,6 +120,14 @@ public class PastSnapshotFinder implements BatchExtension { return pastSnapshot; } + private PastSnapshot findByPreviousVersion(Snapshot projectSnapshot, String property) { + PastSnapshot pastSnapshot = null; + if (StringUtils.equals(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION, property)) { + pastSnapshot = finderByPreviousVersion.findByPreviousVersion(projectSnapshot); + } + return pastSnapshot; + } + private PastSnapshot findByDate(Snapshot projectSnapshot, String property) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersion.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersion.java new file mode 100644 index 00000000000..ea2f23ee7b1 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersion.java @@ -0,0 +1,61 @@ +/* + * 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.batch.components; + +import org.sonar.api.BatchExtension; +import org.sonar.api.CoreProperties; +import org.sonar.api.database.DatabaseSession; +import org.sonar.api.database.model.Snapshot; +import org.sonar.api.resources.Qualifiers; + +import java.util.Date; +import java.util.List; + +public class PastSnapshotFinderByPreviousVersion implements BatchExtension { + + private final DatabaseSession session; + + public PastSnapshotFinderByPreviousVersion(DatabaseSession session) { + this.session = session; + } + + PastSnapshot findByPreviousVersion(Snapshot projectSnapshot) { + String hql = "from " + Snapshot.class.getSimpleName() + + " where version!=:version AND version!='' AND resourceId=:resourceId AND status=:status AND qualifier<>:lib order by createdAt desc"; + List<Snapshot> snapshots = session.createQuery(hql) + .setParameter("version", projectSnapshot.getVersion()) + .setParameter("resourceId", projectSnapshot.getResourceId()) + .setParameter("status", Snapshot.STATUS_PROCESSED) + .setParameter("lib", Qualifiers.LIBRARY) + .setMaxResults(1) + .getResultList(); + + PastSnapshot result; + if (snapshots.isEmpty()) { + result = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); + } else { + Snapshot snapshot = snapshots.get(0); + Date targetDate = snapshot.getCreatedAt(); + result = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION, targetDate, snapshot).setModeParameter(snapshot.getVersion()); + } + return result; + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java index b5e567ce18d..f162a65dc2f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java @@ -19,11 +19,7 @@ */ package org.sonar.batch.components; -import java.util.Date; -import java.util.List; - -import javax.persistence.Query; - +import com.google.common.collect.Lists; import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.StringUtils; import org.slf4j.LoggerFactory; @@ -35,7 +31,10 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.Logs; -import com.google.common.collect.Lists; +import javax.persistence.Query; + +import java.util.Date; +import java.util.List; public class TimeMachineConfiguration implements BatchExtension { @@ -83,6 +82,7 @@ public class TimeMachineConfiguration implements BatchExtension { snapshot.setResourceId(projectId.intValue()); snapshot.setCreatedAt(project.getAnalysisDate()); snapshot.setBuildDate(new Date()); + snapshot.setVersion(project.getAnalysisVersion()); } return snapshot; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest.java new file mode 100644 index 00000000000..793c800758d --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest.java @@ -0,0 +1,47 @@ +/* + * 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.batch.components; + +import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.database.model.Snapshot; +import org.sonar.jpa.test.AbstractDbUnitTestCase; + +import static org.fest.assertions.Assertions.assertThat; + +public class PastSnapshotFinderByPreviousVersionTest extends AbstractDbUnitTestCase { + + @Test + public void shouldFindByPreviousVersion() { + setupData("shared"); + PastSnapshotFinderByPreviousVersion finder = new PastSnapshotFinderByPreviousVersion(getSession()); + + Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); + PastSnapshot foundSnapshot = finder.findByPreviousVersion(currentProjectSnapshot); + assertThat(foundSnapshot.getProjectSnapshotId()).isEqualTo(1009); + assertThat(foundSnapshot.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); + assertThat(foundSnapshot.getModeParameter()).isEqualTo("1.1"); + + // and test also another version to verify that unprocessed snapshots are ignored + currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); + assertThat(finder.findByPreviousVersion(currentProjectSnapshot).getProjectSnapshotId()).isEqualTo(1003); + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java index 3b8b61236ad..2c952a4cf9a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java @@ -37,18 +37,4 @@ public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase { assertThat(finder.findByVersion(currentProjectSnapshot, "1.1").getProjectSnapshotId()).isEqualTo(1009); } - /** - * Revert SONAR-3407 - */ - @Test - public void doNotFailIfUnknownVersion() { - setupData("shared"); - - Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); - PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(getSession()); - - PastSnapshot pastSnapshot = finder.findByVersion(currentProjectSnapshot, "0.1.2"); - assertThat(pastSnapshot).isNotNull(); - assertThat(pastSnapshot.getDate()).isNull(); - } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java index 5f03dce9160..f25775962c6 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java @@ -19,10 +19,14 @@ */ package org.sonar.batch.components; +import org.apache.commons.configuration.BaseConfiguration; +import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentMatcher; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.sonar.api.CoreProperties; import org.sonar.api.database.model.Snapshot; @@ -38,25 +42,44 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class PastSnapshotFinderTest { + @Mock private PastSnapshotFinderByDays finderByDays; + @Mock private PastSnapshotFinderByDate finderByDate; + @Mock private PastSnapshotFinderByVersion finderByVersion; + @Mock private PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis; + @Mock + private PastSnapshotFinderByPreviousVersion finderByPreviousVersion; + private PastSnapshotFinder finder; @Before public void initFinders() { - finderByDays = mock(PastSnapshotFinderByDays.class); - finderByDate = mock(PastSnapshotFinderByDate.class); - finderByVersion = mock(PastSnapshotFinderByVersion.class); - finderByPreviousAnalysis = mock(PastSnapshotFinderByPreviousAnalysis.class); - finder = new PastSnapshotFinder(finderByDays, finderByVersion, finderByDate, finderByPreviousAnalysis); + MockitoAnnotations.initMocks(this); + + finder = new PastSnapshotFinder(finderByDays, finderByVersion, finderByDate, finderByPreviousAnalysis, finderByPreviousVersion); + } + + @Test + public void shouldFind() { + Configuration conf = new BaseConfiguration(); + conf.addProperty("sonar.timemachine.period5", "1.2"); + + when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Date(), new Snapshot())); + + PastSnapshot variationSnapshot = finder.find(null, conf, 5); + + verify(finderByVersion).findByVersion(null, "1.2"); + assertThat(variationSnapshot.getIndex(), is(5)); + assertThat(variationSnapshot.getMode(), is("version")); + assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue())); } @Test @@ -137,6 +160,22 @@ public class PastSnapshotFinderTest { } @Test + public void shouldFindByPreviousVersion() throws ParseException { + final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + final Date date = format.parse("2010-05-18"); + Snapshot snapshot = new Snapshot(); + snapshot.setCreatedAt(date); + when(finderByPreviousVersion.findByPreviousVersion(null)).thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION, date, snapshot)); + + PastSnapshot variationSnapshot = finder.find(null, 2, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); + + verify(finderByPreviousVersion).findByPreviousVersion(null); + assertThat(variationSnapshot.getIndex(), is(2)); + assertThat(variationSnapshot.getMode(), is(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION)); + assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue())); + } + + @Test public void shouldFindByVersion() { when(finderByVersion.findByVersion(null, "1.2")).thenReturn(new PastSnapshot("version", new Date(), new Snapshot())); diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/shared.xml new file mode 100644 index 00000000000..98c40a5fc53 --- /dev/null +++ b/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/shared.xml @@ -0,0 +1,42 @@ +<dataset> + + <projects long_name="[null]" id="1" scope="PRJ" qualifier="TRK" kee="project" name="project" + root_id="[null]" + description="[null]" + enabled="true" language="java" copy_resource_id="[null]" person_id="[null]"/> + + + <!-- version 1.1-SNAPSHOT --> + <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="1000" + project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2008-11-01 13:58:00.00" build_date="2008-11-01 13:58:00.00" version="1.1-SNAPSHOT" path="" + status="P" islast="false" depth="0" /> + + + <!-- version 1.1-SNAPSHOT --> + <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="1003" + project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2008-11-02 13:58:00.00" build_date="2008-11-02 13:58:00.00" version="1.1-SNAPSHOT" path="" + status="P" islast="true" depth="0" /> + + + <!-- unprocessed version 1.1 (to ignore) --> + <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="1006" + project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2008-11-03 13:58:00.00" build_date="2008-11-03 13:58:00.00" version="1.1" path="" + status="U" islast="false" depth="0" /> + + + <!-- version 1.1 --> + <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="1009" + project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2008-11-04 13:58:00.00" build_date="2008-11-04 13:58:00.00" version="1.1" path="" + status="P" islast="false" depth="0" /> + + <!-- current analysis --> + <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="1010" + project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2008-11-05 13:58:00.00" build_date="2008-11-05 13:58:00.00" version="1.2-SNAPSHOT" path="" + status="U" islast="false" depth="0" /> + +</dataset>
\ No newline at end of file |