diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-03 15:28:17 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-03 15:28:17 +0000 |
commit | 5b1b89db6993585f7ecd9c4d69ab10774e4f9a9c (patch) | |
tree | 50ab99bc1b890585d52d2cfab00c2f03b2f728de /plugins | |
parent | 58756ad8e3e3cde1da77120b553bb22fbc0b79fa (diff) | |
download | sonarqube-5b1b89db6993585f7ecd9c4d69ab10774e4f9a9c.tar.gz sonarqube-5b1b89db6993585f7ecd9c4d69ab10774e4f9a9c.zip |
SONAR-249 correctly save the variations configuration into the table SNAPSHOTS
Diffstat (limited to 'plugins')
16 files changed, 334 insertions, 83 deletions
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 406d9aa6bbb..bb61a9004a1 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 @@ -211,13 +211,15 @@ public class CorePlugin implements Plugin { // time machine extensions.add(TendencyDecorator.class); - extensions.add(PeriodLocator.class); + extensions.add(PastSnapshotFinderByDays.class); extensions.add(PastMeasuresLoader.class); + extensions.add(VariationSnapshotFinder.class); extensions.add(TimeMachineConfiguration.class); extensions.add(VariationDecorator.class); extensions.add(PastViolationsLoader.class); extensions.add(ViolationPersisterDecorator.class); extensions.add(NewViolationsDecorator.class); + extensions.add(TimeMachineConfigurationPersister.class); return extensions; } 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 6b808b5c47c..d7a27736296 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,10 +53,10 @@ public class NewViolationsDecorator implements Decorator { public void decorate(Resource resource, DecoratorContext context) { Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS); - for (VariationTarget variationTarget : timeMachineConfiguration.getVariationTargets()) { - Date date = variationTarget.getDate(); - double value = countViolationsAfterDate(context.getViolations(), date) + sumChildren(context, variationTarget.getIndex()); - measure.setVariation(variationTarget.getIndex(), value); + for (VariationSnapshot variationSnapshot : timeMachineConfiguration.getVariationSnapshots()) { + Date date = variationSnapshot.getDate(); + double value = countViolationsAfterDate(context.getViolations(), date) + sumChildren(context, variationSnapshot.getIndex()); + measure.setVariation(variationSnapshot.getIndex(), value); } context.saveMeasure(measure); } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PeriodLocator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java index 5acf136782f..b3235bb8ae3 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PeriodLocator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java @@ -27,16 +27,16 @@ import org.sonar.api.database.model.Snapshot; import java.util.Date; import java.util.List; -public class PeriodLocator implements BatchExtension { +public class PastSnapshotFinderByDays implements BatchExtension{ private Snapshot projectSnapshot; // TODO replace by PersistenceManager private DatabaseSession session; - public PeriodLocator(Snapshot projectSnapshot, DatabaseSession session) { + public PastSnapshotFinderByDays(Snapshot projectSnapshot, DatabaseSession session) { this.projectSnapshot = projectSnapshot; this.session = session; } - Snapshot locate(int days) { + Snapshot findInDays(int days) { List<Snapshot> snapshots = loadSnapshotsFromDatabase(); return getNearestToTarget(snapshots, projectSnapshot.getCreatedAt(), days); } 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 a940ce514a7..8e990ad2d74 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 @@ -21,72 +21,50 @@ package org.sonar.plugins.core.timemachine; import com.google.common.collect.Lists; import org.apache.commons.configuration.Configuration; -import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; -import org.sonar.api.database.DatabaseSession; -import org.sonar.api.database.model.Snapshot; +import java.util.Collections; import java.util.List; -public final class TimeMachineConfiguration implements BatchExtension { +public class TimeMachineConfiguration implements BatchExtension { - private static final int NUMBER_OF_VARIATION_TARGETS = 3; + private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 3; private final Configuration configuration; - private List<VariationTarget> variationTargets; + private List<VariationSnapshot> variationSnapshots; - public TimeMachineConfiguration(Configuration configuration, DatabaseSession session, PeriodLocator periodLocator) { + public TimeMachineConfiguration(Configuration configuration, VariationSnapshotFinder variationSnapshotFinder) { this.configuration = configuration; - initVariationTargets(periodLocator, session); + initVariationSnapshots(variationSnapshotFinder); + } + + private void initVariationSnapshots(VariationSnapshotFinder variationSnapshotFinder) { + variationSnapshots = Lists.newLinkedList(); + for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) { + VariationSnapshot variationSnapshot = variationSnapshotFinder.find(configuration, index); + if (variationSnapshot != null) { + variationSnapshots.add(variationSnapshot); + } + } } /** * for unit tests */ - TimeMachineConfiguration(Configuration configuration, List<VariationTarget> variationTargets) { + TimeMachineConfiguration(Configuration configuration, List<VariationSnapshot> variationSnapshots) { this.configuration = configuration; - this.variationTargets = variationTargets; - } - - private void initVariationTargets(PeriodLocator periodLocator, DatabaseSession session) { - variationTargets = Lists.newLinkedList(); - for (int index = 1; index <= NUMBER_OF_VARIATION_TARGETS; index++) { - VariationTarget target = loadVariationTarget(index, periodLocator); - if (target != null) { - save(target, session); - variationTargets.add(target); - } - } + this.variationSnapshots = variationSnapshots; } - private void save(VariationTarget target, DatabaseSession session) { - Snapshot projectSnapshot = target.getProjectSnapshot(); - switch (target.getIndex()) { - case 1: - projectSnapshot.setVarMode1("PERIOD_IN_DAYS"); - break; - case 2: - projectSnapshot.setVarMode2("PERIOD_IN_DAYS"); - break; - case 3: - projectSnapshot.setVarMode3("PERIOD_IN_DAYS"); - break; - } - session.save(projectSnapshot); + /** + * for unit tests + */ + TimeMachineConfiguration(Configuration configuration) { + this.configuration = configuration; + this.variationSnapshots = Collections.emptyList(); } - private VariationTarget loadVariationTarget(int index, PeriodLocator periodLocator) { - String property = configuration.getString("sonar.timemachine.variation" + index); - if (StringUtils.isNotBlank(property)) { - // todo manage non-integer values - Snapshot projectSnapshot = periodLocator.locate(Integer.parseInt(property)); - if (projectSnapshot != null) { - return new VariationTarget(index, projectSnapshot); - } - } - return null; - } public boolean skipTendencies() { return configuration.getBoolean(CoreProperties.SKIP_TENDENCIES_PROPERTY, CoreProperties.SKIP_TENDENCIES_DEFAULT_VALUE); @@ -96,7 +74,7 @@ public final class TimeMachineConfiguration implements BatchExtension { return configuration.getInt(CoreProperties.CORE_TENDENCY_DEPTH_PROPERTY, CoreProperties.CORE_TENDENCY_DEPTH_DEFAULT_VALUE); } - public List<VariationTarget> getVariationTargets() { - return variationTargets; + public List<VariationSnapshot> 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 new file mode 100644 index 00000000000..df731278b5e --- /dev/null +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java @@ -0,0 +1,60 @@ +/* + * 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 final class TimeMachineConfigurationPersister implements BatchExtension { + + private TimeMachineConfiguration configuration; + private Snapshot projectSnapshot; + private DatabaseSession session; + + public TimeMachineConfigurationPersister(TimeMachineConfiguration configuration, Snapshot projectSnapshot, DatabaseSession session) { + this.configuration = configuration; + this.projectSnapshot = projectSnapshot; + this.session = session; + } + + public void start() { + List<VariationSnapshot> variationSnapshots = configuration.getVariationSnapshots(); + for (VariationSnapshot variationSnapshot : variationSnapshots) { + switch (variationSnapshot.getIndex()) { + case 1: + projectSnapshot.setVarMode1(variationSnapshot.getMode()); + projectSnapshot.setVarLabel1(variationSnapshot.getModeParameter()); + break; + case 2: + projectSnapshot.setVarMode2(variationSnapshot.getMode()); + projectSnapshot.setVarLabel2(variationSnapshot.getModeParameter()); + break; + case 3: + projectSnapshot.setVarMode3(variationSnapshot.getMode()); + projectSnapshot.setVarLabel3(variationSnapshot.getModeParameter()); + 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 08c5e34837a..423971fdf9f 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<VariationTarget> targets; + private List<VariationSnapshot> targets; private PastMeasuresLoader pastMeasuresLoader; public VariationDecorator(PastMeasuresLoader pastMeasuresLoader, TimeMachineConfiguration configuration) { - this(pastMeasuresLoader, configuration.getVariationTargets()); + this(pastMeasuresLoader, configuration.getVariationSnapshots()); } - VariationDecorator(PastMeasuresLoader pastMeasuresLoader, List<VariationTarget> targets) { + VariationDecorator(PastMeasuresLoader pastMeasuresLoader, List<VariationSnapshot> 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 (VariationTarget target : targets) { + for (VariationSnapshot target : targets) { calculateVariation(resource, context, target); } } } - private void calculateVariation(Resource resource, DecoratorContext context, VariationTarget target) { + private void calculateVariation(Resource resource, DecoratorContext context, VariationSnapshot target) { List<MeasureModel> 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/VariationTarget.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java index 185f85c53f4..25fddf3f66b 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationTarget.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java @@ -23,13 +23,15 @@ import org.sonar.api.database.model.Snapshot; import java.util.Date; -public final class VariationTarget { +public final class VariationSnapshot { private int index; + private String mode, modeParameter; private Snapshot projectSnapshot; - public VariationTarget(int index, Snapshot projectSnapshot) { + public VariationSnapshot(int index, String mode, Snapshot projectSnapshot) { this.index = index; + this.mode = mode; this.projectSnapshot = projectSnapshot; } @@ -44,4 +46,17 @@ public final class VariationTarget { 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 new file mode 100644 index 00000000000..e8d290f9d04 --- /dev/null +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinder.java @@ -0,0 +1,50 @@ +/* + * 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/PeriodLocatorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java index d0d99fed25f..d4f70c535ae 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PeriodLocatorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java @@ -36,7 +36,7 @@ import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertThat; import static org.junit.internal.matchers.IsCollectionContaining.hasItems; -public class PeriodLocatorTest extends AbstractDbUnitTestCase { +public class PastSnapshotFinderByDaysTest extends AbstractDbUnitTestCase { private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); @@ -45,8 +45,8 @@ public class PeriodLocatorTest extends AbstractDbUnitTestCase { setupData("shouldLoadSnapshotsFromDatabase"); Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); - PeriodLocator locator = new PeriodLocator(projectSnapshot, getSession()); - List<Snapshot> snapshots = locator.loadSnapshotsFromDatabase(); + PastSnapshotFinderByDays finder = new PastSnapshotFinderByDays(projectSnapshot, getSession()); + List<Snapshot> snapshots = finder.loadSnapshotsFromDatabase(); List<Integer> snapshotIds = Lists.newLinkedList(); for (Snapshot snapshot : snapshots) { snapshotIds.add(snapshot.getId()); @@ -56,7 +56,7 @@ public class PeriodLocatorTest extends AbstractDbUnitTestCase { } @Test - public void shouldLocateNearestSnapshotBefore() throws ParseException { + public void shouldGetNearestSnapshotBefore() throws ParseException { Date current = dateFormat.parse("2010-10-20"); // distance: 15 => target is 2010-10-05 @@ -66,11 +66,11 @@ public class PeriodLocatorTest extends AbstractDbUnitTestCase { newSnapshot(3, "2010-10-08"),// +3 days newSnapshot(4, "2010-10-12") // + 7 days ); - assertThat(PeriodLocator.getNearestToTarget(snapshots, current, 15).getId(), is(2)); + assertThat(PastSnapshotFinderByDays.getNearestToTarget(snapshots, current, 15).getId(), is(2)); } @Test - public void shouldLocateNearestSnapshotAfter() throws ParseException { + public void shouldgetNearestSnapshotAfter() throws ParseException { Date current = dateFormat.parse("2010-10-20"); // distance: 15 => target is 2010-10-05 @@ -80,14 +80,14 @@ public class PeriodLocatorTest extends AbstractDbUnitTestCase { newSnapshot(3, "2010-10-08"),// +3 days newSnapshot(4, "2010-10-12") // + 7 days ); - assertThat(PeriodLocator.getNearestToTarget(snapshots, current, 15).getId(), is(3)); + assertThat(PastSnapshotFinderByDays.getNearestToTarget(snapshots, current, 15).getId(), is(3)); } @Test public void shouldReturnNullIfNoSnapshots() throws ParseException { Date current = dateFormat.parse("2010-10-20"); List<Snapshot> snapshots = Collections.emptyList(); - assertThat(PeriodLocator.getNearestToTarget(snapshots, current, 15), nullValue()); + assertThat(PastSnapshotFinderByDays.getNearestToTarget(snapshots, current, 15), nullValue()); } private Snapshot newSnapshot(int id, String date) throws ParseException { diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java index 48a7ab71d2a..05aea8b53fb 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyDecoratorTest.java @@ -60,7 +60,7 @@ public class TendencyDecoratorTest { } private TimeMachineConfiguration newConf() { - return new TimeMachineConfiguration(new PropertiesConfiguration(), null); + return new TimeMachineConfiguration(new PropertiesConfiguration()); } @Test 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 new file mode 100644 index 00000000000..d1f21db252d --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java @@ -0,0 +1,50 @@ +/* + * 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 java.util.Arrays; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class TimeMachineConfigurationPersisterTest extends AbstractDbUnitTestCase { + + @Test + public void shouldSaveVariationConfigurationInSnapshotsTable() { + 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"); + when(conf.getVariationSnapshots()).thenReturn(Arrays.asList(vs1, vs3)); + Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1000); + + TimeMachineConfigurationPersister persister = new TimeMachineConfigurationPersister(conf, projectSnapshot, getSession()); + persister.start(); + + checkTables("shouldSaveVariationConfigurationInSnapshotsTable", "snapshots"); + } +} 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 755088e2f5b..87d7d9f4235 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 @@ -26,8 +26,7 @@ import org.sonar.jpa.test.AbstractDbUnitTestCase; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { @@ -35,24 +34,30 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { public void shouldSkipTendencies() { PropertiesConfiguration conf = new PropertiesConfiguration(); conf.setProperty(CoreProperties.SKIP_TENDENCIES_PROPERTY, true); - assertThat(new TimeMachineConfiguration(conf, null).skipTendencies(), is(true)); + assertThat(new TimeMachineConfiguration(conf).skipTendencies(), is(true)); } @Test public void shouldNotSkipTendenciesByDefault() { PropertiesConfiguration conf = new PropertiesConfiguration(); - assertThat(new TimeMachineConfiguration(conf, null).skipTendencies(), is(false)); + assertThat(new TimeMachineConfiguration(conf).skipTendencies(), is(false)); } @Test - public void shouldGetPeriodVariationTargets() { + public void shouldInitSnapshotReferences() { PropertiesConfiguration conf = new PropertiesConfiguration(); - conf.setProperty("sonar.timemachine.variation1", "7"); - conf.setProperty("sonar.timemachine.variation2", "30"); - PeriodLocator periodLocator = mock(PeriodLocator.class); - new TimeMachineConfiguration(conf, getSession(), periodLocator).getVariationTargets(); - verify(periodLocator).locate(7); - verify(periodLocator).locate(30); + 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)); + + TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(conf, snapshotReferenceFinder); + + verify(snapshotReferenceFinder).find(conf, 1); + verify(snapshotReferenceFinder).find(conf, 2); + verify(snapshotReferenceFinder).find(conf, 3); + + assertThat(timeMachineConfiguration.getVariationSnapshots().size(), is(2)); } + } 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 new file mode 100644 index 00000000000..d4a01501142 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinderTest.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 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/PeriodLocatorTest/shouldLoadSnapshotsFromDatabase.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shouldLoadSnapshotsFromDatabase.xml index 0fe8ea75a0b..0fe8ea75a0b 100644 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PeriodLocatorTest/shouldLoadSnapshotsFromDatabase.xml +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shouldLoadSnapshotsFromDatabase.xml diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest/shared.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest/shared.xml new file mode 100644 index 00000000000..0df019cf0a5 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest/shared.xml @@ -0,0 +1,16 @@ +<dataset> + <snapshots var_mode_1="[null]" var_label_1="[null]" var_mode_2="[null]" var_label_2="[null]" var_mode_3="[null]" var_label_3="[null]" + id="100" 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" version="[null]" path="" + status="P" islast="false" depth="0" /> + + <snapshots var_mode_1="[null]" var_label_1="[null]" var_mode_2="[null]" var_label_2="[null]" var_mode_3="[null]" var_label_3="[null]" + id="200" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2008-12-15 13:58:00.00" version="[null]" path="" + status="P" islast="false" depth="0" /> + + <snapshots var_mode_1="[null]" var_label_1="[null]" var_mode_2="[null]" var_label_2="[null]" var_mode_3="[null]" var_label_3="[null]" + id="1000" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2009-02-25 13:58:00.00" version="[null]" path="" + status="P" islast="false" depth="0" /> +</dataset>
\ No newline at end of file diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest/shouldSaveVariationConfigurationInSnapshotsTable-result.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest/shouldSaveVariationConfigurationInSnapshotsTable-result.xml new file mode 100644 index 00000000000..5a32afdf914 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest/shouldSaveVariationConfigurationInSnapshotsTable-result.xml @@ -0,0 +1,16 @@ +<dataset> + <snapshots var_mode_1="[null]" var_label_1="[null]" var_mode_2="[null]" var_label_2="[null]" var_mode_3="[null]" var_label_3="[null]" + id="100" 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" version="[null]" path="" + status="P" islast="false" depth="0" /> + + <snapshots var_mode_1="[null]" var_label_1="[null]" var_mode_2="[null]" var_label_2="[null]" var_mode_3="[null]" var_label_3="[null]" + id="200" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2008-12-15 13:58:00.00" version="[null]" path="" + status="P" islast="false" depth="0" /> + + <snapshots var_mode_1="days" var_label_1="30" var_mode_2="[null]" var_label_2="[null]" var_mode_3="version" var_label_3="1.2.3" + id="1000" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2009-02-25 13:58:00.00" version="[null]" path="" + status="P" islast="false" depth="0" /> +</dataset>
\ No newline at end of file |