summaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2013-11-05 17:26:55 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2013-11-05 17:26:55 +0100
commitfda6c2f185412c229b3ce59fc9a5088bb4c583d5 (patch)
tree8477f1e0f27d75b343345e6a72b03a2fb9904543 /sonar-batch
parentab0c1f192ba9619561f9b4a829c581a01575aecd (diff)
downloadsonarqube-fda6c2f185412c229b3ce59fc9a5088bb4c583d5.tar.gz
sonarqube-fda6c2f185412c229b3ce59fc9a5088bb4c583d5.zip
SONAR-4700 Add some UT
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java8
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java95
2 files changed, 71 insertions, 32 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java
index dae7a48cff4..8ac1e37d031 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java
@@ -41,12 +41,8 @@ public class PastSnapshotFinderByDate implements BatchExtension {
}
PastSnapshot findByDate(Snapshot projectSnapshot, Date date) {
- Snapshot snapshot = null;
- if (projectSnapshot != null) {
- snapshot = findSnapshot(projectSnapshot.getResourceId(), date);
- }
- SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT);
- return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DATE, date, snapshot).setModeParameter(format.format(date));
+ Integer projectId = projectSnapshot != null ? projectSnapshot.getResourceId() : null;
+ return findByDate(projectId, date);
}
PastSnapshot findByDate(Integer projectId, Date date) {
diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java
index 78a5b3d2e36..3c694beed89 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java
@@ -19,36 +19,79 @@
*/
package org.sonar.batch.components;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.resources.Project;
+
+import java.util.Date;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
public class TimeMachineConfigurationTest {
private PeriodsDefinition periodsDefinition;
private PastSnapshotFinderByDate pastSnapshotFinderByDate;
-// @Before
-// public void before() {
-// periodsDefinition = mock(PeriodsDefinition.class);
-// pastSnapshotFinderByDate = mock(PastSnapshotFinderByDate.class);
-// }
-//
-// @Test
-// public void should_init_past_snapshots() {
-// Integer projectId = 1;
-// Date date = new Date();
-//
-// PastSnapshot projectPastSnapshot = new PastSnapshot("mode", projectId);
-//
-// when(periodsDefinition.projectPastSnapshots()).thenReturn(newArrayList(new PastSnapshot("mode", projectId)));
-// when(pastSnapshotFinderByDate.findByDate(projectId, date)).thenReturn(newArrayList(new PastSnapshot("mode", new Date())));
-//
-// TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration((Project) new Project("my:project").setId(projectId), periodsDefinition, pastSnapshotFinderByDate);
-// assertThat(timeMachineConfiguration.periods()).hasSize(1);
-// }
-//
-// @Test
-// public void should_not_init_past_snapshots_if_first_analysis() {
-//// new TimeMachineConfiguration(new Project("new:project"), settings, pastSnapshotFinder);
-////
-//// verifyZeroInteractions(pastSnapshotFinder);
-// }
+ @Before
+ public void before() {
+ periodsDefinition = mock(PeriodsDefinition.class);
+ pastSnapshotFinderByDate = mock(PastSnapshotFinderByDate.class);
+ }
+
+ @Test
+ public void get_module_past_snapshot() {
+ Integer projectId = 1;
+ Date targetDate = new Date();
+
+ PastSnapshot projectPastSnapshot = new PastSnapshot("mode", targetDate);
+ PastSnapshot modulePastSnapshot = new PastSnapshot("mode", targetDate);
+
+ when(periodsDefinition.projectPastSnapshots()).thenReturn(newArrayList(projectPastSnapshot));
+ when(pastSnapshotFinderByDate.findByDate(anyInt(), any(Date.class))).thenReturn(modulePastSnapshot);
+
+ TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration((Project) new Project("my:project").setId(projectId), periodsDefinition, pastSnapshotFinderByDate);
+ assertThat(timeMachineConfiguration.periods()).hasSize(1);
+ assertThat(timeMachineConfiguration.modulePastSnapshots()).hasSize(1);
+ }
+
+ @Test
+ public void complete_module_past_snapshot_from_project_past_snapshot() {
+ Integer projectId = 1;
+ Date targetDate = new Date();
+
+ PastSnapshot projectPastSnapshot = new PastSnapshot("mode", targetDate);
+ projectPastSnapshot.setIndex(1);
+ projectPastSnapshot.setMode("mode");
+ projectPastSnapshot.setModeParameter("modeParam");
+
+ PastSnapshot modulePastSnapshot = new PastSnapshot("mode", targetDate);
+
+ when(periodsDefinition.projectPastSnapshots()).thenReturn(newArrayList(projectPastSnapshot));
+ when(pastSnapshotFinderByDate.findByDate(anyInt(), any(Date.class))).thenReturn(modulePastSnapshot);
+
+ TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration((Project) new Project("my:project").setId(projectId), periodsDefinition, pastSnapshotFinderByDate);
+ assertThat(timeMachineConfiguration.modulePastSnapshots()).hasSize(1);
+ assertThat(timeMachineConfiguration.modulePastSnapshots().get(0).getIndex()).isEqualTo(1);
+ assertThat(timeMachineConfiguration.modulePastSnapshots().get(0).getMode()).isEqualTo("mode");
+ assertThat(timeMachineConfiguration.modulePastSnapshots().get(0).getModeParameter()).isEqualTo("modeParam");
+ }
+
+ @Test
+ public void get_no_module_past_snapshot() {
+ Integer projectId = 1;
+ Date targetDate = new Date();
+
+ PastSnapshot projectPastSnapshot = new PastSnapshot("mode", targetDate);
+
+ when(periodsDefinition.projectPastSnapshots()).thenReturn(newArrayList(projectPastSnapshot));
+ when(pastSnapshotFinderByDate.findByDate(eq(projectId), eq(targetDate))).thenReturn(null);
+
+ TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration((Project) new Project("my:project").setId(projectId), periodsDefinition, pastSnapshotFinderByDate);
+ assertThat(timeMachineConfiguration.periods()).isEmpty();
+ }
}