From 46124a16b74a5e20b25a87e804ecce9baf135f16 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 3 Dec 2010 17:51:58 +0000 Subject: SONAR-249 add the select-box to display variation periods --- .../java/org/sonar/plugins/core/CorePlugin.java | 3 +- .../core/timemachine/NewViolationsDecorator.java | 2 +- .../plugins/core/timemachine/PastSnapshot.java | 62 +++++++++++++++ .../core/timemachine/PastSnapshotFinder.java | 89 ++++++++++++++++++++++ .../timemachine/PastSnapshotFinderByVersion.java | 49 ++++++++++++ .../core/timemachine/PastViolationsLoader.java | 9 ++- .../core/timemachine/TimeMachineConfiguration.java | 12 +-- .../TimeMachineConfigurationPersister.java | 16 ++-- .../core/timemachine/VariationDecorator.java | 8 +- .../core/timemachine/VariationSnapshot.java | 62 --------------- .../core/timemachine/VariationSnapshotFinder.java | 50 ------------ .../PastSnapshotFinderByVersionTest.java | 51 +++++++++++++ .../core/timemachine/PastSnapshotFinderTest.java | 59 ++++++++++++++ .../TimeMachineConfigurationPersisterTest.java | 8 +- .../timemachine/TimeMachineConfigurationTest.java | 6 +- .../timemachine/VariationSnapshotFinderTest.java | 59 -------------- .../PastSnapshotFinderByVersionTest/shared.xml | 42 ++++++++++ 17 files changed, 387 insertions(+), 200 deletions(-) create mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java create mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java create mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java delete mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java delete mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinder.java create mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java create mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java delete mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinderTest.java create mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest/shared.xml (limited to 'plugins/sonar-core-plugin') diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index bb61a9004a1..59e25fcd896 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -212,8 +212,9 @@ public class CorePlugin implements Plugin { // time machine extensions.add(TendencyDecorator.class); extensions.add(PastSnapshotFinderByDays.class); + extensions.add(PastSnapshotFinderByVersion.class); extensions.add(PastMeasuresLoader.class); - extensions.add(VariationSnapshotFinder.class); + extensions.add(PastSnapshotFinder.class); extensions.add(TimeMachineConfiguration.class); extensions.add(VariationDecorator.class); extensions.add(PastViolationsLoader.class); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java index d7a27736296..7a134d83bd3 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java @@ -53,7 +53,7 @@ public class NewViolationsDecorator implements Decorator { public void decorate(Resource resource, DecoratorContext context) { Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS); - for (VariationSnapshot variationSnapshot : timeMachineConfiguration.getVariationSnapshots()) { + for (PastSnapshot variationSnapshot : timeMachineConfiguration.getVariationSnapshots()) { Date date = variationSnapshot.getDate(); double value = countViolationsAfterDate(context.getViolations(), date) + sumChildren(context, variationSnapshot.getIndex()); measure.setVariation(variationSnapshot.getIndex(), value); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java new file mode 100644 index 00000000000..3da2517f606 --- /dev/null +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java @@ -0,0 +1,62 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.plugins.core.timemachine; + +import org.sonar.api.database.model.Snapshot; + +import java.util.Date; + +public final class PastSnapshot { + + private int index; + private String mode, modeParameter; + private Snapshot projectSnapshot; + + public PastSnapshot(int index, String mode, Snapshot projectSnapshot) { + this.index = index; + this.mode = mode; + this.projectSnapshot = projectSnapshot; + } + + public int getIndex() { + return index; + } + + public Snapshot getProjectSnapshot() { + return projectSnapshot; + } + + public Date getDate() { + return projectSnapshot.getCreatedAt(); + } + + public String getConfigurationMode() { + return mode; + } + + public String getConfigurationModeParameter() { + return modeParameter; + } + + public PastSnapshot setConfigurationModeParameter(String s) { + this.modeParameter = s; + return this; + } +} diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java new file mode 100644 index 00000000000..11b931c3cd3 --- /dev/null +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java @@ -0,0 +1,89 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.plugins.core.timemachine; + +import org.apache.commons.configuration.Configuration; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.BatchExtension; +import org.sonar.api.database.model.Snapshot; + +public class PastSnapshotFinder implements BatchExtension { + + private PastSnapshotFinderByDays finderByDays; + private PastSnapshotFinderByVersion finderByVersion; + + public PastSnapshotFinder(PastSnapshotFinderByDays finderByDays, PastSnapshotFinderByVersion finderByVersion) { + this.finderByDays = finderByDays; + this.finderByVersion = finderByVersion; + } + + public PastSnapshot find(Configuration conf, int index) { + return find(index, conf.getString("sonar.timemachine.variation" + index)); + } + + public PastSnapshot find(int index, String property) { + if (StringUtils.isBlank(property)) { + return null; + } + + PastSnapshot result = null; + Integer days = getValueInDays(property); + if (days != null) { + result = findSnapshotInDays(index, days); + } else { + String version = getValueVersion(property); + if (StringUtils.isNotBlank(version)) { + result = findSnapshotByVersion(index, version); + } + } + + return result; + } + + private PastSnapshot findSnapshotByVersion(int index, String version) { + Snapshot projectSnapshot = finderByVersion.findVersion(version); + if (projectSnapshot != null) { + return new PastSnapshot(index, "version", projectSnapshot).setConfigurationModeParameter(version); + } + return null; + } + + private String getValueVersion(String property) { + // todo check if it's a version with a regexp + return null; + } + + private PastSnapshot findSnapshotInDays(int index, Integer days) { + Snapshot projectSnapshot = finderByDays.findInDays(days); + if (projectSnapshot != null) { + return new PastSnapshot(index, "days", projectSnapshot).setConfigurationModeParameter(String.valueOf(days)); + } + return null; + } + + private Integer getValueInDays(String property) { + try { + return Integer.parseInt(property); + + } catch (NumberFormatException e) { + return null; + } + } +} diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java new file mode 100644 index 00000000000..fc2ec93bf27 --- /dev/null +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java @@ -0,0 +1,49 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.plugins.core.timemachine; + +import org.sonar.api.BatchExtension; +import org.sonar.api.database.DatabaseSession; +import org.sonar.api.database.model.Snapshot; + +import java.util.List; + +public class PastSnapshotFinderByVersion implements BatchExtension { + private Snapshot projectSnapshot; // TODO replace by PersistenceManager + private DatabaseSession session; + + public PastSnapshotFinderByVersion(Snapshot projectSnapshot, DatabaseSession session) { + this.projectSnapshot = projectSnapshot; + this.session = session; + } + + Snapshot findVersion(String version) { + String hql = "from " + Snapshot.class.getSimpleName() + " where version=:version AND resourceId=:resourceId AND status=:status order by createdAt desc"; + List snapshots = session.createQuery(hql) + .setParameter("version", version) + .setParameter("resourceId", projectSnapshot.getResourceId()) + .setParameter("status", Snapshot.STATUS_PROCESSED) + .setMaxResults(1) + .getResultList(); + + return snapshots.isEmpty() ? null : snapshots.get(0); + } + +} diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java index 30810cef199..d8b064fbee0 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java @@ -6,6 +6,7 @@ import org.sonar.api.database.model.RuleFailureModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.database.model.SnapshotSource; import org.sonar.api.resources.Resource; +import org.sonar.api.utils.SonarException; import org.sonar.batch.index.ResourcePersister; import java.util.Collections; @@ -22,10 +23,14 @@ public class PastViolationsLoader implements BatchExtension { } public List getPastViolations(Resource resource) { - Snapshot snapshot = resourcePersister.getSnapshot(resource); - if (snapshot == null) { // TODO Godin: Prevent NPE with Natural and VB plugins + if (resource == null) { return Collections.emptyList(); } + + Snapshot snapshot = resourcePersister.getSnapshot(resource); + if (snapshot == null) { + throw new SonarException("This resource has no snapshot ???" + resource); + } Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true); if (previousLastSnapshot == null) { return Collections.emptyList(); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java index 8e990ad2d74..1f0894fd085 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java @@ -32,17 +32,17 @@ public class TimeMachineConfiguration implements BatchExtension { private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 3; private final Configuration configuration; - private List variationSnapshots; + private List variationSnapshots; - public TimeMachineConfiguration(Configuration configuration, VariationSnapshotFinder variationSnapshotFinder) { + public TimeMachineConfiguration(Configuration configuration, PastSnapshotFinder variationSnapshotFinder) { this.configuration = configuration; initVariationSnapshots(variationSnapshotFinder); } - private void initVariationSnapshots(VariationSnapshotFinder variationSnapshotFinder) { + private void initVariationSnapshots(PastSnapshotFinder variationSnapshotFinder) { variationSnapshots = Lists.newLinkedList(); for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) { - VariationSnapshot variationSnapshot = variationSnapshotFinder.find(configuration, index); + PastSnapshot variationSnapshot = variationSnapshotFinder.find(configuration, index); if (variationSnapshot != null) { variationSnapshots.add(variationSnapshot); } @@ -52,7 +52,7 @@ public class TimeMachineConfiguration implements BatchExtension { /** * for unit tests */ - TimeMachineConfiguration(Configuration configuration, List variationSnapshots) { + TimeMachineConfiguration(Configuration configuration, List variationSnapshots) { this.configuration = configuration; this.variationSnapshots = variationSnapshots; } @@ -74,7 +74,7 @@ public class TimeMachineConfiguration implements BatchExtension { return configuration.getInt(CoreProperties.CORE_TENDENCY_DEPTH_PROPERTY, CoreProperties.CORE_TENDENCY_DEPTH_DEFAULT_VALUE); } - public List getVariationSnapshots() { + public List getVariationSnapshots() { return variationSnapshots; } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java index df731278b5e..b587d2707c5 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java @@ -38,20 +38,20 @@ public final class TimeMachineConfigurationPersister implements BatchExtension { } public void start() { - List variationSnapshots = configuration.getVariationSnapshots(); - for (VariationSnapshot variationSnapshot : variationSnapshots) { + List variationSnapshots = configuration.getVariationSnapshots(); + for (PastSnapshot variationSnapshot : variationSnapshots) { switch (variationSnapshot.getIndex()) { case 1: - projectSnapshot.setVarMode1(variationSnapshot.getMode()); - projectSnapshot.setVarLabel1(variationSnapshot.getModeParameter()); + projectSnapshot.setVarMode1(variationSnapshot.getConfigurationMode()); + projectSnapshot.setVarLabel1(variationSnapshot.getConfigurationModeParameter()); break; case 2: - projectSnapshot.setVarMode2(variationSnapshot.getMode()); - projectSnapshot.setVarLabel2(variationSnapshot.getModeParameter()); + projectSnapshot.setVarMode2(variationSnapshot.getConfigurationMode()); + projectSnapshot.setVarLabel2(variationSnapshot.getConfigurationModeParameter()); break; case 3: - projectSnapshot.setVarMode3(variationSnapshot.getMode()); - projectSnapshot.setVarLabel3(variationSnapshot.getModeParameter()); + projectSnapshot.setVarMode3(variationSnapshot.getConfigurationMode()); + projectSnapshot.setVarLabel3(variationSnapshot.getConfigurationModeParameter()); break; } session.save(projectSnapshot); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java index 423971fdf9f..03b594c36bf 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java @@ -39,14 +39,14 @@ import java.util.Map; @DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE) public class VariationDecorator implements Decorator { - private List targets; + private List targets; private PastMeasuresLoader pastMeasuresLoader; public VariationDecorator(PastMeasuresLoader pastMeasuresLoader, TimeMachineConfiguration configuration) { this(pastMeasuresLoader, configuration.getVariationSnapshots()); } - VariationDecorator(PastMeasuresLoader pastMeasuresLoader, List targets) { + VariationDecorator(PastMeasuresLoader pastMeasuresLoader, List targets) { this.pastMeasuresLoader = pastMeasuresLoader; this.targets = targets; } @@ -67,13 +67,13 @@ public class VariationDecorator implements Decorator { public void decorate(Resource resource, DecoratorContext context) { if (shouldCalculateVariations(resource)) { - for (VariationSnapshot target : targets) { + for (PastSnapshot target : targets) { calculateVariation(resource, context, target); } } } - private void calculateVariation(Resource resource, DecoratorContext context, VariationSnapshot target) { + private void calculateVariation(Resource resource, DecoratorContext context, PastSnapshot target) { List pastMeasures = pastMeasuresLoader.getPastMeasures(resource, target.getProjectSnapshot()); compareWithPastMeasures(context, target.getIndex(), pastMeasures); } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java deleted file mode 100644 index 25fddf3f66b..00000000000 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * 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.plugins.core.timemachine; - -import org.sonar.api.database.model.Snapshot; - -import java.util.Date; - -public final class VariationSnapshot { - - private int index; - private String mode, modeParameter; - private Snapshot projectSnapshot; - - public VariationSnapshot(int index, String mode, Snapshot projectSnapshot) { - this.index = index; - this.mode = mode; - this.projectSnapshot = projectSnapshot; - } - - public int getIndex() { - return index; - } - - public Snapshot getProjectSnapshot() { - return projectSnapshot; - } - - public Date getDate() { - return projectSnapshot.getCreatedAt(); - } - - public String getMode() { - return mode; - } - - public String getModeParameter() { - return modeParameter; - } - - public VariationSnapshot setModeParameter(String s) { - this.modeParameter = s; - return this; - } -} diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinder.java deleted file mode 100644 index e8d290f9d04..00000000000 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinder.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * 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.plugins.core.timemachine; - -import org.apache.commons.configuration.Configuration; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.BatchExtension; -import org.sonar.api.database.model.Snapshot; - -public class VariationSnapshotFinder implements BatchExtension { - - private PastSnapshotFinderByDays finderInDays; - - public VariationSnapshotFinder(PastSnapshotFinderByDays finderInDays) { - this.finderInDays = finderInDays; - } - - public VariationSnapshot find(Configuration conf, int index) { - return find(index, conf.getString("sonar.timemachine.variation" + index)); - } - - public VariationSnapshot find(int index, String property) { - if (StringUtils.isNotBlank(property)) { - // todo manage non-integer values - int days = Integer.parseInt(property); - Snapshot projectSnapshot = finderInDays.findInDays(days); - if (projectSnapshot != null) { - return new VariationSnapshot(index, "days", projectSnapshot).setModeParameter(String.valueOf(days)); - } - } - return null; - } -} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java new file mode 100644 index 00000000000..8e6d40ef1ea --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java @@ -0,0 +1,51 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.plugins.core.timemachine; + +import org.junit.Test; +import org.sonar.api.database.model.Snapshot; +import org.sonar.jpa.test.AbstractDbUnitTestCase; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase { + + @Test + public void shouldFindByVersion() { + setupData("shared"); + + Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); + PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(currentProjectSnapshot, getSession()); + + assertThat(finder.findVersion("1.1").getId(), is(1009)); + } + + @Test + public void shouldNotFindVersion() { + setupData("shared"); + + Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); + PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(currentProjectSnapshot, getSession()); + + assertThat(finder.findVersion("1.0"), nullValue()); + } +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java new file mode 100644 index 00000000000..535913efbbe --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java @@ -0,0 +1,59 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.plugins.core.timemachine; + +import org.junit.Test; +import org.sonar.api.database.model.Snapshot; + +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.*; + +import static org.hamcrest.core.Is.is; + +public class PastSnapshotFinderTest { + + @Test + public void shouldFindByNumberOfDays() { + PastSnapshotFinderByDays finderByDays = mock(PastSnapshotFinderByDays.class); + when(finderByDays.findInDays(30)).thenReturn(new Snapshot()); + + PastSnapshotFinder finder = new PastSnapshotFinder(finderByDays, null); + PastSnapshot variationSnapshot = finder.find(1, "30"); + + verify(finderByDays).findInDays(30); + assertNotNull(variationSnapshot); + assertThat(variationSnapshot.getIndex(), is(1)); + assertThat(variationSnapshot.getConfigurationMode(), is("days")); + assertThat(variationSnapshot.getConfigurationModeParameter(), is("30")); + } + + @Test + public void shouldNotFindByNumberOfDays() { + PastSnapshotFinderByDays finderByDays = mock(PastSnapshotFinderByDays.class); + + PastSnapshotFinder finder = new PastSnapshotFinder(finderByDays, null); + PastSnapshot variationSnapshot = finder.find(1, "30"); + + verify(finderByDays).findInDays(30); + assertNull(variationSnapshot); + } +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java index d1f21db252d..900f8c45711 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java @@ -35,10 +35,10 @@ public class TimeMachineConfigurationPersisterTest extends AbstractDbUnitTestCas setupData("shared"); TimeMachineConfiguration conf = mock(TimeMachineConfiguration.class); - VariationSnapshot vs1 = new VariationSnapshot(1, "days", getSession().getSingleResult(Snapshot.class, "id", 100)) - .setModeParameter("30"); - VariationSnapshot vs3 = new VariationSnapshot(3, "version", getSession().getSingleResult(Snapshot.class, "id", 300)) - .setModeParameter("1.2.3"); + PastSnapshot vs1 = new PastSnapshot(1, "days", getSession().getSingleResult(Snapshot.class, "id", 100)) + .setConfigurationModeParameter("30"); + PastSnapshot vs3 = new PastSnapshot(3, "version", getSession().getSingleResult(Snapshot.class, "id", 300)) + .setConfigurationModeParameter("1.2.3"); when(conf.getVariationSnapshots()).thenReturn(Arrays.asList(vs1, vs3)); Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1000); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java index 87d7d9f4235..4a3d89ba489 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java @@ -46,9 +46,9 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { @Test public void shouldInitSnapshotReferences() { PropertiesConfiguration conf = new PropertiesConfiguration(); - VariationSnapshotFinder snapshotReferenceFinder = mock(VariationSnapshotFinder.class); - when(snapshotReferenceFinder.find(conf, 1)).thenReturn(new VariationSnapshot(1, "days", null)); - when(snapshotReferenceFinder.find(conf, 3)).thenReturn(new VariationSnapshot(3, "days", null)); + PastSnapshotFinder snapshotReferenceFinder = mock(PastSnapshotFinder.class); + when(snapshotReferenceFinder.find(conf, 1)).thenReturn(new PastSnapshot(1, "days", null)); + when(snapshotReferenceFinder.find(conf, 3)).thenReturn(new PastSnapshot(3, "days", null)); TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(conf, snapshotReferenceFinder); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinderTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinderTest.java deleted file mode 100644 index d4a01501142..00000000000 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinderTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2009 SonarSource SA - * 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.plugins.core.timemachine; - -import org.junit.Test; -import org.sonar.api.database.model.Snapshot; - -import static junit.framework.Assert.assertNull; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.*; - -import static org.hamcrest.core.Is.is; - -public class VariationSnapshotFinderTest { - - @Test - public void shouldFindByNumberOfDays() { - PastSnapshotFinderByDays finderByDays = mock(PastSnapshotFinderByDays.class); - when(finderByDays.findInDays(30)).thenReturn(new Snapshot()); - - VariationSnapshotFinder finder = new VariationSnapshotFinder(finderByDays); - VariationSnapshot variationSnapshot = finder.find(1, "30"); - - verify(finderByDays).findInDays(30); - assertNotNull(variationSnapshot); - assertThat(variationSnapshot.getIndex(), is(1)); - assertThat(variationSnapshot.getMode(), is("days")); - assertThat(variationSnapshot.getModeParameter(), is("30")); - } - - @Test - public void shouldNotFindByNumberOfDays() { - PastSnapshotFinderByDays finderByDays = mock(PastSnapshotFinderByDays.class); - - VariationSnapshotFinder finder = new VariationSnapshotFinder(finderByDays); - VariationSnapshot variationSnapshot = finder.find(1, "30"); - - verify(finderByDays).findInDays(30); - assertNull(variationSnapshot); - } -} diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest/shared.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest/shared.xml new file mode 100644 index 00000000000..34d04755d1a --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest/shared.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3