diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-07-17 16:38:38 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-07-17 16:38:38 +0200 |
commit | 7664f184eb4778d7ff83a1dd9505a18295db85ef (patch) | |
tree | 86d971d8617b0110675665b5713b087de6f48696 | |
parent | 476bdc68a0e83fa504190faeef089a8be0de1341 (diff) | |
download | sonarqube-7664f184eb4778d7ff83a1dd9505a18295db85ef.tar.gz sonarqube-7664f184eb4778d7ff83a1dd9505a18295db85ef.zip |
SONAR-6682 Move computation comment_lines to CommentMeasuresStep
3 files changed, 33 insertions, 51 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/CommentMeasuresStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/CommentMeasuresStep.java index 0fd970dfd48..d36836b2825 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/CommentMeasuresStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/CommentMeasuresStep.java @@ -41,6 +41,7 @@ import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; import static org.sonar.api.measures.CoreMetrics.PUBLIC_API_KEY; import static org.sonar.api.measures.CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY_KEY; import static org.sonar.api.measures.CoreMetrics.PUBLIC_UNDOCUMENTED_API_KEY; +import static org.sonar.server.computation.component.Component.Type.FILE; /** * Computes comments measures on files and then aggregates them on higher components. @@ -58,9 +59,6 @@ public class CommentMeasuresStep implements ComputationStep { this.measureRepository = measureRepository; this.formulas = ImmutableList.<Formula>of( new DocumentationFormula(), - - // TODO replace EmptyCounter by a SumCounter fo compute comment_lines when {@link DUPLICATED_LINES_DENSITY} will be computed in CE - // new SumFormula(CoreMetrics.COMMENT_LINES_KEY), new CommentDensityFormula() ); } @@ -72,53 +70,53 @@ public class CommentMeasuresStep implements ComputationStep { .visit(treeRootHolder.getRoot()); } - private class CommentDensityFormula implements Formula<EmptyCounter> { + private class CommentDensityFormula implements Formula<SumCounter> { private final Metric nclocMetric; - private final Metric commentMetric; public CommentDensityFormula() { this.nclocMetric = metricRepository.getByKey(NCLOC_KEY); - this.commentMetric = metricRepository.getByKey(COMMENT_LINES_KEY); } @Override - public EmptyCounter createNewCounter() { - return new EmptyCounter(); + public SumCounter createNewCounter() { + return new SumCounter(COMMENT_LINES_KEY); } @Override - public Optional<Measure> createMeasure(EmptyCounter counter, CreateMeasureContext context) { - Optional<Measure> nclocsOpt = measureRepository.getRawMeasure(context.getComponent(), nclocMetric); - Optional<Measure> commentsOpt = measureRepository.getRawMeasure(context.getComponent(), commentMetric); - if (nclocsOpt.isPresent() && commentsOpt.isPresent()) { - double nclocs = nclocsOpt.get().getIntValue(); - double comments = commentsOpt.get().getIntValue(); - double divisor = nclocs + comments; - if (divisor > 0d) { - double value = 100d * (comments / divisor); - return Optional.of(Measure.newMeasureBuilder().create(value)); - } + public Optional<Measure> createMeasure(SumCounter counter, CreateMeasureContext context) { + return createCommentLinesMeasure(counter, context) + .or(createCommentLinesDensityMeasure(counter, context)); + } + + private Optional<Measure> createCommentLinesMeasure(SumCounter counter, CreateMeasureContext context) { + Optional<Integer> commentLines = counter.getValue(); + if (context.getMetric().getKey().equals(COMMENT_LINES_KEY) && context.getComponent().getType().isHigherThan(FILE) && commentLines.isPresent()) { + return Optional.of(Measure.newMeasureBuilder().create(commentLines.get())); } return Optional.absent(); } - @Override - public String[] getOutputMetricKeys() { - return new String[] {COMMENT_LINES_DENSITY_KEY}; - } - } - - private static class EmptyCounter implements Counter<EmptyCounter> { - - @Override - public void aggregate(EmptyCounter counter) { - // nothing to do + private Optional<Measure> createCommentLinesDensityMeasure(SumCounter counter, CreateMeasureContext context) { + if (context.getMetric().getKey().equals(COMMENT_LINES_DENSITY_KEY)) { + Optional<Measure> nclocsOpt = measureRepository.getRawMeasure(context.getComponent(), nclocMetric); + Optional<Integer> commentsOpt = counter.getValue(); + if (nclocsOpt.isPresent() && commentsOpt.isPresent()) { + double nclocs = nclocsOpt.get().getIntValue(); + double comments = commentsOpt.get(); + double divisor = nclocs + comments; + if (divisor > 0d) { + double value = 100d * (comments / divisor); + return Optional.of(Measure.newMeasureBuilder().create(value)); + } + } + } + return Optional.absent(); } @Override - public void aggregate(FileAggregateContext context) { - // nothing to do + public String[] getOutputMetricKeys() { + return new String[] {COMMENT_LINES_KEY, COMMENT_LINES_DENSITY_KEY}; } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/CommentMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/CommentMeasuresStepTest.java index e2bb500a23b..f5ecf8e1c74 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/CommentMeasuresStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/CommentMeasuresStepTest.java @@ -21,7 +21,6 @@ package org.sonar.server.computation.step; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.sonar.server.computation.batch.TreeRootHolderRule; @@ -95,7 +94,6 @@ public class CommentMeasuresStepTest { } @Test - @Ignore("Could be reactivated when formula will be added") public void aggregate_comment_lines() { measureRepository.addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(100)); measureRepository.addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(400)); @@ -117,14 +115,11 @@ public class CommentMeasuresStepTest { measureRepository.addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(150)); measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(200)); measureRepository.addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(50)); + measureRepository.addRawMeasure(DIRECTORY_REF, NCLOC_KEY, newMeasureBuilder().create(300)); - measureRepository.addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(200)); measureRepository.addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(300)); - measureRepository.addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(200)); measureRepository.addRawMeasure(MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(300)); - measureRepository.addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(200)); measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(300)); - measureRepository.addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(200)); underTest.execute(); @@ -142,14 +137,11 @@ public class CommentMeasuresStepTest { measureRepository.addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(200)); measureRepository.addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); + measureRepository.addRawMeasure(DIRECTORY_REF, NCLOC_KEY, newMeasureBuilder().create(300)); - measureRepository.addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(300)); - measureRepository.addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(300)); - measureRepository.addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(300)); - measureRepository.addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); underTest.execute(); @@ -167,14 +159,11 @@ public class CommentMeasuresStepTest { measureRepository.addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); + measureRepository.addRawMeasure(DIRECTORY_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(SUB_MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(MODULE_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(0)); - measureRepository.addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(0)); underTest.execute(); @@ -185,10 +174,6 @@ public class CommentMeasuresStepTest { public void not_compute_comment_density_when_no_ncloc() { measureRepository.addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(150)); measureRepository.addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(50)); - measureRepository.addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(200)); - measureRepository.addRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(200)); - measureRepository.addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(200)); - measureRepository.addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, newMeasureBuilder().create(200)); underTest.execute(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java index ea468baea8c..b7a623c1d16 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java @@ -225,7 +225,6 @@ public final class CoreMetrics { .setDirection(Metric.DIRECTION_BETTER) .setQualitative(false) .setDomain(DOMAIN_DOCUMENTATION) - .setFormula(new SumChildValuesFormula(false)) .create(); public static final String COMMENT_LINES_DENSITY_KEY = "comment_lines_density"; |