]> source.dussan.org Git - sonarqube.git/commitdiff
move IT NewTechnicalDebtMeasureTest to SQ repo
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 18 Sep 2015 07:36:04 +0000 (09:36 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 22 Sep 2015 15:20:56 +0000 (17:20 +0200)
and rename it TechnicalDebtMeasureVariationTest

it/it-tests/src/test/java/analysis/suite/AnalysisTestSuite.java
it/it-tests/src/test/java/analysis/suite/measure/DifferentialPeriodsTest.java
it/it-tests/src/test/java/analysis/suite/measure/TechnicalDebtMeasureVariationTest.java [new file with mode: 0644]
it/it-tests/src/test/java/util/ItUtils.java
it/it-tests/src/test/resources/measure/suite/DifferentialPeriodsTest/one-issue-per-line.xml [deleted file]
it/it-tests/src/test/resources/measure/suite/one-issue-per-file.xml [new file with mode: 0644]
it/it-tests/src/test/resources/measure/suite/one-issue-per-line.xml [new file with mode: 0644]

index d2c9250a0a5ab33f42278a64c479b974e14fcfc3..ef43f3e535a56e697dd9920af748f996b1fa1ac9 100644 (file)
@@ -22,6 +22,7 @@ package analysis.suite;
 import analysis.suite.measure.CustomMeasuresTest;
 import analysis.suite.measure.DifferentialPeriodsTest;
 import analysis.suite.measure.MeasureFiltersTest;
+import analysis.suite.measure.TechnicalDebtMeasureVariationTest;
 import analysis.suite.measure.TimeMachineTest;
 import analysis.suite.testing.CoverageTest;
 import analysis.suite.testing.CoverageTrackingTest;
@@ -42,7 +43,8 @@ import util.ItUtils;
   CoverageTrackingTest.class,
   CoverageTest.class,
   NewCoverageTest.class,
-  TestExecutionTest.class
+  TestExecutionTest.class,
+  TechnicalDebtMeasureVariationTest.class
 })
 public class AnalysisTestSuite {
 
index f7b73764f3915c7ee2bcc99628b5b5088cae2e74..b1119b7e7b668bd1c412bc3aaa5cce857189a72e 100644 (file)
@@ -49,7 +49,7 @@ public class DifferentialPeriodsTest {
         .setProperty("sonar.projectDate", "2013-01-01"));
 
     // Second analysis -> issues will be created
-    orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/measure/suite/DifferentialPeriodsTest/one-issue-per-line.xml"));
+    orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/measure/suite/one-issue-per-line.xml"));
     orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
     orchestrator.executeBuild(SonarRunner.create(ItUtils.projectDir("shared/xoo-sample")));
 
diff --git a/it/it-tests/src/test/java/analysis/suite/measure/TechnicalDebtMeasureVariationTest.java b/it/it-tests/src/test/java/analysis/suite/measure/TechnicalDebtMeasureVariationTest.java
new file mode 100644 (file)
index 0000000..24b78eb
--- /dev/null
@@ -0,0 +1,157 @@
+package analysis.suite.measure;
+
+import analysis.suite.AnalysisTestSuite;
+import com.sonar.orchestrator.Orchestrator;
+import com.sonar.orchestrator.locator.FileLocation;
+import java.util.List;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.wsclient.services.Measure;
+import org.sonar.wsclient.services.Resource;
+import org.sonar.wsclient.services.ResourceQuery;
+import util.ItUtils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+/**
+ * SONAR-4776
+ */
+public class TechnicalDebtMeasureVariationTest {
+
+  @ClassRule
+  public static Orchestrator orchestrator = AnalysisTestSuite.ORCHESTRATOR;
+
+  @Before
+  public void cleanUpAnalysisData() {
+    orchestrator.resetData();
+  }
+
+  @Test
+  public void new_technical_debt_measures_from_new_issues() throws Exception {
+    // This test assumes that period 1 is "since previous analysis" and 2 is "over x days"
+
+    // Execute an analysis in the past to have a past snapshot without any issues
+    provisionSampleProject();
+    setSampleProjectQualityProfile("empty");
+    runSampleProjectAnalysis("sonar.projectDate", "2013-01-01");
+
+    // Second analysis -> issues will be created
+    defineQualityProfile("one-issue-per-line");
+    setSampleProjectQualityProfile("one-issue-per-line");
+    runSampleProjectAnalysis();
+
+    // New technical debt only comes from new issues
+    Resource newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt");
+    List<Measure> measures = newTechnicalDebt.getMeasures();
+    assertThat(measures.get(0).getVariation1()).isEqualTo(17);
+    assertThat(measures.get(0).getVariation2()).isEqualTo(17);
+
+    // Third analysis, with exactly the same profile -> no new issues so no new technical debt
+    runSampleProjectAnalysis();
+
+    newTechnicalDebt = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt").setIncludeTrends(true));
+
+    // No variation => measure is purged
+    assertThat(newTechnicalDebt).isNull();
+  }
+
+  @Test
+  public void new_technical_debt_measures_from_technical_debt_update_since_previous_analysis() throws Exception {
+    // This test assumes that period 1 is "since previous analysis"
+
+    // Execute twice analysis
+    defineQualityProfile("one-issue-per-file");
+    provisionSampleProject();
+    setSampleProjectQualityProfile("one-issue-per-file");
+    runSampleProjectAnalysis();
+    runSampleProjectAnalysis();
+
+    // Third analysis, existing issues on OneIssuePerFile will have their technical debt updated with the effort to fix
+    runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10");
+
+    Resource newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt");
+    List<Measure> measures = newTechnicalDebt.getMeasures();
+    assertThat(measures.get(0).getVariation1()).isEqualTo(90);
+
+    // Fourth analysis, with exactly the same profile -> no new issues so no new technical debt since previous analysis
+    runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10");
+
+    newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt");
+    measures = newTechnicalDebt.getMeasures();
+    assertThat(measures.get(0).getVariation1()).isEqualTo(0);
+  }
+
+  @Test
+  public void new_technical_debt_measures_from_technical_debt_update_since_30_days() throws Exception {
+    // This test assumes that period 2 is "over x days"
+
+    // Execute an analysis in the past to have a past snapshot without any issues
+    provisionSampleProject();
+    setSampleProjectQualityProfile("empty");
+    runSampleProjectAnalysis("sonar.projectDate", "2013-01-01");
+
+    // Second analysis -> issues will be created
+    String profileXmlFile = "one-issue-per-file";
+    defineQualityProfile(profileXmlFile);
+    setSampleProjectQualityProfile("one-issue-per-file");
+    runSampleProjectAnalysis();
+
+    // Third analysis, existing issues on OneIssuePerFile will have their technical debt updated with the effort to fix
+    runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10");
+
+    Resource newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt");
+    List<Measure> measures = newTechnicalDebt.getMeasures();
+    assertThat(measures.get(0).getVariation2()).isEqualTo(90);
+
+    // Fourth analysis, with exactly the same profile -> no new issues so no new technical debt since previous analysis but still since 30 days
+    runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10");
+
+    newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt");
+    measures = newTechnicalDebt.getMeasures();
+    assertThat(measures.get(0).getVariation2()).isEqualTo(90);
+  }
+
+  /**
+   * SONAR-5059
+   */
+  @Test
+  public void new_technical_debt_measures_should_never_be_negative() throws Exception {
+    // This test assumes that period 1 is "since previous analysis" and 2 is "over x days"
+
+    // Execute an analysis with a big effort to fix
+    defineQualityProfile("one-issue-per-file");
+    provisionSampleProject();
+    setSampleProjectQualityProfile("one-issue-per-file");
+    runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "100");
+
+    // Execute a second analysis with a smaller effort to fix -> Added technical debt should be 0, not negative
+    runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10");
+
+    Resource newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt");
+    assertThat(newTechnicalDebt).isNull();
+  }
+
+  private void setSampleProjectQualityProfile(String qualityProfileKey) {
+    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", qualityProfileKey);
+  }
+
+  private void provisionSampleProject() {
+    orchestrator.getServer().provisionProject("sample", "sample");
+  }
+
+  private void defineQualityProfile(String qualityProfileKey) {
+    orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/measure/suite/" + qualityProfileKey + ".xml"));
+  }
+
+  private void runSampleProjectAnalysis(String... properties) {
+    ItUtils.runProjectAnalysis(TechnicalDebtMeasureVariationTest.orchestrator, "shared/xoo-sample", properties);
+  }
+
+  private Resource getSampleProjectResourceWithVariations(String metricKey) {
+    return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", metricKey).setIncludeTrends(true));
+  }
+
+}
+
index 322fabb9117a3edfff2fd95fdcd7c5ce4bcbc919..4e299355a7536cbbd5e25b3d96bb135a659281ac 100644 (file)
@@ -4,7 +4,10 @@ package util;/*
  * mailto:contact AT sonarsource DOT com
  */
 
+import com.google.common.collect.ImmutableMap;
+import com.sonar.orchestrator.Orchestrator;
 import com.sonar.orchestrator.build.BuildResult;
+import com.sonar.orchestrator.build.SonarRunner;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.json.simple.JSONValue;
@@ -125,4 +128,14 @@ public class ItUtils {
     }
     return count;
   }
+
+  public static void runProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, String... properties) {
+    SonarRunner sonarRunner = SonarRunner.create(projectDir(projectRelativePath));
+    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
+    for (int i = 0; i < properties.length; i++) {
+      builder.put(properties[i], properties[i+1]);
+      i+=2;
+    }
+    orchestrator.executeBuild(sonarRunner.setProperties(builder.build()));
+  }
 }
diff --git a/it/it-tests/src/test/resources/measure/suite/DifferentialPeriodsTest/one-issue-per-line.xml b/it/it-tests/src/test/resources/measure/suite/DifferentialPeriodsTest/one-issue-per-line.xml
deleted file mode 100644 (file)
index 365aa89..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<profile>
-  <name>one-issue-per-line</name>
-  <language>xoo</language>
-  <rules>
-    <rule>
-      <repositoryKey>xoo</repositoryKey>
-      <key>OneIssuePerLine</key>
-      <priority>MINOR</priority>
-    </rule>
-  </rules>
-</profile>
diff --git a/it/it-tests/src/test/resources/measure/suite/one-issue-per-file.xml b/it/it-tests/src/test/resources/measure/suite/one-issue-per-file.xml
new file mode 100644 (file)
index 0000000..7193ebf
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<profile>
+  <name>one-issue-per-file</name>
+  <language>xoo</language>
+  <rules>
+    <rule>
+      <repositoryKey>xoo</repositoryKey>
+      <key>OneIssuePerFile</key>
+      <priority>MINOR</priority>
+    </rule>
+  </rules>
+</profile>
diff --git a/it/it-tests/src/test/resources/measure/suite/one-issue-per-line.xml b/it/it-tests/src/test/resources/measure/suite/one-issue-per-line.xml
new file mode 100644 (file)
index 0000000..365aa89
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<profile>
+  <name>one-issue-per-line</name>
+  <language>xoo</language>
+  <rules>
+    <rule>
+      <repositoryKey>xoo</repositoryKey>
+      <key>OneIssuePerLine</key>
+      <priority>MINOR</priority>
+    </rule>
+  </rules>
+</profile>