Skip to content

Commit

Permalink
SONAR-5876 add SqaleNewMeasuresVisitor to compute new_sqale_debt_ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Sep 22, 2015
1 parent 42d9977 commit 44236bb
Show file tree
Hide file tree
Showing 19 changed files with 931 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sonar.projectKey=sample
sonar.projectName=Sample
sonar.projectVersion=1.0-SNAPSHOT
sonar.sources=src/main/xoo
sonar.language=xoo
sonar.scm.provider=xoo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sample;

// class comment
public class Sample {

public Sample(int i) {
int j = i++;
}

private String method1() {
return "hello";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ncloc:9
comment_lines:5
ncloc_data:1=1;2=0;3=0;4=1;5=0;6=1;7=1;8=1;9=0;10=1;11=1;12=1;13=1;14=0
classes:1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
1,user1,2015-08-01
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sonar.projectKey=sample
sonar.projectName=Sample
sonar.projectVersion=1.0-SNAPSHOT
sonar.sources=src/main/xoo
sonar.language=xoo
sonar.scm.provider=xoo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sample;

// class comment
public class Sample {

public Sample(int i) {
int j = i++;
}

private String method1() {
return "hello";
}

private String method2() {
return "hello2";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ncloc:12
comment_lines:6
ncloc_data:1=1;2=0;3=0;4=1;5=0;6=1;7=1;8=1;9=0;10=1;11=1;12=1;13=0;14=1;15=1;16=1;17=1;18=0
classes:1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
2,user2,2015-09-17
2,user2,2015-09-17
2,user2,2015-09-17
2,user2,2015-09-17
1,user1,2015-09-01
1,user1,2015-09-01
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sonar.projectKey=sample
sonar.projectName=Sample
sonar.projectVersion=1.0-SNAPSHOT
sonar.sources=src/main/xoo
sonar.language=xoo
sonar.scm.provider=xoo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sample;

// class comment
public class Sample {

public Sample(int i) {
int j = i++;
}

private String method1() {
return "hello";
}

private String method2() {
return "hello2";
}

private String method3() {
String e = "hello3";
return e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ncloc:16
comment_lines:7
ncloc_data:1=1;2=0;3=0;4=1;5=0;6=1;7=1;8=1;9=0;10=1;11=1;12=1;13=0;14=1;15=1;16=1;17=0;18=1;19=1;20=1;21=1;22=1;23=0
classes:1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
1,user1,2015-09-01
2,user2,2015-09-17
2,user2,2015-09-17
2,user2,2015-09-17
2,user2,2015-09-17
3,user2,2015-09-20
3,user2,2015-09-20
3,user2,2015-09-20
3,user2,2015-09-20
3,user2,2015-09-20
1,user1,2015-09-01
1,user1,2015-09-01
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import analysis.suite.measure.CustomMeasuresTest;
import analysis.suite.measure.DifferentialPeriodsTest;
import analysis.suite.measure.MeasureFiltersTest;
import analysis.suite.measure.NewDebtRatioMeasureTest;
import analysis.suite.measure.TechnicalDebtMeasureVariationTest;
import analysis.suite.measure.TimeMachineTest;
import analysis.suite.testing.CoverageTest;
Expand All @@ -44,7 +45,8 @@
CoverageTest.class,
NewCoverageTest.class,
TestExecutionTest.class,
TechnicalDebtMeasureVariationTest.class
TechnicalDebtMeasureVariationTest.class,
NewDebtRatioMeasureTest.class
})
public class AnalysisTestSuite {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package analysis.suite.measure;

import analysis.suite.AnalysisTestSuite;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.locator.FileLocation;
import java.util.List;
import javax.annotation.Nullable;
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;
import static org.assertj.core.api.Assertions.within;

/**
* SONAR-5876
*/
public class NewDebtRatioMeasureTest {

private static final String NEW_DEBT_RATIO_METRIC_KEY = "new_sqale_debt_ratio";

@ClassRule
public static Orchestrator orchestrator = AnalysisTestSuite.ORCHESTRATOR;

@Before
public void cleanUpAnalysisData() {
orchestrator.resetData();
}

@Test
public void new_debt_ratio_is_computed_from_nes_debt_and_new_ncloc_count_per_file() throws Exception {
// This test assumes that period 1 is "since previous analysis" and 2 is "over 30 days"

// run analysis on the day of after the first commit (2015-09-01), with 'one-issue-per-line' profile
// => some issues at date 2015-09-02
defineQualityProfile("one-issue-per-line");
provisionSampleProject();
setSampleProjectQualityProfile("one-issue-per-line");
runSampleProjectAnalysis("v1", "sonar.projectDate", "2015-09-02");

// first analysis, no previous snapshot => periods not resolved => no value
assertNoNewDebtRatio();

// run analysis on the day after of second commit (2015-09-17) 'one-issue-per-line' profile*
// => 3 new issues will be created at date 2015-09-18
runSampleProjectAnalysis("v2", "sonar.projectDate", "2015-09-18");
assertNewDebtRatio(4.44, 4.44);

// run analysis on the day after of third commit (2015-09-20) 'one-issue-per-line' profile*
// => 4 new issues will be created at date 2015-09-21
runSampleProjectAnalysis("v3", "sonar.projectDate", "2015-09-21");
assertNewDebtRatio(4.17, 4.28);
}

private void assertNoNewDebtRatio() {
assertThat(getFileResourceWithVariations(NEW_DEBT_RATIO_METRIC_KEY)).isNull();
}

private void assertNewDebtRatio(@Nullable Double valuePeriod1, @Nullable Double valuePeriod2) {
Resource newTechnicalDebt = getFileResourceWithVariations(NEW_DEBT_RATIO_METRIC_KEY);
List<Measure> measures = newTechnicalDebt.getMeasures();
assertThat(measures.get(0).getVariation1()).isEqualTo(valuePeriod1, within(0.01));
assertThat(measures.get(0).getVariation2()).isEqualTo(valuePeriod2, within(0.01));
}

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 projectVersion, String... properties) {
ItUtils.runProjectAnalysis(
NewDebtRatioMeasureTest.orchestrator,
"measure/xoo-new-debt-ratio-" + projectVersion,
ItUtils.concat(properties,
// disable standard scm support so that it does not interfere with Xoo Scm sensor
"sonar.scm.disabled", "false")
);
}

private Resource getFileResourceWithVariations(String metricKey) {
return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", metricKey).setIncludeTrends(true));
}

}
25 changes: 22 additions & 3 deletions it/it-tests/src/test/java/util/ItUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.SonarRunner;
Expand All @@ -20,6 +21,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.google.common.collect.FluentIterable.from;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.fail;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -132,10 +135,26 @@ public static int countIssuesInJsonReport(BuildResult result, boolean onlyNews)
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++) {
for (int i = 0; i < properties.length; i+=2) {
builder.put(properties[i], properties[i+1]);
i+=2;
}
orchestrator.executeBuild(sonarRunner.setProperties(builder.build()));
orchestrator.executeBuild(sonarRunner.setDebugLogs(true).setProperties(builder.build()));
}

/**
* Concatenates a vararg to a String array.
*
* Useful when using {@link #runProjectAnalysis(Orchestrator, String, String...)}, eg.:
* <pre>
* ItUtils.runProjectAnalysis(orchestrator, "project_name",
* ItUtils.concat(properties, "sonar.scm.disabled", "false")
* );
* </pre>
*/
public static String[] concat(String[] properties, String... str) {
if (properties == null || properties.length == 0) {
return str;
}
return from(Iterables.concat(asList(properties), asList(str))).toArray(String.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.sonar.server.computation.source.SourceLinesRepositoryImpl;
import org.sonar.server.computation.sqale.SqaleMeasuresVisitor;
import org.sonar.server.computation.sqale.SqaleRatingSettings;
import org.sonar.server.computation.sqale.SqaleNewMeasuresVisitor;
import org.sonar.server.computation.step.ComputationSteps;
import org.sonar.server.computation.step.ReportComputationSteps;
import org.sonar.server.view.index.ViewIndex;
Expand Down Expand Up @@ -169,6 +170,7 @@ private static List componentClasses() {
IntegrateIssuesVisitor.class,
CloseIssuesOnRemovedComponentsVisitor.class,
SqaleMeasuresVisitor.class,
SqaleNewMeasuresVisitor.class,
LastCommitVisitor.class,
MeasureComputersVisitor.class,

Expand Down
Loading

0 comments on commit 44236bb

Please sign in to comment.