]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4700 Add some UT
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 6 Nov 2013 09:40:48 +0000 (10:40 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 6 Nov 2013 09:40:48 +0000 (10:40 +0100)
sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java
sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java
sonar-batch/src/test/resources/org/sonar/batch/components/TimeMachineConfigurationTest/shared.xml

index ea5889dd2e9ee9982da811545b24181174a235f1..bb8be2a2947a8cd99e4592f783653fd53897dc08 100644 (file)
@@ -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);
   }
 
index c2d0e0b2bebba2ff220d714fbf4a0259f65afaad..ca2b553060c2b9d7c4ef441dffa29302da7cbb2b 100644 (file)
@@ -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();
   }
 
 }
index c8796e2bb80156e461188928fb8811adf001a44e..2d267fb2963e709843facb986232a23d1201c15e 100644 (file)
@@ -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>