Skip to content

Commit

Permalink
Fix some quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Feb 20, 2015
1 parent eec202c commit a4e409c
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 107 deletions.
Expand Up @@ -23,6 +23,7 @@
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.batch.mediumtest.BatchMediumTester;

import java.text.MessageFormat;

Expand Down Expand Up @@ -64,18 +65,18 @@ private void init(BootstrapProperties bootstrapProps) {
LOG.warn(MessageFormat.format("Property {0} is deprecated. Please use {1} instead.", CoreProperties.DRY_RUN, CoreProperties.ANALYSIS_MODE));
preview = "true".equals(bootstrapProps.property(CoreProperties.DRY_RUN));
incremental = false;
mediumTestMode = false;
} else {
String mode = bootstrapProps.property(CoreProperties.ANALYSIS_MODE);
preview = CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode);
incremental = CoreProperties.ANALYSIS_MODE_INCREMENTAL.equals(mode);
mediumTestMode = CoreProperties.ANALYSIS_MODE_MEDIUM_TEST.equals(mode);
}
mediumTestMode = "true".equals(bootstrapProps.property(BatchMediumTester.MEDIUM_TEST_ENABLED));
if (incremental) {
LOG.info("Incremental mode");
} else if (preview) {
LOG.info("Preview mode");
} else if (mediumTestMode) {
}
if (mediumTestMode) {
LOG.info("Medium test mode");
}
// To stay compatible with plugins that use the old property to check mode
Expand Down
Expand Up @@ -19,8 +19,6 @@
*/
package org.sonar.batch.deprecated;

import org.sonar.api.batch.sensor.internal.SensorStorage;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,15 +31,20 @@
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputPath;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.config.Settings;
import org.sonar.api.design.Dependency;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.*;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.File;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectLink;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.index.ComponentDataCache;
import org.sonar.batch.sensor.DefaultSensorContext;
import org.sonar.batch.sensor.coverage.CoverageExclusions;

Expand All @@ -62,9 +65,9 @@ public class DeprecatedSensorContext extends DefaultSensorContext implements Sen
private final CoverageExclusions coverageFilter;

public DeprecatedSensorContext(SonarIndex index, Project project, Settings settings, FileSystem fs, ActiveRules activeRules,
AnalysisMode analysisMode, ComponentDataCache componentDataCache, CoverageExclusions coverageFilter,
AnalysisMode analysisMode, CoverageExclusions coverageFilter,
SensorStorage sensorStorage) {
super(settings, fs, activeRules, analysisMode, componentDataCache, sensorStorage);
super(settings, fs, activeRules, analysisMode, sensorStorage);
this.index = index;
this.project = project;
this.coverageFilter = coverageFilter;
Expand Down
@@ -0,0 +1,23 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
@ParametersAreNonnullByDefault
package org.sonar.batch.deprecated.decorator;

import javax.annotation.ParametersAreNonnullByDefault;

This file was deleted.

Expand Up @@ -61,11 +61,12 @@
*/
public class BatchMediumTester {

public static final String MEDIUM_TEST_ENABLED = "sonar.mediumTest.enabled";
private Batch batch;

public static BatchMediumTesterBuilder builder() {
BatchMediumTesterBuilder builder = new BatchMediumTesterBuilder().registerCoreMetrics();
builder.bootstrapProperties.put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_MEDIUM_TEST);
builder.bootstrapProperties.put(MEDIUM_TEST_ENABLED, "true");
builder.bootstrapProperties.put(CoreProperties.WORKING_DIRECTORY, Files.createTempDir().getAbsolutePath());
return builder;
}
Expand Down Expand Up @@ -114,6 +115,11 @@ public BatchMediumTesterBuilder addDefaultQProfile(String language, String name)
return this;
}

public BatchMediumTesterBuilder setPreviousAnalysisDate(Date previousAnalysis) {
projectRefProvider.ref.setLastAnalysisDate(previousAnalysis);
return this;
}

public BatchMediumTesterBuilder bootstrapProperties(Map<String, String> props) {
bootstrapProperties.putAll(props);
return this;
Expand Down
Expand Up @@ -77,7 +77,10 @@ private BatchReport.Issue toReportIssue(DefaultIssue issue) {
if (line != null) {
builder.setLine(line);
}
builder.setMsg(issue.message());
String message = issue.message();
if (message != null) {
builder.setMsg(message);
}
if (issue.effortToFix() != null) {
builder.setEffortToFix(issue.effortToFix());
}
Expand Down
Expand Up @@ -139,7 +139,7 @@ public boolean isEmpty() {

private static class FileHashComputer extends CharHandler {
private MessageDigest globalMd5Digest = DigestUtils.getMd5Digest();
private StringBuffer sb = new StringBuffer();
private StringBuilder sb = new StringBuilder();

@Override
void handleIgnoreEoL(char c) {
Expand Down
Expand Up @@ -35,7 +35,6 @@
import org.sonar.api.batch.sensor.measure.NewMeasure;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.config.Settings;
import org.sonar.batch.index.ComponentDataCache;

import java.io.Serializable;

Expand All @@ -44,17 +43,14 @@ public class DefaultSensorContext implements SensorContext {
private final Settings settings;
private final FileSystem fs;
private final ActiveRules activeRules;
private final ComponentDataCache componentDataCache;
private final SensorStorage sensorStorage;
private final AnalysisMode analysisMode;

public DefaultSensorContext(Settings settings, FileSystem fs, ActiveRules activeRules, AnalysisMode analysisMode, ComponentDataCache componentDataCache,
SensorStorage sensorStorage) {
public DefaultSensorContext(Settings settings, FileSystem fs, ActiveRules activeRules, AnalysisMode analysisMode, SensorStorage sensorStorage) {
this.settings = settings;
this.fs = fs;
this.activeRules = activeRules;
this.analysisMode = analysisMode;
this.componentDataCache = componentDataCache;
this.sensorStorage = sensorStorage;
}

Expand Down
Expand Up @@ -78,7 +78,7 @@ public static class ReferenceComparator implements Comparator<Integer>, Serializ
@Override
public int compare(Integer left, Integer right) {
int result;
if (left != null & right != null) {
if (left != null && right != null) {
result = left - right;
} else {
result = left == null ? -1 : 1;
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.CoreProperties;
import org.sonar.api.issue.Issue;
import org.sonar.batch.mediumtest.BatchMediumTester;
import org.sonar.batch.mediumtest.TaskResult;
import org.sonar.batch.protocol.input.ActiveRule;
Expand All @@ -37,7 +38,9 @@
import java.text.SimpleDateFormat;
import java.util.Date;

public class ReportsMediumTest {
import static org.assertj.core.api.Assertions.assertThat;

public class PreviewAndReportsMediumTest {

@org.junit.Rule
public TemporaryFolder temp = new TemporaryFolder();
Expand All @@ -56,7 +59,9 @@ private static Date date(String date) {
.bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW))
.registerPlugin("xoo", new XooPlugin())
.addDefaultQProfile("xoo", "Sonar Way")
.activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo"))
.activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", null, "xoo"))
.activateRule(new ActiveRule("manual", "MyManualIssue", null, "My manual issue", "MAJOR", null, null))
.setPreviousAnalysisDate(new Date())
// Existing issue
.addPreviousIssue(new PreviousIssue().setKey("xyz")
.setComponentKey("sample:xources/hello/HelloJava.xoo")
Expand All @@ -75,6 +80,15 @@ private static Date date(String date) {
.setCreationDate(date("14/03/2004"))
.setChecksum(DigestUtils.md5Hex("dontexist"))
.setStatus("OPEN"))
// Manual issue
.addPreviousIssue(new PreviousIssue().setKey("manual")
.setComponentKey("sample:xources/hello/HelloJava.xoo")
.setRuleKey("manual", "MyManualIssue")
.setLine(1)
.setSeverity("MAJOR")
.setCreationDate(date("14/03/2004"))
.setChecksum(DigestUtils.md5Hex("packagehello;"))
.setStatus("OPEN"))
.build();

@Before
Expand All @@ -87,26 +101,54 @@ public void stop() {
tester.stop();
}

@Test
public void testIssueTracking() throws Exception {
File projectDir = new File(PreviewAndReportsMediumTest.class.getResource("/mediumtest/xoo/sample").toURI());

TaskResult result = tester
.newScanTask(new File(projectDir, "sonar-project.properties"))
.start();

int newIssues = 0;
int openIssues = 0;
int resolvedIssue = 0;
for (Issue issue : result.issues()) {
if (issue.isNew()) {
newIssues++;
} else if (issue.resolution() != null) {
resolvedIssue++;
} else {
openIssues++;
}
}
assertThat(newIssues).isEqualTo(13);
assertThat(openIssues).isEqualTo(2);
assertThat(resolvedIssue).isEqualTo(1);
}

@Test
public void testConsoleReport() throws Exception {
File projectDir = new File(ReportsMediumTest.class.getResource("/mediumtest/xoo/sample").toURI());
File projectDir = new File(PreviewAndReportsMediumTest.class.getResource("/mediumtest/xoo/sample").toURI());

TaskResult result = tester
.newScanTask(new File(projectDir, "sonar-project.properties"))
.property("sonar.issuesReport.console.enable", "true")
.start();

// TODO wait for ability to assert on logs
}

@Test
public void testHtmlReport() throws Exception {
File projectDir = new File(ReportsMediumTest.class.getResource("/mediumtest/xoo/sample").toURI());
File projectDir = new File(PreviewAndReportsMediumTest.class.getResource("/mediumtest/xoo/sample").toURI());

TaskResult result = tester
.newScanTask(new File(projectDir, "sonar-project.properties"))
.property("sonar.issuesReport.html.enable", "true")
.start();

assertThat(new File(projectDir, ".sonar/issues-report/issues-report.html")).exists();
assertThat(new File(projectDir, ".sonar/issues-report/issues-report-light.html")).exists();
}

}
Expand Up @@ -19,8 +19,6 @@
*/
package org.sonar.batch.sensor;

import org.sonar.api.batch.sensor.internal.SensorStorage;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -31,9 +29,10 @@
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.batch.index.ComponentDataCache;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -61,10 +60,9 @@ public void prepare() throws Exception {
when(metricFinder.findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
when(metricFinder.findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
settings = new Settings();
ComponentDataCache componentDataCache = mock(ComponentDataCache.class);
sensorStorage = mock(SensorStorage.class);
analysisMode = mock(AnalysisMode.class);
adaptor = new DefaultSensorContext(settings, fs, activeRules, analysisMode, componentDataCache, sensorStorage);
adaptor = new DefaultSensorContext(settings, fs, activeRules, analysisMode, sensorStorage);
}

@Test
Expand Down
Expand Up @@ -442,11 +442,6 @@ public interface CoreProperties {
*/
String ANALYSIS_MODE_INCREMENTAL = "incremental";

/**
* @since 5.1
*/
String ANALYSIS_MODE_MEDIUM_TEST = "mediumtest";

/**
* @since 4.0
*/
Expand Down

0 comments on commit a4e409c

Please sign in to comment.