summaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2013-11-06 10:40:48 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2013-11-06 10:40:48 +0100
commitfc38babef95e3836779e55b8c8b7fd5deae2bf74 (patch)
tree664178d76fc1baab446433dd4deca7c6177696fc /sonar-batch
parent5a54f466480ea11013f8ff9462cbdf67ce557253 (diff)
downloadsonarqube-fc38babef95e3836779e55b8c8b7fd5deae2bf74.tar.gz
sonarqube-fc38babef95e3836779e55b8c8b7fd5deae2bf74.zip
SONAR-4700 Add some UT
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java5
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java62
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml19
3 files changed, 51 insertions, 35 deletions
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 ea5889dd2e9..bb8be2a2947 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
@@ -67,6 +67,10 @@ public class TimeMachineConfiguration implements BatchExtension {
}
}
+ /**
+ * Only used to get the real date of the snapshot on the current period.
+ * The date is used to calculate new_violations measures
+ */
@CheckForNull
private Snapshot findSnapshot(Snapshot projectSnapshot) {
String hql = "from " + Snapshot.class.getSimpleName() + " where resourceId=:resourceId and (rootId=:rootSnapshotId or id=:rootSnapshotId)";
@@ -75,7 +79,6 @@ public class TimeMachineConfiguration implements BatchExtension {
.setParameter("rootSnapshotId", projectSnapshot.getId())
.setMaxResults(1)
.getResultList();
-
return snapshots.isEmpty() ? null : snapshots.get(0);
}
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 c2d0e0b2beb..ca2b553060c 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
@@ -20,8 +20,8 @@
package org.sonar.batch.components;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
+import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
@@ -38,62 +38,72 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase {
@Before
public void before() {
+ setupData("shared");
periodsDefinition = mock(PeriodsDefinition.class);
}
@Test
- @Ignore
- public void get_module_past_snapshot() {
- Integer projectId = 1;
- Date targetDate = new Date();
+ public void get_project_past_snapshot() {
+ Snapshot projectSnapshot = new Snapshot();
+ projectSnapshot.setId(1010);
+ PastSnapshot projectPastSnapshot = new PastSnapshot("mode", new Date(), projectSnapshot);
+
+ when(periodsDefinition.getRootProjectPastSnapshots()).thenReturn(newArrayList(projectPastSnapshot));
- PastSnapshot projectPastSnapshot = new PastSnapshot("mode", targetDate);
- PastSnapshot modulePastSnapshot = new PastSnapshot("mode", targetDate);
+ TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(getSession(), (Project) new Project("my:project").setId(1), periodsDefinition);
+ assertThat(timeMachineConfiguration.periods()).hasSize(1);
+ assertThat(timeMachineConfiguration.periods().get(0).getDate()).isNotNull();
+ assertThat(timeMachineConfiguration.getProjectPastSnapshots()).hasSize(1);
+ assertThat(timeMachineConfiguration.getProjectPastSnapshots().get(0).getProjectSnapshot().getId()).isEqualTo(1010);
+ }
+
+ @Test
+ public void get_module_past_snapshot() {
+ Snapshot projectSnapshot = new Snapshot();
+ projectSnapshot.setId(1010);
+ PastSnapshot projectPastSnapshot = new PastSnapshot("mode", new Date(), projectSnapshot);
when(periodsDefinition.getRootProjectPastSnapshots()).thenReturn(newArrayList(projectPastSnapshot));
- //when(pastSnapshotFinderByDate.findByDate(anyInt(), any(Date.class))).thenReturn(modulePastSnapshot);
- TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(null, (Project) new Project("my:project").setId(projectId), periodsDefinition);
+ TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(getSession(), (Project) new Project("my:module").setId(2), periodsDefinition);
assertThat(timeMachineConfiguration.periods()).hasSize(1);
+ assertThat(timeMachineConfiguration.periods().get(0).getDate()).isNotNull();
assertThat(timeMachineConfiguration.getProjectPastSnapshots()).hasSize(1);
+ assertThat(timeMachineConfiguration.getProjectPastSnapshots().get(0).getProjectSnapshot().getId()).isEqualTo(1010);
}
@Test
- @Ignore
- public void complete_module_past_snapshot_from_project_past_snapshot() {
- Integer projectId = 1;
- Date targetDate = new Date();
+ public void complete_past_snapshot_from_project_past_snapshot() {
+ Snapshot projectSnapshot = new Snapshot();
+ projectSnapshot.setId(1010);
- PastSnapshot projectPastSnapshot = new PastSnapshot("mode", targetDate);
+ PastSnapshot projectPastSnapshot = new PastSnapshot("mode", new Date(), projectSnapshot);
projectPastSnapshot.setIndex(1);
projectPastSnapshot.setMode("mode");
projectPastSnapshot.setModeParameter("modeParam");
- PastSnapshot modulePastSnapshot = new PastSnapshot("mode", targetDate);
-
when(periodsDefinition.getRootProjectPastSnapshots()).thenReturn(newArrayList(projectPastSnapshot));
- //when(pastSnapshotFinderByDate.findByDate(anyInt(), any(Date.class))).thenReturn(modulePastSnapshot);
- TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(null, (Project) new Project("my:project").setId(projectId), periodsDefinition);
+ TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(getSession(), (Project) new Project("my:project").setId(1), periodsDefinition);
assertThat(timeMachineConfiguration.getProjectPastSnapshots()).hasSize(1);
+ assertThat(timeMachineConfiguration.getProjectPastSnapshots().get(0).getProjectSnapshot().getId()).isEqualTo(1010);
assertThat(timeMachineConfiguration.getProjectPastSnapshots().get(0).getIndex()).isEqualTo(1);
assertThat(timeMachineConfiguration.getProjectPastSnapshots().get(0).getMode()).isEqualTo("mode");
assertThat(timeMachineConfiguration.getProjectPastSnapshots().get(0).getModeParameter()).isEqualTo("modeParam");
}
@Test
- @Ignore
- public void get_no_module_past_snapshot() {
- Integer projectId = 1;
- Date targetDate = new Date();
+ public void get_no_date_on_new_project() {
+ Snapshot projectSnapshot = new Snapshot();
+ projectSnapshot.setId(1010);
- PastSnapshot projectPastSnapshot = new PastSnapshot("mode", targetDate);
+ PastSnapshot projectPastSnapshot = new PastSnapshot("mode", new Date(), projectSnapshot);
when(periodsDefinition.getRootProjectPastSnapshots()).thenReturn(newArrayList(projectPastSnapshot));
- //when(pastSnapshotFinderByDate.findByDate(eq(projectId), eq(targetDate))).thenReturn(null);
- TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(null, (Project) new Project("my:project").setId(projectId), periodsDefinition);
- assertThat(timeMachineConfiguration.periods()).isEmpty();
+ TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(getSession(), new Project("my:project"), periodsDefinition);
+ assertThat(timeMachineConfiguration.periods()).hasSize(1);
+ assertThat(timeMachineConfiguration.periods().get(0).getDate()).isNull();
}
}
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml
index c8796e2bb80..2d267fb2963 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml
@@ -1,12 +1,15 @@
<dataset>
- <projects long_name="[null]" id="1" scope="PRJ" kee="my:project" qualifier="LIB" name="my project as lib"
- root_id="[null]" description="[null]" enabled="true" language="java" copy_resource_id="[null]" person_id="[null]"/>
-
- <projects long_name="[null]" id="2" scope="PRJ" kee="my:project" qualifier="TRK" name="my project"
- root_id="[null]" description="[null]" enabled="true" language="java" copy_resource_id="[null]" person_id="[null]"/>
-
- <projects long_name="[null]" id="3" scope="DIR" kee="my:project:path/to/dir" qualifier="TRK" name="my dir"
- root_id="2" description="[null]" enabled="true" language="java" copy_resource_id="[null]" person_id="[null]"/>
+ <!-- Project -->
+ <snapshots id="1010" 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]"
+ project_id="1" parent_snapshot_id="[null]" root_project_id="[null]" root_snapshot_id="[null]"
+ scope="PRJ" qualifier="TRK" created_at="2008-11-27 13:58:00.00" build_date="2008-11-27 13:58:00.00" version="1.2-SNAPSHOT" path=""
+ status="U" islast="false" depth="0" />
+
+ <!-- Module -->
+ <snapshots id="1011" 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]"
+ project_id="2" parent_snapshot_id="1010" root_project_id="1" root_snapshot_id="1010"
+ scope="PRJ" qualifier="BRC" created_at="2008-11-27 13:58:00.00" build_date="2008-11-27 13:58:00.00" version="1.2-SNAPSHOT" path=""
+ status="U" islast="false" depth="0" />
</dataset>