From 74234ead070059ec4a0ac03c8ccbcefdb98c1596 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 13 Jul 2016 17:35:18 +0200 Subject: [PATCH] SONAR-7870 Do not ignore unit tests when computing measures based on issues --- .../NewQualityModelMeasuresVisitor.java | 4 ---- .../QualityModelMeasuresVisitor.java | 6 ++--- .../NewQualityModelMeasuresVisitorTest.java | 23 ++++--------------- ...lityModelMeasuresVisitorForReportTest.java | 19 ++++++++------- 4 files changed, 18 insertions(+), 34 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/NewQualityModelMeasuresVisitor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/NewQualityModelMeasuresVisitor.java index 0a9df567c79..325c76a4d03 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/NewQualityModelMeasuresVisitor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/NewQualityModelMeasuresVisitor.java @@ -104,10 +104,6 @@ public class NewQualityModelMeasuresVisitor extends PathAwareVisitorAdapter path) { - if (file.getFileAttributes().isUnitTest()) { - return; - } - initNewDebtRatioCounter(file, path); computeAndSaveNewDebtRatioMeasure(file, path); increaseNewDebtAndDevCostOfParent(path); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitor.java index a6bd2569c06..8fb1b81cfd1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitor.java @@ -116,10 +116,8 @@ public class QualityModelMeasuresVisitor extends PathAwareVisitorAdapter path) { - if (!file.getFileAttributes().isUnitTest()) { - path.current().addDevCosts(computeDevelopmentCost(file)); - computeAndSaveMeasures(file, path); - } + path.current().addDevCosts(computeDevelopmentCost(file)); + computeAndSaveMeasures(file, path); } private long computeDevelopmentCost(Component file) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/NewQualityModelMeasuresVisitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/NewQualityModelMeasuresVisitorTest.java index 27f402162b6..35a273cf5db 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/NewQualityModelMeasuresVisitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/NewQualityModelMeasuresVisitorTest.java @@ -195,29 +195,16 @@ public class NewQualityModelMeasuresVisitorTest { } @Test - public void no_new_debt_ratio_when_file_is_unit_test() { + public void new_debt_ratio_when_file_is_unit_test() { setTwoPeriods(); when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST); - setupOneFileAloneInAProject(50, 12, Flag.UT_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET); - measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12)); - - underTest.visit(treeRootHolder.getRoot()); - - assertNoNewDebtRatioMeasure(LANGUAGE_1_FILE_REF); - assertNewDebtRatioValues(ROOT_REF, 0, 0); - } - - @Test - public void new_debt_ratio_is_0_on_non_file_level_when_all_files_are_unit_test() { - setTwoPeriods(); - when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST); - setupOneFileAloneInAProject(50, 12, Flag.UT_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET); - measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(200, 162)); + setupOneFileAloneInAProject(500, 120, Flag.UT_FILE, Flag.WITH_NCLOC, Flag.WITH_CHANGESET); + measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(1200, 820)); underTest.visit(treeRootHolder.getRoot()); - assertNoNewDebtRatioMeasure(LANGUAGE_1_FILE_REF); - assertNewDebtRatioValues(ROOT_REF, 0, 0); + assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 833.33, 0); + assertNewDebtRatioValues(ROOT_REF, 833.33, 0); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitorForReportTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitorForReportTest.java index 0b2dee0df96..caa5d2116f1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitorForReportTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/qualitymodel/QualityModelMeasuresVisitorForReportTest.java @@ -175,6 +175,7 @@ public class QualityModelMeasuresVisitorForReportTest { .addChildren( createFileComponent(LANGUAGE_KEY_1, 1111), createFileComponent(LANGUAGE_KEY_2, 1112), + // Unit test should not be ignored builder(FILE, 1113).setFileAttributes(new FileAttributes(true, LANGUAGE_KEY_1)).build()) .build(), builder(DIRECTORY, 112) @@ -198,9 +199,12 @@ public class QualityModelMeasuresVisitorForReportTest { int ncloc1111 = 10; addRawMeasure(NCLOC_KEY, 1111, ncloc1111); - int ncloc1112 = 10; + int ncloc1112 = 12; addRawMeasure(NCLOC_KEY, 1112, ncloc1112); + int ncloc1113 = 15; + addRawMeasure(NCLOC_KEY, 1113, ncloc1113); + int nclocValue1121 = 30; addRawMeasure(NCLOC_KEY, 1121, nclocValue1121); @@ -211,8 +215,8 @@ public class QualityModelMeasuresVisitorForReportTest { // verify measures on files verifyAddedRawMeasure(1111, DEVELOPMENT_COST_KEY, Long.toString(ncloc1111 * DEV_COST_LANGUAGE_1)); - verifyAddedRawMeasure(1112, DEVELOPMENT_COST_KEY, Long.toString(ncloc1111 * DEV_COST_LANGUAGE_2)); - verifyNoAddedRawMeasure(1113); + verifyAddedRawMeasure(1112, DEVELOPMENT_COST_KEY, Long.toString(ncloc1112 * DEV_COST_LANGUAGE_2)); + verifyAddedRawMeasure(1113, DEVELOPMENT_COST_KEY, Long.toString(ncloc1113 * DEV_COST_LANGUAGE_1)); verifyAddedRawMeasure(1121, DEVELOPMENT_COST_KEY, Long.toString(nclocValue1121 * DEV_COST_LANGUAGE_2)); verifyAddedRawMeasure(1211, DEVELOPMENT_COST_KEY, Long.toString(ncloc1211 * DEV_COST_LANGUAGE_1)); @@ -222,7 +226,8 @@ public class QualityModelMeasuresVisitorForReportTest { // directory has children => dev cost is aggregated verifyAddedRawMeasure(111, DEVELOPMENT_COST_KEY, Long.toString( ncloc1111 * DEV_COST_LANGUAGE_1 + - ncloc1112 * DEV_COST_LANGUAGE_2)); + ncloc1112 * DEV_COST_LANGUAGE_2 + + ncloc1113 * DEV_COST_LANGUAGE_1)); verifyAddedRawMeasure(112, DEVELOPMENT_COST_KEY, Long.toString(nclocValue1121 * DEV_COST_LANGUAGE_2)); verifyAddedRawMeasure(121, DEVELOPMENT_COST_KEY, Long.toString(ncloc1211 * DEV_COST_LANGUAGE_1)); @@ -230,12 +235,14 @@ public class QualityModelMeasuresVisitorForReportTest { verifyAddedRawMeasure(11, DEVELOPMENT_COST_KEY, Long.toString( ncloc1111 * DEV_COST_LANGUAGE_1 + ncloc1112 * DEV_COST_LANGUAGE_2 + + ncloc1113 * DEV_COST_LANGUAGE_1 + nclocValue1121 * DEV_COST_LANGUAGE_2)); verifyAddedRawMeasure(12, DEVELOPMENT_COST_KEY, Long.toString(ncloc1211 * DEV_COST_LANGUAGE_1)); verifyAddedRawMeasure(13, DEVELOPMENT_COST_KEY, "0"); verifyAddedRawMeasure(1, DEVELOPMENT_COST_KEY, Long.toString( ncloc1111 * DEV_COST_LANGUAGE_1 + ncloc1112 * DEV_COST_LANGUAGE_2 + + ncloc1113 * DEV_COST_LANGUAGE_1 + nclocValue1121 * DEV_COST_LANGUAGE_2 + ncloc1211 * DEV_COST_LANGUAGE_1)); } @@ -484,10 +491,6 @@ public class QualityModelMeasuresVisitorForReportTest { assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(value))); } - private void verifyNoAddedRawMeasure(int componentRef) { - assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).isEmpty(); - } - private static ReportComponent createFileComponent(String languageKey1, int fileRef) { return builder(FILE, fileRef).setFileAttributes(new FileAttributes(false, languageKey1)).build(); } -- 2.39.5