Skip to content

Commit

Permalink
Merge pull request #1532 from ivangalkin/CxxUnitTestResultsImportSensor
Browse files Browse the repository at this point in the history
CxxUnitTestResultsImportSensor: simplify and update
  • Loading branch information
guwirth authored Aug 3, 2018
2 parents a385723 + cc0765e commit f801e04
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@
// mailto:info AT sonarsource DOT com

import java.io.File;

import org.sonar.api.batch.ScannerSide;
import org.sonar.api.config.Configuration;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.CxxLanguage;

/**
* CxxUnitTestResultsAggregator (from .Net test library)
*/
@ScannerSide
public class CxxUnitTestResultsAggregator {

private static final String EXIST_CONFIGURATION_PARAMETER = "Exist configuration parameter: '{}':'{}'";
private static final Logger LOG = Loggers.get(CxxUnitTestResultsAggregator.class);
private final UnitTestConfiguration unitTestConf;
private final Configuration settings;
private final VisualStudioTestResultsFileParser visualStudioTestResultsFileParser;
private final XUnitTestResultsFileParser xunitTestResultsFileParser;
private final NUnitTestResultsFileParser nunitTestResultsFileParser;
Expand All @@ -50,71 +46,32 @@ public class CxxUnitTestResultsAggregator {
* @param language C or C++
* @param settings SQ Configuration
*/
public CxxUnitTestResultsAggregator(CxxLanguage language, Configuration settings) {
this(new UnitTestConfiguration(language), settings,
new VisualStudioTestResultsFileParser(),
new XUnitTestResultsFileParser(),
new NUnitTestResultsFileParser()
);
public CxxUnitTestResultsAggregator() {
this(new VisualStudioTestResultsFileParser(), new XUnitTestResultsFileParser(), new NUnitTestResultsFileParser());
}

CxxUnitTestResultsAggregator(UnitTestConfiguration unitTestConf, Configuration settings,
VisualStudioTestResultsFileParser visualStudioTestResultsFileParser,
XUnitTestResultsFileParser xunitTestResultsFileParser,
NUnitTestResultsFileParser nunitTestResultsFileParser
) {
this.unitTestConf = unitTestConf;
this.settings = settings;
CxxUnitTestResultsAggregator(VisualStudioTestResultsFileParser visualStudioTestResultsFileParser,
XUnitTestResultsFileParser xunitTestResultsFileParser, NUnitTestResultsFileParser nunitTestResultsFileParser) {
this.visualStudioTestResultsFileParser = visualStudioTestResultsFileParser;
this.xunitTestResultsFileParser = xunitTestResultsFileParser;
this.nunitTestResultsFileParser = nunitTestResultsFileParser;
}

boolean hasUnitTestResultsProperty() {
return hasVisualStudioTestResultsFile() || hasXUnitTestResultsFile() || hasNUnitTestResultsFile();
}

private boolean hasVisualStudioTestResultsFile() {
if (LOG.isDebugEnabled()) {
LOG.debug(EXIST_CONFIGURATION_PARAMETER, unitTestConf.visualStudioTestResultsFilePropertyKey(),
settings.hasKey(unitTestConf.visualStudioTestResultsFilePropertyKey()));
}
return settings.hasKey(unitTestConf.visualStudioTestResultsFilePropertyKey());
}

private boolean hasXUnitTestResultsFile() {
if (LOG.isDebugEnabled()) {
LOG.debug(EXIST_CONFIGURATION_PARAMETER, unitTestConf.xunitTestResultsFilePropertyKey(),
settings.hasKey(unitTestConf.xunitTestResultsFilePropertyKey()));
}
return settings.hasKey(unitTestConf.xunitTestResultsFilePropertyKey());
}

private boolean hasNUnitTestResultsFile() {
if (LOG.isDebugEnabled()) {
LOG.debug(EXIST_CONFIGURATION_PARAMETER, unitTestConf.nunitTestResultsFilePropertyKey(),
settings.hasKey(unitTestConf.nunitTestResultsFilePropertyKey()));
}
return settings.hasKey(unitTestConf.nunitTestResultsFilePropertyKey());
}

UnitTestResults aggregate(WildcardPatternFileProvider wildcardPatternFileProvider, UnitTestResults unitTestResults) {
if (hasVisualStudioTestResultsFile()) {
aggregate(wildcardPatternFileProvider,
settings.getStringArray(unitTestConf.visualStudioTestResultsFilePropertyKey()),
visualStudioTestResultsFileParser, unitTestResults);
UnitTestResults aggregate(WildcardPatternFileProvider wildcardPatternFileProvider, UnitTestResults unitTestResults,
UnitTestConfiguration unitTestConf) {
if (unitTestConf.hasVisualStudioTestResultsFile()) {
aggregate(wildcardPatternFileProvider, unitTestConf.getVisualStudioTestResultsFiles(),
visualStudioTestResultsFileParser, unitTestResults);
}

if (hasXUnitTestResultsFile()) {
aggregate(wildcardPatternFileProvider,
settings.getStringArray(unitTestConf.xunitTestResultsFilePropertyKey()),
xunitTestResultsFileParser, unitTestResults);
if (unitTestConf.hasXUnitTestResultsFile()) {
aggregate(wildcardPatternFileProvider, unitTestConf.getXUnitTestResultsFiles(), xunitTestResultsFileParser,
unitTestResults);
}

if (hasNUnitTestResultsFile()) {
aggregate(wildcardPatternFileProvider,
settings.getStringArray(unitTestConf.nunitTestResultsFilePropertyKey()),
nunitTestResultsFileParser, unitTestResults);
if (unitTestConf.hasNUnitTestResultsFile()) {
aggregate(wildcardPatternFileProvider, unitTestConf.getNUnitTestResultsFiles(), nunitTestResultsFileParser,
unitTestResults);
}

return unitTestResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,23 @@
// mailto:info AT sonarsource DOT com

import java.io.File;
import org.sonar.api.batch.bootstrap.ProjectDefinition;

import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.CxxLanguage;

public class CxxUnitTestResultsImportSensor implements Sensor {

private static final Logger LOG = Loggers.get(CxxUnitTestResultsImportSensor.class);

private final WildcardPatternFileProvider wildcardPatternFileProvider
= new WildcardPatternFileProvider(new File("."), File.separator);
private final CxxUnitTestResultsAggregator unitTestResultsAggregator;
private final ProjectDefinition projectDef;
protected final CxxLanguage language;

public CxxUnitTestResultsImportSensor(CxxUnitTestResultsAggregator unitTestResultsAggregator,
ProjectDefinition projectDef,
CxxLanguage language) {
this.unitTestResultsAggregator = unitTestResultsAggregator;
this.projectDef = projectDef;
this.language = language;
}

Expand All @@ -57,23 +50,18 @@ public void describe(SensorDescriptor descriptor) {
String name = String.format("%s Unit Test Results Import", this.language.getName());
descriptor.name(name);
descriptor.global();
descriptor.onlyWhenConfiguration(conf -> new UnitTestConfiguration(language, conf).hasUnitTestResultsProperty());
descriptor.onlyOnLanguage(this.language.getKey());
}

@Override
public void execute(SensorContext context) {
if (!unitTestResultsAggregator.hasUnitTestResultsProperty()) {
LOG.info("No unit test results property. Skip Sensor");
return;
}
if (projectDef.getParent() == null) {
analyze(context, new UnitTestResults());
}
analyze(context, new UnitTestResults(), new UnitTestConfiguration(language, context.config()));
}

void analyze(SensorContext context, UnitTestResults unitTestResults) {
void analyze(SensorContext context, UnitTestResults unitTestResults, UnitTestConfiguration unitTestConf) {
UnitTestResults aggregatedResults = unitTestResultsAggregator.aggregate(wildcardPatternFileProvider,
unitTestResults);
unitTestResults, unitTestConf);

context.<Integer>newMeasure()
.forMetric(CoreMetrics.TESTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/
package org.sonar.cxx.sensors.tests.dotnet;

import org.sonar.api.config.Configuration;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

// origin https://github.com/SonarSource/sonar-dotnet-tests-library/
// SonarQube .NET Tests Library
// Copyright (C) 2014-2017 SonarSource SA
Expand All @@ -28,24 +32,60 @@

public class UnitTestConfiguration {

private final CxxLanguage language;
private static final String EXIST_CONFIGURATION_PARAMETER = "Exist configuration parameter: '{}':'{}'";
private static final Logger LOG = Loggers.get(UnitTestConfiguration.class);

public static final String VISUAL_STUDIO_TEST_RESULTS_PROPERTY_KEY = "vstest.reportsPaths";
public static final String XUNIT_TEST_RESULTS_PROPERTY_KEY = "xunit.reportsPaths";
public static final String NUNIT_TEST_RESULTS_PROPERTY_KEY = "nunit.reportsPaths";

public UnitTestConfiguration(CxxLanguage language) {
this.language = language;
private final Configuration config;
private final String vsKeyEffective;
private final String xUnitKeyEffective;
private final String nUnitKeyEffective;

public UnitTestConfiguration(CxxLanguage language, Configuration config) {
this.config = config;
vsKeyEffective = language.getPluginProperty(VISUAL_STUDIO_TEST_RESULTS_PROPERTY_KEY);
xUnitKeyEffective = language.getPluginProperty(XUNIT_TEST_RESULTS_PROPERTY_KEY);
nUnitKeyEffective = language.getPluginProperty(NUNIT_TEST_RESULTS_PROPERTY_KEY);
}

boolean hasVisualStudioTestResultsFile() {
if (LOG.isDebugEnabled()) {
LOG.debug(EXIST_CONFIGURATION_PARAMETER, vsKeyEffective, config.hasKey(vsKeyEffective));
}
return config.hasKey(vsKeyEffective);
}

boolean hasXUnitTestResultsFile() {
if (LOG.isDebugEnabled()) {
LOG.debug(EXIST_CONFIGURATION_PARAMETER, xUnitKeyEffective, config.hasKey(xUnitKeyEffective));
}
return config.hasKey(xUnitKeyEffective);
}

boolean hasNUnitTestResultsFile() {
if (LOG.isDebugEnabled()) {
LOG.debug(EXIST_CONFIGURATION_PARAMETER, nUnitKeyEffective, config.hasKey(nUnitKeyEffective));
}
return config.hasKey(nUnitKeyEffective);
}

String visualStudioTestResultsFilePropertyKey() {
return language.getPluginProperty(VISUAL_STUDIO_TEST_RESULTS_PROPERTY_KEY);
boolean hasUnitTestResultsProperty() {
return hasVisualStudioTestResultsFile() || hasXUnitTestResultsFile() || hasNUnitTestResultsFile();
}

String xunitTestResultsFilePropertyKey() {
return language.getPluginProperty(XUNIT_TEST_RESULTS_PROPERTY_KEY);
String[] getVisualStudioTestResultsFiles() {
return config.getStringArray(vsKeyEffective);
}

String nunitTestResultsFilePropertyKey() {
return language.getPluginProperty(NUNIT_TEST_RESULTS_PROPERTY_KEY);
String[] getXUnitTestResultsFiles() {
return config.getStringArray(xUnitKeyEffective);
}

String[] getNUnitTestResultsFiles() {
return config.getStringArray(nUnitKeyEffective);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,33 @@ public void hasUnitTestResultsProperty() {
when(settings.hasKey(key1)).thenReturn(false);
when(settings.hasKey(key2)).thenReturn(false);
when(settings.hasKey(key3)).thenReturn(false);
assertThat(new CxxUnitTestResultsAggregator(language, settings).hasUnitTestResultsProperty()).isFalse();
assertThat(new UnitTestConfiguration(language, settings).hasUnitTestResultsProperty()).isFalse();

when(settings.hasKey(key1)).thenReturn(true);
when(settings.hasKey(key2)).thenReturn(false);
when(settings.hasKey(key3)).thenReturn(false);
assertThat(new CxxUnitTestResultsAggregator(language, settings).hasUnitTestResultsProperty()).isTrue();
assertThat(new UnitTestConfiguration(language, settings).hasUnitTestResultsProperty()).isTrue();

when(settings.hasKey(key1)).thenReturn(false);
when(settings.hasKey(key2)).thenReturn(true);
when(settings.hasKey(key3)).thenReturn(false);
assertThat(new CxxUnitTestResultsAggregator(language, settings).hasUnitTestResultsProperty()).isTrue();
assertThat(new UnitTestConfiguration(language, settings).hasUnitTestResultsProperty()).isTrue();

when(settings.hasKey(key1)).thenReturn(false);
when(settings.hasKey(key2)).thenReturn(false);
when(settings.hasKey(key3)).thenReturn(true);
assertThat(new CxxUnitTestResultsAggregator(language, settings).hasUnitTestResultsProperty()).isTrue();
assertThat(new UnitTestConfiguration(language, settings).hasUnitTestResultsProperty()).isTrue();

when(settings.hasKey(key1)).thenReturn(true);
when(settings.hasKey(key2)).thenReturn(true);
when(settings.hasKey(key3)).thenReturn(true);
assertThat(new CxxUnitTestResultsAggregator(language, settings).hasUnitTestResultsProperty()).isTrue();
assertThat(new UnitTestConfiguration(language, settings).hasUnitTestResultsProperty()).isTrue();
}

@Test
public void aggregate() {
WildcardPatternFileProvider wildcardPatternFileProvider = mock(WildcardPatternFileProvider.class);
MapSettings settings = new MapSettings();
UnitTestConfiguration unitTestConf = new UnitTestConfiguration(language);

// Visual Studio test results only
settings.setProperty(key1, "foo.trx");
Expand All @@ -106,9 +105,9 @@ public void aggregate() {
XUnitTestResultsFileParser xunitTestResultsFileParser = mock(XUnitTestResultsFileParser.class);
NUnitTestResultsFileParser nunitTestResultsFileParser = mock(NUnitTestResultsFileParser.class);
UnitTestResults results = mock(UnitTestResults.class);
new CxxUnitTestResultsAggregator(unitTestConf, settings.asConfig(),
visualStudioTestResultsFileParser, xunitTestResultsFileParser, nunitTestResultsFileParser)
.aggregate(wildcardPatternFileProvider, results);
new CxxUnitTestResultsAggregator(visualStudioTestResultsFileParser, xunitTestResultsFileParser,
nunitTestResultsFileParser).aggregate(wildcardPatternFileProvider, results,
new UnitTestConfiguration(language, settings.asConfig()));
verify(visualStudioTestResultsFileParser).accept(new File("foo.trx"), results);

// XUnit test results only
Expand All @@ -119,9 +118,9 @@ public void aggregate() {
xunitTestResultsFileParser = mock(XUnitTestResultsFileParser.class);
nunitTestResultsFileParser = mock(NUnitTestResultsFileParser.class);
results = mock(UnitTestResults.class);
new CxxUnitTestResultsAggregator(unitTestConf, settings.asConfig(),
visualStudioTestResultsFileParser, xunitTestResultsFileParser, nunitTestResultsFileParser)
.aggregate(wildcardPatternFileProvider, results);
new CxxUnitTestResultsAggregator(visualStudioTestResultsFileParser, xunitTestResultsFileParser,
nunitTestResultsFileParser).aggregate(wildcardPatternFileProvider, results,
new UnitTestConfiguration(language, settings.asConfig()));
verify(visualStudioTestResultsFileParser, Mockito.never()).accept(Mockito.any(File.class), Mockito.any(UnitTestResults.class));
verify(xunitTestResultsFileParser).accept(new File("foo.xml"), results);
verify(nunitTestResultsFileParser, Mockito.never()).accept(Mockito.any(File.class), Mockito.any(UnitTestResults.class));
Expand All @@ -138,9 +137,9 @@ public void aggregate() {
xunitTestResultsFileParser = mock(XUnitTestResultsFileParser.class);
nunitTestResultsFileParser = mock(NUnitTestResultsFileParser.class);
results = mock(UnitTestResults.class);
new CxxUnitTestResultsAggregator(unitTestConf, settings.asConfig(),
visualStudioTestResultsFileParser, xunitTestResultsFileParser, nunitTestResultsFileParser)
.aggregate(wildcardPatternFileProvider, results);
new CxxUnitTestResultsAggregator(visualStudioTestResultsFileParser, xunitTestResultsFileParser,
nunitTestResultsFileParser).aggregate(wildcardPatternFileProvider, results,
new UnitTestConfiguration(language, settings.asConfig()));
verify(visualStudioTestResultsFileParser).accept(new File("foo.trx"), results);
verify(xunitTestResultsFileParser).accept(new File("foo.xml"), results);
verify(nunitTestResultsFileParser).accept(new File("foo1.xml"), results);
Expand All @@ -151,13 +150,13 @@ public void aggregate() {
xunitTestResultsFileParser = mock(XUnitTestResultsFileParser.class);
nunitTestResultsFileParser = mock(NUnitTestResultsFileParser.class);
results = mock(UnitTestResults.class);
new CxxUnitTestResultsAggregator(unitTestConf, settings.asConfig(),
visualStudioTestResultsFileParser, xunitTestResultsFileParser, nunitTestResultsFileParser)
.aggregate(wildcardPatternFileProvider, results);
new CxxUnitTestResultsAggregator(visualStudioTestResultsFileParser, xunitTestResultsFileParser,
nunitTestResultsFileParser).aggregate(wildcardPatternFileProvider, results,
new UnitTestConfiguration(language, settings.asConfig()));
verify(visualStudioTestResultsFileParser, Mockito.never()).accept(Mockito.any(File.class), Mockito.any(UnitTestResults.class));
verify(xunitTestResultsFileParser, Mockito.never()).accept(Mockito.any(File.class), Mockito.any(UnitTestResults.class));
verify(nunitTestResultsFileParser, Mockito.never()).accept(Mockito.any(File.class), Mockito.any(UnitTestResults.class));

// Multiple files configured
Mockito.reset(wildcardPatternFileProvider);
settings.clear();
Expand All @@ -175,9 +174,9 @@ public void aggregate() {
nunitTestResultsFileParser = mock(NUnitTestResultsFileParser.class);
results = mock(UnitTestResults.class);

new CxxUnitTestResultsAggregator(unitTestConf, settings.asConfig(),
visualStudioTestResultsFileParser, xunitTestResultsFileParser, nunitTestResultsFileParser)
.aggregate(wildcardPatternFileProvider, results);
new CxxUnitTestResultsAggregator(visualStudioTestResultsFileParser, xunitTestResultsFileParser,
nunitTestResultsFileParser).aggregate(wildcardPatternFileProvider, results,
new UnitTestConfiguration(language, settings.asConfig()));

verify(wildcardPatternFileProvider).listFiles("*.trx");
verify(wildcardPatternFileProvider).listFiles("bar.trx");
Expand Down
Loading

0 comments on commit f801e04

Please sign in to comment.