Skip to content

Commit

Permalink
Improve tests and correct execution of MeasureComputers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingmar committed Mar 3, 2016
1 parent e01f120 commit ae6434b
Show file tree
Hide file tree
Showing 20 changed files with 381 additions and 278 deletions.
2 changes: 1 addition & 1 deletion executeMvn.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rem -Denforcer.skip=true

mvn -e -fn clean package com.hello2morrow.sonargraph:maven-sonargraph-plugin:7.2.3:architect-report -Dsonargraph.file=./sonar-sonargraph-plugin.sonargraph -Dsonargraph.prepareForSonar=true sonar:sonar -Dsonar.exclusions=**/generated-sources/**
mvnDebug -e -fn clean package com.hello2morrow.sonargraph:maven-sonargraph-plugin:7.2.3:architect-report -Dsonargraph.file=./sonar-sonargraph-plugin.sonargraph -Dsonargraph.prepareForSonar=true sonar:sonar -Dsonar.exclusions=**/generated-sources/**
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public String toString() {
return "Sonar-Sonargraph-Plugin [" + PluginVersionReader.INSTANCE.getVersion() + "]";
}

private boolean isValidProject(final Project project, final SensorContext sensorContext) {
boolean isValidProject(final Project project, final SensorContext sensorContext) {
if (project == null || sensorContext == null) {
LOG.error("Major error calling Sonargraph Sonar Plugin: Project and / or sensorContext are null. " + "Please check your project configuration!");
return false;
Expand All @@ -103,6 +103,7 @@ private boolean isValidProject(final Project project, final SensorContext sensor
return false;
}

sensorContext.saveMeasure(SonargraphInternalMetrics.ROOT_PROJECT_TO_BE_PROCESSED, SonarQubeUtilities.FALSE);
return true;
}

Expand All @@ -117,8 +118,6 @@ public void analyse(final Project project, final SensorContext sensorContext) {
}

LOG.info("Sonargraph: Execute for module " + project.getName() + " [" + project.getKey() + "]");
sensorContext.saveMeasure(SonargraphInternalMetrics.ROOT_PROJECT_TO_BE_PROCESSED, SonarQubeUtilities.FALSE);

final IReportReader reportReader = new ReportFileReader();
reportReader.readSonargraphReport(project, sensorContext.fileSystem(), settings);
if (PersistenceUtilities.getSonargraphBasePath(reportReader.getReport()) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,14 @@ void internalCompute(final MeasureComputerContext context) {
}

int value = 0;
boolean valuesFound = false;
for (final Iterator<Measure> iterator = iterable.iterator(); iterator.hasNext();) {
value += iterator.next().getIntValue();
valuesFound = true;
}
if (valuesFound) {
context.addMeasure(nextKey, value);
}

context.addMeasure(nextKey, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.hello2morrow.sonarplugin.metric.SonargraphSimpleMetrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ce.measure.Component;
import org.sonar.api.ce.measure.Component.Type;
import org.sonar.api.ce.measure.Measure;
import org.sonar.api.measures.Metric;

Expand Down Expand Up @@ -54,12 +52,6 @@ List<String> getOutputMetrics() {
return SonarQubeUtilities.convertMetricListToKeyList(metrics);
}

@Override
boolean needsProcessing(final MeasureComputerContext context) {
final Type type = context.getComponent().getType();
return type == Component.Type.PROJECT || type == Component.Type.MODULE;
}

@Override
void internalCompute(final MeasureComputerContext context) {
saveViolationMeasures(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package com.hello2morrow.sonarplugin.measurecomputer;

import com.hello2morrow.sonarplugin.metric.internal.SonargraphInternalMetrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ce.measure.Component;
import org.sonar.api.ce.measure.Measure;
import org.sonar.api.ce.measure.MeasureComputer;
Expand All @@ -27,6 +29,8 @@

abstract class SonargraphMeasureComputer implements MeasureComputer {

private static final Logger LOGGER = LoggerFactory.getLogger(SonargraphMeasureComputer.class);

public SonargraphMeasureComputer() {
super();
}
Expand All @@ -51,15 +55,19 @@ public final void compute(final MeasureComputerContext context) {
internalCompute(context);
}

protected final boolean needsProcessing(final MeasureComputerContext context) {
final Measure measure = context.getMeasure(SonargraphInternalMetrics.ROOT_PROJECT_TO_BE_PROCESSED.key());
final boolean needsProcessing = measure != null && measure.getBooleanValue();
if (!needsProcessing) {
LOGGER.error("Not processing for module: " + context.getComponent().getKey());
}
return needsProcessing;
}

abstract void internalCompute(MeasureComputerContext context);

abstract List<String> getOutputMetrics();

abstract List<String> getInputMetrics();

boolean needsProcessing(final MeasureComputerContext context) {
final Measure measure = context.getMeasure(SonargraphInternalMetrics.ROOT_PROJECT_TO_BE_PROCESSED.key());
return measure != null && measure.getBooleanValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ private void transferMetricValue(final MeasureComputerContext context, final Met

@Override
List<String> getInputMetrics() {
return SonarQubeUtilities.convertMetricListToKeyList(Arrays.asList(SonargraphInternalMetrics.SYSTEM_ALL_WARNINGS, SonargraphInternalMetrics.SYSTEM_CYCLE_WARNINGS,
SonargraphInternalMetrics.SYSTEM_THRESHOLD_WARNINGS, SonargraphInternalMetrics.SYSTEM_WORKSPACE_WARNINGS, SonargraphInternalMetrics.SYSTEM_IGNORED_WARNINGS,
SonargraphInternalMetrics.SYSTEM_ALL_TASKS));
return SonarQubeUtilities.convertMetricListToKeyList(Arrays.asList(SonargraphInternalMetrics.MODULE_PROCESSED_BY_SENSOR, SonargraphInternalMetrics.SYSTEM_ALL_WARNINGS,
SonargraphInternalMetrics.SYSTEM_CYCLE_WARNINGS, SonargraphInternalMetrics.SYSTEM_THRESHOLD_WARNINGS, SonargraphInternalMetrics.SYSTEM_WORKSPACE_WARNINGS,
SonargraphInternalMetrics.SYSTEM_IGNORED_WARNINGS, SonargraphInternalMetrics.SYSTEM_ALL_TASKS));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static String getBuildUnitName(final XsdCycleGroup group) {
final String buildUnitName = group.getElementScope();

// special handling for reports produced with free SonarQube license or without Sonargraph system file
if ("My Project".equals(buildUnitName) && group.getParent() != null) {
if (DEFAULT_PROJECT_NAME.equals(buildUnitName) && group.getParent() != null) {
return group.getParent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public class ReportFileReader implements IReportReader {
private ReportContext report;

@Override
public void readSonargraphReport(final Project project, final FileSystem moduleFileSystem, final Settings settings) {
public void readSonargraphReport(final Project project, final FileSystem fileSystem, final Settings settings) {
if (project == null) {
LOG.error("No project provided for reading sonargraph report");
return;
}

final String reportFileName = determineReportFileName(moduleFileSystem, settings);
final String reportFileName = determineReportFileName(fileSystem, settings);
LOG.info("Reading Sonargraph metrics report from: " + reportFileName);
report = null;
InputStream input = null;
Expand Down Expand Up @@ -107,15 +107,15 @@ public ReportContext getReport() {
return report;
}

private static String determineReportFileName(final FileSystem moduleFileSystem, final Settings settings) {
private static String determineReportFileName(final FileSystem fileSystem, final Settings settings) {
final String configuredReportPath = SonargraphUtilities.getConfiguredReportPath(settings);

if (moduleFileSystem == null) {
if (fileSystem == null) {
return configuredReportPath;
}

if (configuredReportPath == null || configuredReportPath.length() == 0) {
return moduleFileSystem.workDir().getParentFile().getPath() + '/' + REPORT_DIR + '/' + REPORT_NAME;
return fileSystem.workDir().getParentFile().getPath() + '/' + REPORT_DIR + '/' + REPORT_NAME;
}

return configuredReportPath;
Expand Down
2 changes: 1 addition & 1 deletion src/test/AlarmClockMain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>AlarmClockMain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>AlarmClockMain</name>
<name>AlarmClockMainSg7</name>


<description>Project aggregating the different modules</description>
Expand Down

This file was deleted.

4 changes: 1 addition & 3 deletions src/test/java/com/hello2morrow/sonarplugin/api/ReadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.hello2morrow.sonarplugin.api;

import com.hello2morrow.sonarplugin.foundation.SonargraphPluginBase;
import com.hello2morrow.sonarplugin.foundation.TestHelper;
import com.hello2morrow.sonarplugin.persistence.IReportReader;
import com.hello2morrow.sonarplugin.persistence.ReportFileReader;
Expand All @@ -33,8 +32,7 @@ public class ReadTest extends TestCase {
@Test
public void testAnalyse() {
final Project project1 = new Project("test");
final Settings settings = TestHelper.initSettings();
settings.setProperty(SonargraphPluginBase.REPORT_PATH, "src/test/resources/infoglue21-report.xml");
final Settings settings = TestHelper.initSettings("src/test/resources/infoglue21-report.xml");
final IReportReader reader = new ReportFileReader();
reader.readSonargraphReport(project1, null, settings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public class SonargraphPluginTest {
/**
* Test method for {@link com.hello2morrow.sonarplugin.api.SonargraphPlugin#getExtensions()}.
*/
@SuppressWarnings("deprecation")
@Test
public void testGetExtensions() {
SonarPlugin plugin = new SonargraphPlugin();
final SonarPlugin plugin = new SonargraphPlugin();
Assert.assertNotNull(plugin.getExtensions());
Assert.assertTrue(plugin.getExtensions().size() > 0);
}
Expand Down
Loading

0 comments on commit ae6434b

Please sign in to comment.