From 86de4c3aff2d4eac1c7944e3658367c19784230c Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 4 May 2011 01:47:38 +0400 Subject: [PATCH] SONAR-2371 Rework UnitTestDecorator and SurefireSensor * UnitTestDecorator should not save zero value for tests metric * SurefireSensor should save zero when no reports and project do not contain any modules --- .../core/sensors/UnitTestDecorator.java | 3 -- .../core/sensors/UnitTestDecoratorTest.java | 29 ------------------- .../surefire/api/AbstractSurefireParser.java | 7 ++++- .../plugins/surefire/SurefireSensorTest.java | 15 ++++------ .../api/AbstractSurefireParserTest.java | 29 ++++++++++++------- 5 files changed, 31 insertions(+), 52 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java index 4229904a924..0658a9733ab 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java @@ -60,9 +60,6 @@ public class UnitTestDecorator implements Decorator { if (isPositive(tests, true) && isPositive(errors, false) && isPositive(failures, false)) { Double errorsAndFailuresRatio = (errors + failures) * 100.0 / tests; context.saveMeasure(CoreMetrics.TEST_SUCCESS_DENSITY, 100.0 - errorsAndFailuresRatio); - } else if (tests == null && ResourceUtils.isProject(resource)) { - // See http://jira.codehaus.org/browse/SONAR-2371 - context.saveMeasure(CoreMetrics.TESTS, 0.0); } } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java index ad2ca4152a1..0bcd1a5756f 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java @@ -32,7 +32,6 @@ import org.sonar.api.batch.DecoratorContext; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.measures.Metric; -import org.sonar.api.resources.JavaPackage; import org.sonar.api.resources.Project; import java.util.Arrays; @@ -86,9 +85,6 @@ public class UnitTestDecoratorTest { when(context.getChildrenMeasures(metric)).thenReturn(Arrays.asList(new Measure(metric, value), new Measure(metric, value))); } - /** - * See http://jira.codehaus.org/browse/SONAR-2371 - */ @Test public void doNotDecorateIfTestsMeasureAlreadyExists() { Project project = mock(Project.class); @@ -101,29 +97,4 @@ public class UnitTestDecoratorTest { verifyNoMoreInteractions(context); } - /** - * See http://jira.codehaus.org/browse/SONAR-2371 - */ - @Test - public void shouldSaveZeroOnProject() { - Project project = new Project("").setAnalysisType(Project.AnalysisType.DYNAMIC); - - decorator.decorate(project, context); - - verify(context).saveMeasure(CoreMetrics.TESTS, 0.0); - } - - /** - * See http://jira.codehaus.org/browse/SONAR-2371 - */ - @Test - public void shouldNotSaveZeroOnPackage() { - JavaPackage pkg = new JavaPackage(); - - decorator.decorate(pkg, context); - - assertThat(decorator.shouldDecorateResource(pkg, context), is(true)); - verify(context, never()).saveMeasure(CoreMetrics.TESTS, 0.0); - } - } diff --git a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java index d0cc6262093..85e7a2682f8 100644 --- a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java +++ b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java @@ -47,7 +47,12 @@ public abstract class AbstractSurefireParser { public void collect(Project project, SensorContext context, File reportsDir) { File[] xmlFiles = getReports(reportsDir); - if (xmlFiles.length != 0) { + if (xmlFiles.length == 0) { + // See http://jira.codehaus.org/browse/SONAR-2371 + if (project.getModules().isEmpty()) { + context.saveMeasure(CoreMetrics.TESTS, 0.0); + } + } else { parseFiles(context, xmlFiles); } } diff --git a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/SurefireSensorTest.java b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/SurefireSensorTest.java index 690603fc091..d288fc12454 100644 --- a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/SurefireSensorTest.java +++ b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/SurefireSensorTest.java @@ -19,6 +19,12 @@ */ package org.sonar.plugins.surefire; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + import org.apache.commons.lang.ObjectUtils; import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.Diff; @@ -40,15 +46,6 @@ import java.io.FileReader; import java.io.StringReader; import java.net.URISyntaxException; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.anyDouble; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.*; - public class SurefireSensorTest { @Test diff --git a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java index 15085fee0c4..e4d195ebe42 100644 --- a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java +++ b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java @@ -26,11 +26,11 @@ import org.sonar.api.batch.SensorContext; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; import org.sonar.api.resources.*; -import org.sonar.api.resources.File; import org.sonar.api.test.IsMeasure; import org.sonar.api.test.IsResource; import java.net.URISyntaxException; +import java.util.Arrays; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyDouble; @@ -52,34 +52,43 @@ public class AbstractSurefireParserTest { verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), argThat(new IsMeasure(CoreMetrics.TEST_DATA))); } + /** + * See http://jira.codehaus.org/browse/SONAR-2371 + */ @Test - public void shouldNotFailIfNoReports() throws URISyntaxException { + public void shouldInsertZeroWhenNoReports() throws URISyntaxException { AbstractSurefireParser parser = newParser(); SensorContext context = mockContext(); + Project project = mock(Project.class); - parser.collect(new Project("foo"), context, getDir("noReports")); + parser.collect(project, context, getDir("noReports")); - verifyZeroInteractions(context); + verify(context).saveMeasure(CoreMetrics.TESTS, 0.0); } + /** + * See http://jira.codehaus.org/browse/SONAR-2371 + */ @Test - public void shouldNotInsertZeroOnFiles() throws URISyntaxException { + public void shouldNotInsertZeroWhenNoReports() throws URISyntaxException { AbstractSurefireParser parser = newParser(); SensorContext context = mockContext(); + Project project = mock(Project.class); + when(project.getModules()).thenReturn(Arrays.asList(new Project("foo"))); - parser.collect(new Project("foo"), context, getDir("noTests")); + parser.collect(project, context, getDir("noReports")); - verify(context, never()).saveMeasure(any(Resource.class),(Metric)anyObject(), anyDouble()); + verify(context, never()).saveMeasure(CoreMetrics.TESTS, 0.0); } @Test - public void shouldNotInsertMeasuresOnPomProjects() throws URISyntaxException { + public void shouldNotInsertZeroOnFiles() throws URISyntaxException { AbstractSurefireParser parser = newParser(); SensorContext context = mockContext(); - parser.collect(new Project("foo").setPackaging("pom"), context, getDir("noReports")); + parser.collect(new Project("foo"), context, getDir("noTests")); - verify(context, never()).saveMeasure(eq(CoreMetrics.TESTS), anyDouble()); + verify(context, never()).saveMeasure(any(Resource.class),(Metric)anyObject(), anyDouble()); } @Test -- 2.39.5